KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > jdbcDriver


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb;
33
34 import java.sql.Connection JavaDoc;
35 import java.sql.Driver JavaDoc;
36 import java.sql.DriverManager JavaDoc;
37 import java.sql.DriverPropertyInfo JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.util.Properties JavaDoc;
40
41 import org.hsqldb.jdbc.jdbcConnection;
42 import org.hsqldb.persist.HsqlDatabaseProperties;
43 import org.hsqldb.persist.HsqlProperties;
44
45 // fredt@users 20011220 - patch 1.7.0 by fredt
46
// new version numbering scheme
47
// fredt@users 20020320 - patch 1.7.0 - JDBC 2 support and error trapping
48
// JDBC 2 methods can now be called from jdk 1.1.x - see javadoc comments
49
// fredt@users 20030528 - patch 1.7.2 suggested by Gerhard Hiller - support for properties in URL
50

51 /**
52  * The following comments are from the http://ldbc.sourceforge.net project.
53  * These are the issues with HSQLDB JDBC implementation that are currently
54  * not resolved. Other issues stated there have been resolved in 1.8.0.
55  *
56  * Time format error: the following statement should work, but throws an exception:
57  * CREATE TABLE Test ( ID INT , Current DATETIME )
58  * insert into test values(1,'2002-01-01 0:0:0.0')
59  * This works:
60  * insert into test values(1,'2002-01-01 00:00:00.0')
61  *
62  * ABS(4) returns data type DECIMAL, should return data type INTEGER
63  *
64  * Should throw a exception if PreparedStatement.setObject(1,null) is called.
65  * See also JDBC API Tutorial and Reference, Second Edition,
66  * page 544, 24.1.5 Sending JDBC NULL as an IN parameter
67  *
68  * Statement / PreparedStatement setMaxFieldSize is ignored.
69  *
70  * The database should support at least Connection.TRANSACTION_READ_COMMITTED.
71  * Currently, only TRANSACTION_READ_UNCOMMITTED is supported.
72  *
73  * Statement.getQueryTimeout doesn't return the value set before.
74  *
75  * When autocommit is on, executing a query on a statement is
76  * supposed to close the old resultset. This is not implemented.
77  */

78
79 /**
80  * Each JDBC driver must supply a class that implements the Driver
81  * interface. <p>
82  *
83  * The Java SQL framework allows for multiple database drivers. <p>
84  *
85  * The DriverManager will try to load as many drivers as it can find and
86  * then for any given connection request, it will ask each driver in turn
87  * to try to connect to the target URL. <p>
88  *
89  * The application developer will normally not need
90  * to call any function of the Driver directly. All required calls are made
91  * by the DriverManager. <p>
92  *
93  * <!-- start release-specific documentation -->
94  * <div class="ReleaseSpecificDocumentation">
95  * <h3>HSQLDB-Specific Information:</h3> <p>
96  * When the HSQL Database Engine Driver class is loaded, it creates an
97  * instance of itself and register it with the DriverManager. This means
98  * that a user can load and register the HSQL Database Engine driver by
99  * calling <pre>
100  * <code>Class.forName("org.hsqldb.jdbcDriver")</code> </pre> For more
101  * information about how to connect to a HSQL Database Engine database,
102  * please see jdbcConnection. </font><p>
103  *
104  * As of version 1.7.0 all JDBC 2 methods can be
105  * called with jdk 1.1.x. Some of these method calls require int values
106  * that are defined in JDBC 2 version of ResultSet. These values are
107  * defined in the jdbcResultSet class when it is compiled with jdk 1.1.x.
108  * When using the JDBC 2 methods that require those values as parameters or
109  * return one of those values, refer to them as follows: (The code will not
110  * be compatible with other JDBC 2 driver, which require ResultSet to be
111  * used instead of jdbcResultSet) (fredt@users)<p>
112  * </div> <!-- end release-specific documentation -->
113  *
114  * jdbcResultSet.FETCH_FORWARD<br>
115  * jdbcResultSet.TYPE_FORWARD_ONLY<br>
116  * jdbcResultSet TYPE_SCROLL_INSENSITIVE<br>
117  * jdbcResultSet.CONCUR_READ_ONLY</font><p>
118  *
119  *
120  * @see jdbcConnection
121  */

