KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > craftsman > spy > SpyStatement


1 /*
2  * Craftsman Spy.
3  * Copyright (C) 2005 Sébastien LECACHEUR
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */

19 package craftsman.spy;
20
21 import java.sql.Connection JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.sql.SQLWarning JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.util.ArrayList JavaDoc;
27
28 /**
29  * This classe is used for executing a static SQL statement and
30  * returning the results it produces.
31  *
32  * @author Sébastien LECACHEUR
33  */

34 public class SpyStatement extends SpyEntity implements Statement JavaDoc {
35     /**
36      * The real SQL statement instance.
37      */

38     private Statement JavaDoc real = null;
39
40
41     /**
42      * The list of all the SQL in the current batch.
43      */

44     protected ArrayList JavaDoc batch = null;
45
46
47     /**
48      * Constructs a new Spy JDBC statement.
49      *
50      * @param c Connection The used connection.
51      * @param stm Statement The real JDBC statement.
52      */

53     protected SpyStatement ( Connection JavaDoc c, Statement JavaDoc stm) {
54         super ( c);
55         real = stm;
56         batch = new ArrayList JavaDoc();
57     }
58
59
60     /**
61      * @see Statement#getFetchDirection()
62      */

63     public int getFetchDirection() throws SQLException JavaDoc {
64         return real.getFetchDirection();
65     }
66
67
68     /**
69      * @see Statement#getFetchSize()
70      */

71     public int getFetchSize() throws SQLException JavaDoc {
72         return real.getFetchSize();
73     }
74
75
76     /**
77      * @see Statement#getMaxFieldSize()
78      */

79     public int getMaxFieldSize() throws SQLException JavaDoc {
80         return real.getMaxFieldSize();
81     }
82
83
84     /**
85      * @see Statement#getMaxRows()
86      */

87     public int getMaxRows() throws SQLException JavaDoc {
88         return real.getMaxRows();
89     }
90
91
92     /**
93      * @see Statement#getQueryTimeout()
94      */

95     public int getQueryTimeout() throws SQLException JavaDoc {
96         return real.getQueryTimeout();
97     }
98
99
100     /**
101      * @see Statement#getResultSetConcurrency()
102      */

103     public int getResultSetConcurrency() throws SQLException JavaDoc {
104         return real.getResultSetConcurrency();
105     }
106
107
108     /**
109      * @see Statement#getResultSetHoldability()
110      */

111     public int getResultSetHoldability() throws SQLException JavaDoc {
112         return real.getResultSetHoldability();
113     }
114
115
116     /**
117      * @see Statement#getResultSetType()
118      */

119     public int getResultSetType() throws SQLException JavaDoc {
120         return real.getResultSetType();
121     }
122
123
124     /**
125      * @see Statement#getUpdateCount()
126      */

127     public int getUpdateCount() throws SQLException JavaDoc {
128         return real.getUpdateCount();
129     }
130
131
132     /**
133      * @see Statement#cancel()
134      */

135     public void cancel() throws SQLException JavaDoc {
136         real.cancel();
137     }
138
139
140     /**
141      * @see Statement#clearBatch()
142      */

143     public void clearBatch() throws SQLException JavaDoc {
144         batch.clear();
145         real.clearBatch();
146     }
147
148
149     /**
150      * @see Statement#clearWarnings()
151      */

152     public void clearWarnings() throws SQLException JavaDoc {
153         real.clearWarnings();
154     }
155
156
157     /**
158      * @see Statement#close()
159      */

160     public void close() throws SQLException JavaDoc {
161         real.close();
162     }
163
164
165     /**
166      * @see Statement#getMoreResults()
167      */

168     public boolean getMoreResults() throws SQLException JavaDoc {
169         return real.getMoreResults();
170     }
171
172
173     /**
174      * @see Statement#executeBatch()
175      */

176     public int[] executeBatch() throws SQLException JavaDoc {
177         long end, start = System.currentTimeMillis();
178         int[] result = null;
179
180
181         try {
182             result = real.executeBatch();
183             end = System.currentTimeMillis();
184
185             if ( batch.size() != result.length) {
186                 if ( log.isWarnEnabled()) log.warn(getId()+":"+"expecting:"+batch.size()+" but was:"+result.length);
187             }
188             if ( log.isInfoEnabled()) {
189                 log.info(getId()+":"+"executing batch...");
190                 for (int i = 0; i < result.length && i <batch.size(); i++) {
191                     switch(result[i]) {
192                         case SUCCESS_NO_INFO: log.info(getId()+":"+batch.get(i)+" => SUCCESS_NO_INFO");
193                                                 break;
194
195                         case EXECUTE_FAILED: log.info(getId()+":"+batch.get(i)+" => EXECUTE_FAILED");
196                                                 break;
197
198                         default: log.info(getId()+":"+batch.get(i)+" => "+result[i]);
199                                                 break;
200                     }
201                 }
202                 log.info(getId()+":"+"batch executed ("+(end-start)+" ms)");
203             }
204         } catch ( SQLException JavaDoc e) {
205             end = System.currentTimeMillis();
206             for (int i = 0; i < batch.size(); i++) {
207                 log.error(getId()+":"+batch.get(i)+" => ...");
208             }
209             if ( log.isWarnEnabled()) log.warn(getId()+":batch.size="+batch.size());
210             if ( log.isErrorEnabled()) log.error(getId()+":"+batch+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
211             throw e;
212         }
213
214         return result;
215     }
216
217
218     /**
219      * @see Statement#setFetchDirection(int)
220      */

221     public void setFetchDirection(int direction) throws SQLException JavaDoc {
222         real.setFetchDirection(direction);
223     }
224
225
226     /**
227      * @see Statement#setFetchSize(int)
228      */

229     public void setFetchSize(int rows) throws SQLException JavaDoc {
230         real.setFetchSize(rows);
231     }
232
233
234     /**
235      * @see Statement#setMaxFieldSize(int)
236      */

237     public void setMaxFieldSize(int max) throws SQLException JavaDoc {
238         real.setMaxFieldSize(max);
239     }
240
241
242     /**
243      * @see Statement#setMaxRows(int)
244      */

245     public void setMaxRows(int max) throws SQLException JavaDoc {
246         real.setMaxRows(max);
247     }
248
249
250     /**
251      * @see Statement#setQueryTimeout(int)
252      */

253     public void setQueryTimeout(int seconds) throws SQLException JavaDoc {
254         real.setQueryTimeout(seconds);
255     }
256
257
258     /**
259      * @see Statement#getMoreResults(int)
260      */

261     public boolean getMoreResults(int current) throws SQLException JavaDoc {
262         return real.getMoreResults(current);
263     }
264
265
266     /**
267      * @see Statement#setEscapeProcessing(boolean)
268      */

269     public void setEscapeProcessing(boolean enable) throws SQLException JavaDoc {
270         real.setEscapeProcessing(enable);
271     }
272
273
274     /**
275      * @see Statement#executeUpdate(String)
276      */

277     public int executeUpdate(String JavaDoc sql) throws SQLException JavaDoc {
278         long end, start = System.currentTimeMillis();
279         int result = 0;
280
281
282         try {
283             result = real.executeUpdate(sql);
284             end = System.currentTimeMillis();
285             if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
286         } catch ( SQLException JavaDoc e) {
287             end = System.currentTimeMillis();
288             if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
289             throw e;
290         }
291
292         return result;
293     }
294
295
296     /**
297      * @see Statement#addBatch(String)
298      */

299     public void addBatch(String JavaDoc sql) throws SQLException JavaDoc {
300         batch.add(sql);
301         real.addBatch(sql);
302     }
303
304
305     /**
306      * @see Statement#setCursorName(String)
307      */

308     public void setCursorName(String JavaDoc name) throws SQLException JavaDoc {
309         real.setCursorName(name);
310     }
311
312
313     /**
314      * @see Statement#execute(String)
315      */

316     public boolean execute(String JavaDoc sql) throws SQLException JavaDoc {
317         long end, start = System.currentTimeMillis();
318         boolean result = false;
319
320
321         try {
322             result = real.execute(sql);
323             end = System.currentTimeMillis();
324             if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
325         } catch ( SQLException JavaDoc e) {
326             end = System.currentTimeMillis();
327             if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
328             throw e;
329         }
330
331         return result;
332     }
333
334
335     /**
336      * @see Statement#executeUpdate(String, int)
337      */

338     public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
339         long end, start = System.currentTimeMillis();
340         int result = 0;
341
342
343         try {
344             result = real.executeUpdate(sql,autoGeneratedKeys);
345             end = System.currentTimeMillis();
346             if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
347         } catch ( SQLException JavaDoc e) {
348             end = System.currentTimeMillis();
349             if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
350             throw e;
351         }
352
353         return result;
354     }
355
356
357     /**
358      * @see Statement#execute(String, int)
359      */

360     public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc {
361         long end, start = System.currentTimeMillis();
362         boolean result = false;
363
364
365         try {
366             result = real.execute(sql,autoGeneratedKeys);
367             end = System.currentTimeMillis();
368             if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
369         } catch ( SQLException JavaDoc e) {
370             end = System.currentTimeMillis();
371             if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
372             throw e;
373         }
374
375         return result;
376     }
377
378
379     /**
380      * @see Statement#executeUpdate(String, int[])
381      */

382     public int executeUpdate(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
383         long end, start = System.currentTimeMillis();
384         int result = 0;
385
386
387         try {
388             result = real.executeUpdate(sql,columnIndexes);
389             end = System.currentTimeMillis();
390             if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
391         } catch ( SQLException JavaDoc e) {
392             end = System.currentTimeMillis();
393             if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
394             throw e;
395         }
396
397         return result;
398     }
399
400
401     /**
402      * @see Statement#execute(String, int[])
403      */

404     public boolean execute(String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc {
405         long end, start = System.currentTimeMillis();
406         boolean result = false;
407
408
409         try {
410             result = real.execute(sql,columnIndexes);
411             end = System.currentTimeMillis();
412             if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
413         } catch ( SQLException JavaDoc e) {
414             end = System.currentTimeMillis();
415             if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
416             throw e;
417         }
418
419         return result;
420     }
421
422
423     /**
424      * @see Statement#getGeneratedKeys()
425      */

426     public ResultSet JavaDoc getGeneratedKeys() throws SQLException JavaDoc {
427         return new SpyResultSet ( getConnection(), this, real.getGeneratedKeys());
428     }
429
430
431     /**
432      * @see Statement#getResultSet()
433      */

434     public ResultSet JavaDoc getResultSet() throws SQLException JavaDoc {
435         return new SpyResultSet ( getConnection(), this, real.getResultSet());
436     }
437
438
439     /**
440      * @see Statement#getWarnings()
441      */

442     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
443         SQLWarning JavaDoc current, sw = real.getWarnings();
444
445
446         if ( (current = sw)!=null) {
447             do {
448                 if ( log.isInfoEnabled()) log.info(getId()+":sql warning state="+current.getSQLState()+",code="+current.getErrorCode()+",message="+current.getMessage());
449             } while ( (current = current.getNextWarning())!=null);
450         }
451
452         return sw;
453     }
454
455
456     /**
457      * @see Statement#executeUpdate(String, String[])
458      */

459     public int executeUpdate(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
460         long end, start = System.currentTimeMillis();
461         int result = 0;
462
463
464         try {
465             result = real.executeUpdate(sql,columnNames);
466             end = System.currentTimeMillis();
467             if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
468         } catch ( SQLException JavaDoc e) {
469             end = System.currentTimeMillis();
470             if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
471             throw e;
472         }
473
474         return result;
475     }
476
477
478     /**
479      * @see Statement#execute(String, String[])
480      */

481     public boolean execute(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc {
482         long end, start = System.currentTimeMillis();
483         boolean result = false;
484
485
486         try {
487             result = real.execute(sql,columnNames);
488             end = System.currentTimeMillis();
489             if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => "+result+" ("+(end-start)+" ms)");
490         } catch ( SQLException JavaDoc e) {
491             end = System.currentTimeMillis();
492             if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
493             throw e;
494         }
495
496         return result;
497     }
498
499
500     /**
501      * @see Statement#executeQuery(String)
502      */

503     public ResultSet JavaDoc executeQuery(String JavaDoc sql) throws SQLException JavaDoc {
504         long end, start = System.currentTimeMillis();
505         ResultSet JavaDoc result = null;
506
507
508         try {
509             result = new SpyResultSet ( getConnection(), this, real.executeQuery(sql));
510             end = System.currentTimeMillis();
511             if ( log.isInfoEnabled()) log.info(getId()+":"+sql+" => ... ("+(end-start)+" ms)");
512         } catch ( SQLException JavaDoc e) {
513             end = System.currentTimeMillis();
514             if ( log.isErrorEnabled()) log.error(getId()+":"+sql+" => state="+e.getSQLState()+",code="+e.getErrorCode()+" ("+(end-start)+" ms)",e);
515             throw e;
516         }
517
518         return result;
519     }
520 }
521
Popular Tags