Source Code Cross Referenced for PreparedStatementWrapper.java in  » Development » jdbclogger » net » sourceforge » jdbclogger » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » jdbclogger » net.sourceforge.jdbclogger.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.sourceforge.jdbclogger.core;
002:
003:        /*
004:         * Licensed to the Apache Software Foundation (ASF) under one or more
005:         * contributor license agreements.  See the NOTICE file distributed with
006:         * this work for additional information regarding copyright ownership.
007:         * The ASF licenses this file to You under the Apache License, Version 2.0
008:         * (the "License"); you may not use this file except in compliance with
009:         * the License.  You may obtain a copy of the License at
010:         *
011:         *      http://www.apache.org/licenses/LICENSE-2.0
012:         *
013:         * Unless required by applicable law or agreed to in writing, software
014:         * distributed under the License is distributed on an "AS IS" BASIS,
015:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016:         * See the License for the specific language governing permissions and
017:         * limitations under the License.
018:         */
019:
020:        import net.sourceforge.jdbclogger.core.formatters.ParameterFormatter;
021:        import net.sourceforge.jdbclogger.core.util.Tokenizer;
022:        import org.apache.commons.logging.Log;
023:        import org.apache.commons.logging.LogFactory;
024:
025:        import java.io.InputStream;
026:        import java.io.Reader;
027:        import java.math.BigDecimal;
028:        import java.net.URL;
029:        import java.sql.*;
030:        import java.sql.Date;
031:        import java.util.*;
032:
033:        /**
034:         * @author Martin Marinschek (latest modification by $Author: catalean $)
035:         * @version $Revision: 123 $ $Date: 2007-09-17 13:44:46 -0700 (Mon, 17 Sep 2007) $
036:         */
037:        public class PreparedStatementWrapper extends StatementWrapper
038:                implements  PreparedStatement {
039:            private static Log log = LogFactory
040:                    .getLog(PreparedStatementWrapper.class);
041:
042:            protected List _paramFormatterList;
043:
044:            protected PreparedStatement _prep;
045:
046:            protected List _batchList;
047:            protected int _currentIndex = 0;
048:
049:            protected Map _paramIndexToListIndex = new HashMap();
050:
051:            private List getSqlAsList() {
052:                if (_batchList == null) {
053:                    _batchList = new ArrayList();
054:                }
055:
056:                if (_batchList.size() == _currentIndex) {
057:                    if (_batchList.size() == 0) {
058:                        _batchList.add(new ArrayList());
059:                    } else {
060:                        _batchList.add(new ArrayList((List) _batchList.get(0)));
061:                    }
062:                } else if (_batchList.size() < _currentIndex) {
063:                    throw new IllegalStateException(
064:                            "batchList size less than currentIndex");
065:                }
066:
067:                return (List) _batchList.get(_currentIndex);
068:            }
069:
070:            public PreparedStatementWrapper(PreparedStatement statement,
071:                    String sql, List formatters) {
072:                super (statement);
073:
074:                Tokenizer tokenizer = new Tokenizer(sql, new String[] { "?" },
075:                        true, true, '\"', '\"');
076:
077:                int delimiterCount = 1;
078:
079:                while (tokenizer.hasMoreTokens()) {
080:                    String token = tokenizer.nextToken();
081:
082:                    if (token.equals("?")) {
083:                        _paramIndexToListIndex.put(new Integer(delimiterCount),
084:                                new Integer(getSqlAsList().size()));
085:                        delimiterCount++;
086:                    }
087:
088:                    getSqlAsList().add(token);
089:                }
090:
091:                _paramFormatterList = formatters;
092:
093:                _prep = statement;
094:            }
095:
096:            protected void setParameter(int parameterIndex, Object parameter) {
097:                Integer listIndex = (Integer) _paramIndexToListIndex
098:                        .get(new Integer(parameterIndex));
099:
100:                if (listIndex != null) {
101:                    String sql;
102:                    ParameterFormatter pf;
103:
104:                    if (parameter == null)
105:                        sql = "null";
106:                    else if ((pf = getParamFormatterFor(parameter.getClass())) != null)
107:                        sql = pf.format(parameter);
108:                    else
109:                        sql = "'" + parameter + "'";
110:
111:                    getSqlAsList().set(listIndex.intValue(), sql);
112:                }
113:            }
114:
115:            protected ParameterFormatter getParamFormatterFor(Class clazz) {
116:                for (int i = 0; i < _paramFormatterList.size(); i++) {
117:                    ParameterFormatter pf = (ParameterFormatter) _paramFormatterList
118:                            .get(i);
119:
120:                    if (pf.getFormattingClass().isAssignableFrom(clazz))
121:                        return pf;
122:                }
123:
124:                return null;
125:            }
126:
127:            protected void logStatement() {
128:                if (_batchList != null) {
129:                    for (int i = 0; i < _batchList.size(); i++) {
130:                        List sqlAsList = (List) _batchList.get(i);
131:
132:                        if (log.isDebugEnabled()) {
133:                            StringBuffer buf = new StringBuffer();
134:
135:                            for (int j = 0; j < sqlAsList.size(); j++) {
136:                                String token = (String) sqlAsList.get(j);
137:                                buf.append(token);
138:                            }
139:
140:                            log.debug("Prepared Statement : " + buf.toString());
141:                        }
142:                    }
143:                }
144:            }
145:
146:            public int executeUpdate() throws SQLException {
147:                logStatement();
148:                long begin = System.currentTimeMillis();
149:
150:                int res = _prep.executeUpdate();
151:
152:                logTime(begin);
153:
154:                return res;
155:            }
156:
157:            public void addBatch() throws SQLException {
158:                _currentIndex++;
159:
160:                _prep.addBatch();
161:            }
162:
163:            public int[] executeBatch() throws SQLException {
164:                logStatement();
165:
166:                _currentIndex = 0;
167:
168:                return super .executeBatch();
169:            }
170:
171:            public void clearBatch() throws SQLException {
172:                _batchList = null;
173:                _currentIndex = 0;
174:
175:                super .clearBatch();
176:            }
177:
178:            public void clearParameters() throws SQLException {
179:                _prep.clearParameters();
180:            }
181:
182:            public boolean execute() throws SQLException {
183:                logStatement();
184:                long begin = System.currentTimeMillis();
185:
186:                boolean res = _prep.execute();
187:
188:                logTime(begin);
189:
190:                return res;
191:            }
192:
193:            public void setByte(int parameterIndex, byte x) throws SQLException {
194:                setParameter(parameterIndex, new Byte(x));
195:
196:                _prep.setByte(parameterIndex, x);
197:            }
198:
199:            public void setDouble(int parameterIndex, double x)
200:                    throws SQLException {
201:                setParameter(parameterIndex, new Double(x));
202:
203:                _prep.setDouble(parameterIndex, x);
204:            }
205:
206:            public void setFloat(int parameterIndex, float x)
207:                    throws SQLException {
208:                setParameter(parameterIndex, new Float(x));
209:
210:                _prep.setFloat(parameterIndex, x);
211:            }
212:
213:            public void setInt(int parameterIndex, int x) throws SQLException {
214:                setParameter(parameterIndex, new Integer(x));
215:
216:                _prep.setInt(parameterIndex, x);
217:            }
218:
219:            public void setNull(int parameterIndex, int sqlType)
220:                    throws SQLException {
221:                setParameter(parameterIndex, null);
222:
223:                _prep.setNull(parameterIndex, sqlType);
224:            }
225:
226:            public void setLong(int parameterIndex, long x) throws SQLException {
227:                setParameter(parameterIndex, new Long(x));
228:
229:                _prep.setLong(parameterIndex, x);
230:            }
231:
232:            public void setShort(int parameterIndex, short x)
233:                    throws SQLException {
234:                setParameter(parameterIndex, new Short(x));
235:
236:                _prep.setShort(parameterIndex, x);
237:            }
238:
239:            public void setBoolean(int parameterIndex, boolean x)
240:                    throws SQLException {
241:                setParameter(parameterIndex, Boolean.valueOf(x));
242:
243:                _prep.setBoolean(parameterIndex, x);
244:            }
245:
246:            public void setBytes(int parameterIndex, byte x[])
247:                    throws SQLException {
248:                //how could this be done ?
249:                setParameter(parameterIndex, x);
250:
251:                _prep.setBytes(parameterIndex, x);
252:            }
253:
254:            public void setAsciiStream(int parameterIndex, InputStream x,
255:                    int length) throws SQLException {
256:                setParameter(parameterIndex, x);
257:
258:                _prep.setAsciiStream(parameterIndex, x, length);
259:            }
260:
261:            public void setBinaryStream(int parameterIndex, InputStream x,
262:                    int length) throws SQLException {
263:                setParameter(parameterIndex, x);
264:
265:                _prep.setBinaryStream(parameterIndex, x, length);
266:            }
267:
268:            /**
269:             * @deprecated
270:             */
271:            public void setUnicodeStream(int parameterIndex, InputStream x,
272:                    int length) throws SQLException {
273:                setParameter(parameterIndex, x);
274:
275:                _prep.setUnicodeStream(parameterIndex, x, length);
276:            }
277:
278:            public void setCharacterStream(int parameterIndex, Reader reader,
279:                    int length) throws SQLException {
280:                setParameter(parameterIndex, reader);
281:
282:                _prep.setCharacterStream(parameterIndex, reader, length);
283:            }
284:
285:            public void setObject(int parameterIndex, Object x)
286:                    throws SQLException {
287:                setParameter(parameterIndex, x);
288:
289:                _prep.setObject(parameterIndex, x);
290:            }
291:
292:            public void setObject(int parameterIndex, Object x,
293:                    int targetSqlType) throws SQLException {
294:                setParameter(parameterIndex, x);
295:
296:                _prep.setObject(parameterIndex, x, targetSqlType);
297:            }
298:
299:            public void setObject(int parameterIndex, Object x,
300:                    int targetSqlType, int scale) throws SQLException {
301:                setParameter(parameterIndex, x);
302:
303:                _prep.setObject(parameterIndex, x, targetSqlType, scale);
304:            }
305:
306:            public void setNull(int paramIndex, int sqlType, String typeName)
307:                    throws SQLException {
308:                _prep.setNull(paramIndex, sqlType, typeName);
309:            }
310:
311:            public void setString(int parameterIndex, String x)
312:                    throws SQLException {
313:                setParameter(parameterIndex, x);
314:
315:                _prep.setString(parameterIndex, x);
316:            }
317:
318:            public void setBigDecimal(int parameterIndex, BigDecimal x)
319:                    throws SQLException {
320:                setParameter(parameterIndex, x);
321:
322:                _prep.setBigDecimal(parameterIndex, x);
323:            }
324:
325:            public void setURL(int parameterIndex, URL x) throws SQLException {
326:                setParameter(parameterIndex, x);
327:
328:                _prep.setURL(parameterIndex, x);
329:            }
330:
331:            public void setArray(int parameterIndex, Array x)
332:                    throws SQLException {
333:                setParameter(parameterIndex, x);
334:
335:                _prep.setArray(parameterIndex, x);
336:            }
337:
338:            public void setBlob(int parameterIndex, Blob x) throws SQLException {
339:                setParameter(parameterIndex, x);
340:
341:                _prep.setBlob(parameterIndex, x);
342:            }
343:
344:            public void setClob(int parameterIndex, Clob x) throws SQLException {
345:                setParameter(parameterIndex, x);
346:
347:                _prep.setClob(parameterIndex, x);
348:            }
349:
350:            public void setDate(int parameterIndex, Date x) throws SQLException {
351:                setParameter(parameterIndex, x);
352:
353:                _prep.setDate(parameterIndex, x);
354:            }
355:
356:            public ParameterMetaData getParameterMetaData() throws SQLException {
357:                return _prep.getParameterMetaData();
358:            }
359:
360:            public void setRef(int parameterIndex, Ref x) throws SQLException {
361:                setParameter(parameterIndex, x);
362:
363:                _prep.setRef(parameterIndex, x);
364:            }
365:
366:            public ResultSet executeQuery() throws SQLException {
367:                logStatement();
368:                long begin = System.currentTimeMillis();
369:
370:                ResultSet res = _prep.executeQuery();
371:
372:                logTime(begin);
373:
374:                return res;
375:            }
376:
377:            public ResultSetMetaData getMetaData() throws SQLException {
378:                return _prep.getMetaData();
379:            }
380:
381:            public void setTime(int parameterIndex, Time x) throws SQLException {
382:                setParameter(parameterIndex, x);
383:
384:                _prep.setTime(parameterIndex, x);
385:            }
386:
387:            public void setTimestamp(int parameterIndex, Timestamp x)
388:                    throws SQLException {
389:                setParameter(parameterIndex, x);
390:
391:                _prep.setTimestamp(parameterIndex, x);
392:            }
393:
394:            public void setDate(int parameterIndex, Date x, Calendar cal)
395:                    throws SQLException {
396:                setParameter(parameterIndex, x);
397:
398:                _prep.setDate(parameterIndex, x, cal);
399:            }
400:
401:            public void setTime(int parameterIndex, Time x, Calendar cal)
402:                    throws SQLException {
403:                setParameter(parameterIndex, x);
404:
405:                _prep.setTime(parameterIndex, x, cal);
406:            }
407:
408:            public void setTimestamp(int parameterIndex, Timestamp x,
409:                    Calendar cal) throws SQLException {
410:                setParameter(parameterIndex, x);
411:
412:                _prep.setTimestamp(parameterIndex, x, cal);
413:            }
414:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.