122 public class jdbcDriver implements Driver JavaDoc {
123
124     /**
125      * Attempts to make a database connection to the given URL. The driver
126      * returns "null" if it realizes it is the wrong kind of driver to
127      * connect to the given URL. This will be common, as when the JDBC
128      * driver manager is asked to connect to a given URL it passes the URL
129      * to each loaded driver in turn.<p>
130      *
131      * The driver raises a SQLException if it is the right driver to
132      * connect to the given URL, but has trouble connecting to the
133      * database.<p>
134      *
135      * The java.util.Properties argument can be used to passed arbitrary
136      * string tag/value pairs as connection arguments.<p>
137      *
138      * <!-- start release-specific documentation -->
139      * <div class="ReleaseSpecificDocumentation">
140      * <h3>HSQLDB-Specific Information:</h3> <p>
141      * For HSQL Database Engine, at least "user" and
142      * "password" properties must be included in the Properties. From
143      * version 1.7.1 two optional properties are supported:<p>
144      * <code>get_column_name</code> if set to false, a
145      * ResultSetMetaData.getColumnName() call will return the user defined
146      * label instead of the column name.
147      * <code>strict_md</code> if set to true, some ResultSetMetaData methods
148      * return more strict values for compatibility reasons.
149      * </div> <!-- end release-specific documentation -->
150      *
151      * @param url the URL of the database to which to connect
152      * @param info a list of arbitrary string tag/value pairs as connection
153      * arguments. Normally at least a "user" and "password" property
154      * should be included.
155      * @return a <code>Connection</code> object that represents a
156      * connection to the URL
157      * @exception SQLException if a database access error occurs
158      */

159     public Connection JavaDoc connect(String JavaDoc url,
160                               Properties JavaDoc info) throws SQLException JavaDoc {
161         return getConnection(url, info);
162     }
163
164     public static Connection JavaDoc getConnection(String JavaDoc url,
165                                            Properties JavaDoc info)
166                                            throws SQLException JavaDoc {
167
168         HsqlProperties props = DatabaseURL.parseURL(url, true);
169
170         if (props == null) {
171
172             // supposed to be an HSQLDB driver url but has errors
173
throw new SQLException JavaDoc(
174                 Trace.getMessage(Trace.INVALID_JDBC_ARGUMENT));
175         } else if (props.isEmpty()) {
176
177             // is not an HSQLDB driver url
178
return null;
179         }
180
181         props.addProperties(info);
182
183         return new jdbcConnection(props);
184     }
185
186     /**
187      * Returns true if the driver thinks that it can open a connection to
188      * the given URL. Typically drivers will return true if they understand
189      * the subprotocol specified in the URL and false if they don't.
190      *
191      * @param url the URL of the database
192      * @return true if this driver can connect to the given URL
193      */

194
195     // fredt@users - patch 1.7.0 - allow mixedcase url's
196
public boolean acceptsURL(String JavaDoc url) {
197
198         return url != null
199                && url.regionMatches(true, 0, DatabaseURL.S_URL_PREFIX, 0,
200                                     DatabaseURL.S_URL_PREFIX.length());
201     }
202
203     /**
204      * Gets information about the possible properties for this driver. <p>
205      *
206      * The getPropertyInfo method is intended to allow a generic GUI tool
207      * to discover what properties it should prompt a human for in order to
208      * get enough information to connect to a database. Note that depending
209      * on the values the human has supplied so far, additional values may
210      * become necessary, so it may be necessary to iterate though several
211      * calls to getPropertyInfo.<p>
212      *
213      * <!-- start release-specific documentation -->
214      * <div class="ReleaseSpecificDocumentation">
215      * <h3>HSQLDB-Specific Information:</h3> <p>
216      * HSQLDB 1.7.2 uses the values submitted in info to set the value for
217      * each DriverPropertyInfo object returned. It does not use the default
218      * value that it would use for the property if the value is null.
219      * </div> <!-- end release-specific documentation -->
220      *
221      * @param url the URL of the database to which to connect
222      * @param info a proposed list of tag/value pairs that will be sent on
223      * connect open
224      * @return an array of DriverPropertyInfo objects describing possible
225      * properties. This array may be an empty array if no properties
226      * are required.
227      */

228     public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info) {
229
230         String JavaDoc[] choices = new String JavaDoc[] {
231             "true", "false"
232         };
233         DriverPropertyInfo JavaDoc[] pinfo = new DriverPropertyInfo JavaDoc[6];
234         DriverPropertyInfo JavaDoc p;
235
236         p = new DriverPropertyInfo JavaDoc("user", null);
237         p.value = info.getProperty("user");
238         p.required = true;
239         pinfo[0] = p;
240         p = new DriverPropertyInfo JavaDoc("password", null);
241         p.value = info.getProperty("password");
242         p.required = true;
243         pinfo[1] = p;
244         p = new DriverPropertyInfo JavaDoc("get_column_name", null);
245         p.value = info.getProperty("get_column_name", "true");
246         p.required = false;
247         p.choices = choices;
248         pinfo[2] = p;
249         p = new DriverPropertyInfo JavaDoc("ifexists", null);
250         p.value = info.getProperty("ifexists");
251         p.required = false;
252         p.choices = choices;
253         pinfo[3] = p;
254         p = new DriverPropertyInfo JavaDoc("default_schema", null);
255         p.value = info.getProperty("default_schema");
256         p.required = false;
257         p.choices = choices;
258         pinfo[4] = p;
259         p = new DriverPropertyInfo JavaDoc("shutdown", null);
260         p.value = info.getProperty("shutdown");
261         p.required = false;
262         p.choices = choices;
263         pinfo[5] = p;
264
265         return pinfo;
266     }
267
268     /**
269      * Gets the driver's major version number.
270      *
271      * @return this driver's major version number
272      */

