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.logging.LogEventStore;
016:
017: import java.math.BigDecimal;
018: import java.io.InputStream;
019: import java.io.Reader;
020: import java.util.Calendar;
021: import java.sql.*;
022: import java.net.URL;
023:
024: /**
025: * A JDBC PreparedStatement wrapped for event logging that can be pooled by
026: * PooledConnection.
027: */
028: public class PooledPreparedStatement extends LoggingStatement implements
029: PreparedStatement {
030:
031: protected final PreparedStatement statement;
032: protected final String sql;
033: private final PreparedStatementPool.Key key;
034:
035: public PooledPreparedStatement next; // linked list for PsPool
036:
037: public PooledPreparedStatement(LoggingConnection con,
038: PreparedStatement statement, LogEventStore pes, String sql,
039: PreparedStatementPool.Key key) {
040: super (con, statement, pes);
041: this .statement = statement;
042: this .sql = sql;
043: this .key = key;
044: }
045:
046: /**
047: * Close the real statement.
048: */
049: public void closeRealStatement() throws SQLException {
050: super .close();
051: }
052:
053: public PreparedStatementPool.Key getKey() {
054: return key;
055: }
056:
057: /**
058: * If this is a pooled ps then return it to the pool otherwise close
059: * it normally.
060: */
061: public void close() throws SQLException {
062: if (key != null)
063: con.returnPreparedStatement(this );
064: else
065: super .close();
066: }
067:
068: protected String getSql() {
069: return sql;
070: }
071:
072: public void setURL(int parameterIndex, URL x) throws SQLException {
073: statement.setURL(parameterIndex, x);
074: }
075:
076: public ParameterMetaData getParameterMetaData() throws SQLException {
077: return statement.getParameterMetaData();
078: }
079:
080: public ResultSet executeQuery() throws SQLException {
081: if (!pes.isFine())
082: return statement.executeQuery();
083: JdbcStatementEvent ev = new JdbcStatementEvent(0, this , sql,
084: JdbcStatementEvent.STAT_EXEC_QUERY);
085: pes.log(ev);
086: try {
087: ResultSet rs = statement.executeQuery();
088: ev.updateResultSetID(rs);
089: if (pes.isFiner())
090: rs = new LoggingResultSet(this , sql, rs, pes);
091: return rs;
092: } catch (SQLException e) {
093: con.setNeedsValidation(true);
094: ev.setErrorMsg(e);
095: throw e;
096: } catch (RuntimeException e) {
097: con.setNeedsValidation(true);
098: ev.setErrorMsg(e);
099: throw e;
100: } finally {
101: ev.updateTotalMs();
102: }
103: }
104:
105: public int executeUpdate() throws SQLException {
106: if (!pes.isFine())
107: return statement.executeUpdate();
108: JdbcStatementEvent ev = new JdbcStatementEvent(0, this , sql,
109: JdbcStatementEvent.STAT_EXEC_UPDATE);
110: pes.log(ev);
111: try {
112: int c = statement.executeUpdate();
113: ev.setUpdateCount(c);
114: return c;
115: } catch (SQLException e) {
116: con.setNeedsValidation(true);
117: ev.setErrorMsg(e);
118: throw e;
119: } catch (RuntimeException e) {
120: con.setNeedsValidation(true);
121: ev.setErrorMsg(e);
122: throw e;
123: } finally {
124: ev.updateTotalMs();
125: }
126: }
127:
128: public void setNull(int parameterIndex, int sqlType)
129: throws SQLException {
130: statement.setNull(parameterIndex, sqlType);
131: }
132:
133: public void setBoolean(int parameterIndex, boolean x)
134: throws SQLException {
135: statement.setBoolean(parameterIndex, x);
136: }
137:
138: public void setByte(int parameterIndex, byte x) throws SQLException {
139: statement.setByte(parameterIndex, x);
140: }
141:
142: public void setShort(int parameterIndex, short x)
143: throws SQLException {
144: statement.setShort(parameterIndex, x);
145: }
146:
147: public void setInt(int parameterIndex, int x) throws SQLException {
148: statement.setInt(parameterIndex, x);
149: }
150:
151: public void setLong(int parameterIndex, long x) throws SQLException {
152: statement.setLong(parameterIndex, x);
153: }
154:
155: public void setFloat(int parameterIndex, float x)
156: throws SQLException {
157: statement.setFloat(parameterIndex, x);
158: }
159:
160: public void setDouble(int parameterIndex, double x)
161: throws SQLException {
162: statement.setDouble(parameterIndex, x);
163: }
164:
165: public void setBigDecimal(int parameterIndex, BigDecimal x)
166: throws SQLException {
167: statement.setBigDecimal(parameterIndex, x);
168: }
169:
170: public void setString(int parameterIndex, String x)
171: throws SQLException {
172: statement.setString(parameterIndex, x);
173: }
174:
175: public void setBytes(int parameterIndex, byte x[])
176: throws SQLException {
177: statement.setBytes(parameterIndex, x);
178: }
179:
180: public void setDate(int parameterIndex, Date x) throws SQLException {
181: statement.setDate(parameterIndex, x);
182: }
183:
184: public void setTime(int parameterIndex, Time x) throws SQLException {
185: statement.setTime(parameterIndex, x);
186: }
187:
188: public void setTimestamp(int parameterIndex, Timestamp x)
189: throws SQLException {
190: statement.setTimestamp(parameterIndex, x);
191: }
192:
193: public void setAsciiStream(int parameterIndex, InputStream x,
194: int length) throws SQLException {
195: statement.setAsciiStream(parameterIndex, x, length);
196: }
197:
198: public void setUnicodeStream(int parameterIndex, InputStream x,
199: int length) throws SQLException {
200: statement.setUnicodeStream(parameterIndex, x, length);
201: }
202:
203: public void setBinaryStream(int parameterIndex, InputStream x,
204: int length) throws SQLException {
205: statement.setBinaryStream(parameterIndex, x, length);
206: }
207:
208: public void clearParameters() throws SQLException {
209: statement.clearParameters();
210: }
211:
212: public void setObject(int parameterIndex, Object x,
213: int targetSqlType, int scale) throws SQLException {
214: statement.setObject(parameterIndex, x, targetSqlType, scale);
215: }
216:
217: public void setObject(int parameterIndex, Object x,
218: int targetSqlType) throws SQLException {
219: statement.setObject(parameterIndex, x, targetSqlType);
220: }
221:
222: public void setObject(int parameterIndex, Object x)
223: throws SQLException {
224: statement.setObject(parameterIndex, x);
225: }
226:
227: public boolean execute() throws SQLException {
228: if (!pes.isFine())
229: return statement.execute();
230: JdbcStatementEvent ev = new JdbcStatementEvent(0, this , sql,
231: JdbcStatementEvent.STAT_EXEC);
232: pes.log(ev);
233: try {
234: boolean ans = statement.execute();
235: ev.setHasResultSet(ans);
236: return ans;
237: } catch (SQLException e) {
238: con.setNeedsValidation(true);
239: ev.setErrorMsg(e);
240: throw e;
241: } catch (RuntimeException e) {
242: con.setNeedsValidation(true);
243: ev.setErrorMsg(e);
244: throw e;
245: } finally {
246: ev.updateTotalMs();
247: }
248: }
249:
250: public void addBatch() throws SQLException {
251: if (!pes.isFiner()) {
252: statement.addBatch();
253: return;
254: }
255: JdbcStatementEvent ev = new JdbcStatementEvent(0, this , null,
256: JdbcStatementEvent.STAT_ADD_BATCH);
257: pes.log(ev);
258: try {
259: statement.addBatch();
260: } catch (SQLException e) {
261: con.setNeedsValidation(true);
262: ev.setErrorMsg(e);
263: throw e;
264: } catch (RuntimeException e) {
265: con.setNeedsValidation(true);
266: ev.setErrorMsg(e);
267: throw e;
268: } finally {
269: ev.updateTotalMs();
270: }
271: }
272:
273: public void setCharacterStream(int parameterIndex, Reader reader,
274: int length) throws SQLException {
275: statement.setCharacterStream(parameterIndex, reader, length);
276: }
277:
278: public void setRef(int i, Ref x) throws SQLException {
279: statement.setRef(i, x);
280: }
281:
282: public void setBlob(int i, Blob x) throws SQLException {
283: statement.setBlob(i, x);
284: }
285:
286: public void setClob(int i, Clob x) throws SQLException {
287: statement.setClob(i, x);
288: }
289:
290: public void setArray(int i, Array x) throws SQLException {
291: statement.setArray(i, x);
292: }
293:
294: public ResultSetMetaData getMetaData() throws SQLException {
295: return statement.getMetaData();
296: }
297:
298: public void setDate(int parameterIndex, Date x, Calendar cal)
299: throws SQLException {
300: statement.setDate(parameterIndex, x, cal);
301: }
302:
303: public void setTime(int parameterIndex, Time x, Calendar cal)
304: throws SQLException {
305: statement.setTime(parameterIndex, x, cal);
306: }
307:
308: public void setTimestamp(int parameterIndex, Timestamp x,
309: Calendar cal) throws SQLException {
310: statement.setTimestamp(parameterIndex, x, cal);
311: }
312:
313: public void setNull(int paramIndex, int sqlType, String typeName)
314: throws SQLException {
315: statement.setNull(paramIndex, sqlType, typeName);
316: }
317:
318: }
|