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.JdbcStatementParamsEvent;
015: import com.versant.core.jdbc.logging.JdbcStatementEvent;
016: import com.versant.core.logging.LogEventStore;
017:
018: import java.sql.*;
019: import java.math.BigDecimal;
020: import java.util.ArrayList;
021:
022: /**
023: * This is a JDBC PreparedStatement that logs the values of all parameters
024: * set.
025: */
026: public final class PooledPSWithParamLogging extends
027: PooledPreparedStatement {
028:
029: protected JdbcStatementParamsEvent.Row row;
030: protected ArrayList rows;
031: protected int nextRowSize = 8;
032:
033: private JdbcStatementParamsEvent.Row lastRow;
034: private ArrayList lastRows;
035:
036: public PooledPSWithParamLogging(LoggingConnection con,
037: PreparedStatement statement, LogEventStore pes, String sql,
038: PreparedStatementPool.Key key) {
039: super (con, statement, pes, sql, key);
040: }
041:
042: /**
043: * Get the number of batches added for the last execute.
044: */
045: public int getLastBatchCount() {
046: return lastRows == null ? 0 : lastRows.size();
047: }
048:
049: /**
050: * Get the parameter data for the last execute in a String.
051: */
052: public String getLastExecParamsString() {
053: if (lastRow == null)
054: return "(no parameters set)";
055: return lastRow.toString();
056: }
057:
058: /**
059: * Get parameter data for the last execute batchEntry in a String.
060: */
061: public String getLastExecParamsString(int batchEntry) {
062: int n = lastRows == null ? 0 : lastRows.size();
063: return "<batch "
064: + batchEntry
065: + "> "
066: + (batchEntry < n ? lastRows.get(batchEntry).toString()
067: : getLastExecParamsString());
068: }
069:
070: /**
071: * Cleanup our stores
072: */
073: public void close() throws SQLException {
074: row = lastRow = null;
075: rows = lastRows = null;
076: super .close();
077: }
078:
079: private void setParam(int index, Object value, int sqlType) {
080: if (row == null) {
081: row = new JdbcStatementParamsEvent.Row(nextRowSize);
082: }
083: row.set(index, value, sqlType);
084: }
085:
086: private void setParam(int index, Object value) {
087: setParam(index, value, 0);
088: }
089:
090: public void setNull(int parameterIndex, int sqlType)
091: throws SQLException {
092: setParam(parameterIndex, null, sqlType);
093: statement.setNull(parameterIndex, sqlType);
094: }
095:
096: public void setBoolean(int parameterIndex, boolean x)
097: throws SQLException {
098: setParam(parameterIndex, x ? Boolean.TRUE : Boolean.FALSE);
099: statement.setBoolean(parameterIndex, x);
100: }
101:
102: public void setByte(int parameterIndex, byte x) throws SQLException {
103: setParam(parameterIndex, new Byte(x));
104: statement.setByte(parameterIndex, x);
105: }
106:
107: public void setShort(int parameterIndex, short x)
108: throws SQLException {
109: setParam(parameterIndex, new Short(x));
110: statement.setShort(parameterIndex, x);
111: }
112:
113: public void setInt(int parameterIndex, int x) throws SQLException {
114: setParam(parameterIndex, new Integer(x));
115: statement.setInt(parameterIndex, x);
116: }
117:
118: public void setLong(int parameterIndex, long x) throws SQLException {
119: setParam(parameterIndex, new Long(x));
120: statement.setLong(parameterIndex, x);
121: }
122:
123: public void setFloat(int parameterIndex, float x)
124: throws SQLException {
125: setParam(parameterIndex, new Float(x));
126: statement.setFloat(parameterIndex, x);
127: }
128:
129: public void setDouble(int parameterIndex, double x)
130: throws SQLException {
131: setParam(parameterIndex, new Double(x));
132: statement.setDouble(parameterIndex, x);
133: }
134:
135: public void setBigDecimal(int parameterIndex, BigDecimal x)
136: throws SQLException {
137: setParam(parameterIndex, x);
138: statement.setBigDecimal(parameterIndex, x);
139: }
140:
141: public void setString(int parameterIndex, String x)
142: throws SQLException {
143: setParam(parameterIndex, x);
144: statement.setString(parameterIndex, x);
145: }
146:
147: public void setBytes(int parameterIndex, byte x[])
148: throws SQLException {
149: setParam(parameterIndex, x);
150: statement.setBytes(parameterIndex, x);
151: }
152:
153: public void setDate(int parameterIndex, Date x) throws SQLException {
154: setParam(parameterIndex, x);
155: statement.setDate(parameterIndex, x);
156: }
157:
158: public void setTime(int parameterIndex, Time x) throws SQLException {
159: setParam(parameterIndex, x);
160: statement.setTime(parameterIndex, x);
161: }
162:
163: public void setTimestamp(int parameterIndex, Timestamp x)
164: throws SQLException {
165: setParam(parameterIndex, x);
166: statement.setTimestamp(parameterIndex, x);
167: }
168:
169: public void clearParameters() throws SQLException {
170: row = null;
171: rows = null;
172: statement.clearParameters();
173: }
174:
175: public void setObject(int parameterIndex, Object x)
176: throws SQLException {
177: setParam(parameterIndex, x);
178: statement.setObject(parameterIndex, x);
179: }
180:
181: protected JdbcStatementEvent createAndLogEventForExec(int type,
182: boolean logSql) {
183: if (rows == null && row == null) {
184: JdbcStatementEvent ev = new JdbcStatementEvent(0, this ,
185: logSql ? sql : null, type);
186: pes.log(ev);
187: lastRow = null;
188: lastRows = null;
189: return ev;
190: } else {
191: JdbcStatementParamsEvent ev = new JdbcStatementParamsEvent(
192: 0, this , logSql ? sql : null, type);
193: if (rows == null) {
194: ev
195: .setParams(new JdbcStatementParamsEvent.Row[] { row });
196: } else {
197: if (row != null)
198: rows.add(row);
199: JdbcStatementParamsEvent.Row[] a = new JdbcStatementParamsEvent.Row[rows
200: .size()];
201: rows.toArray(a);
202: ev.setParams(a);
203: }
204: pes.log(ev);
205: lastRows = rows;
206: lastRow = row;
207: rows = null;
208: row = null;
209: return ev;
210: }
211: }
212:
213: public ResultSet executeQuery() throws SQLException {
214: JdbcStatementEvent ev = createAndLogEventForExec(
215: JdbcStatementEvent.STAT_EXEC_QUERY, true);
216: try {
217: ResultSet rs = statement.executeQuery();
218: ev.updateResultSetID(rs);
219: if (pes.isFiner())
220: rs = new LoggingResultSet(this , sql, rs, pes);
221: return rs;
222: } catch (SQLException e) {
223: con.setNeedsValidation(true);
224: ev.setErrorMsg(e);
225: throw e;
226: } catch (RuntimeException e) {
227: con.setNeedsValidation(true);
228: ev.setErrorMsg(e);
229: throw e;
230: } finally {
231: ev.updateTotalMs();
232: }
233: }
234:
235: public int executeUpdate() throws SQLException {
236: JdbcStatementEvent ev = createAndLogEventForExec(
237: JdbcStatementEvent.STAT_EXEC_UPDATE, true);
238: try {
239: int c = statement.executeUpdate();
240: ev.setUpdateCount(c);
241: return c;
242: } catch (SQLException e) {
243: con.setNeedsValidation(true);
244: ev.setErrorMsg(e);
245: throw e;
246: } catch (RuntimeException e) {
247: con.setNeedsValidation(true);
248: ev.setErrorMsg(e);
249: throw e;
250: } finally {
251: ev.updateTotalMs();
252: }
253: }
254:
255: public boolean execute() throws SQLException {
256: JdbcStatementEvent ev = createAndLogEventForExec(
257: JdbcStatementEvent.STAT_EXEC, true);
258: try {
259: boolean ans = statement.execute();
260: ev.setHasResultSet(ans);
261: return ans;
262: } catch (SQLException e) {
263: con.setNeedsValidation(true);
264: ev.setErrorMsg(e);
265: throw e;
266: } catch (RuntimeException e) {
267: con.setNeedsValidation(true);
268: ev.setErrorMsg(e);
269: throw e;
270: } finally {
271: ev.updateTotalMs();
272: }
273: }
274:
275: public void addBatch() throws SQLException {
276: JdbcStatementParamsEvent ev = new JdbcStatementParamsEvent(0,
277: this , null, JdbcStatementEvent.STAT_ADD_BATCH);
278: if (row != null) {
279: ev.setParams(new JdbcStatementParamsEvent.Row[] { row });
280: if (rows == null)
281: rows = new ArrayList();
282: row.trim();
283: rows.add(row);
284: nextRowSize = row.size;
285: row = null;
286: }
287: pes.log(ev);
288: try {
289: statement.addBatch();
290: } catch (SQLException e) {
291: con.setNeedsValidation(true);
292: ev.setErrorMsg(e);
293: throw e;
294: } catch (RuntimeException e) {
295: con.setNeedsValidation(true);
296: ev.setErrorMsg(e);
297: throw e;
298: } finally {
299: ev.updateTotalMs();
300: }
301: }
302:
303: public int[] executeBatch() throws SQLException {
304: JdbcStatementEvent ev = createAndLogEventForExec(
305: JdbcStatementEvent.STAT_EXEC_BATCH, true);
306: try {
307: int[] a = statement.executeBatch();
308: ev.setUpdateCounts(a);
309: return a;
310: } catch (SQLException e) {
311: con.setNeedsValidation(true);
312: ev.setErrorMsg(e);
313: throw e;
314: } catch (RuntimeException e) {
315: con.setNeedsValidation(true);
316: ev.setErrorMsg(e);
317: throw e;
318: } finally {
319: ev.updateTotalMs();
320: }
321: }
322: }
|