001: /*
002: * Craftsman Spy.
003: * Copyright (C) 2005 Sébastien LECACHEUR
004: *
005: * This program is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package craftsman.spy;
020:
021: import java.sql.Connection;
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024: import java.sql.SQLWarning;
025: import java.sql.Statement;
026: import java.util.ArrayList;
027:
028: /**
029: * This classe is used for executing a static SQL statement and
030: * returning the results it produces.
031: *
032: * @author Sébastien LECACHEUR
033: */
034: public class SpyStatement extends SpyEntity implements Statement {
035: /**
036: * The real SQL statement instance.
037: */
038: private Statement real = null;
039:
040: /**
041: * The list of all the SQL in the current batch.
042: */
043: protected ArrayList batch = null;
044:
045: /**
046: * Constructs a new Spy JDBC statement.
047: *
048: * @param c Connection The used connection.
049: * @param stm Statement The real JDBC statement.
050: */
051: protected SpyStatement(Connection c, Statement stm) {
052: super (c);
053: real = stm;
054: batch = new ArrayList();
055: }
056:
057: /**
058: * @see Statement#getFetchDirection()
059: */
060: public int getFetchDirection() throws SQLException {
061: return real.getFetchDirection();
062: }
063:
064: /**
065: * @see Statement#getFetchSize()
066: */
067: public int getFetchSize() throws SQLException {
068: return real.getFetchSize();
069: }
070:
071: /**
072: * @see Statement#getMaxFieldSize()
073: */
074: public int getMaxFieldSize() throws SQLException {
075: return real.getMaxFieldSize();
076: }
077:
078: /**
079: * @see Statement#getMaxRows()
080: */
081: public int getMaxRows() throws SQLException {
082: return real.getMaxRows();
083: }
084:
085: /**
086: * @see Statement#getQueryTimeout()
087: */
088: public int getQueryTimeout() throws SQLException {
089: return real.getQueryTimeout();
090: }
091:
092: /**
093: * @see Statement#getResultSetConcurrency()
094: */
095: public int getResultSetConcurrency() throws SQLException {
096: return real.getResultSetConcurrency();
097: }
098:
099: /**
100: * @see Statement#getResultSetHoldability()
101: */
102: public int getResultSetHoldability() throws SQLException {
103: return real.getResultSetHoldability();
104: }
105:
106: /**
107: * @see Statement#getResultSetType()
108: */
109: public int getResultSetType() throws SQLException {
110: return real.getResultSetType();
111: }
112:
113: /**
114: * @see Statement#getUpdateCount()
115: */
116: public int getUpdateCount() throws SQLException {
117: return real.getUpdateCount();
118: }
119:
120: /**
121: * @see Statement#cancel()
122: */
123: public void cancel() throws SQLException {
124: real.cancel();
125: }
126:
127: /**
128: * @see Statement#clearBatch()
129: */
130: public void clearBatch() throws SQLException {
131: batch.clear();
132: real.clearBatch();
133: }
134:
135: /**
136: * @see Statement#clearWarnings()
137: */
138: public void clearWarnings() throws SQLException {
139: real.clearWarnings();
140: }
141:
142: /**
143: * @see Statement#close()
144: */
145: public void close() throws SQLException {
146: real.close();
147: }
148:
149: /**
150: * @see Statement#getMoreResults()
151: */
152: public boolean getMoreResults() throws SQLException {
153: return real.getMoreResults();
154: }
155:
156: /**
157: * @see Statement#executeBatch()
158: */
159: public int[] executeBatch() throws SQLException {
160: long end, start = System.currentTimeMillis();
161: int[] result = null;
162:
163: try {
164: result = real.executeBatch();
165: end = System.currentTimeMillis();
166:
167: if (batch.size() != result.length) {
168: if (log.isWarnEnabled())
169: log.warn(getId() + ":" + "expecting:"
170: + batch.size() + " but was:"
171: + result.length);
172: }
173: if (log.isInfoEnabled()) {
174: log.info(getId() + ":" + "executing batch...");
175: for (int i = 0; i < result.length && i < batch.size(); i++) {
176: switch (result[i]) {
177: case SUCCESS_NO_INFO:
178: log.info(getId() + ":" + batch.get(i)
179: + " => SUCCESS_NO_INFO");
180: break;
181:
182: case EXECUTE_FAILED:
183: log.info(getId() + ":" + batch.get(i)
184: + " => EXECUTE_FAILED");
185: break;
186:
187: default:
188: log.info(getId() + ":" + batch.get(i) + " => "
189: + result[i]);
190: break;
191: }
192: }
193: log.info(getId() + ":" + "batch executed ("
194: + (end - start) + " ms)");
195: }
196: } catch (SQLException e) {
197: end = System.currentTimeMillis();
198: for (int i = 0; i < batch.size(); i++) {
199: log.error(getId() + ":" + batch.get(i) + " => ...");
200: }
201: if (log.isWarnEnabled())
202: log.warn(getId() + ":batch.size=" + batch.size());
203: if (log.isErrorEnabled())
204: log.error(getId() + ":" + batch + " => state="
205: + e.getSQLState() + ",code=" + e.getErrorCode()
206: + " (" + (end - start) + " ms)", e);
207: throw e;
208: }
209:
210: return result;
211: }
212:
213: /**
214: * @see Statement#setFetchDirection(int)
215: */
216: public void setFetchDirection(int direction) throws SQLException {
217: real.setFetchDirection(direction);
218: }
219:
220: /**
221: * @see Statement#setFetchSize(int)
222: */
223: public void setFetchSize(int rows) throws SQLException {
224: real.setFetchSize(rows);
225: }
226:
227: /**
228: * @see Statement#setMaxFieldSize(int)
229: */
230: public void setMaxFieldSize(int max) throws SQLException {
231: real.setMaxFieldSize(max);
232: }
233:
234: /**
235: * @see Statement#setMaxRows(int)
236: */
237: public void setMaxRows(int max) throws SQLException {
238: real.setMaxRows(max);
239: }
240:
241: /**
242: * @see Statement#setQueryTimeout(int)
243: */
244: public void setQueryTimeout(int seconds) throws SQLException {
245: real.setQueryTimeout(seconds);
246: }
247:
248: /**
249: * @see Statement#getMoreResults(int)
250: */
251: public boolean getMoreResults(int current) throws SQLException {
252: return real.getMoreResults(current);
253: }
254:
255: /**
256: * @see Statement#setEscapeProcessing(boolean)
257: */
258: public void setEscapeProcessing(boolean enable) throws SQLException {
259: real.setEscapeProcessing(enable);
260: }
261:
262: /**
263: * @see Statement#executeUpdate(String)
264: */
265: public int executeUpdate(String sql) throws SQLException {
266: long end, start = System.currentTimeMillis();
267: int result = 0;
268:
269: try {
270: result = real.executeUpdate(sql);
271: end = System.currentTimeMillis();
272: if (log.isInfoEnabled())
273: log.info(getId() + ":" + sql + " => " + result + " ("
274: + (end - start) + " ms)");
275: } catch (SQLException e) {
276: end = System.currentTimeMillis();
277: if (log.isErrorEnabled())
278: log.error(getId() + ":" + sql + " => state="
279: + e.getSQLState() + ",code=" + e.getErrorCode()
280: + " (" + (end - start) + " ms)", e);
281: throw e;
282: }
283:
284: return result;
285: }
286:
287: /**
288: * @see Statement#addBatch(String)
289: */
290: public void addBatch(String sql) throws SQLException {
291: batch.add(sql);
292: real.addBatch(sql);
293: }
294:
295: /**
296: * @see Statement#setCursorName(String)
297: */
298: public void setCursorName(String name) throws SQLException {
299: real.setCursorName(name);
300: }
301:
302: /**
303: * @see Statement#execute(String)
304: */
305: public boolean execute(String sql) throws SQLException {
306: long end, start = System.currentTimeMillis();
307: boolean result = false;
308:
309: try {
310: result = real.execute(sql);
311: end = System.currentTimeMillis();
312: if (log.isInfoEnabled())
313: log.info(getId() + ":" + sql + " => " + result + " ("
314: + (end - start) + " ms)");
315: } catch (SQLException e) {
316: end = System.currentTimeMillis();
317: if (log.isErrorEnabled())
318: log.error(getId() + ":" + sql + " => state="
319: + e.getSQLState() + ",code=" + e.getErrorCode()
320: + " (" + (end - start) + " ms)", e);
321: throw e;
322: }
323:
324: return result;
325: }
326:
327: /**
328: * @see Statement#executeUpdate(String, int)
329: */
330: public int executeUpdate(String sql, int autoGeneratedKeys)
331: throws SQLException {
332: long end, start = System.currentTimeMillis();
333: int result = 0;
334:
335: try {
336: result = real.executeUpdate(sql, autoGeneratedKeys);
337: end = System.currentTimeMillis();
338: if (log.isInfoEnabled())
339: log.info(getId() + ":" + sql + " => " + result + " ("
340: + (end - start) + " ms)");
341: } catch (SQLException e) {
342: end = System.currentTimeMillis();
343: if (log.isErrorEnabled())
344: log.error(getId() + ":" + sql + " => state="
345: + e.getSQLState() + ",code=" + e.getErrorCode()
346: + " (" + (end - start) + " ms)", e);
347: throw e;
348: }
349:
350: return result;
351: }
352:
353: /**
354: * @see Statement#execute(String, int)
355: */
356: public boolean execute(String sql, int autoGeneratedKeys)
357: throws SQLException {
358: long end, start = System.currentTimeMillis();
359: boolean result = false;
360:
361: try {
362: result = real.execute(sql, autoGeneratedKeys);
363: end = System.currentTimeMillis();
364: if (log.isInfoEnabled())
365: log.info(getId() + ":" + sql + " => " + result + " ("
366: + (end - start) + " ms)");
367: } catch (SQLException e) {
368: end = System.currentTimeMillis();
369: if (log.isErrorEnabled())
370: log.error(getId() + ":" + sql + " => state="
371: + e.getSQLState() + ",code=" + e.getErrorCode()
372: + " (" + (end - start) + " ms)", e);
373: throw e;
374: }
375:
376: return result;
377: }
378:
379: /**
380: * @see Statement#executeUpdate(String, int[])
381: */
382: public int executeUpdate(String sql, int[] columnIndexes)
383: throws SQLException {
384: long end, start = System.currentTimeMillis();
385: int result = 0;
386:
387: try {
388: result = real.executeUpdate(sql, columnIndexes);
389: end = System.currentTimeMillis();
390: if (log.isInfoEnabled())
391: log.info(getId() + ":" + sql + " => " + result + " ("
392: + (end - start) + " ms)");
393: } catch (SQLException e) {
394: end = System.currentTimeMillis();
395: if (log.isErrorEnabled())
396: log.error(getId() + ":" + sql + " => state="
397: + e.getSQLState() + ",code=" + e.getErrorCode()
398: + " (" + (end - start) + " ms)", e);
399: throw e;
400: }
401:
402: return result;
403: }
404:
405: /**
406: * @see Statement#execute(String, int[])
407: */
408: public boolean execute(String sql, int[] columnIndexes)
409: throws SQLException {
410: long end, start = System.currentTimeMillis();
411: boolean result = false;
412:
413: try {
414: result = real.execute(sql, columnIndexes);
415: end = System.currentTimeMillis();
416: if (log.isInfoEnabled())
417: log.info(getId() + ":" + sql + " => " + result + " ("
418: + (end - start) + " ms)");
419: } catch (SQLException e) {
420: end = System.currentTimeMillis();
421: if (log.isErrorEnabled())
422: log.error(getId() + ":" + sql + " => state="
423: + e.getSQLState() + ",code=" + e.getErrorCode()
424: + " (" + (end - start) + " ms)", e);
425: throw e;
426: }
427:
428: return result;
429: }
430:
431: /**
432: * @see Statement#getGeneratedKeys()
433: */
434: public ResultSet getGeneratedKeys() throws SQLException {
435: return new SpyResultSet(getConnection(), this , real
436: .getGeneratedKeys());
437: }
438:
439: /**
440: * @see Statement#getResultSet()
441: */
442: public ResultSet getResultSet() throws SQLException {
443: return new SpyResultSet(getConnection(), this , real
444: .getResultSet());
445: }
446:
447: /**
448: * @see Statement#getWarnings()
449: */
450: public SQLWarning getWarnings() throws SQLException {
451: SQLWarning current, sw = real.getWarnings();
452:
453: if ((current = sw) != null) {
454: do {
455: if (log.isInfoEnabled())
456: log.info(getId() + ":sql warning state="
457: + current.getSQLState() + ",code="
458: + current.getErrorCode() + ",message="
459: + current.getMessage());
460: } while ((current = current.getNextWarning()) != null);
461: }
462:
463: return sw;
464: }
465:
466: /**
467: * @see Statement#executeUpdate(String, String[])
468: */
469: public int executeUpdate(String sql, String[] columnNames)
470: throws SQLException {
471: long end, start = System.currentTimeMillis();
472: int result = 0;
473:
474: try {
475: result = real.executeUpdate(sql, columnNames);
476: end = System.currentTimeMillis();
477: if (log.isInfoEnabled())
478: log.info(getId() + ":" + sql + " => " + result + " ("
479: + (end - start) + " ms)");
480: } catch (SQLException e) {
481: end = System.currentTimeMillis();
482: if (log.isErrorEnabled())
483: log.error(getId() + ":" + sql + " => state="
484: + e.getSQLState() + ",code=" + e.getErrorCode()
485: + " (" + (end - start) + " ms)", e);
486: throw e;
487: }
488:
489: return result;
490: }
491:
492: /**
493: * @see Statement#execute(String, String[])
494: */
495: public boolean execute(String sql, String[] columnNames)
496: throws SQLException {
497: long end, start = System.currentTimeMillis();
498: boolean result = false;
499:
500: try {
501: result = real.execute(sql, columnNames);
502: end = System.currentTimeMillis();
503: if (log.isInfoEnabled())
504: log.info(getId() + ":" + sql + " => " + result + " ("
505: + (end - start) + " ms)");
506: } catch (SQLException e) {
507: end = System.currentTimeMillis();
508: if (log.isErrorEnabled())
509: log.error(getId() + ":" + sql + " => state="
510: + e.getSQLState() + ",code=" + e.getErrorCode()
511: + " (" + (end - start) + " ms)", e);
512: throw e;
513: }
514:
515: return result;
516: }
517:
518: /**
519: * @see Statement#executeQuery(String)
520: */
521: public ResultSet executeQuery(String sql) throws SQLException {
522: long end, start = System.currentTimeMillis();
523: ResultSet result = null;
524:
525: try {
526: result = new SpyResultSet(getConnection(), this , real
527: .executeQuery(sql));
528: end = System.currentTimeMillis();
529: if (log.isInfoEnabled())
530: log.info(getId() + ":" + sql + " => ... ("
531: + (end - start) + " ms)");
532: } catch (SQLException e) {
533: end = System.currentTimeMillis();
534: if (log.isErrorEnabled())
535: log.error(getId() + ":" + sql + " => state="
536: + e.getSQLState() + ",code=" + e.getErrorCode()
537: + " (" + (end - start) + " ms)", e);
538: throw e;
539: }
540:
541: return result;
542: }
543: }
|