001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.sql;
024:
025: import java.io.InputStream;
026: import java.io.Reader;
027: import java.math.BigDecimal;
028: import java.net.URL;
029: import java.sql.Array;
030: import java.sql.Blob;
031: import java.sql.Clob;
032: import java.sql.Date;
033: import java.sql.ParameterMetaData;
034: import java.sql.PreparedStatement;
035: import java.sql.Ref;
036: import java.sql.ResultSet;
037: import java.sql.ResultSetMetaData;
038: import java.sql.SQLException;
039: import java.sql.Time;
040: import java.sql.Timestamp;
041: import java.util.Calendar;
042:
043: /**
044: * TODO Add WrappedPreparedStatement Overview JavaDoc.
045: * <p>
046: *
047: * @author Mark A. Kobold <mkobold at isqlviewer dot com>
048: * @version 1.0
049: */
050: class PreparedStatementWrapper extends StatementWrapper implements
051: PreparedStatement {
052:
053: private PreparedStatement proxy;
054: private boolean hasLogged = false;
055:
056: public PreparedStatementWrapper(ConnectionWrapper connection,
057: PreparedStatement stmt, String sql) {
058:
059: super (connection, stmt, sql);
060: this .proxy = stmt;
061: }
062:
063: public void setCharacterStream(int parameterIndex, Reader reader,
064: int length) throws SQLException {
065:
066: connection.updateLastAccess();
067: try {
068: proxy.setCharacterStream(parameterIndex, reader, length);
069: } catch (SQLException sqle) {
070: error(messages.format("DataSource.GeneralMethodError",
071: "setCharacterStream(int,Reader,int)"), sqle);
072: throw sqle;
073: } catch (Throwable t) {
074: SQLException sqle = connection.createWrappedSQLException(t,
075: "setCharacterStream(int,Reader,int)");
076: throw sqle;
077: }
078: }
079:
080: @Override
081: public void close() throws SQLException {
082:
083: try {
084: proxy.clearParameters();
085: } catch (Throwable t) {
086: error(messages.format("DataSource.GeneralMethodError",
087: "clearParameters()"), t);
088: }
089: super .close();
090:
091: }
092:
093: public ResultSet executeQuery() throws SQLException {
094:
095: connection.updateLastAccess();
096: try {
097: logQuery();
098: ResultSetWrapper wrs = new ResultSetWrapper(this , proxy
099: .executeQuery());
100: openCursors.add(wrs);
101: return wrs;
102: } catch (SQLException sqle) {
103: error(messages.format("DataSource.GeneralMethodError",
104: "executeQuery()"), sqle);
105: throw sqle;
106: } catch (Throwable t) {
107: SQLException sqle = connection.createWrappedSQLException(t,
108: "executeQuery()");
109: throw sqle;
110: }
111: }
112:
113: public int executeUpdate() throws SQLException {
114:
115: connection.updateLastAccess();
116: try {
117: logQuery();
118: return proxy.executeUpdate();
119: } catch (SQLException sqle) {
120: error(messages.format("DataSource.GeneralMethodError",
121: "executeUpdate()"), sqle);
122: throw sqle;
123: } catch (Throwable t) {
124: SQLException sqle = connection.createWrappedSQLException(t,
125: "executeUpdate()");
126: throw sqle;
127: }
128: }
129:
130: public ParameterMetaData getParameterMetaData() throws SQLException {
131:
132: connection.updateLastAccess();
133: try {
134: return proxy.getParameterMetaData();
135: } catch (SQLException sqle) {
136: error(messages.format("DataSource.GeneralMethodError",
137: "getParameterMetaData()"), sqle);
138: throw sqle;
139: } catch (Throwable t) {
140: SQLException sqle = connection.createWrappedSQLException(t,
141: "getParameterMetaData()");
142: throw sqle;
143: }
144: }
145:
146: public void setURL(int parameterIndex, URL x) throws SQLException {
147:
148: connection.updateLastAccess();
149: try {
150: proxy.setURL(parameterIndex, x);
151: } catch (SQLException sqle) {
152: error(messages.format("DataSource.GeneralMethodError",
153: "setURL(int, URL)"), sqle);
154: throw sqle;
155: } catch (Throwable t) {
156: SQLException sqle = connection.createWrappedSQLException(t,
157: "setURL(int, URL)");
158: throw sqle;
159: }
160: }
161:
162: public void addBatch() throws SQLException {
163:
164: connection.updateLastAccess();
165: try {
166: proxy.addBatch();
167: } catch (SQLException sqle) {
168: error(messages.format("DataSource.GeneralMethodError",
169: "addBatch()"), sqle);
170: throw sqle;
171: } catch (Throwable t) {
172: SQLException sqle = connection.createWrappedSQLException(t,
173: "addBatch()");
174: throw sqle;
175: }
176: }
177:
178: public void clearParameters() throws SQLException {
179:
180: connection.updateLastAccess();
181: try {
182: proxy.clearParameters();
183: } catch (SQLException sqle) {
184: error(messages.format("DataSource.GeneralMethodError",
185: "clearParameters()"), sqle);
186: throw sqle;
187: } catch (Throwable t) {
188: SQLException sqle = connection.createWrappedSQLException(t,
189: "clearParameters()");
190: throw sqle;
191: }
192: }
193:
194: public boolean execute() throws SQLException {
195:
196: connection.updateLastAccess();
197: try {
198: logQuery();
199: return proxy.execute();
200: } catch (SQLException sqle) {
201: error(messages.format("DataSource.GeneralMethodError",
202: "execute()"), sqle);
203: throw sqle;
204: } catch (Throwable t) {
205: SQLException sqle = connection.createWrappedSQLException(t,
206: "execute()");
207: throw sqle;
208: }
209: }
210:
211: public ResultSetMetaData getMetaData() throws SQLException {
212:
213: connection.updateLastAccess();
214: try {
215: return proxy.getMetaData();
216: } catch (SQLException sqle) {
217: error(messages.format("DataSource.GeneralMethodError",
218: "getMetaData()"), sqle);
219: throw sqle;
220: } catch (Throwable t) {
221: SQLException sqle = connection.createWrappedSQLException(t,
222: "getMetaData()");
223: throw sqle;
224: }
225: }
226:
227: public void setArray(int i, Array x) throws SQLException {
228:
229: connection.updateLastAccess();
230: try {
231: proxy.setArray(i, x);
232: } catch (SQLException sqle) {
233: error(messages.format("DataSource.GeneralMethodError",
234: "setArray(int, Array)"), sqle);
235: throw sqle;
236: } catch (Throwable t) {
237: SQLException sqle = connection.createWrappedSQLException(t,
238: "setArray(int, Array)");
239: throw sqle;
240: }
241: }
242:
243: public void setAsciiStream(int parameterIndex, InputStream x,
244: int length) throws SQLException {
245:
246: connection.updateLastAccess();
247: try {
248: proxy.setAsciiStream(parameterIndex, x, length);
249: } catch (SQLException sqle) {
250: error(messages.format("DataSource.GeneralMethodError",
251: "setAsciiStream(int,InputStream,int)"), sqle);
252: throw sqle;
253: } catch (Throwable t) {
254: SQLException sqle = connection.createWrappedSQLException(t,
255: "setAsciiStream(int,InputStream,int)");
256: throw sqle;
257: }
258: }
259:
260: public void setBigDecimal(int parameterIndex, BigDecimal x)
261: throws SQLException {
262:
263: connection.updateLastAccess();
264: try {
265: proxy.setBigDecimal(parameterIndex, x);
266: } catch (SQLException sqle) {
267: error(messages.format("DataSource.GeneralMethodError",
268: "setBigDecimal(int,BigDecimal)"), sqle);
269: throw sqle;
270: } catch (Throwable t) {
271: SQLException sqle = connection.createWrappedSQLException(t,
272: "setBigDecimal(int,BigDecimal)");
273: throw sqle;
274: }
275: }
276:
277: public void setBinaryStream(int parameterIndex, InputStream x,
278: int length) throws SQLException {
279:
280: connection.updateLastAccess();
281: try {
282: proxy.setBinaryStream(parameterIndex, x, length);
283: } catch (SQLException sqle) {
284: error(messages.format("DataSource.GeneralMethodError",
285: "setBinaryStream(int,InputStream,int)"), sqle);
286: throw sqle;
287: } catch (Throwable t) {
288: SQLException sqle = connection.createWrappedSQLException(t,
289: "setBinaryStream(int,InputStream,int)");
290: throw sqle;
291: }
292: }
293:
294: public void setBlob(int i, Blob x) throws SQLException {
295:
296: connection.updateLastAccess();
297: try {
298: proxy.setBlob(i, x);
299: } catch (SQLException sqle) {
300: error(messages.format("DataSource.GeneralMethodError",
301: "setBlob(int,Blob)"), sqle);
302: throw sqle;
303: } catch (Throwable t) {
304: SQLException sqle = connection.createWrappedSQLException(t,
305: "setBlob(int,Blob)");
306: throw sqle;
307: }
308: }
309:
310: public void setBoolean(int parameterIndex, boolean x)
311: throws SQLException {
312:
313: connection.updateLastAccess();
314: try {
315: proxy.setBoolean(parameterIndex, x);
316: } catch (SQLException sqle) {
317: error(messages.format("DataSource.GeneralMethodError",
318: "setBoolean(int,boolean)"), sqle);
319: throw sqle;
320: } catch (Throwable t) {
321: SQLException sqle = connection.createWrappedSQLException(t,
322: "setBoolean(int,boolean)");
323: throw sqle;
324: }
325: }
326:
327: public void setByte(int parameterIndex, byte x) throws SQLException {
328:
329: connection.updateLastAccess();
330: try {
331: proxy.setByte(parameterIndex, x);
332: } catch (SQLException sqle) {
333: error(messages.format("DataSource.GeneralMethodError",
334: "setByte(int,byte)"), sqle);
335: throw sqle;
336: } catch (Throwable t) {
337: SQLException sqle = connection.createWrappedSQLException(t,
338: "setByte(int,byte)");
339: throw sqle;
340: }
341: }
342:
343: public void setBytes(int parameterIndex, byte[] x)
344: throws SQLException {
345:
346: connection.updateLastAccess();
347: try {
348: proxy.setBytes(parameterIndex, x);
349: } catch (SQLException sqle) {
350: error(messages.format("DataSource.GeneralMethodError",
351: "setBytes(int,byte[]))"), sqle);
352: throw sqle;
353: } catch (Throwable t) {
354: SQLException sqle = connection.createWrappedSQLException(t,
355: "setBytes(int,bytes[])");
356: throw sqle;
357: }
358: }
359:
360: public void setClob(int i, Clob x) throws SQLException {
361:
362: connection.updateLastAccess();
363: try {
364: proxy.setClob(i, x);
365: } catch (SQLException sqle) {
366: error(messages.format("DataSource.GeneralMethodError",
367: "setClob(int,Clob)"), sqle);
368: throw sqle;
369: } catch (Throwable t) {
370: SQLException sqle = connection.createWrappedSQLException(t,
371: "setClob(int,Clob)");
372: throw sqle;
373: }
374: }
375:
376: public void setDate(int parameterIndex, Date x, Calendar cal)
377: throws SQLException {
378:
379: connection.updateLastAccess();
380: try {
381: proxy.setDate(parameterIndex, x, cal);
382: } catch (SQLException sqle) {
383: error(messages.format("DataSource.GeneralMethodError",
384: "setDate(int,Date,Calendar)"), sqle);
385: throw sqle;
386: } catch (Throwable t) {
387: SQLException sqle = connection.createWrappedSQLException(t,
388: "setDate(int,Date,Calendar)");
389: throw sqle;
390: }
391: }
392:
393: public void setDate(int parameterIndex, Date x) throws SQLException {
394:
395: connection.updateLastAccess();
396: try {
397: proxy.setDate(parameterIndex, x);
398: } catch (SQLException sqle) {
399: error(messages.format("DataSource.GeneralMethodError",
400: "setDate(int,Date)"), sqle);
401: throw sqle;
402: } catch (Throwable t) {
403: SQLException sqle = connection.createWrappedSQLException(t,
404: "setDate(int,Date)");
405: throw sqle;
406: }
407: }
408:
409: public void setDouble(int parameterIndex, double x)
410: throws SQLException {
411:
412: connection.updateLastAccess();
413: try {
414: proxy.setDouble(parameterIndex, x);
415: } catch (SQLException sqle) {
416: error(messages.format("DataSource.GeneralMethodError",
417: "setDouble(int,double)"), sqle);
418: throw sqle;
419: } catch (Throwable t) {
420: SQLException sqle = connection.createWrappedSQLException(t,
421: "setDouble(int,double)");
422: throw sqle;
423: }
424: }
425:
426: public void setFloat(int parameterIndex, float x)
427: throws SQLException {
428:
429: connection.updateLastAccess();
430: try {
431: proxy.setFloat(parameterIndex, x);
432: } catch (SQLException sqle) {
433: error(messages.format("DataSource.GeneralMethodError",
434: "setFloat(int,float)"), sqle);
435: throw sqle;
436: } catch (Throwable t) {
437: SQLException sqle = connection.createWrappedSQLException(t,
438: "setFloat(int,float)");
439: throw sqle;
440: }
441: }
442:
443: public void setInt(int parameterIndex, int x) throws SQLException {
444:
445: connection.updateLastAccess();
446: try {
447: proxy.setInt(parameterIndex, x);
448: } catch (SQLException sqle) {
449: error(messages.format("DataSource.GeneralMethodError",
450: "setInt(int,int)"), sqle);
451: throw sqle;
452: } catch (Throwable t) {
453: SQLException sqle = connection.createWrappedSQLException(t,
454: "setInt(int,int)");
455: throw sqle;
456: }
457: }
458:
459: public void setLong(int parameterIndex, long x) throws SQLException {
460:
461: connection.updateLastAccess();
462: try {
463: proxy.setLong(parameterIndex, x);
464: } catch (SQLException sqle) {
465: error(messages.format("DataSource.GeneralMethodError",
466: "setLong(int,long)"), sqle);
467: throw sqle;
468: } catch (Throwable t) {
469: SQLException sqle = connection.createWrappedSQLException(t,
470: "setLong(int,long)");
471: throw sqle;
472: }
473: }
474:
475: public void setNull(int paramIndex, int sqlType, String typeName)
476: throws SQLException {
477:
478: connection.updateLastAccess();
479: try {
480: proxy.setNull(paramIndex, sqlType, typeName);
481: } catch (SQLException sqle) {
482: error(messages.format("DataSource.GeneralMethodError",
483: "setNull(int,int,String)"), sqle);
484: throw sqle;
485: } catch (Throwable t) {
486: SQLException sqle = connection.createWrappedSQLException(t,
487: "setNull(int,int,String))");
488: throw sqle;
489: }
490: }
491:
492: public void setNull(int parameterIndex, int sqlType)
493: throws SQLException {
494:
495: connection.updateLastAccess();
496: try {
497: proxy.setNull(parameterIndex, sqlType);
498: } catch (SQLException sqle) {
499: error(messages.format("DataSource.GeneralMethodError",
500: "setNull(int,int)"), sqle);
501: throw sqle;
502: } catch (Throwable t) {
503: SQLException sqle = connection.createWrappedSQLException(t,
504: "setNull(int,int)");
505: throw sqle;
506: }
507: }
508:
509: public void setObject(int parameterIndex, Object x,
510: int targetSqlType, int scale) throws SQLException {
511:
512: connection.updateLastAccess();
513: try {
514: proxy.setObject(parameterIndex, x, targetSqlType, scale);
515: } catch (SQLException sqle) {
516: error(messages.format("DataSource.GeneralMethodError",
517: "setObject(int,Object,int,int)"), sqle);
518: throw sqle;
519: } catch (Throwable t) {
520: SQLException sqle = connection.createWrappedSQLException(t,
521: "setObject(int,Object,int,int)");
522: throw sqle;
523: }
524: }
525:
526: public void setObject(int parameterIndex, Object x,
527: int targetSqlType) throws SQLException {
528:
529: connection.updateLastAccess();
530: try {
531: proxy.setObject(parameterIndex, x, targetSqlType);
532: } catch (SQLException sqle) {
533: error(messages.format("DataSource.GeneralMethodError",
534: "setObject(int,Object,int)"), sqle);
535: throw sqle;
536: } catch (Throwable t) {
537: SQLException sqle = connection.createWrappedSQLException(t,
538: "setObject(int,Object,int)");
539: throw sqle;
540: }
541: }
542:
543: public void setObject(int parameterIndex, Object x)
544: throws SQLException {
545:
546: connection.updateLastAccess();
547: try {
548: proxy.setObject(parameterIndex, x);
549: } catch (SQLException sqle) {
550: error(messages.format("DataSource.GeneralMethodError",
551: "setObject(int,Object)"), sqle);
552: throw sqle;
553: } catch (Throwable t) {
554: SQLException sqle = connection.createWrappedSQLException(t,
555: "setObject(int,Object)");
556: throw sqle;
557: }
558: }
559:
560: public void setRef(int i, Ref x) throws SQLException {
561:
562: connection.updateLastAccess();
563: try {
564: proxy.setRef(i, x);
565: } catch (SQLException sqle) {
566: error(messages.format("DataSource.GeneralMethodError",
567: "setRef(int,Ref)"), sqle);
568: throw sqle;
569: } catch (Throwable t) {
570: SQLException sqle = connection.createWrappedSQLException(t,
571: "setRef(int,Ref)");
572: throw sqle;
573: }
574: }
575:
576: public void setShort(int parameterIndex, short x)
577: throws SQLException {
578:
579: connection.updateLastAccess();
580: try {
581: proxy.setShort(parameterIndex, x);
582: } catch (SQLException sqle) {
583: error(messages.format("DataSource.GeneralMethodError",
584: "setShort(int,short)"), sqle);
585: throw sqle;
586: } catch (Throwable t) {
587: SQLException sqle = connection.createWrappedSQLException(t,
588: "setShort(int,short)");
589: throw sqle;
590: }
591: }
592:
593: public void setString(int parameterIndex, String x)
594: throws SQLException {
595:
596: connection.updateLastAccess();
597: try {
598: proxy.setString(parameterIndex, x);
599: } catch (SQLException sqle) {
600: error(messages.format("DataSource.GeneralMethodError",
601: "setString(int,String)"), sqle);
602: throw sqle;
603: } catch (Throwable t) {
604: SQLException sqle = connection.createWrappedSQLException(t,
605: "setString(int,String)");
606: throw sqle;
607: }
608: }
609:
610: public void setTime(int parameterIndex, Time x, Calendar cal)
611: throws SQLException {
612:
613: connection.updateLastAccess();
614: try {
615: proxy.setTime(parameterIndex, x, cal);
616: } catch (SQLException sqle) {
617: error(messages.format("DataSource.GeneralMethodError",
618: "setTime(int,Time,Calendar)"), sqle);
619: throw sqle;
620: } catch (Throwable t) {
621: SQLException sqle = connection.createWrappedSQLException(t,
622: "setTime(int,Time,Calendar)");
623: throw sqle;
624: }
625: }
626:
627: public void setTime(int parameterIndex, Time x) throws SQLException {
628:
629: connection.updateLastAccess();
630: try {
631: proxy.setTime(parameterIndex, x);
632: } catch (SQLException sqle) {
633: error(messages.format("DataSource.GeneralMethodError",
634: "setTime(int,Time)"), sqle);
635: throw sqle;
636: } catch (Throwable t) {
637: SQLException sqle = connection.createWrappedSQLException(t,
638: "setTime(int,Time)");
639: throw sqle;
640: }
641: }
642:
643: public void setTimestamp(int parameterIndex, Timestamp x,
644: Calendar cal) throws SQLException {
645:
646: connection.updateLastAccess();
647: try {
648: proxy.setTimestamp(parameterIndex, x, cal);
649: } catch (SQLException sqle) {
650: error(messages.format("DataSource.GeneralMethodError",
651: "setTimestamp(int,Timestamp,Calendar)"), sqle);
652: throw sqle;
653: } catch (Throwable t) {
654: SQLException sqle = connection.createWrappedSQLException(t,
655: "setTimestamp(int,Timestamp,Calendar)");
656: throw sqle;
657: }
658: }
659:
660: public void setTimestamp(int parameterIndex, Timestamp x)
661: throws SQLException {
662:
663: connection.updateLastAccess();
664: try {
665: proxy.setTimestamp(parameterIndex, x);
666: } catch (SQLException sqle) {
667: error(messages.format("DataSource.GeneralMethodError",
668: "setTimestamp(int,Timestamp)"), sqle);
669: throw sqle;
670: } catch (Throwable t) {
671: SQLException sqle = connection.createWrappedSQLException(t,
672: "setTimestamp(int,Timestamp)");
673: throw sqle;
674: }
675: }
676:
677: public void setUnicodeStream(int parameterIndex, InputStream x,
678: int length) throws SQLException {
679:
680: setAsciiStream(parameterIndex, x, length);
681: }
682:
683: private void logQuery() throws SQLException {
684:
685: if (!hasLogged) {
686: String nsql = connection.nativeSQL(lastStatement);
687: info(messages.format("DataSource.StatementExec", nsql));
688: hasLogged = true;
689: }
690: }
691: }
|