001: package org.mandarax.jdbc;
002:
003: /*
004: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.io.InputStream;
022: import java.io.Reader;
023: import java.math.BigDecimal;
024: import java.net.URL;
025: import java.sql.*;
026: import java.util.Calendar;
027:
028: import org.mandarax.util.logging.LogCategories;
029:
030: /**
031: * Abstract superclass for statements and prepared statements.
032: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
033: * @version 3.3.2 <29 December 2004>
034: * @since 3.0
035: */
036: public abstract class AbstractStatementImpl implements Statement,
037: LogCategories {
038: private Connection connection = null;
039: protected java.sql.ResultSet lastResultSet = null;
040: protected java.sql.ResultSet nextResultSet = null;
041: protected int maxRows = 0;
042:
043: /**
044: * Constructor.
045: * @param connection the connection used
046: */
047: public AbstractStatementImpl(Connection connection) {
048: super ();
049: this .connection = connection;
050: }
051:
052: /**
053: * Executes the given SQL statement, which may return multiple results.
054: */
055: public boolean execute() throws SQLException {
056: this .notSupportedThrowException("execute()");
057: return false;
058: }
059:
060: /**
061: * Executes the given SQL statement, which may return multiple results.
062: */
063: public boolean execute(String sql) throws SQLException {
064: nextResultSet = this .executeQuery(sql);
065: return nextResultSet != null;
066: }
067:
068: /**
069: * Submits a batch of commands to the database for execution and if all commands
070: * execute successfully, returns an array of update counts.
071: */
072: public int[] executeBatch() throws SQLException {
073: this .notSupportedThrowException("executeBatch()");
074: return null;
075: }
076:
077: /**
078: * Get the connection that created this statement.
079: * @return the connection
080: */
081: public Connection getConnection() {
082: return connection;
083: }
084:
085: /**
086: * Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement
087: * or an SQL statement that returns nothing, such as an SQL DDL statement.
088: */
089: public int executeUpdate() throws SQLException {
090: this .notSupportedThrowException("executeUpdate()");
091: return -1;
092: }
093:
094: /**
095: * Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement
096: * or an SQL statement that returns nothing, such as an SQL DDL statement.
097: */
098: public int executeUpdate(String sql) throws SQLException {
099: this .notSupportedThrowException("executeUpdate(String)");
100: return -1;
101: }
102:
103: /**
104: * Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement
105: * or an SQL statement that returns nothing, such as an SQL DDL statement.
106: */
107: public int executeUpdate(String sql, int autoGeneratedKeys)
108: throws SQLException {
109: this .notSupportedThrowException("executeUpdate(String,int)");
110: return -1;
111: }
112:
113: /**
114: * Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement
115: * or an SQL statement that returns nothing, such as an SQL DDL statement.
116: */
117: public int executeUpdate(String sql, int[] columnIndexes)
118: throws SQLException {
119: this .notSupportedThrowException("executeUpdate(String,int[])");
120: return -1;
121: }
122:
123: /**
124: * Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement
125: * or an SQL statement that returns nothing, such as an SQL DDL statement.
126: */
127: public int executeUpdate(String sql, String[] columnNames)
128: throws SQLException {
129: this
130: .notSupportedThrowException("executeUpdate(String,String[])");
131: return -1;
132: }
133:
134: /**
135: * Executes the given SQL statement, which may return multiple results, and signals the driver
136: * that any auto-generated keys should be made available for retrieval.
137: */
138: public boolean execute(String sql, int autoGeneratedKeys)
139: throws SQLException {
140: if (LOG_JDBC.isDebugEnabled())
141: LOG_JDBC
142: .debug("Ignore parameter autoGenerateKeys in execute");
143: return execute(sql);
144: }
145:
146: /**
147: * Executes the given SQL statement, which may return multiple results, and signals the driver
148: * that the auto-generated keys indicated in the given array should be made available for retrieval.
149: */
150: public boolean execute(String sql, int[] columnIndexes)
151: throws SQLException {
152: if (LOG_JDBC.isDebugEnabled())
153: LOG_JDBC.debug("Ignore parameter columnIndexes in execute");
154: return execute(sql);
155: }
156:
157: /**
158: * Executes the given SQL statement, which may return multiple results, and signals the driver
159: * that the auto-generated keys indicated in the given array should be made available for retrieval.
160: */
161: public boolean execute(String sql, String[] columnNames)
162: throws SQLException {
163: if (LOG_JDBC.isDebugEnabled())
164: LOG_JDBC.debug("Ignore parameter columnNames in execute");
165: return execute(sql);
166: }
167:
168: /**
169: * Log a call of an unsupported method.
170: * @param methodName the name of the method
171: */
172: protected void notSupported(String methodName) {
173: JDBCUtils.notSupported(methodName, this );
174: }
175:
176: /**
177: * Log a call of an unsupported method.
178: * This call throws an exception.
179: * @param methodName the name of the method
180: */
181: protected void notSupportedThrowException(String methodName)
182: throws SQLException {
183: JDBCUtils.notSupportedThrowException(methodName, this );
184: }
185:
186: /**
187: * Releases this Statement object's database and JDBC resources immediately instead of
188: * waiting for this to happen when it is automatically closed.
189: */
190: public void close() throws SQLException {
191: // nothing to do here
192: }
193:
194: /**
195: * Retrieves the maximum number of bytes that can be returned for character and binary column values
196: * in a ResultSet object produced by this Statement object. Zero means there is no limit
197: * @return the max field size
198: */
199: public int getMaxFieldSize() throws SQLException {
200: return 0;
201: }
202:
203: /**
204: * Sets the limit for the maximum number of bytes in a ResultSet
205: * column storing character or binary values to the given number of bytes.
206: */
207: public void setMaxFieldSize(int max) throws SQLException {
208: this .notSupported("setMaxFieldSize(int)");
209: }
210:
211: /**
212: * Retrieves the maximum number of rows that a ResultSet object produced by this Statement object can contain.
213: * If this limit is exceeded, the excess rows are silently dropped. Zero means there is no limit.
214: * @return the current maximum number of rows for a ResultSet object produced by this Statement object
215: */
216: public int getMaxRows() throws SQLException {
217: return this .maxRows;
218: }
219:
220: /**
221: * Sets the limit for the maximum number of rows that any ResultSet object can contain to the given number.
222: * If the limit is exceeded, the excess rows are silently dropped.
223: * @param max the new max rows limit; zero means there is no limit
224: */
225: public void setMaxRows(int max) throws SQLException {
226: this .maxRows = max;
227: }
228:
229: /**
230: * Sets escape processing on or off.
231: * @param enable true to enable escape processing; false to disable it
232: */
233: public void setEscapeProcessing(boolean enable) throws SQLException {
234: this .notSupported("setEscapeProcessing(boolean)");
235: }
236:
237: /**
238: * Retrieves the number of seconds the driver will wait for a Statement object to execute.
239: * If the limit is exceeded, a SQLException is thrown.
240: * @return the current query timeout limit in seconds; zero means there is no limit
241: */
242: public int getQueryTimeout() throws SQLException {
243: return 0;
244: }
245:
246: /**
247: * Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds.
248: * If the limit is exceeded, an SQLException is thrown.
249: * @param seconds the new query timeout limit in seconds; zero means there is no limit
250: */
251: public void setQueryTimeout(int seconds) throws SQLException {
252: this .notSupported("setQueryTimeout(int)");
253: }
254:
255: /**
256: * Cancels this Statement object if both the DBMS and driver support aborting an SQL statement.
257: */
258: public void cancel() throws SQLException {
259: this .notSupported("cancel()");
260: }
261:
262: /**
263: * Retrieves the first warning reported by calls on this Statement object.
264: */
265: public SQLWarning getWarnings() throws SQLException {
266: this .notSupported("getWarnings()");
267: return null;
268: }
269:
270: /**
271: * Clears all the warnings reported on this Statement object.
272: */
273: public void clearWarnings() throws SQLException {
274: this .notSupported("clearWarnings()");
275: }
276:
277: /*
278: * Sets the SQL cursor name to the given String, which will be used by
279: * subsequent Statement object execute methods.
280: */
281: public void setCursorName(String name) throws SQLException {
282: this .notSupported("setCursorName(String)");
283: }
284:
285: /**
286: * Retrieves the current result as a ResultSet object.
287: * This method should be called only once per result.
288: */
289: public java.sql.ResultSet getResultSet() throws SQLException {
290: // TODO not clear from the spec whether getMoreResults must be called before calling getResultSet !
291: if (nextResultSet != null)
292: return nextResultSet;
293: return lastResultSet;
294:
295: }
296:
297: /**
298: * Retrieves the current result as an update count; if the result is a ResultSet
299: * object or there are no more results, -1 is returned.
300: * This method should be called only once per result.
301: */
302: public int getUpdateCount() throws SQLException {
303: return -1;
304: }
305:
306: /**
307: * Moves to this Statement object's next result, returns true
308: * if it is a ResultSet object, and implicitly closes any current ResultSet
309: * object(s) obtained with the method getResultSet.
310: */
311: public boolean getMoreResults() throws SQLException {
312: return getMoreResults(KEEP_CURRENT_RESULT);
313: }
314:
315: /**
316: * Gives the driver a hint as to the direction in which rows will be processed in ResultSet
317: * objects created using this Statement object. The default value is ResultSet.FETCH_FORWARD.
318: * @param direction the initial direction for processing rows
319: */
320: public void setFetchDirection(int direction) throws SQLException {
321: this .notSupported("setFetchDirection(int)");
322: }
323:
324: /**
325: * Retrieves the direction for fetching rows from database tables that is the default for
326: * result sets generated from this Statement object.
327: * @return the default fetch direction for result sets generated from this Statement object
328: */
329: public int getFetchDirection() throws SQLException {
330: return java.sql.ResultSet.FETCH_FORWARD;
331: }
332:
333: /**
334: * Gives the JDBC driver a hint as to the number of rows that should be fetched from the database
335: * when more rows are needed. The number of rows specified affects only result sets created using
336: * this statement. If the value specified is zero, then the hint is ignored. The default value is zero.
337: * @param rows the number of rows to fetch
338: */
339: public void setFetchSize(int rows) throws SQLException {
340: this .notSupported("setFetchSize(int)");
341: }
342:
343: /**
344: * Retrieves the number of result set rows that is the default fetch size for ResultSet objects generated
345: * from this Statement object.
346: * @return the default fetch size for result sets generated from this Statement object
347: */
348: public int getFetchSize() throws SQLException {
349: return 0;
350: }
351:
352: /**
353: * Retrieves the result set concurrency for ResultSet objects generated by this Statement object.
354: * @return always ResultSet.CONCUR_READ_ONLY
355: */
356: public int getResultSetConcurrency() throws SQLException {
357: return java.sql.ResultSet.CONCUR_READ_ONLY;
358: }
359:
360: /**
361: * Retrieves the result set type for ResultSet objects generated by this Statement object.
362: * @return one of ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
363: */
364: public int getResultSetType() throws SQLException {
365: return java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE;
366: }
367:
368: /**
369: * Adds the given SQL command to the current list of commmands for this Statement object.
370: * The commands in this list can be executed as a batch by calling the method executeBatch.
371: */
372: public void addBatch(String sql) throws SQLException {
373: this .notSupportedThrowException("addBatch(String)");
374: }
375:
376: /**
377: * Empties this Statement object's current list of SQL commands.
378: */
379: public void clearBatch() throws SQLException {
380: this .notSupportedThrowException("clearBatch()");
381: }
382:
383: /**
384: * Moves to this Statement object's next result, deals with any current ResultSet
385: * object(s) according to the instructions specified by the given flag,
386: * and returns true if the next result is a ResultSet object.
387: * @param current - one of the following Statement constants indicating what
388: * should happen to current ResultSet objects obtained using the method getResultSetCLOSE_CURRENT_RESULT, KEEP_CURRENT_RESULT, or CLOSE_ALL_RESULTS
389: */
390: public boolean getMoreResults(int current) throws SQLException {
391: // process last result set
392: if (lastResultSet != null) {
393: if (current == CLOSE_CURRENT_RESULT
394: || current == CLOSE_ALL_RESULTS)
395: lastResultSet.close();
396: }
397: // set new result set
398: if (nextResultSet != null && current == CLOSE_ALL_RESULTS)
399: nextResultSet.close();
400: lastResultSet = nextResultSet;
401: nextResultSet = null;
402: return lastResultSet != null;
403: }
404:
405: /**
406: * Retrieves any auto-generated keys created as a result of executing this Statement object.
407: * If this Statement object did not generate any keys, an empty ResultSet object is returned.
408: * @param a ResultSet object containing the auto-generated key(s) generated by the execution of this Statement object
409: */
410: public java.sql.ResultSet getGeneratedKeys() throws SQLException {
411: return StaticResultSet.getEmptyResultSet(new String[] {});
412: }
413:
414: /**
415: * Retrieves the result set holdability for ResultSet objects generated by this Statement object.
416: * @return either ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT
417: */
418: public int getResultSetHoldability() throws SQLException {
419: // this does not matter anyway!
420: return java.sql.ResultSet.CLOSE_CURSORS_AT_COMMIT;
421: }
422:
423: /**
424: * All set methods use setObject(int,Object), primitives are wrapped.
425: */
426: public void setNull(int parameterIndex, int sqlType)
427: throws SQLException {
428: setObject(parameterIndex, null);
429: }
430:
431: /**
432: * All set methods use setObject(int,Object), primitives are wrapped.
433: */
434: public void setBoolean(int parameterIndex, boolean x)
435: throws SQLException {
436: setObject(parameterIndex, new Boolean(x));
437: }
438:
439: /**
440: * All set methods use setObject(int,Object), primitives are wrapped.
441: */
442: public void setByte(int parameterIndex, byte x) throws SQLException {
443: setObject(parameterIndex, new Byte(x));
444: }
445:
446: /**
447: * All set methods use setObject(int,Object), primitives are wrapped.
448: */
449: public void setShort(int parameterIndex, short x)
450: throws SQLException {
451: setObject(parameterIndex, new Short(x));
452: }
453:
454: /**
455: * All set methods use setObject(int,Object), primitives are wrapped.
456: */
457: public void setInt(int parameterIndex, int x) throws SQLException {
458: setObject(parameterIndex, new Integer(x));
459: }
460:
461: /**
462: * All set methods use setObject(int,Object), primitives are wrapped.
463: */
464: public void setLong(int parameterIndex, long x) throws SQLException {
465: setObject(parameterIndex, new Long(x));
466: }
467:
468: /**
469: * All set methods use setObject(int,Object), primitives are wrapped.
470: */
471: public void setFloat(int parameterIndex, float x)
472: throws SQLException {
473: setObject(parameterIndex, new Float(x));
474: }
475:
476: /**
477: * All set methods use setObject(int,Object), primitives are wrapped.
478: */
479: public void setDouble(int parameterIndex, double x)
480: throws SQLException {
481: setObject(parameterIndex, new Double(x));
482: }
483:
484: /**
485: * All set methods use setObject(int,Object), primitives are wrapped.
486: */
487: public void setBigDecimal(int parameterIndex, BigDecimal x)
488: throws SQLException {
489: setObject(parameterIndex, x);
490: }
491:
492: /**
493: * All set methods use setObject(int,Object), primitives are wrapped.
494: */
495: public void setString(int parameterIndex, String x)
496: throws SQLException {
497: setObject(parameterIndex, x);
498: }
499:
500: /**
501: * All set methods use setObject(int,Object), primitives are wrapped.
502: */
503: public void setBytes(int parameterIndex, byte[] x)
504: throws SQLException {
505: this .setObject(parameterIndex, x);
506: }
507:
508: /**
509: * All set methods use setObject(int,Object), primitives are wrapped.
510: */
511: public void setDate(int parameterIndex, Date x) throws SQLException {
512: setObject(parameterIndex, x);
513: }
514:
515: /**
516: * All set methods use setObject(int,Object), primitives are wrapped.
517: */
518: public void setTime(int parameterIndex, Time x) throws SQLException {
519: setObject(parameterIndex, x);
520: }
521:
522: /**
523: * All set methods use setObject(int,Object), primitives are wrapped.
524: */
525: public void setTimestamp(int parameterIndex, Timestamp x)
526: throws SQLException {
527: setObject(parameterIndex, x);
528: }
529:
530: /**
531: * All set methods use setObject(int,Object), primitives are wrapped.
532: */
533: public void setAsciiStream(int parameterIndex, InputStream x,
534: int length) throws SQLException {
535: setObject(parameterIndex, x);
536: }
537:
538: /**
539: * All set methods use setObject(int,Object), primitives are wrapped.
540: */
541: public void setUnicodeStream(int parameterIndex, InputStream x,
542: int length) throws SQLException {
543: setObject(parameterIndex, x);
544:
545: }
546:
547: /**Generated method stub. This method is not supported by the mandarax jdbc driver.
548: * @see java.sql.PreparedStatement#setBinaryStream(int, java.io.InputStream, int)
549: */
550: public void setBinaryStream(int parameterIndex, InputStream x,
551: int length) throws SQLException {
552: setObject(parameterIndex, x);
553:
554: }
555:
556: /**
557: * Set the object on a certain index.
558: * @param parameterIndex the index
559: * @param x the object
560: * @param targetSqlType will be ignored!
561: * @param scale will be ignored!
562: */
563: public void setObject(int parameterIndex, Object x,
564: int targetSqlType, int scale) throws SQLException {
565: setObject(parameterIndex, x);
566: }
567:
568: /**
569: * Set the object on a certain index.
570: * @param parameterIndex the index
571: * @param x the object
572: * @param targetSqlType will be ignored!
573: */
574: public void setObject(int parameterIndex, Object x,
575: int targetSqlType) throws SQLException {
576: setObject(parameterIndex, x);
577:
578: }
579:
580: /**
581: * All set methods use setObject(int,Object), primitives are wrapped.
582: */
583: public void setCharacterStream(int parameterIndex, Reader reader,
584: int length) throws SQLException {
585: setObject(parameterIndex, reader);
586: }
587:
588: /**
589: * All set methods use setObject(int,Object), primitives are wrapped.
590: */
591: public void setRef(int i, Ref x) throws SQLException {
592: this .setObject(i, x);
593: }
594:
595: /**
596: * All set methods use setObject(int,Object), primitives are wrapped.
597: */
598: public void setBlob(int i, Blob x) throws SQLException {
599: this .setObject(i, x);
600: }
601:
602: /**
603: * All set methods use setObject(int,Object), primitives are wrapped.
604: */
605: public void setClob(int i, Clob x) throws SQLException {
606: this .setObject(i, x);
607: }
608:
609: /**
610: * All set methods use setObject(int,Object), primitives are wrapped.
611: */
612: public void setArray(int i, Array x) throws SQLException {
613: this .setObject(i, x);
614: }
615:
616: /**
617: * All set methods use setObject(int,Object), primitives are wrapped.
618: * TODO check
619: */
620: public void setDate(int parameterIndex, Date x, Calendar cal)
621: throws SQLException {
622: cal.setTime(x);
623: this .setObject(parameterIndex, cal.getTime());
624: }
625:
626: /**
627: * All set methods use setObject(int,Object), primitives are wrapped.
628: * TODO check
629: */
630: public void setTime(int parameterIndex, Time x, Calendar cal)
631: throws SQLException {
632: cal.setTime(x);
633: this .setObject(parameterIndex, cal.getTime());
634: }
635:
636: /**
637: * All set methods use setObject(int,Object), primitives are wrapped. ]
638: * TODO check
639: */
640: public void setTimestamp(int parameterIndex, Timestamp x,
641: Calendar cal) throws SQLException {
642: cal.setTime(x);
643: this .setObject(parameterIndex, cal.getTime());
644: }
645:
646: /**
647: * Generated method stub. This method is not supported by the mandarax jdbc driver.
648: */
649: public void setNull(int paramIndex, int sqlType, String typeName)
650: throws SQLException {
651: this .setObject(paramIndex, null);
652: }
653:
654: /**
655: * All set methods use setObject(int,Object), primitives are wrapped.
656: */
657: public void setURL(int parameterIndex, URL x) throws SQLException {
658: this .setObject(parameterIndex, x);
659: }
660:
661: /**
662: * Set the object on a certain index.
663: * @param parameterIndex the index
664: * @param x the object
665: */
666: public abstract void setObject(int parameterIndex, Object x)
667: throws SQLException;
668:
669: /**
670: * Retrieves a ResultSetMetaData object that contains information about the
671: * columns of the ResultSet object that will be returned when this PreparedStatement
672: * object is executed.
673: * @return the description of a ResultSet object's columns or null if the driver cannot return a ResultSetMetaData object
674: * @throws SQLException
675: * @see java.sql.PreparedStatement#getMetaData()
676: */
677: public ResultSetMetaData getMetaData() throws SQLException {
678: this .notSupported("getMetaData()");
679: return null;
680: }
681:
682: /**
683: * Adds a set of parameters to this PreparedStatement object's batch of commands.
684: */
685: public void addBatch() throws SQLException {
686: this .notSupportedThrowException("addBatch()");
687: }
688:
689: /**
690: * Retrieves the number, types and properties of this PreparedStatement object's parameters.
691: * TODO
692: * @return ParameterMetaData
693: * @throws SQLException
694: */
695: public ParameterMetaData getParameterMetaData() throws SQLException {
696: this .notSupported("getParameterMetaData()");
697: return null;
698: }
699:
700: }
|