273     public int getMajorVersion() {
274         return HsqlDatabaseProperties.MAJOR;
275     }
276
277     /**
278      * Gets the driver's minor version number.
279      *
280      * @return this driver's minor version number
281      */

282     public int getMinorVersion() {
283         return HsqlDatabaseProperties.MINOR;
284     }
285
286     /**
287      * Reports whether this driver is a genuine JDBC COMPLIANT<sup><font
288      * size=-2>TM</font> </sup> driver. A driver may only report true here
289      * if it passes the JDBC compliance tests; otherwise it is required to
290      * return false. JDBC compliance requires full support for the JDBC API
291      * and full support for SQL 92 Entry Level. It is expected that JDBC
292      * compliant drivers will be available for all the major commercial
293      * databases. <p>
294      *
295      * <!-- start release-specific documentation -->
296      * <div class="ReleaseSpecificDocumentation">
297      * <h3>HSQLDB-Specific Information:</h3> <p>
298      * HSQL Database Engine currently does not yet
299      * support all required SQL 92 Entry Level functionality and thus
300      * returns false. It looks like other drivers return true but do not
301      * support all features.<p>
302      * </div> <!-- end release-specific documentation -->
303      *
304      * This method is not intended to encourage the development of non-JDBC
305      * compliant drivers, but is a recognition of the fact that some
306      * vendors are interested in using the JDBC API and framework for
307      * lightweight databases that do not support full database
308      * functionality, or for special databases such as document information
309      * retrieval where a SQL implementation may not be feasible.
310      *
311      * @return Description of the Return Value
312      */

313     public boolean jdbcCompliant() {
314
315 /** @todo fredt - we should aim to be able to report true */
316         return false;
317     }
318
319     static {
320         try {
321             DriverManager.registerDriver(new jdbcDriver());
322         } catch (Exception JavaDoc e) {}
323     }
324 }
325
Popular Tags