KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > sql > spy > SpyStatement


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.sql.spy;
30
31 import com.caucho.log.Log;
32 import com.caucho.sql.SQLExceptionWrapper;
33 import com.caucho.util.L10N;
34
35 import java.sql.Connection JavaDoc;
36 import java.sql.ResultSet JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.sql.SQLWarning JavaDoc;
39 import java.sql.Statement JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41
42 /**
43  * Spying on a statement;
44  */

45 public class SpyStatement implements java.sql.Statement JavaDoc {
46   protected final static Logger JavaDoc log = Log.open(SpyStatement.class);
47   protected final static L10N L = new L10N(SpyConnection.class);
48
49   protected String JavaDoc _id;
50   
51   // The spy connection
52
protected Connection JavaDoc _conn;
53   
54   // The underlying connection
55
protected Statement JavaDoc _stmt;
56
57   SpyStatement(String JavaDoc id, Connection JavaDoc conn, Statement JavaDoc stmt)
58   {
59     _id = id;
60
61     _conn = conn;
62     _stmt = stmt;
63   }
64
65   public Statement JavaDoc getStatement()
66   {
67     return _stmt;
68   }
69
70   public void addBatch(String JavaDoc sql)
71     throws SQLException JavaDoc
72   {
73     try {
74       log.info(_id + ":addBatch(" + sql + ")");
75       
76       _stmt.addBatch(sql);
77     } catch (Throwable JavaDoc e) {
78       log.info(_id + ":exn-addBatch(" + e + ")");
79       
80       throw SQLExceptionWrapper.create(e);
81     }
82   }
83
84   public void cancel()
85     throws SQLException JavaDoc
86   {
87     try {
88       log.info(_id + ":cancel()");
89       
90       _stmt.cancel();
91     } catch (Throwable JavaDoc e) {
92       log.info(_id + ":exn-cancel(" + e + ")");
93       
94       throw SQLExceptionWrapper.create(e);
95     }
96   }
97
98   public void clearBatch()
99     throws SQLException JavaDoc
100   {
101     try {
102       log.info(_id + ":clearBatch()");
103       
104       _stmt.clearBatch();
105     } catch (Throwable JavaDoc e) {
106       log.info(_id + ":exn-clearBatch(" + e + ")");
107       
108       throw SQLExceptionWrapper.create(e);
109     }
110   }
111
112   public void clearWarnings()
113     throws SQLException JavaDoc
114   {
115     try {
116       log.info(_id + ":clearWarnings()");
117       
118       _stmt.clearWarnings();
119     } catch (Throwable JavaDoc e) {
120       log.info(_id + ":exn-clearWarnings(" + e + ")");
121       
122       throw SQLExceptionWrapper.create(e);
123     }
124   }
125
126   public void close()
127     throws SQLException JavaDoc
128   {
129     try {
130       log.info(_id + ":close()");
131       
132       _stmt.close();
133     } catch (Throwable JavaDoc e) {
134       log.info(_id + ":exn-close(" + e + ")");
135       
136       throw SQLExceptionWrapper.create(e);
137     }
138   }
139
140   public java.sql.ResultSet JavaDoc executeQuery(String JavaDoc sql)
141     throws SQLException JavaDoc
142   {
143     try {
144       log.info(_id + ":executeQuery(" + sql + ")");
145       
146       ResultSet JavaDoc rs = _stmt.executeQuery(sql);
147
148       return rs;
149     } catch (Throwable JavaDoc e) {
150       log.info(_id + ":exn-executeQuery(" + sql + ") -> " + e);
151       
152       throw SQLExceptionWrapper.create(e);
153     }
154   }
155
156   public int executeUpdate(String JavaDoc sql)
157     throws SQLException JavaDoc
158   {
159     try {
160       int count = _stmt.executeUpdate(sql);
161
162       log.info(_id + ":executeUpdate(" + sql + ") -> " + count);
163       
164       return count;
165     } catch (Throwable JavaDoc e) {
166       log.info(_id + ":exn-executeUpdate(" + sql + ") -> " + e);
167       
168       throw SQLExceptionWrapper.create(e);
169     }
170   }
171
172   public boolean execute(String JavaDoc sql)
173     throws SQLException JavaDoc
174   {
175     try {
176       boolean hasResult = _stmt.execute(sql);
177
178       log.info(_id + ":execute(" + sql + ") -> " + hasResult);
179       
180       return hasResult;
181     } catch (Throwable JavaDoc e) {
182       log.info(_id + ":exn-execute(" + sql + ") -> " + e);
183       
184       throw SQLExceptionWrapper.create(e);
185     }
186   }
187
188   public int[]executeBatch()
189     throws SQLException JavaDoc
190   {
191     try {
192       int []result = _stmt.executeBatch();
193
194       log.info(_id + ":executeBatch()");
195       
196       return result;
197     } catch (Throwable JavaDoc e) {
198       log.info(_id + ":exn-executeBatch(" + e + ")");
199       
200       throw SQLExceptionWrapper.create(e);
201     }
202   }
203
204   public java.sql.ResultSet JavaDoc getResultSet()
205     throws SQLException JavaDoc
206   {
207     try {
208       ResultSet JavaDoc result = _stmt.getResultSet();
209
210       log.info(_id + ":getResultSet() -> " + result);
211       
212       return result;
213     } catch (Throwable JavaDoc e) {
214       log.info(_id + ":exn-getResultSet(" + e + ")");
215       
216       throw SQLExceptionWrapper.create(e);
217     }
218   }
219
220   public int getUpdateCount()
221     throws SQLException JavaDoc
222   {
223     try {
224       int updateCount = _stmt.getUpdateCount();
225
226       log.info(_id + ":getUpdateCount() -> " + updateCount);
227       
228       return updateCount;
229     } catch (Throwable JavaDoc e) {
230       log.info(_id + ":exn-getUpdateCount(" + e + ")");
231       
232       throw SQLExceptionWrapper.create(e);
233     }
234   }
235
236   public java.sql.Connection JavaDoc getConnection()
237     throws SQLException JavaDoc
238   {
239     int updateCount = _stmt.getUpdateCount();
240
241     log.info(_id + ":getConnection()");
242       
243     return _conn;
244   }
245
246   public int getFetchDirection()
247     throws SQLException JavaDoc
248   {
249     try {
250       int result = _stmt.getFetchDirection();
251
252       log.info(_id + ":getFetchDirection() -> " + result);
253       
254       return result;
255     } catch (Throwable JavaDoc e) {
256       log.info(_id + ":exn-getFetchDirection(" + e + ")");
257       
258       throw SQLExceptionWrapper.create(e);
259     }
260   }
261
262   public int getFetchSize()
263     throws SQLException JavaDoc
264   {
265     try {
266       int result = _stmt.getFetchSize();
267
268       log.info(_id + ":getFetchSize() -> " + result);
269       
270       return result;
271     } catch (Throwable JavaDoc e) {
272       log.info(_id + ":exn-getFetchSize(" + e + ")");
273       
274       throw SQLExceptionWrapper.create(e);
275     }
276   }
277
278   public int getMaxFieldSize()
279     throws SQLException JavaDoc
280   {
281     try {
282       int result = _stmt.getMaxFieldSize();
283
284       log.info(_id + ":getMaxFieldSize() -> " + result);
285       
286       return result;
287     } catch (Throwable JavaDoc e) {
288       log.info(_id + ":exn-getMaxFieldSize(" + e + ")");
289       
290       throw SQLExceptionWrapper.create(e);
291     }
292   }
293
294   public int getMaxRows()
295     throws SQLException JavaDoc
296   {
297     try {
298       int result = _stmt.getMaxRows();
299
300       log.info(_id + ":getMaxRows() -> " + result);
301       
302       return result;
303     } catch (Throwable JavaDoc e) {
304       log.info(_id + ":exn-getMaxRows(" + e + ")");
305       
306       throw SQLExceptionWrapper.create(e);
307     }
308   }
309   
310   public void setMaxRows(int max)
311     throws SQLException JavaDoc
312   {
313     try {
314       log.info(_id + ":setMaxRows(" + max + ")");
315
316       _stmt.setMaxRows(max);
317     } catch (Throwable JavaDoc e) {
318       log.info(_id + ":exn-setMaxRows(" + e + ")");
319       
320       throw SQLExceptionWrapper.create(e);
321     }
322   }
323
324   public boolean getMoreResults()
325     throws SQLException JavaDoc
326   {
327     try {
328       boolean result = _stmt.getMoreResults();
329
330       log.info(_id + ":getMoreResults() -> " + result);
331       
332       return result;
333     } catch (Throwable JavaDoc e) {
334       log.info(_id + ":exn-getMoreResults(" + e + ")");
335       
336       throw SQLExceptionWrapper.create(e);
337     }
338   }
339
340   public int getQueryTimeout()
341     throws SQLException JavaDoc
342   {
343     try {
344       int result = _stmt.getQueryTimeout();
345
346       log.info(_id + ":getQueryTimeout() -> " + result);
347       
348       return result;
349     } catch (Throwable JavaDoc e) {
350       log.info(_id + ":exn-getQueryTimeout(" + e + ")");
351       
352       throw SQLExceptionWrapper.create(e);
353     }
354   }
355
356   public int getResultSetConcurrency()
357     throws SQLException JavaDoc
358   {
359     try {
360       int result = _stmt.getResultSetConcurrency();
361
362       log.info(_id + ":getResultSetConcurrency() -> " + result);
363       
364       return result;
365     } catch (Throwable JavaDoc e) {
366       log.info(_id + ":exn-getResultSetConcurrency(" + e + ")");
367       
368       throw SQLExceptionWrapper.create(e);
369     }
370   }
371
372   public int getResultSetType()
373     throws SQLException JavaDoc
374   {
375     try {
376       int result = _stmt.getResultSetType();
377
378       log.info(_id + ":getResultSetType() -> " + result);
379       
380       return result;
381     } catch (Throwable JavaDoc e) {
382       log.info(_id + ":exn-getResultSetType(" + e + ")");
383       
384       throw SQLExceptionWrapper.create(e);
385     }
386   }
387
388   public SQLWarning JavaDoc getWarnings()
389     throws SQLException JavaDoc
390   {
391     try {
392       SQLWarning JavaDoc result = _stmt.getWarnings();
393
394       log.info(_id + ":getWarnings() -> " + result);
395       
396       return result;
397     } catch (Throwable JavaDoc e) {
398       log.info(_id + ":exn-getWarnings(" + e + ")");
399       
400       throw SQLExceptionWrapper.create(e);
401     }
402   }
403
404   public void setCursorName(String JavaDoc name)
405     throws SQLException JavaDoc
406   {
407     try {
408       log.info(_id + ":setCursorName(" + name + ")");
409
410       _stmt.setCursorName(name);
411     } catch (Throwable JavaDoc e) {
412       log.info(_id + ":exn-setCursorName(" + e + ")");
413       
414       throw SQLExceptionWrapper.create(e);
415     }
416   }
417
418   public void setEscapeProcessing(boolean enable)
419     throws SQLException JavaDoc
420   {
421     try {
422       log.info(_id + ":setEscapeProcessing(" + enable + ")");
423
424       _stmt.setEscapeProcessing(enable);
425     } catch (Throwable JavaDoc e) {
426       log.info(_id + ":exn-setEscapeProcessing(" + e + ")");
427       
428       throw SQLExceptionWrapper.create(e);
429     }
430   }
431
432   public void setFetchDirection(int direction)
433     throws SQLException JavaDoc
434   {
435     try {
436       log.info(_id + ":setFetchDirection(" + direction + ")");
437
438       _stmt.setFetchDirection(direction);
439     } catch (Throwable JavaDoc e) {
440       log.info(_id + ":exn-setFetchDirection(" + e + ")");
441       
442       throw SQLExceptionWrapper.create(e);
443     }
444   }
445
446   public void setFetchSize(int rows)
447     throws SQLException JavaDoc
448   {
449     try {
450       log.info(_id + ":setFetchSize(" + rows + ")");
451
452       _stmt.setFetchSize(rows);
453     } catch (Throwable JavaDoc e) {
454       log.info(_id + ":exn-setFetchSize(" + e + ")");
455       
456       throw SQLExceptionWrapper.create(e);
457     }
458   }
459
460   public void setMaxFieldSize(int max)
461     throws SQLException JavaDoc
462   {
463     try {
464       log.info(_id + ":setMaxFieldSize(" + max + ")");
465
466       _stmt.setMaxFieldSize(max);
467     } catch (Throwable JavaDoc e) {
468       log.info(_id + ":exn-setMaxFieldSize(" + e + ")");
469       
470       throw SQLExceptionWrapper.create(e);
471     }
472   }
473   
474   public void setQueryTimeout(int seconds)
475     throws SQLException JavaDoc
476   {
477     try {
478       log.info(_id + ":setQueryTimeout(" + seconds + ")");
479
480       _stmt.setQueryTimeout(seconds);
481     } catch (Throwable JavaDoc e) {
482       log.info(_id + ":exn-setQueryTimeout(" + e + ")");
483       
484       throw SQLExceptionWrapper.create(e);
485     }
486   }
487
488   // jdk 1.4
489
public boolean getMoreResults(int count)
490     throws SQLException JavaDoc
491   {
492     return _stmt.getMoreResults(count);
493   }
494   
495   public java.sql.ResultSet JavaDoc getGeneratedKeys()
496     throws SQLException JavaDoc
497   {
498     return _stmt.getGeneratedKeys();
499   }
500   
501   public int executeUpdate(String JavaDoc query, int resultType)
502     throws SQLException JavaDoc
503   {
504     return _stmt.executeUpdate(query, resultType);
505   }
506   
507   public int executeUpdate(String JavaDoc query, int []columns)
508     throws SQLException JavaDoc
509   {
510     return _stmt.executeUpdate(query, columns);
511   }
512   
513   public int executeUpdate(String JavaDoc query, String JavaDoc []columns)
514     throws SQLException JavaDoc
515   {
516     return _stmt.executeUpdate(query, columns);
517   }
518   
519   public boolean execute(String JavaDoc query, int resultType)
520     throws SQLException JavaDoc
521   {
522     return _stmt.execute(query, resultType);
523   }
524   
525   public boolean execute(String JavaDoc query, int []columns)
526     throws SQLException JavaDoc
527   {
528     return _stmt.execute(query, columns);
529   }
530   
531   public boolean execute(String JavaDoc query, String JavaDoc []columns)
532     throws SQLException JavaDoc
533   {
534     return _stmt.execute(query, columns);
535   }
536
537   public int getResultSetHoldability()
538     throws SQLException JavaDoc
539   {
540     return _stmt.getResultSetHoldability();
541   }
542 }
543
Popular Tags