001: /*
002: * tinySQLConnection - a Connection object for the tinySQL JDBC Driver.
003: *
004: * Note that since the tinySQL class is abstract, this class needs to
005: * be abstract, as well. It's only in such manifestations of tinySQL
006: * as textFile that the tinySQLConnection can reach its true potential.
007: *
008: * A lot of this code is based on or directly taken from
009: * George Reese's (borg@imaginary.com) mSQL driver.
010: *
011: * So, it's probably safe to say:
012: *
013: * Portions of this code Copyright (c) 1996 George Reese
014: *
015: * The rest of it:
016: *
017: * Copyright 1996, Brian C. Jepson
018: * (bjepson@ids.net)
019: *
020: * $Author: davis $
021: * $Date: 2004/12/18 21:28:32 $
022: * $Revision: 1.1 $
023: *
024: * This library is free software; you can redistribute it and/or
025: * modify it under the terms of the GNU Lesser General Public
026: * License as published by the Free Software Foundation; either
027: * version 2.1 of the License, or (at your option) any later version.
028: *
029: * This library is distributed in the hope that it will be useful,
030: * but WITHOUT ANY WARRANTY; without even the implied warranty of
031: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
032: * Lesser General Public License for more details.
033: *
034: * You should have received a copy of the GNU Lesser General Public
035: * License along with this library; if not, write to the Free Software
036: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
037: */
038:
039: package com.sqlmagic.tinysql;
040:
041: import java.sql.CallableStatement;
042: import java.sql.DatabaseMetaData;
043: import java.sql.Driver;
044: import java.sql.PreparedStatement;
045: import java.sql.SQLException;
046: import java.sql.SQLWarning;
047: import java.sql.Statement;
048:
049: /**
050: * @author Thomas Morgner <mgs@sherito.org> executetinySQL is now called with a statement
051: * containing the SQL-Query String.
052: */
053: public abstract class tinySQLConnection implements java.sql.Connection {
054:
055: /**
056: *
057: * The tinySQL object
058: *
059: */
060: protected tinySQL tsql = null;
061:
062: /**
063: *
064: * The JDBC driver
065: *
066: */
067: protected Driver driver;
068:
069: /**
070: *
071: * The URL to the datasource
072: *
073: */
074: protected String url;
075:
076: /**
077: *
078: * The user name - currently unused
079: *
080: */
081: protected String user;
082:
083: /**
084: *
085: * the catalog - it's not used by tinySQL
086: *
087: */
088: protected String catalog;
089:
090: /**
091: *
092: * Transaction isolation level - it's not used by tinySQL
093: *
094: */
095: protected int isolation;
096:
097: static boolean debug = false;
098:
099: /**
100: *
101: * Constructs a new JDBC Connection for a tinySQL database
102: *
103: * @exception SQLException in case of an error
104: * @param user the user name - currently unused
105: * @param u the URL used to connect to the datasource
106: * @param d the Driver that instantiated this connection
107: *
108: */
109: public tinySQLConnection(String user, String u, Driver d)
110: throws SQLException {
111:
112: this .url = u;
113: this .user = user;
114: this .driver = d;
115:
116: // call get_tinySQL() to return a new tinySQL object.
117: // get_tinySQL() is an abstract method which allows
118: // subclasses of tinySQL, such as textFile, to be used
119: // as JDBC datasources
120: //
121: tsql = get_tinySQL();
122:
123: }
124:
125: /**
126: *
127: * Create and return a tinySQLStatement.
128: * @see java.sql.Connection#createStatement
129: * @exception SQLException thrown in case of error
130: *
131: */
132: public Statement createStatement() throws SQLException {
133: return (Statement) new tinySQLStatement(this );
134: }
135:
136: /**
137: *
138: * Create and return a PreparedStatement. tinySQL doesn't support
139: * these, so it always throws an exception.
140: *
141: * @see java.sql.Connection#prepareStatement
142: * @param sql the SQL Statement
143: * @exception SQLException gets thrown if you even look at this method
144: *
145: */
146: public PreparedStatement prepareStatement(String sql)
147: throws SQLException {
148: return (PreparedStatement) new tinySQLPreparedStatement(this ,
149: sql);
150: }
151:
152: /**
153: *
154: * Create and return a CallableStatement. tinySQL does not support
155: * stored procs, so this automatically throws an exception.
156: *
157: * @see java.sql.Connection#prepareCall
158: * @param sql the SQL Statement
159: * @exception SQLException gets thrown always
160: *
161: */
162: public CallableStatement prepareCall(String sql)
163: throws SQLException {
164: throw new SQLException(
165: "tinySQL does not support stored procedures.");
166: }
167:
168: /**
169: *
170: * Converts escaped SQL to tinySQL syntax. This is not supported yet,
171: * but some level of it will be meaningful, when tinySQL begins to
172: * support scalar functions. For now, it just returns the original SQL.
173: *
174: * @see java.sql.Connection#nativeSQL
175: * @param sql the SQL statement
176: * @return just what you gave it
177: *
178: */
179: public String nativeSQL(String sql) throws SQLException {
180: return sql;
181: }
182:
183: /**
184: *
185: * Sets autocommit mode - tinySQL has no support for transactions,
186: * so this does nothing.
187: * @see java.sql.Connection#setAutoCommit
188: * @param b this does nothing
189: *
190: */
191: public void setAutoCommit(boolean b) throws SQLException {
192: }
193:
194: /**
195: *
196: * Commits a transaction. Since all SQL statements are implicitly
197: * committed, it's save to preserve the illusion, and when this
198: * method is invoked, it does not throw an exception.
199: * @see java.sql.Connection#commit
200: *
201: */
202: public void commit() throws SQLException {
203: }
204:
205: /**
206: *
207: * Rolls back a transaction. tinySQL does not support transactions,
208: * so this throws an exception.
209: * @see java.sql.Connection#rollback
210: * @exception SQLException gets thrown automatically
211: *
212: */
213: public void rollback() throws SQLException {
214: throw new SQLException("tinySQL does not support rollbacks.");
215: }
216:
217: /**
218: *
219: * Close a Connection object. Does nothing, really.
220: * @see java.sql.Connection#close
221: * @exception SQLException is never thrown
222: *
223: */
224: public void close() throws SQLException {
225: }
226:
227: /**
228: *
229: * Returns the status of the Connection.
230: * @see java.sql.Connection#isClosed
231: * @exception SQLException is never thrown
232: * @return true if the connection is closed, false otherwise
233: *
234: */
235: public boolean isClosed() throws SQLException {
236: return (tsql == null);
237: }
238:
239: tinySQL getTinySqlHandle() {
240: return tsql;
241: }
242:
243: /**
244: *
245: * This method would like to retrieve some DatabaseMetaData, but it
246: * is presently only supported for dBase access
247: * @see java.sql.Connection#getMetData
248: * @exception SQLException is never thrown
249: * @return a DatabaseMetaData object - someday
250: *
251: */
252: public DatabaseMetaData getMetaData() throws SQLException {
253: System.out
254: .println("******con.getMetaData NOT IMPLEMENTED******");
255: return null;
256: }
257:
258: /**
259: * Puts the database in read-only mode... not! This throws an
260: * exception whenever it is called. tinySQL does not support
261: * a read-only mode, and it might be dangerous to let a program
262: * think it's in that mode.
263: * @see java.sql.Connection#setReadOnly
264: * @param b meaningless
265: */
266: public void setReadOnly(boolean b) throws SQLException {
267: throw new SQLException(
268: "tinySQL does not have a read-only mode.");
269: }
270:
271: /**
272: *
273: * Returns true if the database is in read-only mode. It always
274: * returns false.
275: * @see java.sql.Connection#isReadOnly
276: * @return the false will be with you... always
277: *
278: */
279: public boolean isReadOnly() throws SQLException {
280: return false;
281: }
282:
283: /**
284: *
285: * Sets the current catalog within the database. This is not
286: * supported by tinySQL, but we'll set the catalog String anyway.
287: * @see java.sql.Connection#setCatalog
288: * @param str the catalog
289: *
290: */
291: public void setCatalog(String str) throws SQLException {
292: catalog = str;
293: }
294:
295: /**
296: *
297: * Returns the current catalog. This has no significance in tinySQL
298: * @see java.sql.Connection#getCatalog
299: * @return the catalog name
300: *
301: */
302: public String getCatalog() throws SQLException {
303: return catalog;
304: }
305:
306: /**
307: *
308: * Sets the transaction isolation level, which has no meaning in tinySQL.
309: * We'll set the isolation level value anyhow, just to keep it happy.
310: * @see java.sql.Connection#setTransactionIsolation
311: * @param x the isolation level
312: *
313: */
314: public void setTransactionIsolation(int x) throws SQLException {
315: isolation = x;
316: }
317:
318: /**
319: *
320: * Returns the isolation level. This is not significant for tinySQL
321: * @see java.sql.Connection#getTransactionIsolation
322: * @return the transaction isolation level
323: *
324: */
325: public int getTransactionIsolation() throws SQLException {
326: return isolation;
327: }
328:
329: /**
330: *
331: * Disables autoclosing of connections and result sets. This is
332: * not supported by tinySQL.
333: * @see java.sql.Connection#disableAutoClose
334: *
335: */
336: public void disableAutoClose() throws SQLException {
337: }
338:
339: /**
340: *
341: * Returns a chain of warnings for the current connection; this
342: * is not supported by tinySQL.
343: * @see java.sql.Connection#getWarnings
344: * @return the chain of warnings for this connection
345: *
346: */
347: public SQLWarning getWarnings() throws SQLException {
348: return null;
349: }
350:
351: /**
352: *
353: * Clears the non-existant warning chain.
354: * @see java.sql.Connection#clearWarnings
355: *
356: */
357: public void clearWarnings() throws SQLException {
358: }
359:
360: /**
361: *
362: * Execute a tinySQL Statement
363: * @param sql the statement to be executed
364: * @return tsResultSet containing the results of the SQL statement
365: *
366: */
367: public tsResultSet executetinySQL(tinySQLStatement sql)
368: throws SQLException {
369: tsResultSet result;
370:
371: // try to execute the SQL
372: //
373: try {
374: result = tsql.sqlexec(sql);
375: } catch (tinySQLException e) {
376: if (debug)
377: e.printStackTrace();
378: throw new SQLException("Exception: " + e.getMessage());
379: }
380: return result;
381: }
382:
383: public tsResultSet executetinySQL(tinySQLPreparedStatement psql)
384: throws SQLException {
385: tsResultSet result;
386:
387: // try to execute the SQL
388: //
389: try {
390: result = tsql.sqlexec(psql);
391: } catch (tinySQLException e) {
392: if (debug)
393: e.printStackTrace();
394: throw new SQLException("Exception: " + e.getMessage());
395: }
396: return result;
397: }
398:
399: /**
400: *
401: * Execute a tinySQL Statement
402: * @param sql the statement to be executed
403: * @return either the row count for INSERT, UPDATE or DELETE or 0 for SQL statements that return nothing
404: *
405: */
406: public int executetinyUpdate(tinySQLStatement sql)
407: throws SQLException {
408:
409: // the result set
410: //
411: tsResultSet result;
412:
413: // try to execute the SQL
414: //
415: try {
416: result = tsql.sqlexec(sql);
417: } catch (tinySQLException e) {
418: if (debug)
419: e.printStackTrace();
420: throw new SQLException("Exception: " + e.getMessage());
421: }
422: return 0;
423: }
424:
425: public int executetinyUpdate(tinySQLPreparedStatement psql)
426: throws SQLException {
427:
428: // the result set
429: //
430: tsResultSet result;
431:
432: // try to execute the SQL
433: //
434: try {
435: result = tsql.sqlexec(psql);
436: } catch (tinySQLException e) {
437: if (debug)
438: e.printStackTrace();
439: throw new SQLException("Exception: " + e.getMessage());
440: }
441: return 0;
442: }
443:
444: public boolean getAutoCommit() {
445: return true;
446: }
447:
448: public void setAutoClose(boolean l) {
449: }
450:
451: public boolean getAutoClose() {
452: return false;
453: }
454:
455: /**
456: *
457: * creates a new tinySQL object and returns it. Well, not really,
458: * since tinySQL is an abstract class. When you subclass tinySQLConnection,
459: * you will need to include this method, and return some subclass
460: * of tinySQL.
461: *
462: */
463: public abstract tinySQL get_tinySQL();
464:
465: //--------------------------JDBC 2.0-----------------------------
466:
467: /**
468: * JDBC 2.0
469: *
470: * Creates a <code>Statement</code> object that will generate
471: * <code>ResultSet</code> objects with the given type and concurrency.
472: * This method is the same as the <code>createStatement</code> method
473: * above, but it allows the default result set
474: * type and result set concurrency type to be overridden.
475: *
476: * @param resultSetType a result set type; see ResultSet.TYPE_XXX
477: * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
478: * @return a new Statement object
479: * @exception SQLException if a database access error occurs
480: */
481: public Statement createStatement(int resultSetType,
482: int resultSetConcurrency) throws SQLException {
483: throw new SQLException(
484: "tinySQL does not support createStatement with concurrency.");
485: }
486:
487: /**
488: * JDBC 2.0
489: *
490: * Creates a <code>PreparedStatement</code> object that will generate
491: * <code>ResultSet</code> objects with the given type and concurrency.
492: * This method is the same as the <code>prepareStatement</code> method
493: * above, but it allows the default result set
494: * type and result set concurrency type to be overridden.
495: *
496: * @param resultSetType a result set type; see ResultSet.TYPE_XXX
497: * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
498: * @return a new PreparedStatement object containing the
499: * pre-compiled SQL statement
500: * @exception SQLException if a database access error occurs
501: */
502: public PreparedStatement prepareStatement(String sql,
503: int resultSetType, int resultSetConcurrency)
504: throws SQLException {
505: throw new SQLException(
506: "tinySQL does not support preparedStatement with concurrency.");
507: }
508:
509: /**
510: * JDBC 2.0
511: *
512: * Creates a <code>CallableStatement</code> object that will generate
513: * <code>ResultSet</code> objects with the given type and concurrency.
514: * This method is the same as the <code>prepareCall</code> method
515: * above, but it allows the default result set
516: * type and result set concurrency type to be overridden.
517: *
518: * @param resultSetType a result set type; see ResultSet.TYPE_XXX
519: * @param resultSetConcurrency a concurrency type; see ResultSet.CONCUR_XXX
520: * @return a new CallableStatement object containing the
521: * pre-compiled SQL statement
522: * @exception SQLException if a database access error occurs
523: */
524: public CallableStatement prepareCall(String sql, int resultSetType,
525: int resultSetConcurrency) throws SQLException {
526: throw new SQLException(
527: "tinySQL does not support prepareCall with concurrency.");
528: }
529:
530: /**
531: * JDBC 2.0
532: *
533: * Gets the type map object associated with this connection.
534: * Unless the application has added an entry to the type map,
535: * the map returned will be empty.
536: *
537: * @return the <code>java.util.Map</code> object associated
538: * with this <code>Connection</code> object
539: */
540: public java.util.Map getTypeMap() throws SQLException {
541: throw new SQLException("tinySQL does not support getTypeMap.");
542: }
543:
544: /**
545: * JDBC 2.0
546: *
547: * Installs the given type map as the type map for
548: * this connection. The type map will be used for the
549: * custom mapping of SQL structured types and distinct types.
550: *
551: * @param the <code>java.util.Map</code> object to install
552: * as the replacement for this <code>Connection</code>
553: * object's default type map
554: */
555: public void setTypeMap(java.util.Map map) throws SQLException {
556: throw new SQLException("tinySQL does not support setTypeMap.");
557: }
558:
559: }
|