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 org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022:
023: import java.sql.*;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: /**
028: * @author Martin Marinschek (latest modification by $Author: sorin7486 $)
029: * @version $Revision: 97 $ $Date: 2007-07-12 05:47:34 -0700 (Thu, 12 Jul 2007) $
030: */
031: public class StatementWrapper implements Statement {
032: private Statement _statement;
033: private List _batchedStatements;
034:
035: private static Log log = LogFactory.getLog(StatementWrapper.class);
036:
037: public StatementWrapper(Statement statement) {
038: _statement = statement;
039: }
040:
041: protected void logStatement(String sql) {
042: if (log.isDebugEnabled())
043: log.debug("Statement : " + sql);
044: }
045:
046: protected void logTime(long begin) {
047: if (log.isDebugEnabled()) // TODO: find siutable text for logging !!
048: log.debug("last operation took: "
049: + (System.currentTimeMillis() - begin) + "ms");
050: }
051:
052: public int getFetchDirection() throws SQLException {
053: return _statement.getFetchDirection();
054: }
055:
056: public int getFetchSize() throws SQLException {
057: return _statement.getFetchSize();
058: }
059:
060: public int getMaxFieldSize() throws SQLException {
061: return _statement.getMaxFieldSize();
062: }
063:
064: public int getMaxRows() throws SQLException {
065: return _statement.getMaxRows();
066: }
067:
068: public int getQueryTimeout() throws SQLException {
069: return _statement.getQueryTimeout();
070: }
071:
072: public int getResultSetConcurrency() throws SQLException {
073: return _statement.getResultSetConcurrency();
074: }
075:
076: public int getResultSetHoldability() throws SQLException {
077: return _statement.getResultSetHoldability();
078: }
079:
080: public int getResultSetType() throws SQLException {
081: return _statement.getResultSetType();
082: }
083:
084: public int getUpdateCount() throws SQLException {
085: return _statement.getUpdateCount();
086: }
087:
088: public void cancel() throws SQLException {
089: _statement.cancel();
090: }
091:
092: public void clearBatch() throws SQLException {
093: if (_batchedStatements != null)
094: _batchedStatements = null;
095:
096: _statement.clearBatch();
097: }
098:
099: public void clearWarnings() throws SQLException {
100: _statement.clearWarnings();
101: }
102:
103: public void close() throws SQLException {
104: _statement.close();
105: }
106:
107: public boolean getMoreResults() throws SQLException {
108: return _statement.getMoreResults();
109: }
110:
111: public int[] executeBatch() throws SQLException {
112: if (_batchedStatements != null) {
113: for (int i = 0; i < _batchedStatements.size(); i++) {
114: String statement = (String) _batchedStatements.get(i);
115: logStatement(statement);
116: }
117: }
118:
119: _batchedStatements = null;
120:
121: //saving current time for logging
122: long begin = System.currentTimeMillis();
123:
124: //executing statements
125: int[] rez = _statement.executeBatch();
126:
127: //logging time
128: logTime(begin);
129:
130: return rez;
131: }
132:
133: public void setFetchDirection(int direction) throws SQLException {
134: _statement.setFetchDirection(direction);
135: }
136:
137: public void setFetchSize(int rows) throws SQLException {
138: _statement.setFetchSize(rows);
139: }
140:
141: public void setMaxFieldSize(int max) throws SQLException {
142: _statement.setMaxFieldSize(max);
143: }
144:
145: public void setMaxRows(int max) throws SQLException {
146: _statement.setMaxRows(max);
147: }
148:
149: public void setQueryTimeout(int seconds) throws SQLException {
150: _statement.setQueryTimeout(seconds);
151: }
152:
153: public boolean getMoreResults(int current) throws SQLException {
154: return _statement.getMoreResults(current);
155: }
156:
157: public void setEscapeProcessing(boolean enable) throws SQLException {
158: _statement.setEscapeProcessing(enable);
159: }
160:
161: public int executeUpdate(String sql) throws SQLException {
162: return _statement.executeUpdate(sql);
163: }
164:
165: public void addBatch(String sql) throws SQLException {
166: _statement.addBatch(sql);
167: if (_batchedStatements == null) {
168: _batchedStatements = new ArrayList();
169: }
170: _batchedStatements.add(sql);
171: }
172:
173: public void setCursorName(String name) throws SQLException {
174: _statement.setCursorName(name);
175: }
176:
177: public boolean execute(String sql) throws SQLException {
178: logStatement(sql);
179: long begin = System.currentTimeMillis();
180:
181: boolean ret = _statement.execute(sql);
182:
183: logTime(begin);
184:
185: return ret;
186: }
187:
188: public int executeUpdate(String sql, int autoGeneratedKeys)
189: throws SQLException {
190: logStatement(sql);
191: long begin = System.currentTimeMillis();
192:
193: int ret = _statement.executeUpdate(sql, autoGeneratedKeys);
194:
195: logTime(begin);
196:
197: return ret;
198: }
199:
200: public boolean execute(String sql, int autoGeneratedKeys)
201: throws SQLException {
202: logStatement(sql);
203: long begin = System.currentTimeMillis();
204:
205: boolean ret = _statement.execute(sql, autoGeneratedKeys);
206:
207: logTime(begin);
208:
209: return ret;
210: }
211:
212: public int executeUpdate(String sql, int columnIndexes[])
213: throws SQLException {
214: logStatement(sql);
215: long begin = System.currentTimeMillis();
216:
217: int ret = _statement.executeUpdate(sql, columnIndexes);
218:
219: logTime(begin);
220:
221: return ret;
222: }
223:
224: public boolean execute(String sql, int columnIndexes[])
225: throws SQLException {
226: logStatement(sql);
227: long begin = System.currentTimeMillis();
228:
229: boolean ret = _statement.execute(sql, columnIndexes);
230: logTime(begin);
231:
232: return ret;
233: }
234:
235: public Connection getConnection() throws SQLException {
236: return _statement.getConnection();
237: }
238:
239: public ResultSet getGeneratedKeys() throws SQLException {
240: return _statement.getGeneratedKeys();
241: }
242:
243: public ResultSet getResultSet() throws SQLException {
244: return _statement.getResultSet();
245: }
246:
247: public SQLWarning getWarnings() throws SQLException {
248: return _statement.getWarnings();
249: }
250:
251: public int executeUpdate(String sql, String columnNames[])
252: throws SQLException {
253: logStatement(sql);
254: long begin = System.currentTimeMillis();
255:
256: int ret = _statement.executeUpdate(sql, columnNames);
257:
258: logTime(begin);
259:
260: return ret;
261: }
262:
263: public boolean execute(String sql, String columnNames[])
264: throws SQLException {
265: logStatement(sql);
266: long begin = System.currentTimeMillis();
267:
268: boolean ret = _statement.execute(sql, columnNames);
269:
270: logTime(begin);
271:
272: return ret;
273: }
274:
275: public ResultSet executeQuery(String sql) throws SQLException {
276: logStatement(sql);
277: long begin = System.currentTimeMillis();
278:
279: ResultSet ret = _statement.executeQuery(sql);
280:
281: logTime(begin);
282:
283: return ret;
284: }
285: }
|