KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jdbclogger > core > StatementWrapper


1 package net.sourceforge.jdbclogger.core;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import java.sql.*;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * @author Martin Marinschek (latest modification by $Author: sorin7486 $)
28  * @version $Revision: 97 $ $Date: 2007-07-12 15:47:34 +0300 (Thu, 12 Jul 2007) $
29  */

30 public class StatementWrapper implements Statement
31 {
32     private Statement _statement;
33     private List JavaDoc _batchedStatements;
34
35     private static Log log = LogFactory.getLog(StatementWrapper.class);
36
37     public StatementWrapper(Statement statement)
38     {
39         _statement = statement;
40     }
41
42     protected void logStatement(String JavaDoc sql)
43     {
44         if(log.isDebugEnabled())
45             log.debug("Statement : " + sql);
46     }
47
48     protected void logTime(long begin)
49     {
50         if (log.isDebugEnabled ()) // TODO: find siutable text for logging !!
51
log.debug ("last operation took: " + (System.currentTimeMillis () - begin) + "ms");
52     }
53
54     public int getFetchDirection() throws SQLException
55     {
56         return _statement.getFetchDirection();
57     }
58
59     public int getFetchSize() throws SQLException
60     {
61         return _statement.getFetchSize();
62     }
63
64     public int getMaxFieldSize() throws SQLException
65     {
66         return _statement.getMaxFieldSize();
67     }
68
69     public int getMaxRows() throws SQLException
70     {
71         return _statement.getMaxRows();
72     }
73
74     public int getQueryTimeout() throws SQLException
75     {
76         return _statement.getQueryTimeout();
77     }
78
79     public int getResultSetConcurrency() throws SQLException
80     {
81         return _statement.getResultSetConcurrency();
82     }
83
84     public int getResultSetHoldability() throws SQLException
85     {
86         return _statement.getResultSetHoldability();
87     }
88
89     public int getResultSetType() throws SQLException
90     {
91         return _statement.getResultSetType();
92     }
93
94     public int getUpdateCount() throws SQLException
95     {
96         return _statement.getUpdateCount();
97     }
98
99     public void cancel() throws SQLException
100     {
101         _statement.cancel();
102     }
103
104     public void clearBatch() throws SQLException
105     {
106         if(_batchedStatements != null)
107             _batchedStatements = null;
108
109         _statement.clearBatch();
110     }
111
112     public void clearWarnings() throws SQLException
113     {
114         _statement.clearWarnings();
115     }
116
117     public void close() throws SQLException
118     {
119         _statement.close();
120     }
121
122     public boolean getMoreResults() throws SQLException
123     {
124         return _statement.getMoreResults();
125     }
126
127     public int[] executeBatch() throws SQLException
128     {
129         if(_batchedStatements != null)
130         {
131             for (int i = 0; i < _batchedStatements.size(); i++)
132             {
133                 String JavaDoc statement = (String JavaDoc) _batchedStatements.get(i);
134                 logStatement(statement);
135             }
136         }
137
138         _batchedStatements = null;
139
140         //saving current time for logging
141
long begin = System.currentTimeMillis ();
142
143         //executing statements
144
int[] rez = _statement.executeBatch();
145
146         //logging time
147
logTime(begin);
148
149         return rez;
150     }
151
152     public void setFetchDirection(int direction) throws SQLException
153     {
154         _statement.setFetchDirection(direction);
155     }
156
157     public void setFetchSize(int rows) throws SQLException
158     {
159         _statement.setFetchSize(rows);
160     }
161
162     public void setMaxFieldSize(int max) throws SQLException
163     {
164         _statement.setMaxFieldSize(max);
165     }
166
167     public void setMaxRows(int max) throws SQLException
168     {
169         _statement.setMaxRows(max);
170     }
171
172     public void setQueryTimeout(int seconds) throws SQLException
173     {
174         _statement.setQueryTimeout(seconds);
175     }
176
177     public boolean getMoreResults(int current) throws SQLException
178     {
179         return _statement.getMoreResults(current);
180     }
181
182     public void setEscapeProcessing(boolean enable) throws SQLException
183     {
184         _statement.setEscapeProcessing(enable);
185     }
186
187     public int executeUpdate(String JavaDoc sql) throws SQLException
188     {
189         return _statement.executeUpdate(sql);
190     }
191
192     public void addBatch(String JavaDoc sql) throws SQLException
193     {
194         _statement.addBatch(sql);
195         if (_batchedStatements == null){
196             _batchedStatements = new ArrayList JavaDoc();
197         }
198         _batchedStatements.add(sql);
199     }
200
201     public void setCursorName(String JavaDoc name) throws SQLException
202     {
203         _statement.setCursorName(name);
204     }
205
206     public boolean execute(String JavaDoc sql) throws SQLException
207     {
208         logStatement (sql);
209         long begin = System.currentTimeMillis ();
210
211         boolean ret = _statement.execute(sql);
212
213         logTime (begin);
214
215         return ret;
216     }
217
218     public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException
219     {
220         logStatement (sql);
221         long begin = System.currentTimeMillis ();
222
223         int ret = _statement.executeUpdate(sql, autoGeneratedKeys);
224
225         logTime (begin);
226
227         return ret;
228     }
229
230     public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException
231     {
232         logStatement (sql);
233         long begin = System.currentTimeMillis ();
234
235         boolean ret = _statement.execute(sql, autoGeneratedKeys);
236
237         logTime (begin);
238
239         return ret;
240     }
241
242     public int executeUpdate(String JavaDoc sql, int columnIndexes[]) throws SQLException
243     {
244         logStatement (sql);
245         long begin = System.currentTimeMillis ();
246
247         int ret = _statement.executeUpdate(sql, columnIndexes);
248
249         logTime (begin);
250
251         return ret;
252     }
253
254     public boolean execute(String JavaDoc sql, int columnIndexes[]) throws SQLException
255     {
256         logStatement (sql);
257         long begin = System.currentTimeMillis ();
258
259         boolean ret = _statement.execute(sql, columnIndexes);
260         logTime (begin);
261
262         return ret;
263     }
264
265     public Connection getConnection() throws SQLException
266     {
267         return _statement.getConnection();
268     }
269
270     public ResultSet getGeneratedKeys() throws SQLException
271     {
272         return _statement.getGeneratedKeys();
273     }
274
275     public ResultSet getResultSet() throws SQLException
276     {
277         return _statement.getResultSet();
278     }
279
280     public SQLWarning getWarnings() throws SQLException
281     {
282         return _statement.getWarnings();
283     }
284
285     public int executeUpdate(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException
286     {
287         logStatement (sql);
288         long begin = System.currentTimeMillis ();
289
290         int ret = _statement.executeUpdate(sql, columnNames);
291
292         logTime (begin);
293
294         return ret;
295     }
296
297     public boolean execute(String JavaDoc sql, String JavaDoc columnNames[]) throws SQLException
298     {
299         logStatement (sql);
300         long begin = System.currentTimeMillis ();
301
302         boolean ret = _statement.execute(sql, columnNames);
303
304         logTime (begin);
305
306         return ret;
307     }
308
309     public ResultSet executeQuery(String JavaDoc sql) throws SQLException
310     {
311         logStatement (sql);
312         long begin = System.currentTimeMillis ();
313
314         ResultSet ret = _statement.executeQuery(sql);
315
316         logTime (begin);
317
318         return ret;
319     }
320 }
321
Popular Tags