001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdbc.conn;
012:
013: import com.versant.core.logging.LogEventStore;
014: import com.versant.core.jdbc.logging.JdbcStatementEvent;
015: import com.versant.core.jdbc.logging.JdbcLogEvent;
016: import com.versant.core.logging.LogEventStore;
017: import com.versant.core.logging.LogEventStore;
018:
019: import java.sql.*;
020:
021: /**
022: * A JDBC Statement wrapped for event logging.
023: */
024: public class LoggingStatement implements Statement {
025:
026: protected final LoggingConnection con;
027: private final Statement statement;
028: protected final LogEventStore pes;
029:
030: public LoggingStatement(LoggingConnection con, Statement statement,
031: LogEventStore pes) {
032: this .con = con;
033: this .statement = statement;
034: this .pes = pes;
035: }
036:
037: /**
038: * Get the real statement.
039: */
040: public Statement getStatement() {
041: return statement;
042: }
043:
044: public boolean getMoreResults(int current) throws SQLException {
045: return statement.getMoreResults(current);
046: }
047:
048: public ResultSet getGeneratedKeys() throws SQLException {
049: return statement.getGeneratedKeys();
050: }
051:
052: public int executeUpdate(String sql, int autoGeneratedKeys)
053: throws SQLException {
054: return statement.executeUpdate(sql, autoGeneratedKeys);
055: }
056:
057: public int executeUpdate(String sql, int columnIndexes[])
058: throws SQLException {
059: return statement.executeUpdate(sql, columnIndexes);
060: }
061:
062: public int executeUpdate(String sql, String columnNames[])
063: throws SQLException {
064: return statement.executeUpdate(sql, columnNames);
065: }
066:
067: public boolean execute(String sql, int autoGeneratedKeys)
068: throws SQLException {
069: return statement.execute(sql, autoGeneratedKeys);
070: }
071:
072: public boolean execute(String sql, int columnIndexes[])
073: throws SQLException {
074: return statement.execute(sql, columnIndexes);
075: }
076:
077: public boolean execute(String sql, String columnNames[])
078: throws SQLException {
079: return statement.execute(sql, columnNames);
080: }
081:
082: public int getResultSetHoldability() throws SQLException {
083: return statement.getResultSetHoldability();
084: }
085:
086: public ResultSet executeQuery(String sql) throws SQLException {
087: if (!pes.isFine())
088: return statement.executeQuery(sql);
089: JdbcStatementEvent ev = new JdbcStatementEvent(0, this , sql,
090: JdbcStatementEvent.STAT_EXEC_QUERY);
091: pes.log(ev);
092: try {
093: ResultSet rs = statement.executeQuery(sql);
094: ev.updateResultSetID(rs);
095: if (pes.isFiner())
096: rs = new LoggingResultSet(this , sql, rs, pes);
097: return rs;
098: } catch (SQLException e) {
099: con.setNeedsValidation(true);
100: ev.setErrorMsg(e);
101: throw e;
102: } catch (RuntimeException e) {
103: con.setNeedsValidation(true);
104: ev.setErrorMsg(e);
105: throw e;
106: } finally {
107: ev.updateTotalMs();
108: }
109: }
110:
111: public int executeUpdate(String sql) throws SQLException {
112: if (!pes.isFine()) {
113: return statement.executeUpdate(sql);
114: }
115: JdbcStatementEvent ev = new JdbcStatementEvent(0, this , sql,
116: JdbcStatementEvent.STAT_EXEC_UPDATE);
117: pes.log(ev);
118: try {
119: int c = statement.executeUpdate(sql);
120: ev.setUpdateCount(c);
121: return c;
122: } catch (SQLException e) {
123: con.setNeedsValidation(true);
124: ev.setErrorMsg(e);
125: throw e;
126: } catch (RuntimeException e) {
127: con.setNeedsValidation(true);
128: ev.setErrorMsg(e);
129: throw e;
130: } finally {
131: ev.updateTotalMs();
132: }
133: }
134:
135: public void close() throws SQLException {
136: if (!pes.isFiner()) {
137: statement.close();
138: return;
139: }
140: JdbcStatementEvent ev = new JdbcStatementEvent(0, this , null,
141: JdbcStatementEvent.STAT_CLOSE);
142: pes.log(ev);
143: try {
144: statement.close();
145: } catch (SQLException e) {
146: con.setNeedsValidation(true);
147: ev.setErrorMsg(e);
148: throw e;
149: } catch (RuntimeException e) {
150: con.setNeedsValidation(true);
151: ev.setErrorMsg(e);
152: throw e;
153: } finally {
154: ev.updateTotalMs();
155: }
156: }
157:
158: public int getMaxFieldSize() throws SQLException {
159: return statement.getMaxFieldSize();
160: }
161:
162: public void setMaxFieldSize(int max) throws SQLException {
163: statement.setMaxFieldSize(max);
164: }
165:
166: public int getMaxRows() throws SQLException {
167: return statement.getMaxRows();
168: }
169:
170: public void setMaxRows(int max) throws SQLException {
171: JdbcLogEvent ev = null;
172: if (pes.isFiner()) {
173: ev = new JdbcLogEvent(0, JdbcLogEvent.STAT_MAX_ROWS,
174: Integer.toString(max));
175: pes.log(ev);
176: }
177: try {
178: statement.setMaxRows(max);
179: } catch (SQLException e) {
180: if (ev != null)
181: ev.setErrorMsg(e);
182: throw e;
183: } catch (RuntimeException e) {
184: if (ev != null)
185: ev.setErrorMsg(e);
186: throw e;
187: } finally {
188: if (ev != null)
189: ev.updateTotalMs();
190: }
191: }
192:
193: public void setEscapeProcessing(boolean enable) throws SQLException {
194: statement.setEscapeProcessing(enable);
195: }
196:
197: public int getQueryTimeout() throws SQLException {
198: return statement.getQueryTimeout();
199: }
200:
201: public void setQueryTimeout(int seconds) throws SQLException {
202: statement.setQueryTimeout(seconds);
203: }
204:
205: public void cancel() throws SQLException {
206: statement.cancel();
207: }
208:
209: public SQLWarning getWarnings() throws SQLException {
210: return statement.getWarnings();
211: }
212:
213: public void clearWarnings() throws SQLException {
214: statement.clearWarnings();
215: }
216:
217: public void setCursorName(String name) throws SQLException {
218: statement.setCursorName(name);
219: }
220:
221: public boolean execute(String sql) throws SQLException {
222: if (!pes.isFine())
223: return statement.execute(sql);
224: JdbcStatementEvent ev = new JdbcStatementEvent(0, this , sql,
225: JdbcStatementEvent.STAT_EXEC);
226: pes.log(ev);
227: try {
228: boolean ans = statement.execute(sql);
229: ev.setHasResultSet(ans);
230: return ans;
231: } catch (SQLException e) {
232: con.setNeedsValidation(true);
233: ev.setErrorMsg(e);
234: throw e;
235: } catch (RuntimeException e) {
236: con.setNeedsValidation(true);
237: ev.setErrorMsg(e);
238: throw e;
239: } finally {
240: ev.updateTotalMs();
241: }
242: }
243:
244: public java.sql.ResultSet getResultSet() throws SQLException {
245: return statement.getResultSet();
246: }
247:
248: public int getUpdateCount() throws SQLException {
249: return statement.getUpdateCount();
250: }
251:
252: public boolean getMoreResults() throws SQLException {
253: return statement.getMoreResults();
254: }
255:
256: public void setFetchDirection(int direction) throws SQLException {
257: statement.setFetchDirection(direction);
258: }
259:
260: public int getFetchDirection() throws SQLException {
261: return statement.getFetchDirection();
262: }
263:
264: public void setFetchSize(int rows) throws SQLException {
265: statement.setFetchSize(rows);
266: }
267:
268: public int getFetchSize() throws SQLException {
269: return statement.getFetchSize();
270: }
271:
272: public int getResultSetConcurrency() throws SQLException {
273: return statement.getResultSetConcurrency();
274: }
275:
276: public int getResultSetType() throws SQLException {
277: return statement.getResultSetType();
278: }
279:
280: public void addBatch(String sql) throws SQLException {
281: statement.addBatch(sql);
282: }
283:
284: public void clearBatch() throws SQLException {
285: statement.clearBatch();
286: }
287:
288: public int[] executeBatch() throws SQLException {
289: if (!pes.isFine())
290: return statement.executeBatch();
291: JdbcStatementEvent ev = new JdbcStatementEvent(0, this ,
292: getSql(), JdbcStatementEvent.STAT_EXEC_BATCH);
293: pes.log(ev);
294: try {
295: int[] a = statement.executeBatch();
296: ev.setUpdateCounts(a);
297: return a;
298: } catch (SQLException e) {
299: con.setNeedsValidation(true);
300: ev.setErrorMsg(e);
301: throw e;
302: } catch (RuntimeException e) {
303: con.setNeedsValidation(true);
304: ev.setErrorMsg(e);
305: throw e;
306: } finally {
307: ev.updateTotalMs();
308: }
309: }
310:
311: /**
312: * PreparedStatement subclasses override this to return the SQL for
313: * event logging.
314: */
315: protected String getSql() {
316: return null;
317: }
318:
319: public Connection getConnection() throws SQLException {
320: return con;
321: }
322:
323: }
|