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.sql.Connection;
036: import java.sql.ResultSet;
037: import java.sql.SQLException;
038: import java.sql.SQLWarning;
039: import java.sql.Statement;
040: import java.util.logging.*;
041:
042: /**
043: * Spying on a statement;
044: */
045: public class SpyStatement implements java.sql.Statement {
046: protected final static Logger log = Log.open(SpyStatement.class);
047: protected final static L10N L = new L10N(SpyConnection.class);
048:
049: protected String _id;
050:
051: // The spy connection
052: protected SpyConnection _conn;
053:
054: // The underlying connection
055: protected Statement _stmt;
056:
057: SpyStatement(String id, SpyConnection conn, Statement stmt) {
058: _id = id;
059:
060: _conn = conn;
061: _stmt = stmt;
062: }
063:
064: public String getId() {
065: if (_id == null)
066: _id = _conn.createStatementId();
067:
068: return _id;
069: }
070:
071: public Statement getStatement() {
072: return _stmt;
073: }
074:
075: public void addBatch(String sql) throws SQLException {
076: try {
077: if (log.isLoggable(Level.FINE))
078: log.fine(getId() + ":addBatch(" + sql + ")");
079:
080: _stmt.addBatch(sql);
081: } catch (Throwable e) {
082: if (log.isLoggable(Level.FINE))
083: log.fine(getId() + ":exn-addBatch(" + e + ")");
084:
085: throw SQLExceptionWrapper.create(e);
086: }
087: }
088:
089: public void cancel() throws SQLException {
090: try {
091: if (log.isLoggable(Level.FINE))
092: log.fine(getId() + ":cancel()");
093:
094: _stmt.cancel();
095: } catch (Throwable e) {
096: if (log.isLoggable(Level.FINE))
097: log.fine(getId() + ":exn-cancel(" + e + ")");
098:
099: throw SQLExceptionWrapper.create(e);
100: }
101: }
102:
103: public void clearBatch() throws SQLException {
104: try {
105: if (log.isLoggable(Level.FINE))
106: log.fine(getId() + ":clearBatch()");
107:
108: _stmt.clearBatch();
109: } catch (Throwable e) {
110: if (log.isLoggable(Level.FINE))
111: log.fine(getId() + ":exn-clearBatch(" + e + ")");
112:
113: throw SQLExceptionWrapper.create(e);
114: }
115: }
116:
117: public void clearWarnings() throws SQLException {
118: try {
119: if (log.isLoggable(Level.FINE))
120: log.fine(getId() + ":clearWarnings()");
121:
122: _stmt.clearWarnings();
123: } catch (Throwable e) {
124: if (log.isLoggable(Level.FINE))
125: log.fine(getId() + ":exn-clearWarnings(" + e + ")");
126:
127: throw SQLExceptionWrapper.create(e);
128: }
129: }
130:
131: public void close() throws SQLException {
132: try {
133: if (log.isLoggable(Level.FINE))
134: log.fine(getId() + ":close()");
135:
136: _stmt.close();
137: } catch (Throwable e) {
138: if (log.isLoggable(Level.FINE))
139: log.fine(getId() + ":exn-close(" + e + ")");
140:
141: throw SQLExceptionWrapper.create(e);
142: }
143: }
144:
145: public java.sql.ResultSet executeQuery(String sql)
146: throws SQLException {
147: try {
148: if (log.isLoggable(Level.FINE))
149: log.fine(getId() + ":executeQuery(" + sql + ")");
150:
151: ResultSet rs = _stmt.executeQuery(sql);
152:
153: return rs;
154: } catch (Throwable e) {
155: if (log.isLoggable(Level.FINE))
156: log.fine(getId() + ":exn-executeQuery(" + sql + ") -> "
157: + e);
158:
159: throw SQLExceptionWrapper.create(e);
160: }
161: }
162:
163: public int executeUpdate(String sql) throws SQLException {
164: try {
165: int count = _stmt.executeUpdate(sql);
166:
167: if (log.isLoggable(Level.FINE))
168: log.fine(getId() + ":executeUpdate(" + sql + ") -> "
169: + count);
170:
171: return count;
172: } catch (Throwable e) {
173: if (log.isLoggable(Level.FINE))
174: log.fine(getId() + ":exn-executeUpdate(" + sql
175: + ") -> " + e);
176:
177: throw SQLExceptionWrapper.create(e);
178: }
179: }
180:
181: public boolean execute(String sql) throws SQLException {
182: try {
183: boolean hasResult = _stmt.execute(sql);
184:
185: if (log.isLoggable(Level.FINE))
186: log.fine(getId() + ":execute(" + sql + ") -> "
187: + hasResult);
188:
189: return hasResult;
190: } catch (Throwable e) {
191: if (log.isLoggable(Level.FINE))
192: log.fine(getId() + ":exn-execute(" + sql + ") -> " + e);
193:
194: throw SQLExceptionWrapper.create(e);
195: }
196: }
197:
198: public int[] executeBatch() throws SQLException {
199: try {
200: int[] result = _stmt.executeBatch();
201:
202: if (log.isLoggable(Level.FINE))
203: log.fine(getId() + ":executeBatch()");
204:
205: return result;
206: } catch (Throwable e) {
207: if (log.isLoggable(Level.FINE))
208: log.fine(getId() + ":exn-executeBatch(" + e + ")");
209:
210: throw SQLExceptionWrapper.create(e);
211: }
212: }
213:
214: public java.sql.ResultSet getResultSet() throws SQLException {
215: try {
216: ResultSet result = _stmt.getResultSet();
217:
218: if (log.isLoggable(Level.FINE))
219: log.fine(getId()
220: + ":getResultSet() -> "
221: + (result != null ? result.getClass().getName()
222: : ""));
223:
224: return result;
225: } catch (Throwable e) {
226: if (log.isLoggable(Level.FINE))
227: log.fine(getId() + ":exn-getResultSet(" + e + ")");
228:
229: throw SQLExceptionWrapper.create(e);
230: }
231: }
232:
233: public int getUpdateCount() throws SQLException {
234: try {
235: int updateCount = _stmt.getUpdateCount();
236:
237: if (log.isLoggable(Level.FINE))
238: log.fine(getId() + ":getUpdateCount() -> "
239: + updateCount);
240:
241: return updateCount;
242: } catch (Throwable e) {
243: if (log.isLoggable(Level.FINE))
244: log.fine(getId() + ":exn-getUpdateCount(" + e + ")");
245:
246: throw SQLExceptionWrapper.create(e);
247: }
248: }
249:
250: public java.sql.Connection getConnection() throws SQLException {
251: int updateCount = _stmt.getUpdateCount();
252:
253: if (log.isLoggable(Level.FINE))
254: log.fine(getId() + ":getConnection()");
255:
256: return _conn;
257: }
258:
259: public int getFetchDirection() throws SQLException {
260: try {
261: int result = _stmt.getFetchDirection();
262:
263: if (log.isLoggable(Level.FINE))
264: log.fine(getId() + ":getFetchDirection() -> " + result);
265:
266: return result;
267: } catch (Throwable e) {
268: if (log.isLoggable(Level.FINE))
269: log.fine(getId() + ":exn-getFetchDirection(" + e + ")");
270:
271: throw SQLExceptionWrapper.create(e);
272: }
273: }
274:
275: public int getFetchSize() throws SQLException {
276: try {
277: int result = _stmt.getFetchSize();
278:
279: if (log.isLoggable(Level.FINE))
280: log.fine(getId() + ":getFetchSize() -> " + result);
281:
282: return result;
283: } catch (Throwable e) {
284: if (log.isLoggable(Level.FINE))
285: log.fine(getId() + ":exn-getFetchSize(" + e + ")");
286:
287: throw SQLExceptionWrapper.create(e);
288: }
289: }
290:
291: public int getMaxFieldSize() throws SQLException {
292: try {
293: int result = _stmt.getMaxFieldSize();
294:
295: if (log.isLoggable(Level.FINE))
296: log.fine(getId() + ":getMaxFieldSize() -> " + result);
297:
298: return result;
299: } catch (Throwable e) {
300: if (log.isLoggable(Level.FINE))
301: log.fine(getId() + ":exn-getMaxFieldSize(" + e + ")");
302:
303: throw SQLExceptionWrapper.create(e);
304: }
305: }
306:
307: public int getMaxRows() throws SQLException {
308: try {
309: int result = _stmt.getMaxRows();
310:
311: if (log.isLoggable(Level.FINE))
312: log.fine(getId() + ":getMaxRows() -> " + result);
313:
314: return result;
315: } catch (Throwable e) {
316: if (log.isLoggable(Level.FINE))
317: log.fine(getId() + ":exn-getMaxRows(" + e + ")");
318:
319: throw SQLExceptionWrapper.create(e);
320: }
321: }
322:
323: public void setMaxRows(int max) throws SQLException {
324: try {
325: if (log.isLoggable(Level.FINE))
326: log.fine(getId() + ":setMaxRows(" + max + ")");
327:
328: _stmt.setMaxRows(max);
329: } catch (Throwable e) {
330: if (log.isLoggable(Level.FINE))
331: log.fine(getId() + ":exn-setMaxRows(" + e + ")");
332:
333: throw SQLExceptionWrapper.create(e);
334: }
335: }
336:
337: public boolean getMoreResults() throws SQLException {
338: try {
339: boolean result = _stmt.getMoreResults();
340:
341: if (log.isLoggable(Level.FINE))
342: log.fine(getId() + ":getMoreResults() -> " + result);
343:
344: return result;
345: } catch (Throwable e) {
346: if (log.isLoggable(Level.FINE))
347: log.fine(getId() + ":exn-getMoreResults(" + e + ")");
348:
349: throw SQLExceptionWrapper.create(e);
350: }
351: }
352:
353: public int getQueryTimeout() throws SQLException {
354: try {
355: int result = _stmt.getQueryTimeout();
356:
357: if (log.isLoggable(Level.FINE))
358: log.fine(getId() + ":getQueryTimeout() -> " + result);
359:
360: return result;
361: } catch (Throwable e) {
362: if (log.isLoggable(Level.FINE))
363: log.fine(getId() + ":exn-getQueryTimeout(" + e + ")");
364:
365: throw SQLExceptionWrapper.create(e);
366: }
367: }
368:
369: public int getResultSetConcurrency() throws SQLException {
370: try {
371: int result = _stmt.getResultSetConcurrency();
372:
373: if (log.isLoggable(Level.FINE))
374: log.fine(getId() + ":getResultSetConcurrency() -> "
375: + result);
376:
377: return result;
378: } catch (Throwable e) {
379: if (log.isLoggable(Level.FINE))
380: log.fine(getId() + ":exn-getResultSetConcurrency(" + e
381: + ")");
382:
383: throw SQLExceptionWrapper.create(e);
384: }
385: }
386:
387: public int getResultSetType() throws SQLException {
388: try {
389: int result = _stmt.getResultSetType();
390:
391: if (log.isLoggable(Level.FINE))
392: log.fine(getId() + ":getResultSetType() -> " + result);
393:
394: return result;
395: } catch (Throwable e) {
396: if (log.isLoggable(Level.FINE))
397: log.fine(getId() + ":exn-getResultSetType(" + e + ")");
398:
399: throw SQLExceptionWrapper.create(e);
400: }
401: }
402:
403: public SQLWarning getWarnings() throws SQLException {
404: try {
405: SQLWarning result = _stmt.getWarnings();
406:
407: if (log.isLoggable(Level.FINE))
408: log.fine(getId() + ":getWarnings() -> " + result);
409:
410: return result;
411: } catch (Throwable e) {
412: if (log.isLoggable(Level.FINE))
413: log.fine(getId() + ":exn-getWarnings(" + e + ")");
414:
415: throw SQLExceptionWrapper.create(e);
416: }
417: }
418:
419: public void setCursorName(String name) throws SQLException {
420: try {
421: if (log.isLoggable(Level.FINE))
422: log.fine(getId() + ":setCursorName(" + name + ")");
423:
424: _stmt.setCursorName(name);
425: } catch (Throwable e) {
426: if (log.isLoggable(Level.FINE))
427: log.fine(getId() + ":exn-setCursorName(" + e + ")");
428:
429: throw SQLExceptionWrapper.create(e);
430: }
431: }
432:
433: public void setEscapeProcessing(boolean enable) throws SQLException {
434: try {
435: if (log.isLoggable(Level.FINE))
436: log.fine(getId() + ":setEscapeProcessing(" + enable
437: + ")");
438:
439: _stmt.setEscapeProcessing(enable);
440: } catch (Throwable e) {
441: if (log.isLoggable(Level.FINE))
442: log.fine(getId() + ":exn-setEscapeProcessing(" + e
443: + ")");
444:
445: throw SQLExceptionWrapper.create(e);
446: }
447: }
448:
449: public void setFetchDirection(int direction) throws SQLException {
450: try {
451: if (log.isLoggable(Level.FINE))
452: log.fine(getId() + ":setFetchDirection(" + direction
453: + ")");
454:
455: _stmt.setFetchDirection(direction);
456: } catch (Throwable e) {
457: if (log.isLoggable(Level.FINE))
458: log.fine(getId() + ":exn-setFetchDirection(" + e + ")");
459:
460: throw SQLExceptionWrapper.create(e);
461: }
462: }
463:
464: public void setFetchSize(int rows) throws SQLException {
465: try {
466: if (log.isLoggable(Level.FINE))
467: log.fine(getId() + ":setFetchSize(" + rows + ")");
468:
469: _stmt.setFetchSize(rows);
470: } catch (Throwable e) {
471: if (log.isLoggable(Level.FINE))
472: log.fine(getId() + ":exn-setFetchSize(" + e + ")");
473:
474: throw SQLExceptionWrapper.create(e);
475: }
476: }
477:
478: public void setMaxFieldSize(int max) throws SQLException {
479: try {
480: if (log.isLoggable(Level.FINE))
481: log.fine(getId() + ":setMaxFieldSize(" + max + ")");
482:
483: _stmt.setMaxFieldSize(max);
484: } catch (Throwable e) {
485: if (log.isLoggable(Level.FINE))
486: log.fine(getId() + ":exn-setMaxFieldSize(" + e + ")");
487:
488: throw SQLExceptionWrapper.create(e);
489: }
490: }
491:
492: public void setQueryTimeout(int seconds) throws SQLException {
493: try {
494: if (log.isLoggable(Level.FINE))
495: log.fine(getId() + ":setQueryTimeout(" + seconds + ")");
496:
497: _stmt.setQueryTimeout(seconds);
498: } catch (Throwable e) {
499: if (log.isLoggable(Level.FINE))
500: log.fine(getId() + ":exn-setQueryTimeout(" + e + ")");
501:
502: throw SQLExceptionWrapper.create(e);
503: }
504: }
505:
506: // jdk 1.4
507: public boolean getMoreResults(int count) throws SQLException {
508: return _stmt.getMoreResults(count);
509: }
510:
511: public java.sql.ResultSet getGeneratedKeys() throws SQLException {
512: return _stmt.getGeneratedKeys();
513: }
514:
515: public int executeUpdate(String query, int resultType)
516: throws SQLException {
517: return _stmt.executeUpdate(query, resultType);
518: }
519:
520: public int executeUpdate(String query, int[] columns)
521: throws SQLException {
522: return _stmt.executeUpdate(query, columns);
523: }
524:
525: public int executeUpdate(String query, String[] columns)
526: throws SQLException {
527: return _stmt.executeUpdate(query, columns);
528: }
529:
530: public boolean execute(String query, int resultType)
531: throws SQLException {
532: return _stmt.execute(query, resultType);
533: }
534:
535: public boolean execute(String query, int[] columns)
536: throws SQLException {
537: return _stmt.execute(query, columns);
538: }
539:
540: public boolean execute(String query, String[] columns)
541: throws SQLException {
542: return _stmt.execute(query, columns);
543: }
544:
545: public int getResultSetHoldability() throws SQLException {
546: return _stmt.getResultSetHoldability();
547: }
548:
549: public boolean isClosed() throws SQLException {
550: throw new UnsupportedOperationException("Not supported yet.");
551: }
552:
553: public void setPoolable(boolean poolable) throws SQLException {
554: throw new UnsupportedOperationException("Not supported yet.");
555: }
556:
557: public boolean isPoolable() throws SQLException {
558: throw new UnsupportedOperationException("Not supported yet.");
559: }
560:
561: public <T> T unwrap(Class<T> iface) throws SQLException {
562: throw new UnsupportedOperationException("Not supported yet.");
563: }
564:
565: public boolean isWrapperFor(Class<?> iface) throws SQLException {
566: throw new UnsupportedOperationException("Not supported yet.");
567: }
568: }
|