KickJava   Java API By Example, From Geeks To Geeks.

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


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 import net.sourceforge.jdbclogger.core.util.Tokenizer;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.math.BigDecimal JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.sql.*;
27 import java.sql.Date JavaDoc;
28 import java.util.*;
29
30 /**
31  * @author Martin Marinschek (latest modification by $Author: catalean $)
32  * @version $Revision: 83 $ $Date: 2007-07-08 00:00:58 +0300 (Sun, 08 Jul 2007) $
33  */

34 public class PreparedStatementWrapper extends StatementWrapper implements PreparedStatement
35 {
36     private static Log log = LogFactory.getLog(PreparedStatementWrapper.class);
37
38     protected List _paramFormatterList;
39
40     protected PreparedStatement _prep;
41
42     protected List _batchList;
43     protected int _currentIndex = 0;
44
45     protected Map _paramIndexToListIndex = new HashMap();
46
47     private List getSqlAsList()
48     {
49         if(_batchList == null)
50         {
51             _batchList = new ArrayList();
52         }
53
54         if(_batchList.size()==_currentIndex)
55         {
56             if(_batchList.size()==0)
57             {
58                 _batchList.add(new ArrayList());
59             }
60             else
61             {
62                 _batchList.add(new ArrayList((List) _batchList.get(0)));
63             }
64         }
65         else if(_batchList.size()<_currentIndex)
66         {
67             throw new IllegalStateException JavaDoc("batchList size less than currentIndex");
68         }
69
70         return (List) _batchList.get(_currentIndex);
71     }
72
73     public PreparedStatementWrapper(
74             PreparedStatement statement, String JavaDoc sql, List formatters)
75     {
76         super(statement);
77
78         Tokenizer tokenizer = new Tokenizer(sql,new String JavaDoc[]{"?"},true,
79                 true,'\"','\"');
80
81         int delimiterCount = 1;
82
83         while(tokenizer.hasMoreTokens())
84         {
85             String JavaDoc token = tokenizer.nextToken();
86
87             if(token.equals("?"))
88             {
89                 _paramIndexToListIndex.put(new Integer JavaDoc(delimiterCount),
90                         new Integer JavaDoc(getSqlAsList().size()));
91                 delimiterCount++;
92             }
93
94             getSqlAsList().add(token);
95         }
96
97         _paramFormatterList = formatters;
98
99         _prep = statement;
100     }
101
102     protected void setParameter(int parameterIndex, Object JavaDoc parameter)
103     {
104         Integer JavaDoc listIndex = (Integer JavaDoc)
105                 _paramIndexToListIndex.get(new Integer JavaDoc(parameterIndex));
106
107         if(listIndex != null)
108         {
109             String JavaDoc sql;
110             ParameterFormatter pf;
111
112             if (parameter == null)
113                 sql = "null";
114             else if ((pf = getParamFormatterFor(parameter.getClass ())) != null)
115                 sql = pf.format (parameter);
116             else
117                 sql = "'"+parameter+"'";
118
119             getSqlAsList().set(listIndex.intValue(), sql);
120         }
121     }
122
123     protected ParameterFormatter getParamFormatterFor (Class JavaDoc clazz)
124     {
125         for (int i = 0; i < _paramFormatterList.size (); i++)
126         {
127             ParameterFormatter pf = (ParameterFormatter) _paramFormatterList.get (i);
128
129             if (pf.getFormattingClass () .isAssignableFrom (clazz))
130                 return pf;
131         }
132
133         return null;
134     }
135
136     protected void logStatement()
137     {
138         if(_batchList!=null)
139         {
140             for(int i=0; i<_batchList.size();i++)
141             {
142                 List sqlAsList = (List) _batchList.get(i);
143
144                 if(log.isDebugEnabled())
145                 {
146                     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
147
148                     for (int j = 0; j < sqlAsList.size(); j++)
149                     {
150                         String JavaDoc token = (String JavaDoc) sqlAsList.get(j);
151                         buf.append(token);
152                     }
153
154                     log.debug("Prepared Statement : " + buf.toString());
155                 }
156             }
157         }
158     }
159
160     public int executeUpdate() throws SQLException
161     {
162         logStatement();
163         long begin = System.currentTimeMillis ();
164
165         int res = _prep.executeUpdate();
166
167         logTime (begin);
168
169         return res;
170     }
171
172     public void addBatch() throws SQLException
173     {
174         _currentIndex++;
175
176         _prep.addBatch();
177     }
178
179     public int[] executeBatch() throws SQLException
180     {
181         logStatement();
182
183         _batchList = null;
184         _currentIndex = 0;
185
186         return super.executeBatch();
187     }
188
189     public void clearBatch() throws SQLException
190     {
191         _batchList = null;
192         _currentIndex = 0;
193
194         super.clearBatch();
195     }
196
197     public void clearParameters() throws SQLException
198     {
199         _prep.clearParameters();
200     }
201
202     public boolean execute() throws SQLException
203     {
204         logStatement();
205         long begin = System.currentTimeMillis ();
206
207         boolean res = _prep.execute();
208
209         logTime (begin);
210
211         return res;
212     }
213
214     public void setByte(int parameterIndex, byte x) throws SQLException
215     {
216         setParameter(parameterIndex, new Byte JavaDoc(x));
217
218         _prep.setByte(parameterIndex, x);
219     }
220
221     public void setDouble(int parameterIndex, double x) throws SQLException
222     {
223         setParameter(parameterIndex, new Double JavaDoc(x));
224
225         _prep.setDouble(parameterIndex, x);
226     }
227
228     public void setFloat(int parameterIndex, float x) throws SQLException
229     {
230         setParameter(parameterIndex, new Float JavaDoc(x));
231
232         _prep.setFloat(parameterIndex, x);
233     }
234
235     public void setInt(int parameterIndex, int x) throws SQLException
236     {
237         setParameter(parameterIndex, new Integer JavaDoc(x));
238
239         _prep.setInt(parameterIndex, x);
240     }
241
242     public void setNull(int parameterIndex, int sqlType) throws SQLException
243     {
244         setParameter(parameterIndex, null);
245
246         _prep.setNull(parameterIndex, sqlType);
247     }
248
249     public void setLong(int parameterIndex, long x) throws SQLException
250     {
251         setParameter(parameterIndex, new Long JavaDoc(x));
252
253         _prep.setLong(parameterIndex, x);
254     }
255
256     public void setShort(int parameterIndex, short x) throws SQLException
257     {
258         setParameter(parameterIndex, new Short JavaDoc(x));
259
260         _prep.setShort(parameterIndex, x);
261     }
262
263     public void setBoolean(int parameterIndex, boolean x) throws SQLException
264     {
265         setParameter(parameterIndex, Boolean.valueOf(x));
266
267         _prep.setBoolean(parameterIndex, x);
268     }
269
270     public void setBytes(int parameterIndex, byte x[]) throws SQLException
271     {
272         _prep.setBytes(parameterIndex, x);
273     }
274
275     public void setAsciiStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException
276     {
277         _prep.setAsciiStream(parameterIndex, x, length);
278     }
279
280     public void setBinaryStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException
281     {
282         _prep.setBinaryStream(parameterIndex, x, length);
283     }
284
285     public void setUnicodeStream(int parameterIndex, InputStream JavaDoc x, int length) throws SQLException
286     {
287         _prep.setUnicodeStream(parameterIndex, x, length);
288     }
289
290     public void setCharacterStream(int parameterIndex, Reader JavaDoc reader, int length) throws SQLException
291     {
292         _prep.setCharacterStream(parameterIndex, reader, length);
293     }
294
295     public void setObject(int parameterIndex, Object JavaDoc x) throws SQLException
296     {
297         setParameter(parameterIndex, x);
298
299         _prep.setObject(parameterIndex, x);
300     }
301
302     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType) throws SQLException
303     {
304         setParameter(parameterIndex, x);
305
306         _prep.setObject(parameterIndex, x, targetSqlType);
307     }
308
309     public void setObject(int parameterIndex, Object JavaDoc x, int targetSqlType, int scale) throws SQLException
310     {
311         setParameter(parameterIndex, x);
312
313         _prep.setObject(parameterIndex, x, targetSqlType, scale);
314     }
315
316     public void setNull(int paramIndex, int sqlType, String JavaDoc typeName) throws SQLException
317     {
318         _prep.setNull(paramIndex, sqlType, typeName);
319     }
320
321     public void setString(int parameterIndex, String JavaDoc x) throws SQLException
322     {
323         setParameter(parameterIndex, x);
324
325         _prep.setString(parameterIndex, x);
326     }
327
328     public void setBigDecimal(int parameterIndex, BigDecimal JavaDoc x) throws SQLException
329     {
330         _prep.setBigDecimal(parameterIndex, x);
331     }
332
333     public void setURL(int parameterIndex, URL JavaDoc x) throws SQLException
334     {
335         _prep.setURL(parameterIndex, x);
336     }
337
338     public void setArray(int i, Array x) throws SQLException
339     {
340         _prep.setArray(i, x);
341     }
342
343     public void setBlob(int i, Blob x) throws SQLException
344     {
345         _prep.setBlob(i, x);
346     }
347
348     public void setClob(int i, Clob x) throws SQLException
349     {
350         _prep.setClob(i, x);
351     }
352
353     public void setDate(int parameterIndex, Date JavaDoc x) throws SQLException
354     {
355         _prep.setDate(parameterIndex, x);
356     }
357
358     public ParameterMetaData getParameterMetaData() throws SQLException
359     {
360         return _prep.getParameterMetaData();
361     }
362
363     public void setRef(int i, Ref x) throws SQLException
364     {
365         _prep.setRef(i, x);
366     }
367
368     public ResultSet executeQuery() throws SQLException
369     {
370         logStatement();
371         long begin = System.currentTimeMillis ();
372
373         ResultSet res = _prep.executeQuery();
374
375         logTime (begin);
376
377         return res;
378     }
379
380     public ResultSetMetaData getMetaData() throws SQLException
381     {
382         return _prep.getMetaData();
383     }
384
385     public void setTime(int parameterIndex, Time x) throws SQLException
386     {
387         setParameter(parameterIndex, x);
388
389         _prep.setTime(parameterIndex, x);
390     }
391
392     public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
393     {
394         setParameter(parameterIndex, x);
395
396         _prep.setTimestamp(parameterIndex, x);
397     }
398
399     public void setDate(int parameterIndex, Date JavaDoc x, Calendar cal) throws SQLException
400     {
401         setParameter(parameterIndex, x);
402
403         _prep.setDate(parameterIndex, x, cal);
404     }
405
406     public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException
407     {
408         setParameter(parameterIndex, x);
409
410         _prep.setTime(parameterIndex, x, cal);
411     }
412
413     public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException
414     {
415         setParameter(parameterIndex, x);
416
417         _prep.setTimestamp(parameterIndex, x, cal);
418     }
419 }
420
Popular Tags