1 25 26 package org.objectweb.easybeans.component.jdbcpool; 27 28 import java.io.InputStream ; 29 import java.io.Reader ; 30 import java.math.BigDecimal ; 31 import java.net.URL ; 32 import java.sql.Array ; 33 import java.sql.Blob ; 34 import java.sql.Clob ; 35 import java.sql.Connection ; 36 import java.sql.Date ; 37 import java.sql.ParameterMetaData ; 38 import java.sql.PreparedStatement ; 39 import java.sql.Ref ; 40 import java.sql.ResultSet ; 41 import java.sql.ResultSetMetaData ; 42 import java.sql.SQLException ; 43 import java.sql.SQLWarning ; 44 import java.sql.Time ; 45 import java.sql.Timestamp ; 46 import java.util.Calendar ; 47 48 import org.objectweb.easybeans.log.JLog; 49 import org.objectweb.easybeans.log.JLogFactory; 50 51 57 public class JStatement implements PreparedStatement { 58 59 62 private boolean changed = false; 63 64 67 private boolean opened = false; 68 69 72 private boolean closing = false; 73 74 77 private PreparedStatement ps; 78 79 82 private JManagedConnection mc; 83 84 87 private int hashCode; 88 89 92 private String sql; 93 94 97 private JLog logger = JLogFactory.getLog(JStatement.class); 98 99 105 public JStatement(final PreparedStatement ps, final JManagedConnection mc, final String sql) { 106 this.ps = ps; 107 this.mc = mc; 108 this.sql = sql; 109 hashCode = sql.hashCode(); 110 opened = true; 111 } 112 113 116 public String getSql() { 117 return sql; 118 } 119 120 123 @Override 124 public int hashCode() { 125 return hashCode; 126 } 127 128 132 @Override 133 public boolean equals(final Object stmt) { 134 if (stmt == null) { 135 return false; 136 } 137 if (this.hashCode != stmt.hashCode()) { 139 return false; 140 } 141 142 if (!(stmt instanceof JStatement)) { 144 logger.warn("Bad class {0}", stmt); 145 return false; 146 } 147 JStatement psw = (JStatement) stmt; 148 if (sql == null && psw.getSql() != null) { 149 return false; 150 } 151 if (sql != null && !sql.equals(psw.getSql())) { 152 return false; 153 } 154 try { 155 if (psw.getResultSetType() != getResultSetType()) { 156 return false; 157 } 158 if (psw.getResultSetConcurrency() != getResultSetConcurrency()) { 159 return false; 160 } 161 } catch (SQLException e) { 162 logger.warn("Cannot compare statements", e); 163 return false; 164 } 165 logger.debug("Found"); 166 return true; 167 } 168 169 174 public boolean forceClose() { 175 if (opened) { 176 logger.debug("Statements should be closed explicitly."); 177 opened = false; 178 return true; 179 } 180 return false; 181 } 182 183 187 public void reuse() throws SQLException { 188 ps.clearParameters(); 189 ps.clearWarnings(); 190 opened = true; 191 if (changed) { 192 logger.debug("Properties statement have been changed, reset default properties"); 193 ps.clearBatch(); 194 ps.setFetchDirection(ResultSet.FETCH_FORWARD); 195 ps.setMaxFieldSize(0); 196 ps.setMaxRows(0); 197 ps.setQueryTimeout(0); 198 changed = false; 199 } 200 } 201 202 205 public boolean isClosed() { 206 return !opened && !closing; 207 } 208 209 213 public void forget() { 214 try { 215 ps.close(); 216 } catch (SQLException e) { 217 logger.error("Cannot close the PreparedStatement", e); 218 } 219 } 220 221 224 public int executeUpdate() throws SQLException { 225 return ps.executeUpdate(); 226 } 227 228 231 public void addBatch() throws SQLException { 232 changed = true; 233 ps.addBatch(); 234 } 235 236 239 public void clearParameters() throws SQLException { 240 ps.clearParameters(); 241 } 242 243 246 public boolean execute() throws SQLException { 247 return ps.execute(); 248 } 249 250 253 public void setByte(final int parameterIndex, final byte x) throws SQLException { 254 ps.setByte(parameterIndex, x); 255 } 256 257 260 public void setDouble(final int parameterIndex, final double x) throws SQLException { 261 ps.setDouble(parameterIndex, x); 262 } 263 264 267 public void setFloat(final int parameterIndex, final float x) throws SQLException { 268 ps.setFloat(parameterIndex, x); 269 } 270 271 274 public void setInt(final int parameterIndex, final int x) throws SQLException { 275 ps.setInt(parameterIndex, x); 276 } 277 278 281 public void setNull(final int parameterIndex, final int sqlType) throws SQLException { 282 ps.setNull(parameterIndex, sqlType); 283 } 284 285 288 public void setLong(final int parameterIndex, final long x) throws SQLException { 289 ps.setLong(parameterIndex, x); 290 } 291 292 295 public void setShort(final int parameterIndex, final short x) throws SQLException { 296 ps.setShort(parameterIndex, x); 297 } 298 299 302 public void setBoolean(final int parameterIndex, final boolean x) throws SQLException { 303 ps.setBoolean(parameterIndex, x); 304 } 305 306 309 public void setBytes(final int parameterIndex, final byte[] x) throws SQLException { 310 ps.setBytes(parameterIndex, x); 311 } 312 313 316 public void setAsciiStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { 317 ps.setAsciiStream(parameterIndex, x, length); 318 } 319 320 323 public void setBinaryStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { 324 ps.setBinaryStream(parameterIndex, x, length); 325 } 326 327 330 @SuppressWarnings ("deprecation") 331 public void setUnicodeStream(final int parameterIndex, final InputStream x, final int length) throws SQLException { 332 ps.setUnicodeStream(parameterIndex, x, length); 333 } 334 335 338 public void setCharacterStream(final int parameterIndex, final Reader reader, final int length) throws SQLException { 339 ps.setCharacterStream(parameterIndex, reader, length); 340 } 341 342 345 public void setObject(final int parameterIndex, final Object x) throws SQLException { 346 ps.setObject(parameterIndex, x); 347 } 348 349 352 public void setObject(final int parameterIndex, final Object x, final int targetSqlType) throws SQLException { 353 ps.setObject(parameterIndex, x, targetSqlType); 354 } 355 356 359 public void setObject(final int parameterIndex, final Object x, 360 final int targetSqlType, final int scale) throws SQLException { 361 ps.setObject(parameterIndex, x, targetSqlType, scale); 362 } 363 364 367 public void setNull(final int paramIndex, final int sqlType, final String typeName) throws SQLException { 368 ps.setNull(paramIndex, sqlType, typeName); 369 } 370 371 374 public void setString(final int parameterIndex, final String x) throws SQLException { 375 ps.setString(parameterIndex, x); 376 } 377 378 381 public void setBigDecimal(final int parameterIndex, final BigDecimal x) throws SQLException { 382 ps.setBigDecimal(parameterIndex, x); 383 } 384 385 388 public void setURL(final int parameterIndex, final URL x) throws SQLException { 389 ps.setURL(parameterIndex, x); 390 } 391 392 395 public void setArray(final int i, final Array x) throws SQLException { 396 ps.setArray(i, x); 397 } 398 399 402 public void setBlob(final int i, final Blob x) throws SQLException { 403 ps.setBlob(i, x); 404 } 405 406 409 public void setClob(final int i, final Clob x) throws SQLException { 410 ps.setClob(i, x); 411 } 412 413 416 public void setDate(final int parameterIndex, final Date x) throws SQLException { 417 ps.setDate(parameterIndex, x); 418 } 419 420 423 public ParameterMetaData getParameterMetaData() throws SQLException { 424 return ps.getParameterMetaData(); 425 } 426 427 430 public void setRef(final int i, final Ref x) throws SQLException { 431 ps.setRef(i, x); 432 } 433 434 437 public ResultSet executeQuery() throws SQLException { 438 return ps.executeQuery(); 439 } 440 441 444 public ResultSetMetaData getMetaData() throws SQLException { 445 return ps.getMetaData(); 446 } 447 448 451 public void setTime(final int parameterIndex, final Time x) throws SQLException { 452 ps.setTime(parameterIndex, x); 453 } 454 455 458 public void setTimestamp(final int parameterIndex, final Timestamp x) throws SQLException { 459 ps.setTimestamp(parameterIndex, x); 460 } 461 462 465 public void setDate(final int parameterIndex, final Date x, final Calendar cal) throws SQLException { 466 ps.setDate(parameterIndex, x, cal); 467 } 468 469 472 public void setTime(final int parameterIndex, final Time x, final Calendar cal) throws SQLException { 473 ps.setTime(parameterIndex, x, cal); 474 } 475 476 479 public void setTimestamp(final int parameterIndex, final Timestamp x, final Calendar cal) throws SQLException { 480 ps.setTimestamp(parameterIndex, x, cal); 481 } 482 483 486 public int getFetchDirection() throws SQLException { 487 return ps.getFetchDirection(); 488 } 489 490 493 public int getFetchSize() throws SQLException { 494 return ps.getFetchSize(); 495 } 496 497 500 public int getMaxFieldSize() throws SQLException { 501 return ps.getMaxFieldSize(); 502 } 503 504 507 public int getMaxRows() throws SQLException { 508 return ps.getMaxRows(); 509 } 510 511 514 public int getQueryTimeout() throws SQLException { 515 return ps.getQueryTimeout(); 516 } 517 518 521 public int getResultSetConcurrency() throws SQLException { 522 return ps.getResultSetConcurrency(); 523 } 524 525 528 public int getResultSetHoldability() throws SQLException { 529 return ps.getResultSetHoldability(); 530 } 531 532 535 public int getResultSetType() throws SQLException { 536 return ps.getResultSetType(); 537 } 538 539 542 public int getUpdateCount() throws SQLException { 543 return ps.getUpdateCount(); 544 } 545 546 549 public void cancel() throws SQLException { 550 ps.cancel(); 551 } 552 553 556 public void clearBatch() throws SQLException { 557 ps.clearBatch(); 558 } 559 560 563 public void clearWarnings() throws SQLException { 564 ps.clearWarnings(); 565 } 566 567 570 public void close() throws SQLException { 571 if (!opened) { 572 logger.debug("Statement already closed"); 573 return; 574 } 575 opened = false; 576 closing = true; 577 mc.notifyPsClose(this); 578 closing = false; 579 } 580 581 584 public boolean getMoreResults() throws SQLException { 585 return ps.getMoreResults(); 586 } 587 588 591 public int[] executeBatch() throws SQLException { 592 return ps.executeBatch(); 593 } 594 595 598 public void setFetchDirection(final int direction) throws SQLException { 599 changed = true; 600 ps.setFetchDirection(direction); 601 } 602 603 606 public void setFetchSize(final int rows) throws SQLException { 607 changed = true; 608 ps.setFetchSize(rows); 609 } 610 611 614 public void setMaxFieldSize(final int max) throws SQLException { 615 changed = true; 616 ps.setMaxFieldSize(max); 617 } 618 619 622 public void setMaxRows(final int max) throws SQLException { 623 changed = true; 624 ps.setMaxRows(max); 625 } 626 627 630 public void setQueryTimeout(final int seconds) throws SQLException { 631 changed = true; 632 ps.setQueryTimeout(seconds); 633 } 634 635 638 public boolean getMoreResults(final int current) throws SQLException { 639 return ps.getMoreResults(current); 640 } 641 642 645 public void setEscapeProcessing(final boolean enable) throws SQLException { 646 ps.setEscapeProcessing(enable); 647 } 648 649 652 public int executeUpdate(final String sql) throws SQLException { 653 return ps.executeUpdate(sql); 654 } 655 656 659 public void addBatch(final String sql) throws SQLException { 660 changed = true; 661 ps.addBatch(sql); 662 } 663 664 667 public void setCursorName(final String name) throws SQLException { 668 ps.setCursorName(name); 669 } 670 671 674 public boolean execute(final String sql) throws SQLException { 675 changed = true; 676 return ps.execute(sql); 677 } 678 679 682 public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException { 683 changed = true; 684 return ps.executeUpdate(sql, autoGeneratedKeys); 685 } 686 687 690 public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException { 691 changed = true; 692 return ps.execute(sql, autoGeneratedKeys); 693 } 694 695 698 public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException { 699 changed = true; 700 return ps.executeUpdate(sql, columnIndexes); 701 } 702 703 706 public boolean execute(final String sql, final int[] columnIndexes) throws SQLException { 707 changed = true; 708 return ps.execute(sql, columnIndexes); 709 } 710 711 714 public Connection getConnection() throws SQLException { 715 return ps.getConnection(); 716 } 717 718 721 public ResultSet getGeneratedKeys() throws SQLException { 722 return ps.getGeneratedKeys(); 723 } 724 725 728 public ResultSet getResultSet() throws SQLException { 729 return ps.getResultSet(); 730 } 731 732 735 public SQLWarning getWarnings() throws SQLException { 736 return ps.getWarnings(); 737 } 738 739 742 public int executeUpdate(final String sql, final String [] columnNames) throws SQLException { 743 return ps.executeUpdate(sql, columnNames); 744 } 745 746 749 public boolean execute(final String sql, final String [] columnNames) throws SQLException { 750 return ps.execute(sql, columnNames); 751 } 752 753 756 public ResultSet executeQuery(final String sql) throws SQLException { 757 return ps.executeQuery(sql); 758 } 759 760 } 761 | Popular Tags |