1 2 12 package com.versant.core.jdbc.conn; 13 14 import com.versant.core.logging.LogEventStore; 15 import com.versant.core.jdbc.logging.JdbcStatementParamsEvent; 16 import com.versant.core.jdbc.logging.JdbcStatementEvent; 17 import com.versant.core.logging.LogEventStore; 18 19 import java.sql.*; 20 import java.math.BigDecimal ; 21 import java.util.ArrayList ; 22 23 27 public final class PooledPSWithParamLogging extends PooledPreparedStatement { 28 29 protected JdbcStatementParamsEvent.Row row; 30 protected ArrayList rows; 31 protected int nextRowSize = 8; 32 33 private JdbcStatementParamsEvent.Row lastRow; 34 private ArrayList lastRows; 35 36 public PooledPSWithParamLogging(LoggingConnection con, 37 PreparedStatement statement, LogEventStore pes, String sql, 38 PreparedStatementPool.Key key) { 39 super(con, statement, pes, sql, key); 40 } 41 42 45 public int getLastBatchCount() { 46 return lastRows == null ? 0 : lastRows.size(); 47 } 48 49 52 public String getLastExecParamsString() { 53 if (lastRow == null) return "(no parameters set)"; 54 return lastRow.toString(); 55 } 56 57 60 public String getLastExecParamsString(int batchEntry) { 61 int n = lastRows == null ? 0 : lastRows.size(); 62 return "<batch " + batchEntry + "> " + 63 (batchEntry < n 64 ? lastRows.get(batchEntry).toString() 65 : getLastExecParamsString()); 66 } 67 68 71 public void close() throws SQLException { 72 row = lastRow = null; 73 rows = lastRows = null; 74 super.close(); 75 } 76 77 private void setParam(int index, Object value, int sqlType) { 78 if (row == null) { 79 row = new JdbcStatementParamsEvent.Row(nextRowSize); 80 } 81 row.set(index, value, sqlType); 82 } 83 84 private void setParam(int index, Object value) { 85 setParam(index, value, 0); 86 } 87 88 public void setNull(int parameterIndex, int sqlType) throws SQLException { 89 setParam(parameterIndex, null, sqlType); 90 statement.setNull(parameterIndex,sqlType); 91 } 92 93 public void setBoolean(int parameterIndex, boolean x) throws SQLException { 94 setParam(parameterIndex, x ? Boolean.TRUE : Boolean.FALSE); 95 statement.setBoolean(parameterIndex,x); 96 } 97 98 public void setByte(int parameterIndex, byte x) throws SQLException { 99 setParam(parameterIndex, new Byte (x)); 100 statement.setByte(parameterIndex,x); 101 } 102 103 public void setShort(int parameterIndex, short x) throws SQLException { 104 setParam(parameterIndex, new Short (x)); 105 statement.setShort(parameterIndex,x); 106 } 107 108 public void setInt(int parameterIndex, int x) throws SQLException { 109 setParam(parameterIndex, new Integer (x)); 110 statement.setInt(parameterIndex,x); 111 } 112 113 public void setLong(int parameterIndex, long x) throws SQLException { 114 setParam(parameterIndex, new Long (x)); 115 statement.setLong(parameterIndex,x); 116 } 117 118 public void setFloat(int parameterIndex, float x) throws SQLException { 119 setParam(parameterIndex, new Float (x)); 120 statement.setFloat(parameterIndex,x); 121 } 122 123 public void setDouble(int parameterIndex, double x) throws SQLException { 124 setParam(parameterIndex, new Double (x)); 125 statement.setDouble(parameterIndex,x); 126 } 127 128 public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { 129 setParam(parameterIndex, x); 130 statement.setBigDecimal(parameterIndex,x); 131 } 132 133 public void setString(int parameterIndex, String x) throws SQLException { 134 setParam(parameterIndex, x); 135 statement.setString(parameterIndex,x); 136 } 137 138 public void setBytes(int parameterIndex, byte x[]) throws SQLException { 139 setParam(parameterIndex, x); 140 statement.setBytes(parameterIndex,x); 141 } 142 143 public void setDate(int parameterIndex, Date x) 144 throws SQLException { 145 setParam(parameterIndex, x); 146 statement.setDate(parameterIndex,x); 147 } 148 149 public void setTime(int parameterIndex, Time x) 150 throws SQLException { 151 setParam(parameterIndex, x); 152 statement.setTime(parameterIndex,x); 153 } 154 155 public void setTimestamp(int parameterIndex, Timestamp x) 156 throws SQLException { 157 setParam(parameterIndex, x); 158 statement.setTimestamp(parameterIndex,x); 159 } 160 161 public void clearParameters() throws SQLException { 162 row = null; 163 rows = null; 164 statement.clearParameters(); 165 } 166 167 public void setObject(int parameterIndex, Object x) throws SQLException { 168 setParam(parameterIndex, x); 169 statement.setObject(parameterIndex,x); 170 } 171 172 protected JdbcStatementEvent createAndLogEventForExec(int type, boolean logSql) { 173 if (rows == null && row == null) { 174 JdbcStatementEvent ev = new JdbcStatementEvent(0, this, 175 logSql ? sql : null, type); 176 pes.log(ev); 177 lastRow = null; 178 lastRows = null; 179 return ev; 180 } else { 181 JdbcStatementParamsEvent ev = new JdbcStatementParamsEvent( 182 0, this, logSql ? sql : null, type); 183 if (rows == null) { 184 ev.setParams(new JdbcStatementParamsEvent.Row[]{row}); 185 } else { 186 if (row != null) rows.add(row); 187 JdbcStatementParamsEvent.Row[] a = 188 new JdbcStatementParamsEvent.Row[rows.size()]; 189 rows.toArray(a); 190 ev.setParams(a); 191 } 192 pes.log(ev); 193 lastRows = rows; 194 lastRow = row; 195 rows = null; 196 row = null; 197 return ev; 198 } 199 } 200 201 public ResultSet executeQuery() throws SQLException { 202 JdbcStatementEvent ev = createAndLogEventForExec( 203 JdbcStatementEvent.STAT_EXEC_QUERY, true); 204 try { 205 ResultSet rs = statement.executeQuery(); 206 ev.updateResultSetID(rs); 207 if (pes.isFiner()) rs = new LoggingResultSet(this, sql, rs, pes); 208 return rs; 209 } catch (SQLException e) { 210 con.setNeedsValidation(true); 211 ev.setErrorMsg(e); 212 throw e; 213 } catch (RuntimeException e) { 214 con.setNeedsValidation(true); 215 ev.setErrorMsg(e); 216 throw e; 217 } finally { 218 ev.updateTotalMs(); 219 } 220 } 221 222 public int executeUpdate() throws SQLException { 223 JdbcStatementEvent ev = createAndLogEventForExec( 224 JdbcStatementEvent.STAT_EXEC_UPDATE, true); 225 try { 226 int c = statement.executeUpdate(); 227 ev.setUpdateCount(c); 228 return c; 229 } catch (SQLException e) { 230 con.setNeedsValidation(true); 231 ev.setErrorMsg(e); 232 throw e; 233 } catch (RuntimeException e) { 234 con.setNeedsValidation(true); 235 ev.setErrorMsg(e); 236 throw e; 237 } finally { 238 ev.updateTotalMs(); 239 } 240 } 241 242 public boolean execute() throws SQLException { 243 JdbcStatementEvent ev = createAndLogEventForExec( 244 JdbcStatementEvent.STAT_EXEC, true); 245 try { 246 boolean ans = statement.execute(); 247 ev.setHasResultSet(ans); 248 return ans; 249 } catch (SQLException e) { 250 con.setNeedsValidation(true); 251 ev.setErrorMsg(e); 252 throw e; 253 } catch (RuntimeException e) { 254 con.setNeedsValidation(true); 255 ev.setErrorMsg(e); 256 throw e; 257 } finally { 258 ev.updateTotalMs(); 259 } 260 } 261 262 public void addBatch() throws SQLException { 263 JdbcStatementParamsEvent ev = new JdbcStatementParamsEvent( 264 0, this, null, JdbcStatementEvent.STAT_ADD_BATCH); 265 if (row != null) { 266 ev.setParams(new JdbcStatementParamsEvent.Row[]{row}); 267 if (rows == null) rows = new ArrayList (); 268 row.trim(); 269 rows.add(row); 270 nextRowSize = row.size; 271 row = null; 272 } 273 pes.log(ev); 274 try { 275 statement.addBatch(); 276 } catch (SQLException e) { 277 con.setNeedsValidation(true); 278 ev.setErrorMsg(e); 279 throw e; 280 } catch (RuntimeException e) { 281 con.setNeedsValidation(true); 282 ev.setErrorMsg(e); 283 throw e; 284 } finally { 285 ev.updateTotalMs(); 286 } 287 } 288 289 public int[] executeBatch() throws SQLException { 290 JdbcStatementEvent ev = createAndLogEventForExec( 291 JdbcStatementEvent.STAT_EXEC_BATCH, true); 292 try { 293 int[] a = statement.executeBatch(); 294 ev.setUpdateCounts(a); 295 return a; 296 } catch (SQLException e) { 297 con.setNeedsValidation(true); 298 ev.setErrorMsg(e); 299 throw e; 300 } catch (RuntimeException e) { 301 con.setNeedsValidation(true); 302 ev.setErrorMsg(e); 303 throw e; 304 } finally { 305 ev.updateTotalMs(); 306 } 307 } 308 } 309 310 | Popular Tags |