001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.sql.spy;
030:
031: import com.caucho.log.Log;
032: import com.caucho.sql.SQLExceptionWrapper;
033: import com.caucho.util.L10N;
034:
035: import java.io.InputStream;
036: import java.io.Reader;
037: import java.math.BigDecimal;
038: import java.net.URL;
039: import java.sql.*;
040: import java.util.Calendar;
041: import java.util.logging.*;
042:
043: /**
044: * Spying on a statement;
045: */
046: public class SpyPreparedStatement extends SpyStatement implements
047: java.sql.PreparedStatement {
048: protected final static Logger log = Log
049: .open(SpyPreparedStatement.class);
050: protected static L10N L = new L10N(SpyPreparedStatement.class);
051:
052: private String _sql;
053: protected PreparedStatement _pstmt;
054:
055: SpyPreparedStatement(String id, SpyConnection conn,
056: PreparedStatement stmt, String sql) {
057: super (id, conn, stmt);
058:
059: _pstmt = stmt;
060: _sql = sql;
061: }
062:
063: public java.sql.ResultSet executeQuery() throws SQLException {
064: try {
065: if (log.isLoggable(Level.FINE))
066: log.fine(getId() + ":executeQuery(" + _sql + ")");
067:
068: ResultSet rs = _pstmt.executeQuery();
069:
070: return rs;
071: } catch (Throwable e) {
072: if (log.isLoggable(Level.FINE))
073: log.fine(getId() + ":exn-executeQuery(" + e + ")");
074:
075: throw SQLExceptionWrapper.create(e);
076: }
077: }
078:
079: public int executeUpdate() throws SQLException {
080: try {
081: int result = _pstmt.executeUpdate();
082:
083: if (log.isLoggable(Level.FINE))
084: log.fine(getId() + ":executeUpdate(" + _sql + ") -> "
085: + result);
086:
087: return result;
088: } catch (Throwable e) {
089: if (log.isLoggable(Level.FINE))
090: log.fine(getId() + ":exn-executeUpdate(" + e + ")");
091:
092: throw SQLExceptionWrapper.create(e);
093: }
094: }
095:
096: public boolean execute() throws SQLException {
097: try {
098: boolean result = _pstmt.execute();
099:
100: if (log.isLoggable(Level.FINE))
101: log.fine(getId() + ":execute(" + _sql + ") -> "
102: + result);
103:
104: return result;
105: } catch (Throwable e) {
106: if (log.isLoggable(Level.FINE))
107: log.fine(getId() + ":exn-execute(" + e + ")");
108:
109: throw SQLExceptionWrapper.create(e);
110: }
111: }
112:
113: public void addBatch() throws SQLException {
114: try {
115: _pstmt.addBatch();
116:
117: if (log.isLoggable(Level.FINE))
118: log.fine(getId() + ":addBatch()");
119: } catch (Throwable e) {
120: if (log.isLoggable(Level.FINE))
121: log.fine(getId() + ":exn-addBatch(" + e + ")");
122:
123: throw SQLExceptionWrapper.create(e);
124: }
125: }
126:
127: public void clearParameters() throws SQLException {
128: try {
129: if (log.isLoggable(Level.FINE))
130: log.fine(getId() + ":clearParameters()");
131:
132: _pstmt.clearParameters();
133: } catch (Throwable e) {
134: if (log.isLoggable(Level.FINE))
135: log.fine(getId() + ":exn-clearParameters(" + e + ")");
136:
137: throw SQLExceptionWrapper.create(e);
138: }
139: }
140:
141: public ResultSetMetaData getMetaData() throws SQLException {
142: try {
143: ResultSetMetaData result = _pstmt.getMetaData();
144:
145: if (log.isLoggable(Level.FINE))
146: log.fine(getId() + ":getMetaData() -> " + result);
147:
148: return result;
149: } catch (Throwable e) {
150: if (log.isLoggable(Level.FINE))
151: log.fine(getId() + ":exn-getMetaData(" + e + ")");
152:
153: throw SQLExceptionWrapper.create(e);
154: }
155: }
156:
157: public ParameterMetaData getParameterMetaData() throws SQLException {
158: return _pstmt.getParameterMetaData();
159: }
160:
161: public void setNull(int parameterIndex, int sqlType)
162: throws SQLException {
163: try {
164: if (log.isLoggable(Level.FINE))
165: log.fine(getId() + ":setNull(" + parameterIndex
166: + ",type=" + sqlType + ")");
167:
168: _pstmt.setNull(parameterIndex, sqlType);
169: } catch (Throwable e) {
170: if (log.isLoggable(Level.FINE))
171: log.fine(getId() + ":exn-setNull(" + e + ")");
172:
173: throw SQLExceptionWrapper.create(e);
174: }
175: }
176:
177: public void setNull(int parameterIndex, int sqlType, String typeName)
178: throws SQLException {
179: try {
180: if (log.isLoggable(Level.FINE))
181: log.fine(getId() + ":setNull(" + parameterIndex
182: + ",type=" + sqlType + ",typeName=" + typeName
183: + ")");
184:
185: _pstmt.setNull(parameterIndex, sqlType, typeName);
186: } catch (Throwable e) {
187: if (log.isLoggable(Level.FINE))
188: log.fine(getId() + ":exn-setNull(" + e + ")");
189:
190: throw SQLExceptionWrapper.create(e);
191: }
192: }
193:
194: public void setBoolean(int index, boolean value)
195: throws SQLException {
196: try {
197: if (log.isLoggable(Level.FINE))
198: log.fine(getId() + ":setBoolean(" + index + "," + value
199: + ")");
200:
201: _pstmt.setBoolean(index, value);
202: } catch (Throwable e) {
203: if (log.isLoggable(Level.FINE))
204: log.fine(getId() + ":exn-setBoolean(" + e + ")");
205:
206: throw SQLExceptionWrapper.create(e);
207: }
208: }
209:
210: public void setByte(int index, byte value) throws SQLException {
211: try {
212: if (log.isLoggable(Level.FINE))
213: log.fine(getId() + ":setByte(" + index + "," + value
214: + ")");
215:
216: _pstmt.setByte(index, value);
217: } catch (Throwable e) {
218: if (log.isLoggable(Level.FINE))
219: log.fine(getId() + ":exn-setByte(" + e + ")");
220:
221: throw SQLExceptionWrapper.create(e);
222: }
223: }
224:
225: public void setShort(int index, short value) throws SQLException {
226: try {
227: if (log.isLoggable(Level.FINE))
228: log.fine(getId() + ":setShort(" + index + "," + value
229: + ")");
230:
231: _pstmt.setShort(index, value);
232: } catch (Throwable e) {
233: if (log.isLoggable(Level.FINE))
234: log.fine(getId() + ":exn-setShort(" + e + ")");
235:
236: throw SQLExceptionWrapper.create(e);
237: }
238: }
239:
240: public void setInt(int index, int value) throws SQLException {
241: try {
242: if (log.isLoggable(Level.FINE))
243: log.fine(getId() + ":setInt(" + index + "," + value
244: + ")");
245:
246: _pstmt.setInt(index, value);
247: } catch (Throwable e) {
248: if (log.isLoggable(Level.FINE))
249: log.fine(getId() + ":exn-setInt(" + e + ")");
250:
251: throw SQLExceptionWrapper.create(e);
252: }
253: }
254:
255: public void setLong(int index, long value) throws SQLException {
256: try {
257: if (log.isLoggable(Level.FINE))
258: log.fine(getId() + ":setLong(" + index + "," + value
259: + ")");
260:
261: _pstmt.setLong(index, value);
262: } catch (Throwable e) {
263: if (log.isLoggable(Level.FINE))
264: log.fine(getId() + ":exn-setLong(" + e + ")");
265:
266: throw SQLExceptionWrapper.create(e);
267: }
268: }
269:
270: public void setFloat(int index, float value) throws SQLException {
271: try {
272: if (log.isLoggable(Level.FINE))
273: log.fine(getId() + ":setFloat(" + index + "," + value
274: + ")");
275:
276: _pstmt.setFloat(index, value);
277: } catch (Throwable e) {
278: if (log.isLoggable(Level.FINE))
279: log.fine(getId() + ":exn-setFloat(" + e + ")");
280:
281: throw SQLExceptionWrapper.create(e);
282: }
283: }
284:
285: public void setDouble(int index, double value) throws SQLException {
286: try {
287: if (log.isLoggable(Level.FINE))
288: log.fine(getId() + ":setDouble(" + index + "," + value
289: + ")");
290:
291: _pstmt.setDouble(index, value);
292: } catch (Throwable e) {
293: if (log.isLoggable(Level.FINE))
294: log.fine(getId() + ":exn-setDouble(" + e + ")");
295:
296: throw SQLExceptionWrapper.create(e);
297: }
298: }
299:
300: public void setBigDecimal(int index, BigDecimal value)
301: throws SQLException {
302: try {
303: if (log.isLoggable(Level.FINE))
304: log.fine(getId() + ":setBigDecimal(" + index + ","
305: + value + ")");
306:
307: _pstmt.setBigDecimal(index, value);
308: } catch (Throwable e) {
309: if (log.isLoggable(Level.FINE))
310: log.fine(getId() + ":exn-setBigDecimal(" + e + ")");
311:
312: throw SQLExceptionWrapper.create(e);
313: }
314: }
315:
316: public void setString(int index, String value) throws SQLException {
317: try {
318: if (log.isLoggable(Level.FINE))
319: log.fine(getId() + ":setString(" + index + "," + value
320: + ")");
321:
322: _pstmt.setString(index, value);
323: } catch (Throwable e) {
324: if (log.isLoggable(Level.FINE))
325: log.fine(getId() + ":exn-setString(" + e + ")");
326:
327: throw SQLExceptionWrapper.create(e);
328: }
329: }
330:
331: public void setBytes(int index, byte[] value) throws SQLException {
332: try {
333: if (value != null)
334: if (log.isLoggable(Level.FINE))
335: log.fine(getId() + ":setBytes(" + index + ",len="
336: + value.length + ")");
337: else if (log.isLoggable(Level.FINE))
338: log.fine(getId() + ":setBytes(" + index + ",null");
339:
340: _pstmt.setBytes(index, value);
341: } catch (Throwable e) {
342: if (log.isLoggable(Level.FINE))
343: log.fine(getId() + ":exn-setBytes(" + e + ")");
344:
345: throw SQLExceptionWrapper.create(e);
346: }
347: }
348:
349: public void setDate(int index, Date value) throws SQLException {
350: try {
351: if (log.isLoggable(Level.FINE))
352: log.fine(getId() + ":setDate(" + index + "," + value
353: + ")");
354:
355: _pstmt.setDate(index, value);
356: } catch (Throwable e) {
357: if (log.isLoggable(Level.FINE))
358: log.fine(getId() + ":exn-setDate(" + e + ")");
359:
360: throw SQLExceptionWrapper.create(e);
361: }
362: }
363:
364: public void setDate(int index, Date value, Calendar cal)
365: throws SQLException {
366: try {
367: if (log.isLoggable(Level.FINE))
368: log.fine(getId() + ":setDate(" + index + "," + value
369: + ",cal=" + cal + ")");
370:
371: _pstmt.setDate(index, value, cal);
372: } catch (Throwable e) {
373: if (log.isLoggable(Level.FINE))
374: log.fine(getId() + ":exn-setDate(" + e + ")");
375:
376: throw SQLExceptionWrapper.create(e);
377: }
378: }
379:
380: public void setTime(int index, Time value) throws SQLException {
381: try {
382: if (log.isLoggable(Level.FINE))
383: log.fine(getId() + ":setTime(" + index + "," + value
384: + ")");
385:
386: _pstmt.setTime(index, value);
387: } catch (Throwable e) {
388: if (log.isLoggable(Level.FINE))
389: log.fine(getId() + ":exn-setTime(" + e + ")");
390:
391: throw SQLExceptionWrapper.create(e);
392: }
393: }
394:
395: public void setTime(int index, Time value, Calendar cal)
396: throws SQLException {
397: try {
398: if (log.isLoggable(Level.FINE))
399: log.fine(getId() + ":setTime(" + index + "," + value
400: + ",cal=" + cal + ")");
401:
402: _pstmt.setTime(index, value, cal);
403: } catch (Throwable e) {
404: if (log.isLoggable(Level.FINE))
405: log.fine(getId() + ":exn-setTime(" + e + ")");
406:
407: throw SQLExceptionWrapper.create(e);
408: }
409: }
410:
411: public void setTimestamp(int index, Timestamp value)
412: throws SQLException {
413: try {
414: if (log.isLoggable(Level.FINE))
415: log.fine(getId() + ":setTimestamp(" + index + ","
416: + value + ")");
417:
418: _pstmt.setTimestamp(index, value);
419: } catch (Throwable e) {
420: if (log.isLoggable(Level.FINE))
421: log.fine(getId() + ":exn-setTimestamp(" + e + ")");
422:
423: throw SQLExceptionWrapper.create(e);
424: }
425: }
426:
427: public void setTimestamp(int index, Timestamp value, Calendar cal)
428: throws SQLException {
429: try {
430: if (log.isLoggable(Level.FINE))
431: log.fine(getId() + ":setTimestamp(" + index + ","
432: + value + ",cal=" + cal + ")");
433:
434: _pstmt.setTimestamp(index, value, cal);
435: } catch (Throwable e) {
436: if (log.isLoggable(Level.FINE))
437: log.fine(getId() + ":exn-setTimestamp(" + e + ")");
438:
439: throw SQLExceptionWrapper.create(e);
440: }
441: }
442:
443: public void setAsciiStream(int index, InputStream value, int length)
444: throws SQLException {
445: try {
446: if (log.isLoggable(Level.FINE))
447: log.fine(getId() + ":setAsciiStream(" + index + ","
448: + value + ",len=" + length + ")");
449:
450: _pstmt.setAsciiStream(index, value, length);
451: } catch (Throwable e) {
452: if (log.isLoggable(Level.FINE))
453: log.fine(getId() + ":exn-setAsciiStream(" + e + ")");
454:
455: throw SQLExceptionWrapper.create(e);
456: }
457: }
458:
459: public void setUnicodeStream(int index, InputStream value,
460: int length) throws SQLException {
461: try {
462: if (log.isLoggable(Level.FINE))
463: log.fine(getId() + ":setUnicodeStream(" + index + ","
464: + value + ",len=" + length + ")");
465:
466: _pstmt.setUnicodeStream(index, value, length);
467: } catch (Throwable e) {
468: if (log.isLoggable(Level.FINE))
469: log.fine(getId() + ":exn-setUnicodeStream(" + e + ")");
470:
471: throw SQLExceptionWrapper.create(e);
472: }
473: }
474:
475: public void setBinaryStream(int index, InputStream value, int length)
476: throws SQLException {
477: try {
478: if (log.isLoggable(Level.FINE))
479: log.fine(getId() + ":setBinaryStream(" + index + ","
480: + value + ",len=" + length + ")");
481:
482: _pstmt.setBinaryStream(index, value, length);
483: } catch (Throwable e) {
484: if (log.isLoggable(Level.FINE))
485: log.fine(getId() + ":exn-setBinaryStream(" + e + ")");
486:
487: throw SQLExceptionWrapper.create(e);
488: }
489: }
490:
491: public void setCharacterStream(int index, Reader value, int length)
492: throws SQLException {
493: try {
494: if (log.isLoggable(Level.FINE))
495: log.fine(getId() + ":setCharacterStream(" + index + ","
496: + value + ",len=" + length + ")");
497:
498: _pstmt.setCharacterStream(index, value, length);
499: } catch (Throwable e) {
500: if (log.isLoggable(Level.FINE))
501: log
502: .fine(getId() + ":exn-setCharacterStream(" + e
503: + ")");
504:
505: throw SQLExceptionWrapper.create(e);
506: }
507: }
508:
509: public void setObject(int index, Object value, int type, int scale)
510: throws SQLException {
511: try {
512: if (log.isLoggable(Level.FINE))
513: log.fine(getId() + ":setObject(" + index + "," + value
514: + ",type=" + type + ",scale=" + scale + ")");
515:
516: _pstmt.setObject(index, value, type, scale);
517: } catch (Throwable e) {
518: if (log.isLoggable(Level.FINE))
519: log.fine(getId() + ":exn-setObject(" + e + ")");
520:
521: throw SQLExceptionWrapper.create(e);
522: }
523: }
524:
525: public void setObject(int index, Object value, int type)
526: throws SQLException {
527: try {
528: if (log.isLoggable(Level.FINE))
529: log.fine(getId() + ":setObject(" + index + "," + value
530: + ",type=" + type + ")");
531:
532: _pstmt.setObject(index, value, type);
533: } catch (Throwable e) {
534: if (log.isLoggable(Level.FINE))
535: log.fine(getId() + ":exn-setObject(" + e + ")");
536:
537: throw SQLExceptionWrapper.create(e);
538: }
539: }
540:
541: public void setObject(int index, Object value) throws SQLException {
542: try {
543: if (log.isLoggable(Level.FINE))
544: log.fine(getId() + ":setObject(" + index + "," + value
545: + ")");
546:
547: _pstmt.setObject(index, value);
548: } catch (Throwable e) {
549: e.printStackTrace();
550:
551: if (log.isLoggable(Level.FINE))
552: log.fine(getId() + ":exn-setObject(" + e + ")");
553:
554: throw SQLExceptionWrapper.create(e);
555: }
556: }
557:
558: public void setRef(int index, Ref value) throws SQLException {
559: try {
560: if (log.isLoggable(Level.FINE))
561: log.fine(getId() + ":setRef(" + index + "," + value
562: + ")");
563:
564: _pstmt.setRef(index, value);
565: } catch (Throwable e) {
566: if (log.isLoggable(Level.FINE))
567: log.fine(getId() + ":exn-setRef(" + e + ")");
568:
569: throw SQLExceptionWrapper.create(e);
570: }
571: }
572:
573: public void setBlob(int index, Blob value) throws SQLException {
574: try {
575: if (log.isLoggable(Level.FINE))
576: log.fine(getId() + ":setBlob(" + index + "," + value
577: + ")");
578:
579: _pstmt.setBlob(index, value);
580: } catch (Throwable e) {
581: if (log.isLoggable(Level.FINE))
582: log.fine(getId() + ":exn-setBlob(" + e + ")");
583:
584: throw SQLExceptionWrapper.create(e);
585: }
586: }
587:
588: public void setClob(int index, Clob value) throws SQLException {
589: try {
590: if (log.isLoggable(Level.FINE))
591: log.fine(getId() + ":setClob(" + index + "," + value
592: + ")");
593:
594: _pstmt.setClob(index, value);
595: } catch (Throwable e) {
596: if (log.isLoggable(Level.FINE))
597: log.fine(getId() + ":exn-setClob(" + e + ")");
598:
599: throw SQLExceptionWrapper.create(e);
600: }
601: }
602:
603: public void setArray(int index, Array value) throws SQLException {
604: try {
605: if (log.isLoggable(Level.FINE))
606: log.fine(getId() + ":setArray(" + index + "," + value
607: + ")");
608:
609: _pstmt.setArray(index, value);
610: } catch (Throwable e) {
611: if (log.isLoggable(Level.FINE))
612: log.fine(getId() + ":exn-setArray(" + e + ")");
613:
614: throw SQLExceptionWrapper.create(e);
615: }
616: }
617:
618: public void setURL(int index, URL value) throws SQLException {
619: throw new UnsupportedOperationException();
620: }
621:
622: public boolean isClosed() throws SQLException {
623: throw new UnsupportedOperationException("Not supported yet.");
624: }
625:
626: public void setPoolable(boolean poolable) throws SQLException {
627: throw new UnsupportedOperationException("Not supported yet.");
628: }
629:
630: public boolean isPoolable() throws SQLException {
631: throw new UnsupportedOperationException("Not supported yet.");
632: }
633:
634: public <T> T unwrap(Class<T> iface) throws SQLException {
635: throw new UnsupportedOperationException("Not supported yet.");
636: }
637:
638: public boolean isWrapperFor(Class<?> iface) throws SQLException {
639: throw new UnsupportedOperationException("Not supported yet.");
640: }
641:
642: public void setRowId(int parameterIndex, RowId x)
643: throws SQLException {
644: throw new UnsupportedOperationException("Not supported yet.");
645: }
646:
647: public void setNString(int parameterIndex, String value)
648: throws SQLException {
649: throw new UnsupportedOperationException("Not supported yet.");
650: }
651:
652: public void setNCharacterStream(int parameterIndex, Reader value,
653: long length) throws SQLException {
654: throw new UnsupportedOperationException("Not supported yet.");
655: }
656:
657: public void setNClob(int parameterIndex, NClob value)
658: throws SQLException {
659: throw new UnsupportedOperationException("Not supported yet.");
660: }
661:
662: public void setClob(int parameterIndex, Reader reader, long length)
663: throws SQLException {
664: throw new UnsupportedOperationException("Not supported yet.");
665: }
666:
667: public void setBlob(int parameterIndex, InputStream inputStream,
668: long length) throws SQLException {
669: throw new UnsupportedOperationException("Not supported yet.");
670: }
671:
672: public void setNClob(int parameterIndex, Reader reader, long length)
673: throws SQLException {
674: throw new UnsupportedOperationException("Not supported yet.");
675: }
676:
677: public void setSQLXML(int parameterIndex, SQLXML xmlObject)
678: throws SQLException {
679: throw new UnsupportedOperationException("Not supported yet.");
680: }
681:
682: public void setAsciiStream(int parameterIndex, InputStream x,
683: long length) throws SQLException {
684: throw new UnsupportedOperationException("Not supported yet.");
685: }
686:
687: public void setBinaryStream(int parameterIndex, InputStream x,
688: long length) throws SQLException {
689: throw new UnsupportedOperationException("Not supported yet.");
690: }
691:
692: public void setCharacterStream(int parameterIndex, Reader reader,
693: long length) throws SQLException {
694: throw new UnsupportedOperationException("Not supported yet.");
695: }
696:
697: public void setAsciiStream(int parameterIndex, InputStream x)
698: throws SQLException {
699: throw new UnsupportedOperationException("Not supported yet.");
700: }
701:
702: public void setBinaryStream(int parameterIndex, InputStream x)
703: throws SQLException {
704: throw new UnsupportedOperationException("Not supported yet.");
705: }
706:
707: public void setCharacterStream(int parameterIndex, Reader reader)
708: throws SQLException {
709: throw new UnsupportedOperationException("Not supported yet.");
710: }
711:
712: public void setNCharacterStream(int parameterIndex, Reader value)
713: throws SQLException {
714: throw new UnsupportedOperationException("Not supported yet.");
715: }
716:
717: public void setClob(int parameterIndex, Reader reader)
718: throws SQLException {
719: throw new UnsupportedOperationException("Not supported yet.");
720: }
721:
722: public void setBlob(int parameterIndex, InputStream inputStream)
723: throws SQLException {
724: throw new UnsupportedOperationException("Not supported yet.");
725: }
726:
727: public void setNClob(int parameterIndex, Reader reader)
728: throws SQLException {
729: throw new UnsupportedOperationException("Not supported yet.");
730: }
731: }
|