1 30 31 32 package org.hsqldb.jdbc; 33 34 import java.sql.BatchUpdateException ; 36 37 import java.sql.Connection ; 39 import java.sql.ResultSet ; 40 import java.sql.SQLException ; 41 import java.sql.SQLWarning ; 42 import java.sql.Statement ; 43 44 import org.hsqldb.HsqlException; 45 import org.hsqldb.Result; 46 import org.hsqldb.ResultConstants; 47 import org.hsqldb.Trace; 48 import org.hsqldb.Types; 49 50 61 127 public class jdbcStatement implements Statement { 128 129 134 volatile boolean isClosed; 135 136 137 private boolean isEscapeProcessing = true; 138 139 140 protected jdbcConnection connection; 141 142 143 protected int maxRows; 144 145 146 protected Result resultIn; 147 148 149 protected int rsType = jdbcResultSet.TYPE_FORWARD_ONLY; 150 151 152 protected Result resultOut = new Result(ResultConstants.SQLEXECDIRECT); 153 154 155 protected Result batchResultOut = null; 156 157 168 193 public ResultSet executeQuery(String sql) throws SQLException { 194 195 checkClosed(); 196 connection.clearWarningsNoCheck(); 197 fetchResult(sql); 198 199 return new jdbcResultSet(this, resultIn, connection.connProperties, 200 connection.isNetConn); 201 } 202 203 218 public int executeUpdate(String sql) throws SQLException { 219 220 checkClosed(); 221 connection.clearWarningsNoCheck(); 222 fetchResult(sql); 223 224 if (resultIn == null || resultIn.isData()) { 225 226 230 throw new SQLException ( 231 Trace.getMessage(Trace.jdbcStatement_executeUpdate)); 232 } else if (resultIn.isError()) { 233 Util.throwError(resultIn); 234 } 235 236 return resultIn.getUpdateCount(); 237 } 238 239 259 public synchronized void close() throws SQLException { 260 261 if (isClosed) { 262 return; 263 } 264 265 batchResultOut = null; 266 connection = null; 267 resultIn = null; 268 resultOut = null; 269 isClosed = true; 270 } 271 272 274 300 public int getMaxFieldSize() throws SQLException { 301 302 checkClosed(); 303 304 return 0; 305 } 306 307 338 public void setMaxFieldSize(int max) throws SQLException { 339 340 checkClosed(); 341 342 if (max < 0) { 343 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT); 344 } 345 } 346 347 361 public int getMaxRows() throws SQLException { 362 363 checkClosed(); 364 365 return maxRows; 366 } 367 368 381 public void setMaxRows(int max) throws SQLException { 382 383 checkClosed(); 384 385 if (max < 0) { 386 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT); 387 } 388 389 maxRows = max; 390 } 391 392 407 public void setEscapeProcessing(boolean enable) throws SQLException { 408 409 checkClosed(); 410 411 isEscapeProcessing = enable; 412 } 413 414 435 public int getQueryTimeout() throws SQLException { 436 437 checkClosed(); 438 439 return 0; 440 } 441 442 465 public void setQueryTimeout(int seconds) throws SQLException { 466 467 checkClosed(); 468 469 if (seconds < 0) { 470 throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT); 471 } 472 } 473 474 493 public void cancel() throws SQLException { 494 checkClosed(); 495 } 496 497 528 public SQLWarning getWarnings() throws SQLException { 529 530 checkClosed(); 531 532 return null; 533 } 534 535 556 public void clearWarnings() throws SQLException { 557 checkClosed(); 558 } 559 560 593 public void setCursorName(String name) throws SQLException { 594 checkClosed(); 595 } 596 597 599 624 public boolean execute(String sql) throws SQLException { 625 626 checkClosed(); 627 connection.clearWarningsNoCheck(); 628 fetchResult(sql); 629 630 return resultIn.isData(); 631 } 632 633 655 public ResultSet getResultSet() throws SQLException { 656 657 checkClosed(); 658 659 return resultIn == null ||!resultIn.isData() ? null 660 : new jdbcResultSet(this, 661 resultIn, 662 connection.connProperties, 663 connection.isNetConn); 664 } 665 666 678 public int getUpdateCount() throws SQLException { 679 680 return (resultIn == null || resultIn.isData()) ? -1 684 : resultIn 685 .getUpdateCount(); 686 } 687 688 707 public boolean getMoreResults() throws SQLException { 708 709 checkClosed(); 710 711 resultIn = null; 712 713 return false; 714 } 715 716 718 754 public void setFetchDirection(int direction) throws SQLException { 755 756 checkClosed(); 757 758 if (direction != jdbcResultSet.FETCH_FORWARD) { 759 throw Util.notSupported(); 760 } 761 } 762 763 788 public int getFetchDirection() throws SQLException { 789 790 checkClosed(); 791 792 return jdbcResultSet.FETCH_FORWARD; 793 } 794 795 825 public void setFetchSize(int rows) throws SQLException { 826 checkClosed(); 827 } 828 829 856 public int getFetchSize() throws SQLException { 857 858 checkClosed(); 859 860 return 0; 861 } 862 863 884 public int getResultSetConcurrency() throws SQLException { 885 886 checkClosed(); 887 888 return jdbcResultSet.CONCUR_READ_ONLY; 889 } 890 891 916 public int getResultSetType() throws SQLException { 917 918 return rsType; 922 } 923 924 949 public void addBatch(String sql) throws SQLException { 950 951 checkClosed(); 952 953 if (isEscapeProcessing) { 954 sql = connection.nativeSQL(sql); 955 } 956 957 if (batchResultOut == null) { 958 batchResultOut = new Result(ResultConstants.BATCHEXECDIRECT, 959 new int[]{ Types.VARCHAR }, 0); 960 } 961 962 batchResultOut.add(new Object []{ sql }); 963 } 964 965 987 public void clearBatch() throws SQLException { 988 989 checkClosed(); 990 991 if (batchResultOut != null) { 992 batchResultOut.clear(); 993 } 994 } 995 996 1067 public int[] executeBatch() throws SQLException { 1068 1069 int[] updateCounts; 1070 int batchCount; 1071 HsqlException he; 1072 1073 checkClosed(); 1074 connection.clearWarningsNoCheck(); 1075 1076 if (batchResultOut == null) { 1077 batchResultOut = new Result(ResultConstants.BATCHEXECDIRECT, 1078 new int[]{ Types.VARCHAR }, 0); 1079 } 1080 1081 batchCount = batchResultOut.getSize(); 1082 1083 try { 1084 resultIn = connection.sessionProxy.execute(batchResultOut); 1085 } catch (HsqlException e) { 1086 batchResultOut.clear(); 1087 1088 throw Util.sqlException(e); 1089 } 1090 1091 batchResultOut.clear(); 1092 1093 if (resultIn.isError()) { 1094 Util.throwError(resultIn); 1095 } 1096 1097 updateCounts = resultIn.getUpdateCounts(); 1098 1099 if (updateCounts.length != batchCount) { 1101 throw new BatchUpdateException ("failed batch", updateCounts); 1102 } 1103 1104 return updateCounts; 1106 } 1107 1108 1119 public Connection getConnection() throws SQLException { 1120 1121 checkClosed(); 1122 1123 return connection; 1124 } 1125 1126 1128 1166 public boolean getMoreResults(int current) throws SQLException { 1168 throw Util.notSupported(); 1169 } 1170 1171 1173 1197 public ResultSet getGeneratedKeys() throws SQLException { 1199 throw Util.notSupported(); 1200 } 1201 1202 1204 1239 public int executeUpdate(String sql, 1241 int autoGeneratedKeys) throws SQLException { 1242 throw Util.notSupported(); 1243 } 1244 1245 1247 1278 public int executeUpdate(String sql, 1280 int[] columnIndexes) throws SQLException { 1281 throw Util.notSupported(); 1282 } 1283 1284 1286 1315 public int executeUpdate(String sql, 1317 String [] columnNames) throws SQLException { 1318 throw Util.notSupported(); 1319 } 1320 1321 1323 1371 public boolean execute(String sql, 1373 int autoGeneratedKeys) throws SQLException { 1374 throw Util.notSupported(); 1375 } 1376 1377 1379 1426 public boolean execute(String sql, 1428 int[] columnIndexes) throws SQLException { 1429 throw Util.notSupported(); 1430 } 1431 1432 1434 1482 public boolean execute(String sql, 1484 String [] columnNames) throws SQLException { 1485 throw Util.notSupported(); 1486 } 1487 1488 1490 1509 public int getResultSetHoldability() throws SQLException { 1511 return jdbcResultSet.HOLD_CURSORS_OVER_COMMIT; 1512 } 1513 1514 1517 1524 jdbcStatement(jdbcConnection c, int type) { 1525 1526 connection = c; 1529 rsType = type; 1530 } 1531 1532 1535 synchronized boolean isClosed() { 1536 return isClosed; 1537 } 1538 1539 1544 void checkClosed() throws SQLException { 1545 1546 if (isClosed) { 1547 throw Util.sqlException(Trace.STATEMENT_IS_CLOSED); 1548 } 1549 1550 if (connection.isClosed) { 1551 throw Util.sqlException(Trace.CONNECTION_IS_CLOSED); 1552 } 1553 } 1554 1555 1561 private void fetchResult(String sql) throws SQLException { 1562 1563 if (isEscapeProcessing) { 1564 sql = connection.nativeSQL(sql); 1565 } 1566 1567 resultIn = null; 1568 1569 resultOut.setMainString(sql); 1570 resultOut.setMaxRows(maxRows); 1571 1572 try { 1573 resultIn = connection.sessionProxy.execute(resultOut); 1574 1575 if (resultIn.isError()) { 1576 throw new HsqlException(resultIn); 1577 } 1578 } catch (HsqlException e) { 1579 throw Util.sqlException(e); 1580 } 1581 } 1582} 1583 | Popular Tags |