KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sqlmagic > tinysql > tinySQLConnection


1 /*
2  * tinySQLConnection - a Connection object for the tinySQL JDBC Driver.
3  *
4  * Note that since the tinySQL class is abstract, this class needs to
5  * be abstract, as well. It's only in such manifestations of tinySQL
6  * as textFile that the tinySQLConnection can reach its true potential.
7  *
8  * A lot of this code is based on or directly taken from
9  * George Reese's (borg@imaginary.com) mSQL driver.
10  *
11  * So, it's probably safe to say:
12  *
13  * Portions of this code Copyright (c) 1996 George Reese
14  *
15  * The rest of it:
16  *
17  * Copyright 1996, Brian C. Jepson
18  * (bjepson@ids.net)
19  *
20  * $Author: davis $
21  * $Date: 2004/12/18 21:28:32 $
22  * $Revision: 1.1 $
23  *
24  * This library is free software; you can redistribute it and/or
25  * modify it under the terms of the GNU Lesser General Public
26  * License as published by the Free Software Foundation; either
27  * version 2.1 of the License, or (at your option) any later version.
28  *
29  * This library is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32  * Lesser General Public License for more details.
33  *
34  * You should have received a copy of the GNU Lesser General Public
35  * License along with this library; if not, write to the Free Software
36  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37  */

38
39 package com.sqlmagic.tinysql;
40
41 import java.sql.CallableStatement JavaDoc;
42 import java.sql.DatabaseMetaData JavaDoc;
43 import java.sql.Driver JavaDoc;
44 import java.sql.PreparedStatement JavaDoc;
45 import java.sql.SQLException JavaDoc;
46 import java.sql.SQLWarning JavaDoc;
47 import java.sql.Statement JavaDoc;
48
49 /**
50  * @author Thomas Morgner <mgs@sherito.org> executetinySQL is now called with a statement
51  * containing the SQL-Query String.
52  */

53 public abstract class tinySQLConnection implements java.sql.Connection JavaDoc {
54
55   /**
56    *
57    * The tinySQL object
58    *
59    */

60   protected tinySQL tsql = null;
61
62   /**
63    *
64    * The JDBC driver
65    *
66    */

67   protected Driver JavaDoc driver;
68
69   /**
70    *
71    * The URL to the datasource
72    *
73    */

74   protected String JavaDoc url;
75
76   /**
77    *
78    * The user name - currently unused
79    *
80    */

81   protected String JavaDoc user;
82
83   /**
84    *
85    * the catalog - it's not used by tinySQL
86    *
87    */

88   protected String JavaDoc catalog;
89
90   /**
91    *
92    * Transaction isolation level - it's not used by tinySQL
93    *
94    */

95   protected int isolation;
96
97   static boolean debug=false;
98   /**
99    *
100    * Constructs a new JDBC Connection for a tinySQL database
101    *
102    * @exception SQLException in case of an error
103    * @param user the user name - currently unused
104    * @param u the URL used to connect to the datasource
105    * @param d the Driver that instantiated this connection
106    *
107    */

108   public tinySQLConnection(String JavaDoc user, String JavaDoc u, Driver JavaDoc d)
109        throws SQLException JavaDoc {
110
111     this.url = u;
112     this.user = user;
113     this.driver = d;
114
115     // call get_tinySQL() to return a new tinySQL object.
116
// get_tinySQL() is an abstract method which allows
117
// subclasses of tinySQL, such as textFile, to be used
118
// as JDBC datasources
119
//
120
tsql = get_tinySQL();
121
122   }
123
124   /**
125    *
126    * Create and return a tinySQLStatement.
127    * @see java.sql.Connection#createStatement
128    * @exception SQLException thrown in case of error
129    *
130    */

131   public Statement JavaDoc createStatement() throws SQLException JavaDoc {
132     return (Statement JavaDoc)new tinySQLStatement(this);
133   }
134
135   /**
136    *
137    * Create and return a PreparedStatement. tinySQL doesn't support
138    * these, so it always throws an exception.
139    *
140    * @see java.sql.Connection#prepareStatement
141    * @param sql the SQL Statement
142    * @exception SQLException gets thrown if you even look at this method
143    *
144    */

145   public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql)
146        throws SQLException JavaDoc {
147     return (PreparedStatement JavaDoc)new tinySQLPreparedStatement(this,sql);
148   }
149
150   /**
151    *
152    * Create and return a CallableStatement. tinySQL does not support
153    * stored procs, so this automatically throws an exception.
154    *
155    * @see java.sql.Connection#prepareCall
156    * @param sql the SQL Statement
157    * @exception SQLException gets thrown always
158    *
159    */

