1 5 package org.h2.jdbc; 6 7 8 import java.sql.BatchUpdateException ; 9 import java.sql.Connection ; 10 import java.sql.ResultSet ; 11 import java.sql.SQLException ; 12 import java.sql.SQLWarning ; 13 import java.sql.Statement ; 14 15 import org.h2.command.CommandInterface; 16 import org.h2.engine.SessionInterface; 17 import org.h2.message.Message; 18 import org.h2.message.TraceObject; 19 import org.h2.result.ResultInterface; 20 import org.h2.util.ObjectArray; 21 22 25 public class JdbcStatement extends TraceObject implements Statement { 26 27 protected JdbcConnection conn; 28 protected SessionInterface session; 29 protected JdbcResultSet resultSet; 30 protected int maxRows; 31 protected boolean escapeProcessing=true; 32 protected int queryTimeout; 33 protected boolean queryTimeoutSet; 34 protected int fetchSize; 35 protected boolean fetchSizeSet; 36 protected int updateCount; 37 private CommandInterface executingCommand; 38 private ObjectArray batchCommands; 39 protected int resultSetType; 40 protected boolean closedByResultSet; 41 42 49 public ResultSet executeQuery(String sql) throws SQLException { 50 try { 51 int id = getNextId(TraceObject.RESULT_SET); 52 if(debug()) { 53 debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id); 54 debugCodeCall("executeQuery", sql); 55 } 56 checkClosed(); 57 closeOld(); 58 if(escapeProcessing) { 59 sql=conn.translateSQL(sql); 60 } 61 CommandInterface command=conn.prepareCommand(sql); 62 ResultInterface result; 63 synchronized(session) { 64 setExecutingStatement(command); 65 try { 66 boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY; 67 result = command.executeQuery(maxRows, scrollable); 68 } finally { 69 setExecutingStatement(null); 70 } 71 } 72 command.close(); 73 resultSet = new JdbcResultSet(session, conn, this, result, id, closedByResultSet); 74 return resultSet; 75 } catch(Throwable e) { 76 throw logAndConvert(e); 77 } 78 } 79 80 97 public int executeUpdate(String sql) throws SQLException { 98 try { 99 debugCodeCall("executeUpdate", sql); 100 checkClosed(); 101 closeOld(); 102 if(escapeProcessing) { 103 sql = conn.translateSQL(sql); 104 } 105 CommandInterface command = conn.prepareCommand(sql); 106 synchronized(session) { 107 setExecutingStatement(command); 108 try { 109 updateCount = command.executeUpdate(); 110 } finally { 111 setExecutingStatement(null); 112 } 113 } 114 command.close(); 115 return updateCount; 116 } catch(Throwable e) { 117 throw logAndConvert(e); 118 } 119 } 120 121 132 public boolean execute(String sql) throws SQLException { 133 try { 134 int id = getNextId(TraceObject.RESULT_SET); 135 if(debug()) { 136 debugCodeCall("execute", sql); 137 } 138 checkClosed(); 139 closeOld(); 140 if(escapeProcessing) { 141 sql = conn.translateSQL(sql); 142 } 143 CommandInterface command=conn.prepareCommand(sql); 144 boolean returnsResultSet; 145 synchronized(session) { 146 setExecutingStatement(command); 147 try { 148 if(command.isQuery()) { 149 returnsResultSet = true; 150 boolean scrollable = resultSetType != ResultSet.TYPE_FORWARD_ONLY; 151 ResultInterface result = command.executeQuery(maxRows, scrollable); 152 resultSet = new JdbcResultSet(session, conn, this, result, id, closedByResultSet); 153 } else { 154 returnsResultSet = false; 155 updateCount = command.executeUpdate(); 156 } 157 } finally { 158 setExecutingStatement(null); 159 } 160 } 161 command.close(); 162 return returnsResultSet; 163 } catch(Throwable e) { 164 throw logAndConvert(e); 165 } 166 } 167 168 173 public ResultSet getResultSet() throws SQLException { 174 try { 175 checkClosed(); 176 if(resultSet != null) { 177 int id = resultSet.getTraceId(); 178 debugCodeAssign("ResultSet", TraceObject.RESULT_SET, id); 179 } 180 debugCodeCall("getResultSet"); 181 return resultSet; 182 } catch(Throwable e) { 183 throw logAndConvert(e); 184 } 185 } 186 187 195 public int getUpdateCount() throws SQLException { 196 try { 197 debugCodeCall("getUpdateCount"); 198 checkClosed(); 199 return updateCount; 200 } catch(Throwable e) { 201 throw logAndConvert(e); 202 } 203 } 204 205 210 public void close() throws SQLException { 211 try { 212 debugCodeCall("close"); 213 closeOld(); 214 if(conn != null) { 215 conn = null; 216 } 217 } catch(Throwable e) { 218 throw logAndConvert(e); 219 } 220 } 221 222 227 public Connection getConnection() throws SQLException { 228 try { 229 debugCodeCall("getConnection"); 230 checkClosed(); 231 return conn; 232 } catch(Throwable e) { 233 throw logAndConvert(e); 234 } 235 } 236 237 243 public SQLWarning getWarnings() throws SQLException { 244 try { 245 debugCodeCall("getWarnings"); 246 checkClosed(); 247 return null; 248 } catch(Throwable e) { 249 throw logAndConvert(e); 250 } 251 } 252 253 257 public void clearWarnings() throws SQLException { 258 try { 259 debugCodeCall("clearWarnings"); 260 checkClosed(); 261 } catch(Throwable e) { 262 throw logAndConvert(e); 263 } 264 } 265 266 274 public boolean getMoreResults() throws SQLException { 275 try { 276 debugCodeCall("getMoreResults"); 277 checkClosed(); 278 closeOld(); 279 return false; 280 } catch(Throwable e) { 281 throw logAndConvert(e); 282 } 283 } 284 285 291 public void setCursorName(String name) throws SQLException { 292 try { 293 debugCodeCall("setCursorName", name); 294 checkClosed(); 295 } catch(Throwable e) { 296 throw logAndConvert(e); 297 } 298 } 299 300 307 public void setFetchDirection(int direction) throws SQLException { 308 try { 309 debugCodeCall("setFetchDirection", direction); 310 checkClosed(); 311 } catch(Throwable e) { 312 throw logAndConvert(e); 313 } 314 } 315 316 322 public int getFetchDirection() throws SQLException { 323 try { 324 debugCodeCall("getFetchDirection"); 325 checkClosed(); 326 return ResultSet.FETCH_FORWARD; 327 } catch(Throwable e) { 328 throw logAndConvert(e); 329 } 330 } 331 332 338 public int getMaxRows() throws SQLException { 339 try { 340 debugCodeCall("getMaxRows"); 341 checkClosed(); 342 return maxRows; 343 } catch(Throwable e) { 344 throw logAndConvert(e); 345 } 346 } 347 348 354 public void setMaxRows(int maxRows) throws SQLException { 355 try { 356 debugCodeCall("setMaxRows", maxRows); 357 checkClosed(); 358 if(maxRows < 0) { 359 throw Message.getInvalidValueException(""+maxRows, "maxRows"); 360 } 361 this.maxRows = maxRows; 362 } catch(Throwable e) { 363 throw logAndConvert(e); 364 } 365 } 366 367 376 public void setFetchSize(int rows) throws SQLException { 377 try { 378 debugCodeCall("setFetchSize", rows); 379 checkClosed(); 380 if(rows<0 || (rows>0 && maxRows>0 && rows>maxRows)) { 381 throw Message.getInvalidValueException(""+rows, "rows"); 382 } 383 fetchSize=rows; 384 fetchSizeSet=true; 385 } catch(Throwable e) { 386 throw logAndConvert(e); 387 } 388 } 389 390 396 public int getFetchSize() throws SQLException { 397 try { 398 debugCodeCall("getFetchSize"); 399 checkClosed(); 400 return fetchSize; 401 } catch(Throwable e) { 402 throw logAndConvert(e); 403 } 404 } 405 406 412 public int getResultSetConcurrency() throws SQLException { 413 try { 414 debugCodeCall("getResultSetConcurrency"); 415 checkClosed(); 416 return ResultSet.CONCUR_UPDATABLE; 417 } catch(Throwable e) { 418 throw logAndConvert(e); 419 } 420 } 421 422 428 public int getResultSetType() throws SQLException { 429 try { 430 debugCodeCall("getResultSetType"); 431 checkClosed(); 432 return resultSetType; 433 } catch(Throwable e) { 434 throw logAndConvert(e); 435 } 436 } 437 438 444 public int getMaxFieldSize() throws SQLException { 445 try { 446 debugCodeCall("getMaxFieldSize"); 447 checkClosed(); 448 return 0; 449 } catch(Throwable e) { 450 throw logAndConvert(e); 451 } 452 } 453 454 461 public void setMaxFieldSize(int max) throws SQLException { 462 try { 463 debugCodeCall("setMaxFieldSize", max); 464 checkClosed(); 465 } catch(Throwable e) { 466 throw logAndConvert(e); 467 } 468 } 469 470 477 public void setEscapeProcessing(boolean enable) throws SQLException { 478 try { 479 if(debug()) { 480 debugCode("setEscapeProcessing("+enable+");"); 481 } 482 checkClosed(); 483 escapeProcessing=enable; 484 } catch(Throwable e) { 485 throw logAndConvert(e); 486 } 487 } 488 489 497 public void cancel() throws SQLException { 498 try { 499 debugCodeCall("cancel"); 500 checkClosed(); 501 CommandInterface c = executingCommand; 503 try { 504 if(c != null) { 505 c.cancel(); 506 } 507 } finally { 508 setExecutingStatement(null); 509 } 510 } catch(Throwable e) { 511 throw logAndConvert(e); 512 } 513 } 514 515 521 public int getQueryTimeout() throws SQLException { 522 try { 523 debugCodeCall("getQueryTimeout"); 524 checkClosed(); 525 return queryTimeout; 526 } catch(Throwable e) { 527 throw logAndConvert(e); 528 } 529 } 530 531 539 public void setQueryTimeout(int seconds) throws SQLException { 540 try { 541 debugCodeCall("setQueryTimeout", seconds); 542 checkClosed(); 543 if(seconds<0) { 544 throw Message.getInvalidValueException(""+seconds, "seconds"); 545 } 546 queryTimeout=seconds; 547 queryTimeoutSet=true; 548 } catch(Throwable e) { 549 throw logAndConvert(e); 550 } 551 } 552 553 556 public void addBatch(String sql) throws SQLException { 557 try { 558 debugCodeCall("addBatch", sql); 559 checkClosed(); 560 if(escapeProcessing) { 561 sql = conn.translateSQL(sql); 562 } 563 if(batchCommands == null) { 564 batchCommands = new ObjectArray(); 565 } 566 batchCommands.add(sql); 567 } catch(Throwable e) { 568 throw logAndConvert(e); 569 } 570 } 571 572 575 public void clearBatch() throws SQLException { 576 try { 577 debugCodeCall("clearBatch"); 578 checkClosed(); 579 batchCommands = null; 580 } catch(Throwable e) { 581 throw logAndConvert(e); 582 } 583 } 584 585 590 public int[] executeBatch() throws SQLException { 591 try { 592 debugCodeCall("executeBatch"); 593 checkClosed(); 594 if(batchCommands == null) { 595 batchCommands = new ObjectArray(); 597 } 598 int[] result = new int[batchCommands.size()]; 599 boolean error = false; 600 for(int i=0; i<batchCommands.size(); i++) { 601 String sql = (String ) batchCommands.get(i); 602 try { 603 result[i] = executeUpdate(sql); 604 } catch(SQLException e) { 605 logAndConvert(e); 606 result[i] = Statement.EXECUTE_FAILED; 608 error = true; 610 } 611 } 612 batchCommands = null; 613 if(error) { 614 throw new BatchUpdateException (result); 615 } 616 return result; 617 } catch(Throwable e) { 618 throw logAndConvert(e); 619 } 620 } 621 622 628 public ResultSet getGeneratedKeys() throws SQLException { 629 try { 630 debugCodeCall("getGeneratedKeys"); 631 checkClosed(); 632 return conn.getGeneratedKeys(this); 633 } catch(Throwable e) { 634 throw logAndConvert(e); 635 } 636 } 637 638 642 public boolean getMoreResults(int current) throws SQLException { 643 try { 644 debugCodeCall("getMoreResults"); 645 throw Message.getUnsupportedException(); 646 } catch(Throwable e) { 647 throw logAndConvert(e); 648 } 649 } 650 651 662 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 663 try { 664 if(debug()) { 665 debugCode("executeUpdate("+quote(sql)+", "+autoGeneratedKeys+");"); 666 } 667 return executeUpdate(sql); 668 } catch(Throwable e) { 669 throw logAndConvert(e); 670 } 671 } 672 673 684 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 685 try { 686 if(debug()) { 687 debugCode("executeUpdate("+quote(sql)+", "+quoteIntArray(columnIndexes)+");"); 688 } 689 return executeUpdate(sql); 690 } catch(Throwable e) { 691 throw logAndConvert(e); 692 } 693 } 694 695 706 public int executeUpdate(String sql, String [] columnNames) throws SQLException { 707 try { 708 if(debug()) { 709 debugCode("executeUpdate("+quote(sql)+", "+quoteArray(columnNames)+");"); 710 } 711 return executeUpdate(sql); 712 } catch(Throwable e) { 713 throw logAndConvert(e); 714 } 715 } 716 717 728 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 729 try { 730 if(debug()) { 731 debugCode("execute("+quote(sql)+", "+autoGeneratedKeys+");"); 732 } 733 return execute(sql); 734 } catch(Throwable e) { 735 throw logAndConvert(e); 736 } 737 } 738 739 750 public boolean execute(String sql, int[] columnIndexes) throws SQLException { 751 try { 752 if(debug()) { 753 debugCode("execute("+quote(sql)+", "+quoteIntArray(columnIndexes)+");"); 754 } 755 return execute(sql); 756 } catch(Throwable e) { 757 throw logAndConvert(e); 758 } 759 } 760 761 772 public boolean execute(String sql, String [] columnNames) throws SQLException { 773 try { 774 if(debug()) { 775 debugCode("execute("+quote(sql)+", "+quoteArray(columnNames)+");"); 776 } 777 return execute(sql); 778 } catch(Throwable e) { 779 throw logAndConvert(e); 780 } 781 } 782 783 788 public int getResultSetHoldability() throws SQLException { 790 try { 791 debugCodeCall("getResultSetHoldability"); 792 checkClosed(); 793 return ResultSet.HOLD_CURSORS_OVER_COMMIT; 794 } catch(Throwable e) { 795 throw logAndConvert(e); 796 } 797 } 798 800 802 JdbcStatement(SessionInterface session, JdbcConnection conn, int resultSetType, int id, boolean closeWithResultSet) { 803 setTrace(session.getTrace(), TraceObject.STATEMENT, id); 804 this.session = session; 805 this.conn = conn; 806 this.resultSetType = resultSetType; 807 this.closedByResultSet = closeWithResultSet; 808 } 809 810 void checkClosed() throws SQLException { 811 if(conn == null) { 812 throw Message.getSQLException(Message.OBJECT_CLOSED); 813 } 814 conn.checkClosed(); 815 } 816 817 void closeOld() throws SQLException { 818 try { 819 if(!closedByResultSet) { 820 if(resultSet != null) { 821 resultSet.closeInternal(); 822 } 823 } 824 } finally { 825 resultSet = null; 826 updateCount=-1; 827 } 828 } 829 830 protected void setExecutingStatement(CommandInterface c) { 831 conn.setExecutingStatement(c == null ? null : this); 832 executingCommand = c; 833 } 834 835 840 public boolean isClosed() throws SQLException { 841 try { 842 debugCodeCall("isClosed"); 843 return conn == null; 844 } catch(Throwable e) { 845 throw logAndConvert(e); 846 } 847 } 848 849 853 859 861 865 871 873 877 public boolean isPoolable() throws SQLException { 878 debugCodeCall("isPoolable"); 879 return false; 880 } 881 882 888 public void setPoolable(boolean poolable) throws SQLException { 889 if(debug()) { 890 debugCode("setPoolable("+poolable+");"); 891 } 892 } 893 894 904 } 905 906 | Popular Tags |