1 package jodd.db; 3 4 import java.sql.Connection ; 5 import java.sql.PreparedStatement ; 6 import java.sql.SQLException ; 7 import java.sql.ParameterMetaData ; 8 import java.sql.ResultSet ; 9 import java.util.ArrayList ; 10 import java.util.Date ; 11 import java.util.StringTokenizer ; 12 import java.net.URL ; 13 import java.io.InputStream ; 14 15 22 public class LoggablePreparedStatement implements PreparedStatement { 23 24 27 private ArrayList parameterValues; 28 29 32 private String sqlTemplate; 33 34 37 private PreparedStatement wrappedStatement; 38 39 52 public LoggablePreparedStatement(Connection connection, String sql) throws SQLException { 53 wrappedStatement = connection.prepareStatement(sql); 54 sqlTemplate = sql; 55 parameterValues = new ArrayList (); 56 } 57 58 public LoggablePreparedStatement(Connection connection, String sql, int resultType, int resultSetConcurrency) throws SQLException { 59 wrappedStatement = connection.prepareStatement(sql, resultType, resultSetConcurrency); 60 sqlTemplate = sql; 61 parameterValues = new ArrayList (); 62 } 63 64 72 public void addBatch() throws SQLException { 73 wrappedStatement.addBatch(); 74 } 75 85 public void addBatch(String sql) throws SQLException { 86 wrappedStatement.addBatch(sql); 87 } 88 96 public void cancel() throws SQLException { 97 wrappedStatement.cancel(); 98 } 99 108 public void clearBatch() throws SQLException { 109 wrappedStatement.clearBatch(); 110 } 111 121 public void clearParameters() throws SQLException { 122 wrappedStatement.clearParameters(); 123 } 124 132 public void clearWarnings() throws SQLException { 133 wrappedStatement.clearWarnings(); 134 } 135 148 public void close() throws SQLException { 149 wrappedStatement.close(); 150 } 151 160 public boolean execute() throws SQLException { 161 return wrappedStatement.execute(); 162 } 163 186 public boolean execute(String sql) throws SQLException { 187 return wrappedStatement.execute(sql); 188 } 189 201 public int[] executeBatch() throws SQLException { 202 return wrappedStatement.executeBatch(); 203 } 204 212 public ResultSet executeQuery() throws SQLException { 213 return wrappedStatement.executeQuery(); 214 } 215 223 public ResultSet executeQuery(String sql) throws SQLException { 224 return wrappedStatement.executeQuery(sql); 225 } 226 237 public int executeUpdate() throws SQLException { 238 return wrappedStatement.executeUpdate(); 239 } 240 251 public int executeUpdate(String sql) throws SQLException { 252 return wrappedStatement.executeUpdate(sql); 253 } 254 262 public java.sql.Connection getConnection() throws SQLException { 263 return wrappedStatement.getConnection(); 264 } 265 279 public int getFetchDirection() throws SQLException { 280 return wrappedStatement.getFetchDirection(); 281 } 282 295 public int getFetchSize() throws SQLException { 296 return wrappedStatement.getFetchSize(); 297 } 298 311 public int getMaxFieldSize() throws SQLException { 312 return wrappedStatement.getMaxFieldSize(); 313 } 314 322 public int getMaxRows() throws SQLException { 323 return wrappedStatement.getMaxRows(); 324 } 325 333 public java.sql.ResultSetMetaData getMetaData() throws SQLException { 334 return wrappedStatement.getMetaData(); 335 } 336 349 public boolean getMoreResults() throws SQLException { 350 return wrappedStatement.getMoreResults(); 351 } 352 360 public int getQueryTimeout() throws SQLException { 361 return wrappedStatement.getQueryTimeout(); 362 } 363 372 public ResultSet getResultSet() throws SQLException { 373 return wrappedStatement.getResultSet(); 374 } 375 380 public int getResultSetConcurrency() throws SQLException { 381 return wrappedStatement.getResultSetConcurrency(); 382 } 383 388 public int getResultSetType() throws SQLException { 389 return wrappedStatement.getResultSetType(); 390 } 391 402 public int getUpdateCount() throws SQLException { 403 return wrappedStatement.getUpdateCount(); 404 } 405 420 public java.sql.SQLWarning getWarnings() throws SQLException { 421 return wrappedStatement.getWarnings(); 422 } 423 432 public void setArray(int i, java.sql.Array x) throws SQLException { 433 wrappedStatement.setArray(i, x); 434 saveQueryParamValue(i, x); 435 436 } 437 455 public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { 456 wrappedStatement.setAsciiStream(parameterIndex, x, length); 457 saveQueryParamValue(parameterIndex, x); 458 } 459 468 public void setBigDecimal(int parameterIndex, java.math.BigDecimal x) throws SQLException { 469 wrappedStatement.setBigDecimal(parameterIndex, x); 470 saveQueryParamValue(parameterIndex, x); 471 472 } 473 474 491 public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { 492 wrappedStatement.setBinaryStream(parameterIndex, x, length); 493 saveQueryParamValue(parameterIndex, x); 494 495 } 496 505 public void setBlob(int i, java.sql.Blob x) throws SQLException { 506 wrappedStatement.setBlob(i, x); 507 saveQueryParamValue(i, x); 508 } 509 517 public void setBoolean(int parameterIndex, boolean x) throws SQLException { 518 wrappedStatement.setBoolean(parameterIndex, x); 519 saveQueryParamValue(parameterIndex, Boolean.valueOf(x)); 520 521 } 522 530 public void setByte(int parameterIndex, byte x) throws SQLException { 531 wrappedStatement.setByte(parameterIndex, x); 532 saveQueryParamValue(parameterIndex, new Integer (x)); 533 } 534 544 public void setBytes(int parameterIndex, byte[] x) throws SQLException { 545 wrappedStatement.setBytes(parameterIndex, x); 546 saveQueryParamValue(parameterIndex, x); 547 } 548 568 public void setCharacterStream(int parameterIndex, java.io.Reader reader, int length) throws SQLException { 569 wrappedStatement.setCharacterStream(parameterIndex, reader, length); 570 saveQueryParamValue(parameterIndex, reader); 571 572 } 573 582 public void setClob(int i, java.sql.Clob x) throws SQLException { 583 wrappedStatement.setClob(i, x); 584 saveQueryParamValue(i, x); 585 586 } 587 607 public void setCursorName(String name) throws SQLException { 608 wrappedStatement.setCursorName(name); 609 610 } 611 619 public void setDate(int parameterIndex, java.sql.Date x) throws SQLException { 620 wrappedStatement.setDate(parameterIndex, x); 621 saveQueryParamValue(parameterIndex, x); 622 } 623 624 642 public void setDate(int parameterIndex, java.sql.Date x, java.util.Calendar cal) throws SQLException { 643 wrappedStatement.setDate(parameterIndex, x, cal); 644 saveQueryParamValue(parameterIndex, x); 645 } 646 654 public void setDouble(int parameterIndex, double x) throws SQLException { 655 wrappedStatement.setDouble(parameterIndex, x); 656 saveQueryParamValue(parameterIndex, new Double (x)); 657 } 658 670 public void setEscapeProcessing(boolean enable) throws SQLException { 671 wrappedStatement.setEscapeProcessing(enable); 672 673 } 674 692 public void setFetchDirection(int direction) throws SQLException { 693 wrappedStatement.setFetchDirection(direction); 694 } 695 708 public void setFetchSize(int rows) throws SQLException { 709 wrappedStatement.setFetchSize(rows); 710 } 711 719 public void setFloat(int parameterIndex, float x) throws SQLException { 720 wrappedStatement.setFloat(parameterIndex, x); 721 saveQueryParamValue(parameterIndex, new Float (x)); 722 723 } 724 732 public void setInt(int parameterIndex, int x) throws SQLException { 733 wrappedStatement.setInt(parameterIndex, x); 734 saveQueryParamValue(parameterIndex, new Integer (x)); 735 } 736 744 public void setLong(int parameterIndex, long x) throws SQLException { 745 wrappedStatement.setLong(parameterIndex, x); 746 saveQueryParamValue(parameterIndex, new Long (x)); 747 748 } 749 761 public void setMaxFieldSize(int max) throws SQLException { 762 wrappedStatement.setMaxFieldSize(max); 763 764 } 765 774 public void setMaxRows(int max) throws SQLException { 775 wrappedStatement.setMaxRows(max); 776 } 777 786 public void setNull(int parameterIndex, int sqlType) throws SQLException { 787 wrappedStatement.setNull(parameterIndex, sqlType); 788 saveQueryParamValue(parameterIndex, null); 789 } 790 818 public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { 819 wrappedStatement.setNull(parameterIndex, sqlType, typeName); 820 saveQueryParamValue(parameterIndex, null); 821 822 } 823 851 public void setObject(int parameterIndex, Object x) throws SQLException { 852 wrappedStatement.setObject(parameterIndex, x); 853 saveQueryParamValue(parameterIndex, x); 854 } 855 865 public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { 866 wrappedStatement.setObject(parameterIndex, x, targetSqlType); 867 saveQueryParamValue(parameterIndex, x); 868 } 869 898 public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException { 899 wrappedStatement.setObject(parameterIndex, x, targetSqlType, scale); 900 saveQueryParamValue(parameterIndex, x); 901 } 902 911 public void setQueryTimeout(int seconds) throws SQLException { 912 wrappedStatement.setQueryTimeout(seconds); 913 } 914 923 public void setRef(int i, java.sql.Ref x) throws SQLException { 924 wrappedStatement.setRef(i, x); 925 saveQueryParamValue(i, x); 926 927 } 928 936 public void setShort(int parameterIndex, short x) throws SQLException { 937 wrappedStatement.setShort(parameterIndex, x); 938 saveQueryParamValue(parameterIndex, new Integer (x)); 939 } 940 950 public void setString(int parameterIndex, String x) throws SQLException { 951 952 wrappedStatement.setString(parameterIndex, x); 953 saveQueryParamValue(parameterIndex, x); 954 } 955 963 public void setTime(int parameterIndex, java.sql.Time x) throws SQLException { 964 wrappedStatement.setTime(parameterIndex, x); 965 saveQueryParamValue(parameterIndex, x); 966 } 967 985 public void setTime(int parameterIndex, java.sql.Time x, java.util.Calendar cal) throws SQLException { 986 wrappedStatement.setTime(parameterIndex, x, cal); 987 saveQueryParamValue(parameterIndex, x); 988 989 } 990 999 public void setTimestamp(int parameterIndex, java.sql.Timestamp x) throws SQLException { 1000 wrappedStatement.setTimestamp(parameterIndex, x); 1001 saveQueryParamValue(parameterIndex, x); 1002 } 1003 1021 public void setTimestamp(int parameterIndex, java.sql.Timestamp x, java.util.Calendar cal) throws SQLException { 1022 wrappedStatement.setTimestamp(parameterIndex, x, cal); 1023 saveQueryParamValue(parameterIndex, x); 1024 } 1025 1047 public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { 1048 wrappedStatement.setUnicodeStream(parameterIndex, x, length); 1050 saveQueryParamValue(parameterIndex, x); 1051 } 1052 1053 1055 1065 public void setURL(int parameterIndex, URL x) throws SQLException { 1066 wrappedStatement.setURL(parameterIndex, x); 1067 saveQueryParamValue(parameterIndex, x); 1068 } 1069 1070 1081 public ParameterMetaData getParameterMetaData() throws SQLException { 1082 return wrappedStatement.getParameterMetaData(); 1083 } 1084 1085 1094 public int getResultSetHoldability() throws SQLException { 1095 return wrappedStatement.getResultSetHoldability(); 1096 } 1097 1098 1128 public boolean getMoreResults(int current) throws SQLException { 1129 return wrappedStatement.getMoreResults(current); 1130 } 1131 1132 1143 public ResultSet getGeneratedKeys() throws SQLException { 1144 return wrappedStatement.getGeneratedKeys(); 1145 } 1146 1147 1169 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 1170 return wrappedStatement.executeUpdate(sql, autoGeneratedKeys); 1171 } 1172 1173 1211 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 1212 return wrappedStatement.execute(sql, autoGeneratedKeys); 1213 } 1214 1215 1235 public int executeUpdate(String sql, int columnIndexes[]) throws SQLException { 1236 return wrappedStatement.executeUpdate(sql, columnIndexes); 1237 } 1238 1239 1275 public boolean execute(String sql, int columnIndexes[]) throws SQLException { 1276 return wrappedStatement.execute(sql, columnIndexes); 1277 } 1278 1279 1298 public int executeUpdate(String sql, String columnNames[]) throws SQLException { 1299 return wrappedStatement.executeUpdate(sql, columnNames); 1300 } 1301 1302 1339 public boolean execute(String sql, String columnNames[]) throws SQLException { 1340 return wrappedStatement.execute(sql, columnNames); 1341 } 1342 1343 1345 1352 public String getQueryString() { 1353 StringBuffer buf = new StringBuffer (); 1354 int qMarkCount = 0; 1355 StringTokenizer tok = new StringTokenizer (sqlTemplate + ' ', "?"); 1356 while (tok.hasMoreTokens()) { 1357 String oneChunk = tok.nextToken(); 1358 buf.append(oneChunk); 1359 try { 1360 Object value; 1361 if (parameterValues.size() > 1 + qMarkCount) { 1362 value = parameterValues.get(1 + qMarkCount); 1363 qMarkCount++; 1364 } else { 1365 if (tok.hasMoreTokens()) { 1366 value = null; 1367 } else { 1368 value = ""; 1369 } 1370 } 1371 buf.append(value); 1372 } catch (Throwable th) { 1373 buf.append("--- Exception occurs while creating query string for log: ").append(th.toString()); 1374 } 1375 } 1376 return buf.toString().trim(); 1377 } 1378 1379 1385 private void saveQueryParamValue(int position, Object obj) { 1386 String strValue; 1387 if (obj instanceof String || obj instanceof Date ) { 1388 strValue = "'" + obj + '\''; } else { 1390 1391 if (obj == null) { 1392 strValue = "<null>"; } else { 1394 strValue = obj.toString(); } 1396 } 1397 1398 while (position >= parameterValues.size()) { 1400 parameterValues.add(null); 1401 } 1402 parameterValues.set(position, strValue); 1403 } 1404} 1405 | Popular Tags |