160   public CallableStatement JavaDoc prepareCall(String JavaDoc sql)
161        throws SQLException JavaDoc {
162     throw new SQLException JavaDoc("tinySQL does not support stored procedures.");
163   }
164
165   /**
166    *
167    * Converts escaped SQL to tinySQL syntax. This is not supported yet,
168    * but some level of it will be meaningful, when tinySQL begins to
169    * support scalar functions. For now, it just returns the original SQL.
170    *
171    * @see java.sql.Connection#nativeSQL
172    * @param sql the SQL statement
173    * @return just what you gave it
174    *
175    */

176   public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc {
177     return sql;
178   }
179
180   /**
181    *
182    * Sets autocommit mode - tinySQL has no support for transactions,
183    * so this does nothing.
184    * @see java.sql.Connection#setAutoCommit
185    * @param b this does nothing
186    *
187    */

188   public void setAutoCommit(boolean b) throws SQLException JavaDoc {
189   }
190
191   /**
192    *
193    * Commits a transaction. Since all SQL statements are implicitly
194    * committed, it's save to preserve the illusion, and when this
195    * method is invoked, it does not throw an exception.
196    * @see java.sql.Connection#commit
197    *
198    */

199   public void commit() throws SQLException JavaDoc {
200   }
201
202   /**
203    *
204    * Rolls back a transaction. tinySQL does not support transactions,
205    * so this throws an exception.
206    * @see java.sql.Connection#rollback
207    * @exception SQLException gets thrown automatically
208    *
209    */

210   public void rollback() throws SQLException JavaDoc {
211     throw new SQLException JavaDoc("tinySQL does not support rollbacks.");
212   }
213
214   /**
215    *
216    * Close a Connection object. Does nothing, really.
217    * @see java.sql.Connection#close
218    * @exception SQLException is never thrown
219    *
220    */

221   public void close() throws SQLException JavaDoc {
222   }
223
224   /**
225    *
226    * Returns the status of the Connection.
227    * @see java.sql.Connection#isClosed
228    * @exception SQLException is never thrown
229    * @return true if the connection is closed, false otherwise
230    *
231    */

232   public boolean isClosed() throws SQLException JavaDoc {
233     return (tsql == null);
234   }
235
236   tinySQL getTinySqlHandle() {
237     return tsql;
238   }
239
240   /**
241    *
242    * This method would like to retrieve some DatabaseMetaData, but it
243    * is presently only supported for dBase access
244    * @see java.sql.Connection#getMetData
245    * @exception SQLException is never thrown
246    * @return a DatabaseMetaData object - someday
247    *
248    */

249   public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
250     System.out.println("******con.getMetaData NOT IMPLEMENTED******");
251     return null;
252   }
253
254   /**
255    * Puts the database in read-only mode... not! This throws an
256    * exception whenever it is called. tinySQL does not support
257    * a read-only mode, and it might be dangerous to let a program
258    * think it's in that mode.
259    * @see java.sql.Connection#setReadOnly
260    * @param b meaningless
261    */

262   public void setReadOnly(boolean b) throws SQLException JavaDoc {
263     throw new SQLException JavaDoc("tinySQL does not have a read-only mode.");
264   }
265
266   /**
267    *
268    * Returns true if the database is in read-only mode. It always
269    * returns false.
270    * @see java.sql.Connection#isReadOnly
271    * @return the false will be with you... always
272    *
273    */

