1 26 27 package org.objectweb.jonas.resource; 28 29 import java.io.InputStream ; 30 import java.io.Reader ; 31 import java.math.BigDecimal ; 32 import java.net.URL ; 33 import java.sql.Array ; 34 import java.sql.Blob ; 35 import java.sql.Clob ; 36 import java.sql.Connection ; 37 import java.sql.Date ; 38 import java.sql.ParameterMetaData ; 39 import java.sql.PreparedStatement ; 40 import java.sql.Ref ; 41 import java.sql.ResultSet ; 42 import java.sql.ResultSetMetaData ; 43 import java.sql.SQLException ; 44 import java.sql.SQLWarning ; 45 import java.sql.Time ; 46 import java.sql.Timestamp ; 47 import java.util.Arrays ; 48 import java.util.Calendar ; 49 50 import org.objectweb.util.monolog.api.BasicLevel; 51 import org.objectweb.util.monolog.api.Logger; 52 53 57 public class PreparedStatementWrapper implements PreparedStatement { 58 59 62 private static final int[] PRIME_NUMBERS = new int[] {11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; 63 64 67 private PreparedStatement pstmt = null; 68 69 72 private String user = null; 73 74 77 private String sql = null; 78 79 82 private boolean invalid = false; 83 84 87 private boolean closed = false; 88 89 92 private int hashCode; 93 94 99 private int resultSetType = -1; 100 101 106 private int resultSetConcurrency = -1; 107 108 113 private int resultSetHoldability = -1; 114 115 120 private int autoGeneratedKeys = -1; 121 122 126 private int[] columnIndexes = null; 127 128 132 private String [] columnNames = null; 133 134 137 private final Logger trace; 138 139 142 private boolean setFetchDirectionCalled = false; 143 144 147 private boolean setEscapeProcessingCalled = false; 148 149 152 private boolean setMaxFieldSizeCalled = false; 153 154 157 private final boolean isDebugLogging; 158 159 187 private PreparedStatementWrapper(final String user, final String sql, final int resultSetType, 188 final int resultSetConcurrency, final int resultSetHoldability, final int autoGeneratedKeys, 189 final int[] columnIndexes, final String [] columnNames, final Logger trace, final boolean isDebugLogging) { 190 191 this.user = user; 192 this.sql = sql; 193 this.resultSetType = resultSetType; 194 this.resultSetConcurrency = resultSetConcurrency; 195 this.resultSetHoldability = resultSetHoldability; 196 this.autoGeneratedKeys = autoGeneratedKeys; 197 this.columnIndexes = columnIndexes; 198 this.columnNames = columnNames; 199 this.trace = trace; 200 201 int i = 0; 202 if (sql != null) { 204 hashCode = sql.hashCode(); 205 } else { 206 hashCode = PRIME_NUMBERS[i++]; 207 } 208 if (user != null) { 209 hashCode ^= user.hashCode(); 210 } else { 211 hashCode ^= PRIME_NUMBERS[i++]; 212 } 213 hashCode ^= (resultSetType * PRIME_NUMBERS[i++]); 214 hashCode ^= (resultSetConcurrency * PRIME_NUMBERS[i++]); 215 hashCode ^= (resultSetHoldability * PRIME_NUMBERS[i++]); 216 hashCode ^= (autoGeneratedKeys * PRIME_NUMBERS[i++]); 217 if (columnIndexes != null) { 218 hashCode ^= columnIndexes.hashCode(); 219 } else { 220 hashCode ^= PRIME_NUMBERS[i++]; 221 } 222 if (columnNames != null) { 223 hashCode ^= columnNames.hashCode(); 224 } else { 225 hashCode ^= PRIME_NUMBERS[i++]; 226 } 227 this.isDebugLogging = isDebugLogging; 228 } 229 230 231 232 252 public PreparedStatementWrapper(final String user, final String sql, final int resultSetType, 253 final int resultSetConcurrency, final Logger trace, final boolean isDebugLogging) { 254 this(user, sql, resultSetType, resultSetConcurrency, -1, -1, null, null, trace, isDebugLogging); 255 } 256 257 282 public PreparedStatementWrapper(final String user, final String sql, final int resultSetType, 283 final int resultSetConcurrency, final int resultSetHoldability, final Logger trace, final boolean isDebugLogging) { 284 this(user, sql, resultSetType, resultSetConcurrency, resultSetHoldability, -1, null, null, trace, isDebugLogging); 285 } 286 287 304 public PreparedStatementWrapper(final String user, final String sql, final int autoGeneratedKeys, final Logger trace, final boolean isDebugLogging) { 305 this(user, sql, -1, -1, -1, autoGeneratedKeys, null, null, trace, isDebugLogging); 306 } 307 308 328 public PreparedStatementWrapper(final String user, final String sql, final int[] columnIndexes, final Logger trace, final boolean isDebugLogging) { 329 this(user, sql, -1, -1, -1, -1, columnIndexes, null, trace, isDebugLogging); 330 } 331 332 351 public PreparedStatementWrapper(final String user, final String sql, final String [] columnNames, final Logger trace, final boolean isDebugLogging) { 352 this(user, sql, -1, -1, -1, -1, null, columnNames, trace, isDebugLogging); 353 } 354 355 359 public void checkIfValid() throws SQLException { 360 if (invalid) { 361 SQLException se = new SQLException ("PreparedStatement(" + this + ") is invalid."); 362 trace.log(BasicLevel.ERROR, se); 363 throw se; 364 } 365 366 } 367 368 372 public void clearPstmtValues() { 373 closed = false; 374 try { 375 pstmt.clearParameters(); 376 pstmt.clearBatch(); 377 } catch (Throwable ex) { 378 if (isDebugLogging) { 379 trace.log(BasicLevel.DEBUG, "Cannot clear parameters", ex); 380 } 381 } 382 if (setEscapeProcessingCalled) { 383 try { 384 pstmt.setEscapeProcessing(true); 385 setEscapeProcessingCalled = false; 386 } catch (Throwable ex) { 387 if (isDebugLogging) { 388 trace.log(BasicLevel.DEBUG, "Cannot reset escape processing to true", ex); 389 } 390 } 391 } 392 if (setFetchDirectionCalled) { 393 try { 394 pstmt.setFetchDirection(ResultSet.FETCH_FORWARD); 395 setFetchDirectionCalled = false; 396 } catch (Throwable ex) { 397 if (isDebugLogging) { 398 trace.log(BasicLevel.DEBUG, "Cannot set fetch direction to ResultSet.FETCH_FORWARD", ex); 399 } 400 } 401 } 402 if (setMaxFieldSizeCalled) { 403 try { 404 pstmt.setMaxFieldSize(0); 405 setMaxFieldSizeCalled=false; 406 } catch (Throwable ex) { 407 if (isDebugLogging) { 408 trace.log(BasicLevel.DEBUG, "Cannot set max field size to 0", ex); 409 } 410 } 411 } 412 try { 413 pstmt.setMaxRows(0); 414 } catch (Throwable ex) { 415 if (isDebugLogging) { 416 trace.log(BasicLevel.DEBUG, "Cannot set max rows to 0", ex); 417 } 418 } 419 try { 420 pstmt.setQueryTimeout(0); 421 } catch (Throwable ex) { 422 if (isDebugLogging) { 423 trace.log(BasicLevel.DEBUG, "Cannot set query timeout to 0", ex); 424 } 425 } 426 try { 427 pstmt.clearWarnings(); 428 } catch (Throwable ex) { 429 if (isDebugLogging) { 430 trace.log(BasicLevel.DEBUG, "Cannot clear warnings", ex); 431 } 432 } 433 } 434 435 439 public void closePstmt() throws SQLException { 440 closed = true; 441 pstmt.close(); 442 } 443 444 448 public void destroy() throws SQLException { 449 if (isDebugLogging) { 450 trace.log(BasicLevel.DEBUG, "" + this); 451 } 452 try { 453 pstmt.close(); 454 } finally { 455 invalid = true; 456 closed = true; 457 } 458 } 459 460 464 public boolean equals(Object stmt) { 465 if (stmt == null) { 466 return false; 467 } 468 if (this.hashCode != stmt.hashCode()) { 470 return false; 471 } 472 473 if (!(stmt instanceof PreparedStatementWrapper)) { 475 return false; 476 } 477 PreparedStatementWrapper psw = (PreparedStatementWrapper) stmt; 478 479 if (invalid) { 480 if (isDebugLogging) { 481 trace.log(BasicLevel.DEBUG, "Prepared Statement is invalid"); 482 } 483 return false; 484 } 485 if (sql == null && psw.sql != null) { 486 if (isDebugLogging) { 487 trace.log(BasicLevel.DEBUG, "Stmt sql: " + psw.sql + " not equal to " + sql); 488 } 489 return false; 490 } 491 if (sql != null && !sql.equals(psw.sql)) { 492 if (isDebugLogging) { 493 trace.log(BasicLevel.DEBUG, "Stmt sql: " + psw.sql + " not equal to " + sql); 494 } 495 return false; 496 } 497 if (user == null && psw.user != null) { 498 if (isDebugLogging) { 499 trace.log(BasicLevel.DEBUG, "Stmt user: " + psw.user + " not equal to " + user); 500 } 501 return false; 502 } 503 if (user != null && !user.equals(psw.user)) { 504 if (isDebugLogging) { 505 trace.log(BasicLevel.DEBUG, "Stmt user: " + psw.user + " not equal to " + user); 506 } 507 return false; 508 } 509 if (resultSetType != psw.resultSetType) { 510 if (isDebugLogging) { 511 trace.log(BasicLevel.DEBUG, "Stmt resultSetType: " + psw.resultSetType + " not equal to " 512 + resultSetType); 513 } 514 return false; 515 } 516 if (resultSetConcurrency != psw.resultSetConcurrency) { 517 if (isDebugLogging) { 518 trace.log(BasicLevel.DEBUG, "Stmt resultSetConcurrency: " + psw.resultSetConcurrency + " not equal to " 519 + resultSetConcurrency); 520 } 521 return false; 522 } 523 if (resultSetHoldability != psw.resultSetHoldability) { 524 if (isDebugLogging) { 525 trace.log(BasicLevel.DEBUG, "Stmt resultSetHoldability: " + psw.resultSetHoldability + " not equal to " 526 + resultSetHoldability); 527 } 528 return false; 529 } 530 if (autoGeneratedKeys != psw.autoGeneratedKeys) { 531 if (isDebugLogging) { 532 trace.log(BasicLevel.DEBUG, "Stmt autoGeneratedKeys: " + psw.autoGeneratedKeys + " not equal to " 533 + autoGeneratedKeys); 534 } 535 return false; 536 } 537 if (!Arrays.equals(columnIndexes, psw.columnIndexes)) { 538 if (isDebugLogging) { 539 trace.log(BasicLevel.DEBUG, "Stmt columnIndexes: " + psw.columnIndexes + " not equal to " 540 + columnIndexes); 541 } 542 return false; 543 } 544 if (!Arrays.equals(columnNames, psw.columnNames)) { 545 if (isDebugLogging) { 546 trace.log(BasicLevel.DEBUG, "Stmt columnNames: " + psw.columnNames + " not equal to " + columnNames); 547 } 548 return false; 549 } 550 if (isDebugLogging) { 551 trace.log(BasicLevel.DEBUG, "Stmt found at " + this); 552 } 553 return true; 554 } 555 556 560 public void setClosed(boolean val) { 561 closed = val; 562 } 563 564 568 public void setPreparedStatement(PreparedStatement pstmt) { 569 this.pstmt = pstmt; 570 } 571 572 575 576 583 public void addBatch() throws SQLException { 584 checkIfValid(); 585 pstmt.addBatch(); 586 } 587 588 598 public void clearParameters() throws SQLException { 599 checkIfValid(); 600 pstmt.clearParameters(); 601 } 602 603 625 public boolean execute() throws SQLException { 626 checkIfValid(); 627 return pstmt.execute(); 628 } 629 630 666 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 667 checkIfValid(); 668 return pstmt.execute(sql, autoGeneratedKeys); 669 } 670 671 707 public boolean execute(String sql, int[] columnIndexes) throws SQLException { 708 checkIfValid(); 709 return pstmt.execute(sql, columnIndexes); 710 } 711 712 747 public boolean execute(String sql, String [] columnNames) throws SQLException { 748 checkIfValid(); 749 return pstmt.execute(sql, columnNames); 750 } 751 752 760 public ResultSet executeQuery() throws SQLException { 761 checkIfValid(); 762 return pstmt.executeQuery(); 763 } 764 765 776 public int executeUpdate() throws SQLException { 777 checkIfValid(); 778 return pstmt.executeUpdate(); 779 } 780 781 801 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 802 checkIfValid(); 803 return pstmt.executeUpdate(sql, autoGeneratedKeys); 804 } 805 806 827 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 828 checkIfValid(); 829 return pstmt.executeUpdate(sql, columnIndexes); 830 } 831 832 853 public int executeUpdate(String sql, String [] columnNames) throws SQLException { 854 checkIfValid(); 855 return pstmt.executeUpdate(sql, columnNames); 856 } 857 858 870 public ResultSet getGeneratedKeys() throws SQLException { 871 checkIfValid(); 872 return pstmt.getGeneratedKeys(); 873 } 874 875 897 public ResultSetMetaData getMetaData() throws SQLException { 898 checkIfValid(); 899 return pstmt.getMetaData(); 900 } 901 902 933 public boolean getMoreResults(int current) throws SQLException { 934 checkIfValid(); 935 return pstmt.getMoreResults(current); 936 937 } 938 939 950 public ParameterMetaData getParameterMetaData() throws SQLException { 951 checkIfValid(); 952 return pstmt.getParameterMetaData(); 953 954 } 955 956 965 public int getResultSetHoldability() throws SQLException { 966 checkIfValid(); 967 return pstmt.getResultSetHoldability(); 968 } 969 970 980 public void setArray(int i, Array x) throws SQLException { 981 checkIfValid(); 982 pstmt.setArray(i, x); 983 } 984 985 1000 public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { 1001 checkIfValid(); 1002 pstmt.setAsciiStream(parameterIndex, x, length); 1003 1004 } 1005 1006 1014 public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { 1015 checkIfValid(); 1016 pstmt.setBigDecimal(parameterIndex, x); 1017 } 1018 1019 1033 public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { 1034 checkIfValid(); 1035 pstmt.setBinaryStream(parameterIndex, x, length); 1036 } 1037 1038 1048 public void setBlob(int i, Blob x) throws SQLException { 1049 checkIfValid(); 1050 pstmt.setBlob(i, x); 1051 } 1052 1053 1061 public void setBoolean(int parameterIndex, boolean x) throws SQLException { 1062 checkIfValid(); 1063 pstmt.setBoolean(parameterIndex, x); 1064 } 1065 1066 1074 public void setByte(int parameterIndex, byte x) throws SQLException { 1075 checkIfValid(); 1076 pstmt.setByte(parameterIndex, x); 1077 } 1078 1079 1089 public void setBytes(int parameterIndex, byte[] x) throws SQLException { 1090 checkIfValid(); 1091 pstmt.setBytes(parameterIndex, x); 1092 } 1093 1094 1112 public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { 1113 checkIfValid(); 1114 pstmt.setCharacterStream(parameterIndex, reader, length); 1115 } 1116 1117 1127 public void setClob(int i, Clob x) throws SQLException { 1128 checkIfValid(); 1129 pstmt.setClob(i, x); 1130 } 1131 1132 1140 public void setDate(int parameterIndex, Date x) throws SQLException { 1141 checkIfValid(); 1142 pstmt.setDate(parameterIndex, x); 1143 } 1144 1145 1161 public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { 1162 checkIfValid(); 1163 pstmt.setDate(parameterIndex, x, cal); 1164 } 1165 1166 1174 public void setDouble(int parameterIndex, double x) throws SQLException { 1175 checkIfValid(); 1176 pstmt.setDouble(parameterIndex, x); 1177 } 1178 1179 1187 public void setFloat(int parameterIndex, float x) throws SQLException { 1188 checkIfValid(); 1189 pstmt.setFloat(parameterIndex, x); 1190 } 1191 1192 1200 public void setInt(int parameterIndex, int x) throws SQLException { 1201 checkIfValid(); 1202 pstmt.setInt(parameterIndex, x); 1203 } 1204 1205 1213 public void setLong(int parameterIndex, long x) throws SQLException { 1214 checkIfValid(); 1215 pstmt.setLong(parameterIndex, x); 1216 } 1217 1218 1226 public void setNull(int parameterIndex, int sqlType) throws SQLException { 1227 checkIfValid(); 1228 pstmt.setNull(parameterIndex, sqlType); 1229 } 1230 1231 1253 public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException { 1254 checkIfValid(); 1255 pstmt.setNull(paramIndex, sqlType, typeName); 1256 } 1257 1258 1287 public void setObject(int parameterIndex, Object x) throws SQLException { 1288 checkIfValid(); 1289 pstmt.setObject(parameterIndex, x); 1290 } 1291 1292 1302 public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { 1303 checkIfValid(); 1304 pstmt.setObject(parameterIndex, x, targetSqlType); 1305 } 1306 1307 1335 public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException { 1336 checkIfValid(); 1337 pstmt.setObject(parameterIndex, x, targetSqlType, scale); 1338 } 1339 1340 1349 public void setRef(int i, Ref x) throws SQLException { 1350 checkIfValid(); 1351 pstmt.setRef(i, x); 1352 } 1353 1354 1362 public void setShort(int parameterIndex, short x) throws SQLException { 1363 checkIfValid(); 1364 pstmt.setShort(parameterIndex, x); 1365 } 1366 1367 1377 public void setString(int parameterIndex, String x) throws SQLException { 1378 checkIfValid(); 1379 pstmt.setString(parameterIndex, x); 1380 } 1381 1382 1390 public void setTime(int parameterIndex, Time x) throws SQLException { 1391 checkIfValid(); 1392 pstmt.setTime(parameterIndex, x); 1393 } 1394 1395 1411 public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { 1412 checkIfValid(); 1413 pstmt.setTime(parameterIndex, x, cal); 1414 } 1415 1416 1424 public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { 1425 checkIfValid(); 1426 pstmt.setTimestamp(parameterIndex, x); 1427 } 1428 1429 1446 public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { 1447 checkIfValid(); 1448 pstmt.setTimestamp(parameterIndex, x, cal); 1449 } 1450 1451 1470 public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { 1471 checkIfValid(); 1472 pstmt.setUnicodeStream(parameterIndex, x, length); 1473 } 1474 1475 1477 1490 public void addBatch(String s) throws SQLException { 1491 checkIfValid(); 1492 pstmt.addBatch(s); 1493 } 1494 1495 1501 public void cancel() throws SQLException { 1502 checkIfValid(); 1503 pstmt.cancel(); 1504 } 1505 1506 1516 public void clearBatch() throws SQLException { 1517 checkIfValid(); 1518 pstmt.clearBatch(); 1519 } 1520 1521 1528 public void clearWarnings() throws SQLException { 1529 checkIfValid(); 1530 pstmt.clearWarnings(); 1531 } 1532 1533 1549 public void close() throws SQLException { 1550 if (isDebugLogging) { 1551 trace.log(BasicLevel.DEBUG, "" + this); 1552 } 1553 closed = true; 1554 } 1555 1556 1577 public boolean execute(String s) throws SQLException { 1578 checkIfValid(); 1579 return pstmt.execute(s); 1580 } 1581 1582 1630 public int[] executeBatch() throws SQLException { 1631 checkIfValid(); 1632 return pstmt.executeBatch(); 1633 } 1634 1635 1646 public ResultSet executeQuery(String s) throws SQLException { 1647 checkIfValid(); 1648 return pstmt.executeQuery(s); 1649 } 1650 1651 1664 public int executeUpdate(String s) throws SQLException { 1665 checkIfValid(); 1666 return pstmt.executeUpdate(s); 1667 } 1668 1669 1676 public Connection getConnection() throws SQLException { 1677 checkIfValid(); 1678 return pstmt.getConnection(); 1679 } 1680 1681 1693 public int getFetchDirection() throws SQLException { 1694 checkIfValid(); 1695 return pstmt.getFetchDirection(); 1696 } 1697 1698 1710 public int getFetchSize() throws SQLException { 1711 checkIfValid(); 1712 return pstmt.getFetchSize(); 1713 } 1714 1715 1728 public int getMaxFieldSize() throws SQLException { 1729 checkIfValid(); 1730 return pstmt.getMaxFieldSize(); 1731 } 1732 1733 1743 public int getMaxRows() throws SQLException { 1744 checkIfValid(); 1745 return pstmt.getMaxRows(); 1746 } 1747 1748 1767 public boolean getMoreResults() throws SQLException { 1768 checkIfValid(); 1769 return pstmt.getMoreResults(); 1770 } 1771 1772 1781 public int getQueryTimeout() throws SQLException { 1782 checkIfValid(); 1783 return pstmt.getQueryTimeout(); 1784 } 1785 1786 1795 public ResultSet getResultSet() throws SQLException { 1796 checkIfValid(); 1797 return pstmt.getResultSet(); 1798 } 1799 1800 1808 public int getResultSetConcurrency() throws SQLException { 1809 checkIfValid(); 1810 return pstmt.getResultSetConcurrency(); 1811 } 1812 1813 1822 public int getResultSetType() throws SQLException { 1823 checkIfValid(); 1824 return pstmt.getResultSetType(); 1825 } 1826 1827 1836 public int getUpdateCount() throws SQLException { 1837 checkIfValid(); 1838 return pstmt.getUpdateCount(); 1839 } 1840 1841 1860 public SQLWarning getWarnings() throws SQLException { 1861 checkIfValid(); 1862 return pstmt.getWarnings(); 1863 } 1864 1865 1884 public void setCursorName(String name) throws SQLException { 1885 checkIfValid(); 1886 pstmt.setCursorName(name); 1887 } 1888 1889 1899 public void setEscapeProcessing(boolean enable) throws SQLException { 1900 checkIfValid(); 1901 setEscapeProcessingCalled = true; 1903 pstmt.setEscapeProcessing(enable); 1904 } 1905 1906 1923 public void setFetchDirection(int direction) throws SQLException { 1924 checkIfValid(); 1925 setFetchDirectionCalled = true; 1927 pstmt.setFetchDirection(direction); 1928 } 1929 1930 1943 public void setFetchSize(int rows) throws SQLException { 1944 checkIfValid(); 1945 pstmt.setFetchSize(rows); 1946 } 1947 1948 1963 public void setMaxFieldSize(int max) throws SQLException { 1964 checkIfValid(); 1965 setMaxFieldSizeCalled = true; 1967 pstmt.setMaxFieldSize(max); 1968 } 1969 1970 1979 public void setMaxRows(int max) throws SQLException { 1980 checkIfValid(); 1981 pstmt.setMaxRows(max); 1982 } 1983 1984 1995 public void setQueryTimeout(int seconds) throws SQLException { 1996 checkIfValid(); 1997 pstmt.setQueryTimeout(seconds); 1998 } 1999 2000 2010 public void setURL(int parameterIndex, URL x) throws SQLException { 2011 checkIfValid(); 2012 pstmt.setURL(parameterIndex, x); 2013 } 2014 2015 2018 public int hashCode() { 2019 return hashCode; 2020 } 2021 2022 2025 public boolean isClosed() { 2026 return closed; 2027 } 2028 2029} 2030 | Popular Tags |