1 6 7 package org.jfox.jdbc; 8 9 import java.net.URL ; 10 import java.sql.ParameterMetaData ; 11 import java.sql.PreparedStatement ; 12 import java.sql.ResultSet ; 13 import java.sql.SQLException ; 14 import java.util.ArrayList ; 15 import java.util.Date ; 16 import java.util.StringTokenizer ; 17 18 26 27 public class LoggablePreparedStatement implements PreparedStatement { 28 29 32 private ArrayList parameterValues; 33 34 37 private String sqlTemplate; 38 39 42 private PreparedStatement pstm; 43 44 57 58 public LoggablePreparedStatement(PreparedStatement pst, String sql) { 59 pstm = pst; 60 sqlTemplate = sql; 61 parameterValues = new ArrayList (); 62 } 63 64 72 public void addBatch() throws java.sql.SQLException { 73 pstm.addBatch(); 74 } 75 76 86 public void addBatch(String sql) throws java.sql.SQLException { 87 pstm.addBatch(sql); 88 } 89 90 98 public void cancel() throws SQLException { 99 pstm.cancel(); 100 } 101 102 111 public void clearBatch() throws java.sql.SQLException { 112 pstm.clearBatch(); 113 } 114 115 125 public void clearParameters() throws java.sql.SQLException { 126 pstm.clearParameters(); 127 } 128 129 137 public void clearWarnings() throws java.sql.SQLException { 138 pstm.clearWarnings(); 139 } 140 141 154 public void close() throws java.sql.SQLException { 155 pstm.close(); 156 } 157 158 167 public boolean execute() throws java.sql.SQLException { 168 return pstm.execute(); 169 } 170 171 194 public boolean execute(String sql) throws java.sql.SQLException { 195 return pstm.execute(sql); 196 } 197 198 210 public int[] executeBatch() throws java.sql.SQLException { 211 return pstm.executeBatch(); 212 } 213 214 222 public java.sql.ResultSet executeQuery() throws java.sql.SQLException { 223 return pstm.executeQuery(); 224 } 225 226 234 public java.sql.ResultSet executeQuery(String sql) 235 throws java.sql.SQLException { 236 return pstm.executeQuery(sql); 237 } 238 239 250 public int executeUpdate() throws java.sql.SQLException { 251 return pstm.executeUpdate(); 252 } 253 254 265 public int executeUpdate(String sql) throws java.sql.SQLException { 266 return pstm.executeUpdate(sql); 267 } 268 269 278 public java.sql.Connection getConnection() throws java.sql.SQLException { 279 return pstm.getConnection(); 280 } 281 282 296 public int getFetchDirection() throws java.sql.SQLException { 297 return pstm.getFetchDirection(); 298 } 299 300 314 public int getFetchSize() throws java.sql.SQLException { 315 return pstm.getFetchSize(); 316 } 317 318 331 public int getMaxFieldSize() throws java.sql.SQLException { 332 return pstm.getMaxFieldSize(); 333 } 334 335 343 public int getMaxRows() throws java.sql.SQLException { 344 return pstm.getMaxRows(); 345 } 346 347 355 public java.sql.ResultSetMetaData getMetaData() 356 throws java.sql.SQLException { 357 return pstm.getMetaData(); 358 } 359 360 373 public boolean getMoreResults() throws java.sql.SQLException { 374 return pstm.getMoreResults(); 375 } 376 377 385 public int getQueryTimeout() throws java.sql.SQLException { 386 return pstm.getQueryTimeout(); 387 } 388 389 398 public java.sql.ResultSet getResultSet() throws java.sql.SQLException { 399 return pstm.getResultSet(); 400 } 401 402 407 public int getResultSetConcurrency() throws java.sql.SQLException { 408 return pstm.getResultSetConcurrency(); 409 } 410 411 416 public int getResultSetType() throws java.sql.SQLException { 417 return pstm.getResultSetType(); 418 } 419 420 431 public int getUpdateCount() throws java.sql.SQLException { 432 return pstm.getUpdateCount(); 433 } 434 435 450 public java.sql.SQLWarning getWarnings() throws java.sql.SQLException { 451 return pstm.getWarnings(); 452 } 453 454 463 public void setArray(int i, java.sql.Array x) 464 throws java.sql.SQLException { 465 466 pstm.setArray(i, x); 467 saveQueryParamValue(i, x); 468 469 } 470 471 489 public void setAsciiStream(int parameterIndex, 490 java.io.InputStream x, 491 int length) 492 throws java.sql.SQLException { 493 494 pstm.setAsciiStream(parameterIndex, x, length); 495 saveQueryParamValue(parameterIndex, x); 496 } 497 498 507 public void setBigDecimal(int parameterIndex, java.math.BigDecimal x) 508 throws java.sql.SQLException { 509 pstm.setBigDecimal(parameterIndex, x); 510 saveQueryParamValue(parameterIndex, x); 511 512 } 513 514 531 public void setBinaryStream(int parameterIndex, 532 java.io.InputStream x, 533 int length) 534 throws java.sql.SQLException { 535 pstm.setBinaryStream(parameterIndex, x, length); 536 saveQueryParamValue(parameterIndex, x); 537 538 } 539 540 549 public void setBlob(int i, java.sql.Blob x) throws java.sql.SQLException { 550 pstm.setBlob(i, x); 551 saveQueryParamValue(i, x); 552 } 553 554 562 public void setBoolean(int parameterIndex, boolean x) 563 throws java.sql.SQLException { 564 pstm.setBoolean(parameterIndex, x); 565 saveQueryParamValue(parameterIndex, new Boolean (x)); 566 567 } 568 569 577 public void setByte(int parameterIndex, byte x) 578 throws java.sql.SQLException { 579 pstm.setByte(parameterIndex, x); 580 saveQueryParamValue(parameterIndex, new Integer (x)); 581 } 582 583 593 public void setBytes(int parameterIndex, byte[] x) 594 throws java.sql.SQLException { 595 pstm.setBytes(parameterIndex, x); 596 saveQueryParamValue(parameterIndex, x); 597 } 598 599 619 public void setCharacterStream(int parameterIndex, 620 java.io.Reader reader, 621 int length) 622 throws java.sql.SQLException { 623 pstm.setCharacterStream(parameterIndex, reader, length); 624 saveQueryParamValue(parameterIndex, reader); 625 626 } 627 628 637 public void setClob(int i, java.sql.Clob x) throws java.sql.SQLException { 638 pstm.setClob(i, x); 639 saveQueryParamValue(i, x); 640 641 } 642 643 663 public void setCursorName(String name) throws java.sql.SQLException { 664 pstm.setCursorName(name); 665 666 } 667 668 676 public void setDate(int parameterIndex, java.sql.Date x) 677 throws java.sql.SQLException { 678 679 pstm.setDate(parameterIndex, x); 680 saveQueryParamValue(parameterIndex, x); 681 } 682 683 701 public void setDate(int parameterIndex, 702 java.sql.Date x, 703 java.util.Calendar cal) 704 throws java.sql.SQLException { 705 pstm.setDate(parameterIndex, x, cal); 706 saveQueryParamValue(parameterIndex, x); 707 } 708 709 717 public void setDouble(int parameterIndex, double x) 718 throws java.sql.SQLException { 719 pstm.setDouble(parameterIndex, x); 720 saveQueryParamValue(parameterIndex, new Double (x)); 721 } 722 723 735 public void setEscapeProcessing(boolean enable) 736 throws java.sql.SQLException { 737 pstm.setEscapeProcessing(enable); 738 739 } 740 741 760 public void setFetchDirection(int direction) throws java.sql.SQLException { 761 pstm.setFetchDirection(direction); 762 } 763 764 777 public void setFetchSize(int rows) throws java.sql.SQLException { 778 pstm.setFetchSize(rows); 779 } 780 781 789 public void setFloat(int parameterIndex, float x) 790 throws java.sql.SQLException { 791 pstm.setFloat(parameterIndex, x); 792 saveQueryParamValue(parameterIndex, new Float (x)); 793 794 } 795 796 804 public void setInt(int parameterIndex, int x) 805 throws java.sql.SQLException { 806 pstm.setInt(parameterIndex, x); 807 saveQueryParamValue(parameterIndex, new Integer (x)); 808 } 809 810 818 public void setLong(int parameterIndex, long x) 819 throws java.sql.SQLException { 820 pstm.setLong(parameterIndex, x); 821 saveQueryParamValue(parameterIndex, new Long (x)); 822 823 } 824 825 837 public void setMaxFieldSize(int max) throws java.sql.SQLException { 838 pstm.setMaxFieldSize(max); 839 840 } 841 842 851 public void setMaxRows(int max) throws java.sql.SQLException { 852 pstm.setMaxRows(max); 853 } 854 855 864 public void setNull(int parameterIndex, int sqlType) 865 throws java.sql.SQLException { 866 pstm.setNull(parameterIndex, sqlType); 867 saveQueryParamValue(parameterIndex, null); 868 } 869 870 897 public void setNull(int paramIndex, int sqlType, String typeName) 898 throws java.sql.SQLException { 899 pstm.setNull(paramIndex, sqlType, typeName); 900 saveQueryParamValue(paramIndex, null); 901 902 } 903 904 932 public void setObject(int parameterIndex, Object x) 933 throws java.sql.SQLException { 934 pstm.setObject(parameterIndex, x); 935 saveQueryParamValue(parameterIndex, x); 936 } 937 938 948 public void setObject(int parameterIndex, Object x, int targetSqlType) 949 throws java.sql.SQLException { 950 pstm.setObject(parameterIndex, x, targetSqlType); 951 saveQueryParamValue(parameterIndex, x); 952 } 953 954 983 public void setObject(int parameterIndex, 984 Object x, 985 int targetSqlType, 986 int scale) 987 throws java.sql.SQLException { 988 989 pstm.setObject(parameterIndex, x, targetSqlType, scale); 990 saveQueryParamValue(parameterIndex, x); 991 } 992 993 1002 public void setQueryTimeout(int seconds) throws java.sql.SQLException { 1003 pstm.setQueryTimeout(seconds); 1004 } 1005 1006 1015 public void setRef(int i, java.sql.Ref x) throws java.sql.SQLException { 1016 pstm.setRef(i, x); 1017 saveQueryParamValue(i, x); 1018 1019 } 1020 1021 1029 public void setShort(int parameterIndex, short x) 1030 throws java.sql.SQLException { 1031 pstm.setShort(parameterIndex, x); 1032 saveQueryParamValue(parameterIndex, new Integer (x)); 1033 } 1034 1035 1045 public void setString(int parameterIndex, String x) 1046 throws java.sql.SQLException { 1047 1048 pstm.setString(parameterIndex, x); 1049 saveQueryParamValue(parameterIndex, x); 1050 } 1051 1052 1060 public void setTime(int parameterIndex, java.sql.Time x) 1061 throws java.sql.SQLException { 1062 pstm.setTime(parameterIndex, x); 1063 saveQueryParamValue(parameterIndex, x); 1064 } 1065 1066 1084 public void setTime(int parameterIndex, 1085 java.sql.Time x, 1086 java.util.Calendar cal) 1087 throws java.sql.SQLException { 1088 pstm.setTime(parameterIndex, x, cal); 1089 saveQueryParamValue(parameterIndex, x); 1090 1091 } 1092 1093 1102 public void setTimestamp(int parameterIndex, java.sql.Timestamp x) 1103 throws java.sql.SQLException { 1104 pstm.setTimestamp(parameterIndex, x); 1105 saveQueryParamValue(parameterIndex, x); 1106 } 1107 1108 1126 public void setTimestamp(int parameterIndex, 1127 java.sql.Timestamp x, 1128 java.util.Calendar cal) 1129 throws java.sql.SQLException { 1130 pstm.setTimestamp(parameterIndex, x, cal); 1131 saveQueryParamValue(parameterIndex, x); 1132 } 1133 1134 1156 public void setUnicodeStream(int parameterIndex, 1157 java.io.InputStream x, 1158 int length) 1159 throws java.sql.SQLException { 1160 pstm.setUnicodeStream(parameterIndex, x, length); 1161 saveQueryParamValue(parameterIndex, x); 1162 } 1163 1164 public ResultSet getGeneratedKeys() throws SQLException { 1165 return pstm.getGeneratedKeys(); 1166 } 1167 1168 public boolean execute(String sql, int columnIndexes[]) throws SQLException { 1169 return pstm.execute(sql, columnIndexes); 1170 } 1171 1172 public boolean execute(String sql, String columnNames[]) throws SQLException { 1173 return pstm.execute(sql, columnNames); 1174 } 1175 1176 public int executeUpdate(String sql, String columnNames[]) throws SQLException { 1177 return pstm.executeUpdate(sql, columnNames); 1178 } 1179 1180 public ParameterMetaData getParameterMetaData() throws SQLException { 1181 return pstm.getParameterMetaData(); 1182 } 1183 1184 public void setURL(int parameterIndex, URL x) throws SQLException { 1185 pstm.setURL(parameterIndex, x); 1186 saveQueryParamValue(parameterIndex, x); 1187 } 1188 1189 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 1190 return pstm.executeUpdate(sql, autoGeneratedKeys); 1191 } 1192 1193 public boolean getMoreResults(int current) throws SQLException { 1194 return pstm.getMoreResults(current); 1195 } 1196 1197 public int getResultSetHoldability() throws SQLException { 1198 return pstm.getResultSetHoldability(); 1199 } 1200 1201 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 1202 return pstm.execute(sql, autoGeneratedKeys); 1203 } 1204 1205 public int executeUpdate(String sql, int columnIndexes[]) throws SQLException { 1206 return pstm.executeUpdate(sql, columnIndexes); 1207 } 1208 1209 1217 public String getSQLString() { 1218 StringBuffer buf = new StringBuffer (); 1219 int qMarkCount = 0; 1220 StringTokenizer tok = new StringTokenizer (sqlTemplate + " ", "?"); 1222 while(tok.hasMoreTokens()) { 1223 String oneChunk = tok.nextToken(); 1224 buf.append(oneChunk); 1225 1226 try { 1227 Object value; 1228 if(parameterValues.size() > 1 + qMarkCount) { 1229 value = parameterValues.get(1 + qMarkCount++); 1230 } 1231 else { 1232 if(tok.hasMoreTokens()) { 1233 value = null; 1234 } 1235 else { 1236 value = ""; 1237 } 1238 } 1239 buf.append("" + value); 1240 } 1241 catch(Throwable e) { 1242 buf.append("ERROR WHEN PRODUCING QUERY STRING FOR LOG." 1243 + e.toString()); 1244 } 1246 } 1247 return buf.toString().trim(); 1248 } 1249 1250 1256 private void saveQueryParamValue(int position, Object obj) { 1257 String strValue; 1258 if(obj instanceof String || obj instanceof Date ) { 1259 strValue = "'" + obj + "'"; 1261 } 1262 else { 1263 1264 if(obj == null) { 1265 strValue = "null"; 1267 } 1268 else { 1269 strValue = obj.toString(); 1271 } 1272 } 1273 1274 while(position >= parameterValues.size()) { 1276 parameterValues.add(null); 1277 } 1278 parameterValues.set(position, strValue); 1280 } 1281 1282} 1283 1284 | Popular Tags |