274   public boolean isReadOnly() throws SQLException JavaDoc {
275     return false;
276   }
277
278   /**
279    *
280    * Sets the current catalog within the database. This is not
281    * supported by tinySQL, but we'll set the catalog String anyway.
282    * @see java.sql.Connection#setCatalog
283    * @param str the catalog
284    *
285    */

286   public void setCatalog(String JavaDoc str) throws SQLException JavaDoc {
287     catalog = str;
288   }
289
290   /**
291    *
292    * Returns the current catalog. This has no significance in tinySQL
293    * @see java.sql.Connection#getCatalog
294    * @return the catalog name
295    *
296    */

297   public String JavaDoc getCatalog() throws SQLException JavaDoc {
298     return catalog;
299   }
300
301   /**
302    *
303    * Sets the transaction isolation level, which has no meaning in tinySQL.
304    * We'll set the isolation level value anyhow, just to keep it happy.
305    * @see java.sql.Connection#setTransactionIsolation
306    * @param x the isolation level
307    *
308    */

309   public void setTransactionIsolation(int x)
310        throws SQLException JavaDoc {
311     isolation = x;
312   }
313
314   /**
315    *
316    * Returns the isolation level. This is not significant for tinySQL
317    * @see java.sql.Connection#getTransactionIsolation
318    * @return the transaction isolation level
319    *
320    */

321   public int getTransactionIsolation() throws SQLException JavaDoc {
322     return isolation;
323   }
324
325   /**
326    *
327    * Disables autoclosing of connections and result sets. This is
328    * not supported by tinySQL.
329    * @see java.sql.Connection#disableAutoClose
330    *
331    */

332   public void disableAutoClose() throws SQLException JavaDoc {
333   }
334
335   /**
336    *
337    * Returns a chain of warnings for the current connection; this
338    * is not supported by tinySQL.
339    * @see java.sql.Connection#getWarnings
340    * @return the chain of warnings for this connection
341    *
342    */

343   public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
344     return null;
345   }
346
347   /**
348    *
349    * Clears the non-existant warning chain.
350    * @see java.sql.Connection#clearWarnings
351    *
352    */

353   public void clearWarnings() throws SQLException JavaDoc {
354   }
355
356   /**
357    *
358    * Execute a tinySQL Statement
359    * @param sql the statement to be executed
360    * @return tsResultSet containing the results of the SQL statement
361    *
362    */

363    public tsResultSet executetinySQL(tinySQLStatement sql) throws SQLException JavaDoc
364    {
365     tsResultSet result;
366
367     // try to execute the SQL
368
//
369
try {
370       result = tsql.sqlexec(sql);
371     } catch( tinySQLException e ) {
372       if ( debug ) e.printStackTrace();
373       throw new SQLException JavaDoc("Exception: " + e.getMessage());
374     }
375     return result;
376   }
377    public tsResultSet executetinySQL(tinySQLPreparedStatement psql) throws SQLException JavaDoc
378    {
379     tsResultSet result;
380
381     // try to execute the SQL
382
//
383
try {
384       result = tsql.sqlexec(psql);
385     } catch( tinySQLException e ) {
386       if ( debug ) e.printStackTrace();
387       throw new SQLException JavaDoc("Exception: " + e.getMessage());
388     }
389     return result;
390   }
391
392   /**
393    *
394    * Execute a tinySQL Statement
395    * @param sql the statement to be executed
396    * @return either the row count for INSERT, UPDATE or DELETE or 0 for SQL statements that return nothing
397    *
398    */

399    public int executetinyUpdate(tinySQLStatement sql) throws SQLException JavaDoc {
400
401     // the result set
402
//
403
tsResultSet result;
404
405     // try to execute the SQL
406
//
407
try {
408       result = tsql.sqlexec(sql);
409     } catch( tinySQLException e ) {
410       if ( debug ) e.printStackTrace();
411       throw new SQLException JavaDoc("Exception: " + e.getMessage());
412     }
413     return 0;
414   }
415    public int executetinyUpdate(tinySQLPreparedStatement psql) throws SQLException JavaDoc {
416
417     // the result set
418
//
419
tsResultSet result;
420
421     // try to execute the SQL
422
//
423
try {
424       result = tsql.sqlexec(psql);
425     } catch( tinySQLException e ) {
426       if ( debug ) e.printStackTrace();
427       throw new SQLException JavaDoc("Exception: " + e.getMessage());
428     }
429     return 0;
430   }
431
432   public boolean getAutoCommit() {
433     return true;
434   }
435
436   public void setAutoClose(boolean l) {
437   }
438
439   public boolean getAutoClose() {
440     return false;
441   }
442
443
444   /**
445    *
446    * creates a new tinySQL object and returns it. Well, not really,
447    * since tinySQL is an abstract class. When you subclass tinySQLConnection,
448    * you will need to include this method, and return some subclass
449    * of tinySQL.
450    *
451    */

