001: /*
002: * XAPool: Open Source XA JDBC Pool
003: * Copyright (C) 2003 Objectweb.org
004: * Initial Developer: Lutris Technologies Inc.
005: * Contact: xapool-public@lists.debian-sf.objectweb.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: */
022: package org.enhydra.jdbc.core;
023:
024: import org.enhydra.jdbc.util.Logger;
025: import org.enhydra.jdbc.util.JdbcUtil;
026:
027: import java.io.InputStream;
028: import java.io.Reader;
029: import java.math.BigDecimal;
030: import java.sql.Array;
031: import java.sql.Blob;
032: import java.sql.Clob;
033: import java.sql.Connection;
034: import java.sql.Date;
035: import java.sql.PreparedStatement;
036: import java.sql.Ref;
037: import java.sql.ResultSet;
038: import java.sql.ResultSetMetaData;
039: import java.sql.SQLException;
040: import java.sql.SQLWarning;
041: import java.sql.Time;
042: import java.sql.Timestamp;
043: import java.util.Calendar;
044:
045: /**
046: * A very simple implementation of PreparedStatement. When created
047: * it is supplied with another PreparedStatement to which all
048: * of this class' methods delegate their work.
049: */
050: public abstract class CorePreparedStatement extends JdbcUtil implements
051: PreparedStatement {
052:
053: public PreparedStatement ps;
054:
055: // the PreparedStatement which does most of the work.
056:
057: public void setLogger(Logger alog) {
058: log = alog;
059: }
060:
061: public void addBatch() throws SQLException {
062: //preInvoke();
063: try {
064: ps.addBatch();
065: } catch (SQLException e) {
066: catchInvoke(e);
067: }
068: }
069:
070: public void clearParameters() throws SQLException {
071: //preInvoke();
072: try {
073: ps.clearParameters();
074: } catch (SQLException e) {
075: catchInvoke(e);
076: }
077: }
078:
079: public boolean execute() throws SQLException {
080: //preInvoke();
081: try {
082: return ps.execute();
083: } catch (SQLException e) {
084: catchInvoke(e);
085: }
086: return false;
087: }
088:
089: public ResultSet executeQuery() throws SQLException {
090: //preInvoke();
091: try {
092: return ps.executeQuery();
093: } catch (SQLException e) {
094: catchInvoke(e);
095: }
096: return null;
097: }
098:
099: public int executeUpdate() throws SQLException {
100: //preInvoke();
101: try {
102: return ps.executeUpdate();
103: } catch (SQLException e) {
104: catchInvoke(e);
105: }
106: return 0;
107: }
108:
109: public ResultSetMetaData getMetaData() throws SQLException {
110: //preInvoke();
111: try {
112: return ps.getMetaData();
113: } catch (SQLException e) {
114: catchInvoke(e);
115: }
116: return null;
117: }
118:
119: public void setArray(int i, Array x) throws SQLException {
120: //preInvoke();
121: try {
122: ps.setArray(i, x);
123: } catch (SQLException e) {
124: catchInvoke(e);
125: }
126: }
127:
128: public void setAsciiStream(int parameterIndex, InputStream x,
129: int length) throws SQLException {
130: //preInvoke();
131: try {
132: ps.setAsciiStream(parameterIndex, x, length);
133: } catch (SQLException e) {
134: catchInvoke(e);
135: }
136: }
137:
138: public void setBigDecimal(int parameterIndex, BigDecimal x)
139: throws SQLException {
140: //preInvoke();
141: try {
142: ps.setBigDecimal(parameterIndex, x);
143: } catch (SQLException e) {
144: catchInvoke(e);
145: }
146: }
147:
148: public void setBinaryStream(int parameterIndex, InputStream x,
149: int length) throws SQLException {
150: //preInvoke();
151: try {
152: ps.setBinaryStream(parameterIndex, x, length);
153: } catch (SQLException e) {
154: catchInvoke(e);
155: }
156: }
157:
158: public void setBlob(int i, Blob x) throws SQLException {
159: //preInvoke();
160: try {
161: ps.setBlob(i, x);
162: } catch (SQLException e) {
163: catchInvoke(e);
164: }
165: }
166:
167: public void setBoolean(int parameterIndex, boolean x)
168: throws SQLException {
169: //preInvoke();
170: try {
171: ps.setBoolean(parameterIndex, x);
172: } catch (SQLException e) {
173: catchInvoke(e);
174: }
175: }
176:
177: public void setByte(int parameterIndex, byte x) throws SQLException {
178: //preInvoke();
179: try {
180: ps.setByte(parameterIndex, x);
181: } catch (SQLException e) {
182: catchInvoke(e);
183: }
184: }
185:
186: public void setBytes(int parameterIndex, byte x[])
187: throws SQLException {
188: //preInvoke();
189: try {
190: ps.setBytes(parameterIndex, x);
191: } catch (SQLException e) {
192: catchInvoke(e);
193: }
194: }
195:
196: public void setCharacterStream(int parameterIndex, Reader reader,
197: int length) throws SQLException {
198: //preInvoke();
199: try {
200: ps.setCharacterStream(parameterIndex, reader, length);
201: } catch (SQLException e) {
202: catchInvoke(e);
203: }
204: }
205:
206: public void setClob(int i, Clob x) throws SQLException {
207: //preInvoke();
208: try {
209: ps.setClob(i, x);
210: } catch (SQLException e) {
211: catchInvoke(e);
212: }
213: }
214:
215: public void setDate(int parameterIndex, Date x) throws SQLException {
216: //preInvoke();
217: try {
218: ps.setDate(parameterIndex, x);
219: } catch (SQLException e) {
220: catchInvoke(e);
221: }
222: }
223:
224: public void setDate(int parameterIndex, Date x, Calendar cal)
225: throws SQLException {
226: //preInvoke();
227: try {
228: ps.setDate(parameterIndex, x, cal);
229: } catch (SQLException e) {
230: catchInvoke(e);
231: }
232: }
233:
234: public void setDouble(int parameterIndex, double x)
235: throws SQLException {
236: //preInvoke();
237: try {
238: ps.setDouble(parameterIndex, x);
239: } catch (SQLException e) {
240: catchInvoke(e);
241: }
242: }
243:
244: public void setFloat(int parameterIndex, float x)
245: throws SQLException {
246: //preInvoke();
247: try {
248: ps.setFloat(parameterIndex, x);
249: } catch (SQLException e) {
250: catchInvoke(e);
251: }
252: }
253:
254: public void setInt(int parameterIndex, int x) throws SQLException {
255: //preInvoke();
256: try {
257: ps.setInt(parameterIndex, x);
258: } catch (SQLException e) {
259: catchInvoke(e);
260: }
261: }
262:
263: public void setLong(int parameterIndex, long x) throws SQLException {
264: //preInvoke();
265: try {
266: ps.setLong(parameterIndex, x);
267: } catch (SQLException e) {
268: catchInvoke(e);
269: }
270: }
271:
272: public void setNull(int parameterIndex, int sqlType)
273: throws SQLException {
274: //preInvoke();
275: try {
276: ps.setNull(parameterIndex, sqlType);
277: } catch (SQLException e) {
278: catchInvoke(e);
279: }
280: }
281:
282: public void setNull(int paramIndex, int sqlType, String typeName)
283: throws SQLException {
284: //preInvoke();
285: try {
286: ps.setNull(paramIndex, sqlType, typeName);
287: } catch (SQLException e) {
288: catchInvoke(e);
289: }
290: }
291:
292: public void setObject(int parameterIndex, Object x)
293: throws SQLException {
294: //preInvoke();
295: try {
296: ps.setObject(parameterIndex, x);
297: } catch (SQLException e) {
298: catchInvoke(e);
299: }
300: }
301:
302: public void setObject(int parameterIndex, Object x,
303: int targetSqlType) throws SQLException {
304: //preInvoke();
305: try {
306: ps.setObject(parameterIndex, x, targetSqlType);
307: } catch (SQLException e) {
308: catchInvoke(e);
309: }
310: }
311:
312: public void setObject(int parameterIndex, Object x,
313: int targetSqlType, int scale) throws SQLException {
314: //preInvoke();
315: try {
316: ps.setObject(parameterIndex, x, targetSqlType, scale);
317: } catch (SQLException e) {
318: catchInvoke(e);
319: }
320: }
321:
322: public void setRef(int i, Ref x) throws SQLException {
323: //preInvoke();
324: try {
325: ps.setRef(i, x);
326: } catch (SQLException e) {
327: catchInvoke(e);
328: }
329: }
330:
331: public void setShort(int parameterIndex, short x)
332: throws SQLException {
333: //preInvoke();
334: try {
335: ps.setShort(parameterIndex, x);
336: } catch (SQLException e) {
337: catchInvoke(e);
338: }
339: }
340:
341: public void setString(int parameterIndex, String x)
342: throws SQLException {
343: //preInvoke();
344: try {
345: ps.setString(parameterIndex, x);
346: } catch (SQLException e) {
347: catchInvoke(e);
348: }
349: }
350:
351: public void setTime(int parameterIndex, Time x) throws SQLException {
352: //preInvoke();
353: try {
354: ps.setTime(parameterIndex, x);
355: } catch (SQLException e) {
356: catchInvoke(e);
357: }
358: }
359:
360: public void setTime(int parameterIndex, Time x, Calendar cal)
361: throws SQLException {
362: //preInvoke();
363: try {
364: ps.setTime(parameterIndex, x, cal);
365: } catch (SQLException e) {
366: catchInvoke(e);
367: }
368: }
369:
370: public void setTimestamp(int parameterIndex, Timestamp x)
371: throws SQLException {
372: //preInvoke();
373: try {
374: ps.setTimestamp(parameterIndex, x);
375: } catch (SQLException e) {
376: catchInvoke(e);
377: }
378: }
379:
380: public void setTimestamp(int parameterIndex, Timestamp x,
381: Calendar cal) throws SQLException {
382: //preInvoke();
383: try {
384: ps.setTimestamp(parameterIndex, x, cal);
385: } catch (SQLException e) {
386: catchInvoke(e);
387: }
388: }
389:
390: public void setUnicodeStream(int parameterIndex, InputStream x,
391: int length) throws SQLException {
392: //preInvoke();
393: try {
394: ps.setUnicodeStream(parameterIndex, x, length);
395: } catch (SQLException e) {
396: catchInvoke(e);
397: }
398: }
399:
400: // From Statement
401:
402: public void close() throws SQLException {
403: if (ps != null) {
404: ps.close();
405: }
406: }
407:
408: public int[] executeBatch() throws SQLException {
409: //preInvoke();
410: try {
411: return ps.executeBatch();
412: } catch (SQLException e) {
413: catchInvoke(e);
414: }
415: return null;
416: }
417:
418: public int getMaxFieldSize() throws SQLException {
419: //preInvoke();
420: try {
421: return ps.getMaxFieldSize();
422: } catch (SQLException e) {
423: catchInvoke(e);
424: }
425: return 0;
426: }
427:
428: public void setMaxFieldSize(int max) throws SQLException {
429: //preInvoke();
430: try {
431: ps.setMaxFieldSize(max);
432: } catch (SQLException e) {
433: catchInvoke(e);
434: }
435: }
436:
437: public int getMaxRows() throws SQLException {
438: //preInvoke();
439: try {
440: return ps.getMaxRows();
441: } catch (SQLException e) {
442: catchInvoke(e);
443: }
444: return 0;
445: }
446:
447: public void setMaxRows(int max) throws SQLException {
448: //preInvoke();
449: try {
450: ps.setMaxRows(max);
451: } catch (SQLException e) {
452: catchInvoke(e);
453: }
454: }
455:
456: public void setEscapeProcessing(boolean enable) throws SQLException {
457: //preInvoke();
458: try {
459: ps.setEscapeProcessing(enable);
460: } catch (SQLException e) {
461: catchInvoke(e);
462: }
463: }
464:
465: public int getQueryTimeout() throws SQLException {
466: //preInvoke();
467: try {
468: return ps.getQueryTimeout();
469: } catch (SQLException e) {
470: catchInvoke(e);
471: }
472: return 0;
473: }
474:
475: public void setQueryTimeout(int seconds) throws SQLException {
476: //preInvoke();
477: try {
478: ps.setQueryTimeout(seconds);
479: } catch (SQLException e) {
480: catchInvoke(e);
481: }
482: }
483:
484: public void cancel() throws SQLException {
485: //preInvoke();
486: try {
487: ps.cancel();
488: } catch (SQLException e) {
489: catchInvoke(e);
490: }
491: }
492:
493: public SQLWarning getWarnings() throws SQLException {
494: //preInvoke();
495: try {
496: return ps.getWarnings();
497: } catch (SQLException e) {
498: catchInvoke(e);
499: }
500: return null;
501: }
502:
503: public void clearWarnings() throws SQLException {
504: //preInvoke();
505: try {
506: ps.clearWarnings();
507: } catch (SQLException e) {
508: catchInvoke(e);
509: }
510: }
511:
512: public void setCursorName(String name) throws SQLException {
513: //preInvoke();
514: try {
515: ps.setCursorName(name);
516: } catch (SQLException e) {
517: catchInvoke(e);
518: }
519: }
520:
521: public ResultSet getResultSet() throws SQLException {
522: //preInvoke();
523: try {
524: return ps.getResultSet();
525: } catch (SQLException e) {
526: catchInvoke(e);
527: }
528: return null;
529: }
530:
531: public int getUpdateCount() throws SQLException {
532: //preInvoke();
533: try {
534: return ps.getUpdateCount();
535: } catch (SQLException e) {
536: catchInvoke(e);
537: }
538: return 0;
539: }
540:
541: public boolean getMoreResults() throws SQLException {
542: //preInvoke();
543: try {
544: return ps.getMoreResults();
545: } catch (SQLException e) {
546: catchInvoke(e);
547: }
548: return false;
549: }
550:
551: public void setFetchDirection(int direction) throws SQLException {
552: //preInvoke();
553: try {
554: ps.setFetchDirection(direction);
555: } catch (SQLException e) {
556: catchInvoke(e);
557: }
558: }
559:
560: public int getFetchDirection() throws SQLException {
561: //preInvoke();
562: try {
563: return ps.getFetchDirection();
564: } catch (SQLException e) {
565: catchInvoke(e);
566: }
567: return 0;
568: }
569:
570: public void setFetchSize(int rows) throws SQLException {
571: //preInvoke();
572: try {
573: ps.setFetchSize(rows);
574: } catch (SQLException e) {
575: catchInvoke(e);
576: }
577: }
578:
579: public int getFetchSize() throws SQLException {
580: //preInvoke();
581: try {
582: return ps.getFetchSize();
583: } catch (SQLException e) {
584: catchInvoke(e);
585: }
586: return 0;
587: }
588:
589: public int getResultSetConcurrency() throws SQLException {
590: //preInvoke();
591: try {
592: return ps.getResultSetConcurrency();
593: } catch (SQLException e) {
594: catchInvoke(e);
595: }
596: return 0;
597: }
598:
599: public int getResultSetType() throws SQLException {
600: //preInvoke();
601: try {
602: return ps.getResultSetType();
603: } catch (SQLException e) {
604: catchInvoke(e);
605: }
606: return 0;
607: }
608:
609: public Connection getConnection() throws SQLException {
610: //preInvoke();
611: try {
612: return ps.getConnection();
613: } catch (SQLException e) {
614: catchInvoke(e);
615: }
616: return null;
617: }
618:
619: public void clearBatch() throws SQLException {
620: //preInvoke();
621: try {
622: ps.clearBatch();
623: } catch (SQLException e) {
624: catchInvoke(e);
625: }
626: }
627:
628: public void addBatch(String s) throws SQLException {
629: //preInvoke();
630: try {
631: ps.addBatch(s);
632: } catch (SQLException e) {
633: catchInvoke(e);
634: }
635: }
636:
637: public boolean execute(String s) throws SQLException {
638: //preInvoke();
639: try {
640: return ps.execute(s);
641: } catch (SQLException e) {
642: catchInvoke(e);
643: }
644: return false;
645: }
646:
647: public ResultSet executeQuery(String s) throws SQLException {
648: //preInvoke();
649: try {
650: return ps.executeQuery(s);
651: } catch (SQLException e) {
652: catchInvoke(e);
653: }
654: return null;
655: }
656:
657: public int executeUpdate(String s) throws SQLException {
658: //preInvoke();
659: try {
660: return ps.executeUpdate(s);
661: } catch (SQLException e) {
662: catchInvoke(e);
663: }
664: return 0;
665: }
666:
667: /*
668: * Add those following methods to compile on JDK 1.4.
669: * Instead those methods are defined in the java.sql.PreparedStatement interface
670: * only since JDK 1.4.
671: */
672: // java.sql.Statements methods
673: public boolean execute(String sql, int autoGeneratedKeys)
674: throws SQLException {
675: try {
676: return ps.execute(sql, autoGeneratedKeys);
677: } catch (SQLException e) {
678: catchInvoke(e);
679: }
680: return false;
681: }
682:
683: public boolean execute(String sql, int[] columnIndexes)
684: throws SQLException {
685: try {
686: return ps.execute(sql, columnIndexes);
687: } catch (SQLException e) {
688: catchInvoke(e);
689: }
690: return false;
691: }
692:
693: public boolean execute(String sql, String[] columnNames)
694: throws SQLException {
695: try {
696: return ps.execute(sql, columnNames);
697: } catch (SQLException e) {
698: catchInvoke(e);
699: }
700: return false;
701: }
702:
703: public int executeUpdate(String sql, int autoGeneratedKeys)
704: throws SQLException {
705: try {
706: return ps.executeUpdate(sql, autoGeneratedKeys);
707: } catch (SQLException e) {
708: catchInvoke(e);
709: }
710: return 0;
711: }
712:
713: public int executeUpdate(String sql, int[] columnIndexes)
714: throws SQLException {
715: try {
716: return ps.executeUpdate(sql, columnIndexes);
717: } catch (SQLException e) {
718: catchInvoke(e);
719: }
720: return 0;
721: }
722:
723: public int executeUpdate(String sql, String[] columnNames)
724: throws SQLException {
725: try {
726: return ps.executeUpdate(sql, columnNames);
727: } catch (SQLException e) {
728: catchInvoke(e);
729: }
730: return 0;
731: }
732:
733: public java.sql.ResultSet getGeneratedKeys() throws SQLException {
734: try {
735: return ps.getGeneratedKeys();
736: } catch (SQLException e) {
737: catchInvoke(e);
738: }
739: return null;
740: }
741:
742: public boolean getMoreResults(int current) throws SQLException {
743: try {
744: return ps.getMoreResults(current);
745: } catch (SQLException e) {
746: catchInvoke(e);
747: }
748: return false;
749: }
750:
751: public int getResultSetHoldability() throws SQLException {
752: try {
753: return ps.getResultSetHoldability();
754: } catch (SQLException e) {
755: catchInvoke(e);
756: }
757: return 0;
758: }
759:
760: // java.sql.PreparedStatement methods
761: public java.sql.ParameterMetaData getParameterMetaData()
762: throws SQLException {
763: try {
764: return ps.getParameterMetaData();
765: } catch (SQLException e) {
766: catchInvoke(e);
767: }
768: return null;
769: }
770:
771: public void setURL(int parameterIndex, java.net.URL x)
772: throws SQLException {
773: try {
774: ps.setURL(parameterIndex, x);
775: } catch (SQLException e) {
776: catchInvoke(e);
777: }
778: }
779:
780: /**
781: * Methods used to do some works before and during the catch
782: * clause, to prevent the pool that a connection is broken.
783: */
784: abstract public void preInvoke() throws SQLException;
785:
786: abstract public void catchInvoke(SQLException e)
787: throws SQLException;
788:
789: }
|