1 | /* ============================================================= |
2 | * SmallSQL : a free Java DBMS library for the Java(tm) platform |
3 | * ============================================================= |
4 | * |
5 | * (C) Copyright 2004-2006, by Volker Berlin. |
6 | * |
7 | * Project Info: http://www.smallsql.de/ |
8 | * |
9 | * This library is free software; you can redistribute it and/or modify it |
10 | * under the terms of the GNU Lesser General Public License as published by |
11 | * the Free Software Foundation; either version 2.1 of the License, or |
12 | * (at your option) any later version. |
13 | * |
14 | * This library is distributed in the hope that it will be useful, but |
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
16 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 | * License for more details. |
18 | * |
19 | * You should have received a copy of the GNU Lesser General Public |
20 | * License along with this library; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
22 | * USA. |
23 | * |
24 | * [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
25 | * in the United States and other countries.] |
26 | * |
27 | * --------------- |
28 | * SSDriver.java |
29 | * --------------- |
30 | * Author: Volker Berlin |
31 | * |
32 | */ |
33 | package smallsql.database; |
34 | |
35 | import java.io.IOException; |
36 | import java.io.StringBufferInputStream; |
37 | import java.sql.*; |
38 | import java.util.Properties; |
39 | |
40 | public class SSDriver implements Driver { |
41 | |
42 | static final String URL_PREFIX = "jdbc:smallsql"; |
43 | |
44 | static SSDriver drv; |
45 | static { |
46 | try{ |
47 | drv = new SSDriver(); |
48 | java.sql.DriverManager.registerDriver(drv); |
49 | //DriverManager.setLogStream(System.out); |
50 | //DriverManager.setLogStream(new java.io.PrintStream(new java.io.FileOutputStream("jdbc.log"))); |
51 | }catch(Throwable e){} |
52 | } |
53 | |
54 | |
55 | public Connection connect(String url, Properties info) throws SQLException { |
56 | Properties props = (Properties)info.clone(); |
57 | if(!acceptsURL(url)) return null; |
58 | int idx1 = url.indexOf( ':', 5); // search after "jdbc:" |
59 | if(idx1 > 0){ |
60 | int idx2 = url.indexOf('?', idx1+1); |
61 | String dbPath = (idx2 > 0) ? url.substring(idx1+1, idx2) : url.substring(idx1+1); |
62 | props.setProperty("dbpath", dbPath); |
63 | if(idx2 > 0){ |
64 | String propsString = url.substring(idx2+1); |
65 | propsString = propsString.replace(';', '\n'); |
66 | try { |
67 | Properties urlProps = new Properties(props); |
68 | urlProps.load(new StringBufferInputStream(propsString)); |
69 | props = urlProps; |
70 | } catch (Exception e) { |
71 | e.printStackTrace(); |
72 | } |
73 | } |
74 | } |
75 | return new SSConnection( props ); |
76 | } |
77 | |
78 | |
79 | public boolean acceptsURL(String url) throws SQLException { |
80 | return url.startsWith(URL_PREFIX); |
81 | } |
82 | |
83 | |
84 | public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { |
85 | /**@todo: Implement this java.sql.Driver method*/ |
86 | throw new java.lang.UnsupportedOperationException("Method getPropertyInfo() not yet implemented."); |
87 | } |
88 | |
89 | |
90 | public int getMajorVersion() { |
91 | return 0; |
92 | } |
93 | |
94 | |
95 | public int getMinorVersion() { |
96 | return 15; |
97 | } |
98 | |
99 | |
100 | public boolean jdbcCompliant() { |
101 | return true; |
102 | } |
103 | } |