452   public abstract tinySQL get_tinySQL();
453
454     //--------------------------JDBC 2.0-----------------------------
455

456     /**
457      * JDBC 2.0
458      *
459          * Creates a <code>Statement</code> object that will generate
460          * <code>ResultSet</code> objects with the given type and concurrency.
461      * This method is the same as the <code>createStatement</code> method
462          * above, but it allows the default result set
463      * type and result set concurrency type to be overridden.
464      *
465      * @param resultSetType a result set type; see ResultSet.TYPE_XXX
466      * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
467      * @return a new Statement object
468      * @exception SQLException if a database access error occurs
469      */

470     public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency)
471       throws SQLException JavaDoc {
472           throw new SQLException JavaDoc("tinySQL does not support createStatement with concurrency.");
473       }
474
475     /**
476      * JDBC 2.0
477      *
478          * Creates a <code>PreparedStatement</code> object that will generate
479          * <code>ResultSet</code> objects with the given type and concurrency.
480      * This method is the same as the <code>prepareStatement</code> method
481          * above, but it allows the default result set
482      * type and result set concurrency type to be overridden.
483      *
484      * @param resultSetType a result set type; see ResultSet.TYPE_XXX
485      * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
486      * @return a new PreparedStatement object containing the
487      * pre-compiled SQL statement
488      * @exception SQLException if a database access error occurs
489      */

490      public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType,
491                                         int resultSetConcurrency)
492        throws SQLException JavaDoc {
493        throw new SQLException JavaDoc("tinySQL does not support preparedStatement with concurrency.");
494      }
495
496     /**
497      * JDBC 2.0
498      *
499          * Creates a <code>CallableStatement</code> object that will generate
500          * <code>ResultSet</code> objects with the given type and concurrency.
501      * This method is the same as the <code>prepareCall</code> method
502          * above, but it allows the default result set
503      * type and result set concurrency type to be overridden.
504      *
505      * @param resultSetType a result set type; see ResultSet.TYPE_XXX
506      * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
507      * @return a new CallableStatement object containing the
508      * pre-compiled SQL statement
509      * @exception SQLException if a database access error occurs
510      */

511     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType,
512                                  int resultSetConcurrency) throws SQLException JavaDoc {
513        throw new SQLException JavaDoc("tinySQL does not support prepareCall with concurrency.");
514     }
515
516     /**
517      * JDBC 2.0
518      *
519      * Gets the type map object associated with this connection.
520      * Unless the application has added an entry to the type map,
521          * the map returned will be empty.
522          *
523          * @return the <code>java.util.Map</code> object associated
524          * with this <code>Connection</code> object
525      */

526     public java.util.Map JavaDoc getTypeMap() throws SQLException JavaDoc {
527       throw new SQLException JavaDoc("tinySQL does not support getTypeMap.");
528     }
529
530     /**
531      * JDBC 2.0
532      *
533      * Installs the given type map as the type map for
534      * this connection. The type map will be used for the
535          * custom mapping of SQL structured types and distinct types.
536          *
537          * @param the <code>java.util.Map</code> object to install
538          * as the replacement for this <code>Connection</code>
539          * object's default type map
540      */

541     public void setTypeMap(java.util.Map JavaDoc map) throws SQLException JavaDoc {
542       throw new SQLException JavaDoc("tinySQL does not support setTypeMap.");
543     }
544
545 }
546
Popular Tags