1 21 package org.apache.derby.client.am; 22 23 import java.sql.SQLException ; 24 25 import org.apache.derby.shared.common.reference.JDBC30Translation; 26 import org.apache.derby.shared.common.reference.SQLState; 27 28 public class Statement implements java.sql.Statement , StatementCallbackInterface{ 29 30 public final static int CLOSE_CURRENT_RESULT = 1; 34 35 public final static int KEEP_CURRENT_RESULT = 2; 39 40 public final static int CLOSE_ALL_RESULTS = 3; 44 45 47 public MaterialStatement materialStatement_ = null; 48 49 public Connection connection_; 50 public Section section_; 51 public Agent agent_; 52 53 public ResultSet resultSet_ = null; 54 55 int updateCount_ = -1; 57 int returnValueFromProcedure_; 58 59 static final int executeQueryMethod__ = 1; 61 static final int executeUpdateMethod__ = 2; 62 static final int executeMethod__ = 3; 63 64 protected int sqlMode_ = 0; 68 static final int isQuery__ = 0x1; static final int isCall__ = 0x2; static final int isUpdate__ = 0x4; 73 public int sqlUpdateMode_ = 0; 75 public final static int isCommitSql__ = 0x1; 77 public final static int isRollbackSql__ = 0x2; 78 final static int isPositionedUpdateDeleteSql__ = 0x10; 79 final static int isInsertSql__ = 0x20; final static int isDeleteSql__ = 0x40; final static int isUpdateSql__ = 0x80; 83 84 public ColumnMetaData resultSetMetaData_; 86 90 public Cursor cachedCursor_ = null; 92 public Cursor cachedSingletonRowData_ = null; 93 public boolean isPreparedStatement_ = false; 94 public boolean isCallableStatement_ = false; 96 102 public DatabaseMetaData databaseMetaData_; 104 105 107 String cursorName_ = null; 120 121 boolean openOnClient_ = true; 125 boolean openOnServer_ = false; 132 133 134 protected int indexOfCurrentResultSet_ = -1; 136 ResultSet[] resultSetList_ = null; 138 protected final static String TIMEOUT_STATEMENT = "SET STATEMENT_TIMEOUT "; 139 protected java.util.ArrayList timeoutArrayList = new java.util.ArrayList (1); 140 protected boolean doWriteTimeout = false; 141 int timeout_ = 0; int maxRows_ = 0; 143 int maxFieldSize_ = 0; boolean escapedProcedureCallWithResult_ = false; 145 146 public boolean isAutoCommittableStatement_ = true; 150 151 boolean isCatalogQuery_ = false; 155 156 157 java.util.ArrayList batch_ = new java.util.ArrayList (); 161 162 163 public int resultSetType_ = java.sql.ResultSet.TYPE_FORWARD_ONLY; 165 public int resultSetConcurrency_ = java.sql.ResultSet.CONCUR_READ_ONLY; 166 public int resultSetHoldability_; 167 public int fetchSize_ = 0; 171 public int fetchDirection_ = java.sql.ResultSet.FETCH_FORWARD; 172 173 public Cursor singletonRowData_ = null; 177 178 public int numInvisibleRS_ = 0; 180 181 public String cursorAttributesToSendOnPrepare_ = null; 184 185 public PreparedStatement preparedStatementForAutoGeneratedKeys_; 187 public ResultSet generatedKeysResultSet_; 188 public String [] generatedKeysColumnNames_; 189 public int autoGeneratedKeys_ = java.sql.Statement.NO_GENERATED_KEYS; 190 191 194 private SqlWarning warnings_ = null; 195 196 protected boolean isPoolable = false; 199 200 202 private Statement() throws SqlException { 203 initStatement(); 204 } 205 206 private void resetStatement() throws SqlException { 207 initStatement(); 208 } 209 210 private void initStatement() throws SqlException { 211 materialStatement_ = null; 212 connection_ = null; 213 agent_ = null; 214 databaseMetaData_ = null; 215 resultSetType_ = java.sql.ResultSet.TYPE_FORWARD_ONLY; 216 resultSetConcurrency_ = java.sql.ResultSet.CONCUR_READ_ONLY; 217 resultSetHoldability_ = 0; 218 cursorAttributesToSendOnPrepare_ = null; 219 if (timeoutArrayList.size() == 0) { 220 timeoutArrayList.add(null); } 222 223 initResetStatement(); 224 } 225 226 private void initResetStatement() throws SqlException { 227 initResetPreparedStatement(); 228 229 if (section_ != null) { 231 section_.free(); 232 } 233 sqlMode_ = 0; 234 sqlUpdateMode_ = 0; 235 resultSetMetaData_ = null; 236 } 237 238 protected void initResetPreparedStatement() { 239 warnings_ = null; 240 resultSet_ = null; 242 updateCount_ = -1; 243 returnValueFromProcedure_ = 0; 244 cursorName_ = null; 245 openOnClient_ = true; 246 openOnServer_ = false; 247 indexOfCurrentResultSet_ = -1; 248 resultSetList_ = null; 249 timeout_ = 0; 250 doWriteTimeout = false; 251 maxRows_ = 0; 252 maxFieldSize_ = 0; 253 escapedProcedureCallWithResult_ = false; 254 isCatalogQuery_ = false; 255 isAutoCommittableStatement_ = true; 256 257 if (batch_ == null) { 258 batch_ = new java.util.ArrayList (); 259 } else { 260 batch_.clear(); 261 } 262 fetchSize_ = 0; 263 fetchDirection_ = java.sql.ResultSet.FETCH_FORWARD; 264 singletonRowData_ = null; 265 numInvisibleRS_ = 0; 266 preparedStatementForAutoGeneratedKeys_ = null; 267 generatedKeysResultSet_ = null; 268 generatedKeysColumnNames_ = null; 269 autoGeneratedKeys_ = java.sql.Statement.NO_GENERATED_KEYS; 270 271 isPreparedStatement_ = false; 273 } 274 275 public void reset(boolean fullReset) throws SqlException { 284 if (fullReset) { 285 connection_.resetStatement(this); 286 } else { 287 initResetStatement(); 288 materialStatement_.reset_(); 289 } 290 } 291 292 public Statement(Agent agent, Connection connection) throws SqlException { 293 this(); 294 initStatement(agent, connection); 295 } 296 297 public void resetStatement(Agent agent, Connection connection) throws SqlException { 298 resetStatement(); 299 initStatement(agent, connection); 300 } 301 302 private void initStatement(Agent agent, Connection connection) { 303 agent_ = agent; 304 connection_ = connection; 305 databaseMetaData_ = connection.databaseMetaData_; 306 } 307 308 public Statement(Agent agent, Connection connection, int type, int concurrency, int holdability, 310 int autoGeneratedKeys, String [] columnNames) throws SqlException { 311 this(agent, connection); 312 initStatement(type, concurrency, holdability, autoGeneratedKeys, columnNames); 313 } 314 315 public void resetStatement(Agent agent, Connection connection, int type, int concurrency, int holdability, 316 int autoGeneratedKeys, String [] columnNames) throws SqlException { 317 resetStatement(agent, connection); 318 initStatement(type, concurrency, holdability, autoGeneratedKeys, columnNames); 319 } 320 321 private void initStatement(int type, int concurrency, int holdability, 322 int autoGeneratedKeys, String [] columnNames) throws SqlException { 323 switch (type) { 324 case java.sql.ResultSet.TYPE_FORWARD_ONLY: 325 case java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE: 326 case java.sql.ResultSet.TYPE_SCROLL_SENSITIVE: 327 resultSetType_ = type; 328 break; 329 default: 330 throw new SqlException(agent_.logWriter_, 331 new ClientMessageId(SQLState.INVALID_API_PARAMETER), 332 new Integer (type), "type", "createStatement()"); 333 } 334 335 switch (concurrency) { 336 case java.sql.ResultSet.CONCUR_READ_ONLY: 337 case java.sql.ResultSet.CONCUR_UPDATABLE: 338 resultSetConcurrency_ = concurrency; 339 break; 340 default: 341 throw new SqlException(agent_.logWriter_, 342 new ClientMessageId(SQLState.INVALID_API_PARAMETER), 343 new Integer (concurrency), "concurrency", 344 "createStatement()"); 345 } 346 347 switch (holdability) { 348 case JDBC30Translation.CLOSE_CURSORS_AT_COMMIT: 349 case JDBC30Translation.HOLD_CURSORS_OVER_COMMIT: 350 resultSetHoldability_ = holdability; 351 break; 352 default: 353 throw new SqlException(agent_.logWriter_, 354 new ClientMessageId(SQLState.INVALID_API_PARAMETER), 355 new Integer (holdability), "holdability", 356 "createStatement()"); 357 } 358 359 switch (autoGeneratedKeys) { 360 case java.sql.Statement.NO_GENERATED_KEYS: 361 case java.sql.Statement.RETURN_GENERATED_KEYS: 362 autoGeneratedKeys_ = autoGeneratedKeys; 363 break; 364 default: 365 throw new SqlException(agent_.logWriter_, 366 new ClientMessageId(SQLState.INVALID_API_PARAMETER), 367 new Integer (autoGeneratedKeys), 368 "autoGeneratedKeys", "createStatement"); 369 } 370 371 generatedKeysColumnNames_ = columnNames; 372 } 373 374 381 protected void finalize() throws java.lang.Throwable { 382 if (agent_.loggingEnabled()) { 383 agent_.logWriter_.traceEntry(this, "finalize"); 384 } 385 if (openOnClient_) { 386 markClosed(); 387 } 388 super.finalize(); 389 } 390 391 394 protected SqlWarning getSqlWarnings() { 395 return warnings_; 396 } 397 398 400 public java.sql.ResultSet executeQuery(String sql) throws SQLException { 401 try 402 { 403 synchronized (connection_) { 404 if (agent_.loggingEnabled()) { 405 agent_.logWriter_.traceEntry(this, "executeQuery", sql); 406 } 407 ResultSet resultSet = executeQueryX(sql); 408 if (agent_.loggingEnabled()) { 409 agent_.logWriter_.traceExit(this, "executeQuery", resultSet); 410 } 411 return resultSet; 412 } 413 } 414 catch ( SqlException se ) 415 { 416 throw se.getSQLException(); 417 } 418 419 } 420 421 private ResultSet executeQueryX(String sql) throws SqlException { 422 flowExecute(executeQueryMethod__, sql); 423 return resultSet_; 424 } 425 426 public int executeUpdate(String sql) throws SQLException { 427 try 428 { 429 synchronized (connection_) { 430 if (agent_.loggingEnabled()) { 431 agent_.logWriter_.traceEntry(this, "executeUpdate", sql); 432 } 433 int updateValue = executeUpdateX(sql); 434 if (agent_.loggingEnabled()) { 435 agent_.logWriter_.traceExit(this, "executeUpdate", updateValue); 436 } 437 return updateValue; 438 } 439 } 440 catch ( SqlException se ) 441 { 442 throw se.getSQLException(); 443 } 444 } 445 446 private int executeUpdateX(String sql) throws SqlException { 447 flowExecute(executeUpdateMethod__, sql); 448 return updateCount_; 449 } 450 451 462 public boolean isWrapperFor(Class iface) throws SQLException { 463 try { 464 checkForClosedStatement(); 465 } catch (SqlException se) { 466 throw se.getSQLException(); 467 } 468 return iface.isInstance(this); 469 } 470 471 478 public boolean isClosed() 479 throws SQLException { 480 if (agent_.loggingEnabled()) { 481 agent_.logWriter_.traceEntry(this, "isClosed", !openOnClient_); 482 } 483 if (agent_.loggingEnabled()) { 484 agent_.logWriter_.traceExit(this, "isClosed", !openOnClient_); 485 } 486 return !openOnClient_; 487 } 488 489 public void close() throws SQLException { 491 try 492 { 493 synchronized (connection_) { 494 if (agent_.loggingEnabled()) { 495 agent_.logWriter_.traceEntry(this, "close"); 496 } 497 closeX(); 498 } 499 } 500 catch ( SqlException se ) 501 { 502 throw se.getSQLException(); 503 } 504 } 505 506 525 public void closeX() throws SqlException { 526 if (!openOnClient_) { 527 return; 528 } 529 int numberOfResultSetsToClose = (resultSetList_ == null) ? 0 : resultSetList_.length; 532 boolean willTickleServer = willTickleServer(numberOfResultSetsToClose, true); 533 try { 534 if (willTickleServer) { 535 flowClose(); 536 } else { 537 flowCloseOutsideUOW(); 538 } 539 } finally { 540 markClosed(true); 541 } 542 } 543 544 551 public boolean isPoolable() throws SQLException { 552 try { 553 synchronized (connection_) { 554 if (agent_.loggingEnabled()) { 555 agent_.logWriter_.traceEntry(this, "isPoolable"); 556 } 557 checkForClosedStatement(); 559 560 return isPoolable; 561 } 562 } 563 catch (SqlException se) { 564 throw se.getSQLException(); 565 } 566 } 567 568 575 public void setPoolable(boolean poolable) throws SQLException { 576 try { 577 synchronized (connection_) { 578 if (agent_.loggingEnabled()) { 579 agent_.logWriter_.traceEntry(this, "setPoolable", poolable); 580 } 581 checkForClosedStatement(); 583 584 isPoolable = poolable; 585 } 586 } 587 catch (SqlException se) { 588 throw se.getSQLException(); 589 } 590 } 591 592 public int getMaxFieldSize() throws SQLException { 593 try 594 { 595 if (agent_.loggingEnabled()) { 596 agent_.logWriter_.traceEntry(this, "getMaxFieldSize"); 597 } 598 checkForClosedStatement(); 599 return maxFieldSize_; 600 } 601 catch ( SqlException se ) 602 { 603 throw se.getSQLException(); 604 } 605 } 606 607 public void setMaxFieldSize(int max) throws SQLException { 608 try 609 { 610 synchronized (connection_) { 611 if (agent_.loggingEnabled()) { 612 agent_.logWriter_.traceEntry(this, "setMaxFieldSize", max); 613 } 614 checkForClosedStatement(); 615 if (max < 0) { 616 throw new SqlException(agent_.logWriter_, 617 new ClientMessageId(SQLState.INVALID_MAXFIELD_SIZE), 618 new Integer (max)); 619 } 620 maxFieldSize_ = max; 621 } 622 } 623 catch ( SqlException se ) 624 { 625 throw se.getSQLException(); 626 } 627 } 628 629 public int getMaxRows() throws SQLException { 630 try 631 { 632 checkForClosedStatement(); 633 if (agent_.loggingEnabled()) { 634 agent_.logWriter_.traceExit(this, "getMaxRows", maxRows_); 635 } 636 return maxRows_; 637 } 638 catch ( SqlException se ) 639 { 640 throw se.getSQLException(); 641 } 642 } 643 644 public void setMaxRows(int maxRows) throws SQLException { 645 try 646 { 647 synchronized (connection_) { 648 if (agent_.loggingEnabled()) { 649 agent_.logWriter_.traceEntry(this, "setMaxRows", maxRows); 650 } 651 checkForClosedStatement(); if (maxRows < 0) { 653 throw new SqlException(agent_.logWriter_, 654 new ClientMessageId(SQLState.INVALID_MAX_ROWS_VALUE), 655 new Integer (maxRows)); 656 } 657 maxRows_ = maxRows; 658 } 659 } 660 catch ( SqlException se ) 661 { 662 throw se.getSQLException(); 663 } 664 } 665 666 public void setEscapeProcessing(boolean enable) throws SQLException { 667 try 668 { 669 synchronized (connection_) { 670 if (agent_.loggingEnabled()) { 671 agent_.logWriter_.traceEntry(this, "setEscapeProcessing", enable); 672 } 673 checkForClosedStatement(); } 675 } 676 catch ( SqlException se ) 677 { 678 throw se.getSQLException(); 679 } 680 } 681 682 public int getQueryTimeout() throws SQLException { 683 try 684 { 685 checkForClosedStatement(); 686 if (agent_.loggingEnabled()) { 687 agent_.logWriter_.traceExit(this, "getQueryTimeout", timeout_); 688 } 689 return timeout_; 690 } 691 catch ( SqlException se ) 692 { 693 throw se.getSQLException(); 694 } 695 } 696 697 public void setQueryTimeout(int seconds) throws SQLException { 698 try 699 { 700 synchronized (connection_) { 701 if (agent_.loggingEnabled()) { 702 agent_.logWriter_.traceEntry(this, "setQueryTimeout", seconds); 703 } 704 checkForClosedStatement(); if (seconds < 0) { 706 throw new SqlException(agent_.logWriter_, 707 new ClientMessageId(SQLState.INVALID_QUERYTIMEOUT_VALUE), 708 new Integer (seconds)); 709 } 710 if (seconds != timeout_) { 711 timeout_ = seconds; 712 doWriteTimeout = true; 713 } 714 } 715 } 716 catch ( SqlException se ) 717 { 718 throw se.getSQLException(); 719 } 720 } 721 722 public void cancel() throws SQLException { 723 try 724 { 725 if (agent_.loggingEnabled()) { 726 agent_.logWriter_.traceEntry(this, "cancel"); 727 } 728 checkForClosedStatement(); throw new SqlException(agent_.logWriter_, 730 new ClientMessageId(SQLState.CANCEL_NOT_SUPPORTED_BY_SERVER)); 731 } 732 catch ( SqlException se ) 733 { 734 throw se.getSQLException(); 735 } 736 } 737 738 public java.sql.SQLWarning getWarnings() throws SQLException { 739 if (agent_.loggingEnabled()) { 740 agent_.logWriter_.traceExit(this, "getWarnings", warnings_); 741 } 742 try { 743 checkForClosedStatement(); 744 } catch (SqlException se) { 745 throw se.getSQLException(); 746 } 747 return warnings_ == null ? null : warnings_.getSQLWarning(); 748 } 749 750 public void clearWarnings() throws SQLException { 751 synchronized (connection_) { 752 if (agent_.loggingEnabled()) { 753 agent_.logWriter_.traceEntry(this, "clearWarnings"); 754 } 755 try { 756 checkForClosedStatement(); 757 } catch (SqlException se) { 758 throw se.getSQLException(); 759 } 760 clearWarningsX(); 761 } 762 } 763 764 public void clearWarningsX() { 766 warnings_ = null; 767 } 768 769 public void setCursorName(String name) throws SQLException { 775 try 776 { 777 synchronized (connection_) { 778 if (agent_.loggingEnabled()) { 779 agent_.logWriter_.traceEntry(this, "setCursorName", name); 780 } 781 checkForClosedStatement(); if (name == null || name.equals("")) { 783 throw new SqlException(agent_.logWriter_, 784 new ClientMessageId(SQLState.CURSOR_INVALID_NAME), 785 name); 786 } 787 788 if (resultSet_ != null && resultSet_.openOnClient_) { 790 throw new SqlException(agent_.logWriter_, 791 new ClientMessageId(SQLState.LANG_CANT_INVALIDATE_OPEN_RESULT_SET), 792 "setCursorName()", "Statement"); 793 } 794 795 798 cursorName_ = name; 799 } 800 } 801 catch ( SqlException se ) 802 { 803 throw se.getSQLException(); 804 } 805 } 806 807 809 810 public boolean execute(String sql) throws SQLException { 811 try 812 { 813 synchronized (connection_) { 814 if (agent_.loggingEnabled()) { 815 agent_.logWriter_.traceEntry(this, "execute", sql); 816 } 817 boolean b = executeX(sql); 818 if (agent_.loggingEnabled()) { 819 agent_.logWriter_.traceExit(this, "execute", b); 820 } 821 return b; 822 } 823 } 824 catch ( SqlException se ) 825 { 826 throw se.getSQLException(); 827 } 828 } 829 830 boolean executeX(String sql) throws SqlException { 831 flowExecute(executeMethod__, sql); 832 return resultSet_ != null; 833 } 834 835 public java.sql.ResultSet getResultSet() throws SQLException { 836 try 837 { 838 synchronized (connection_) { 839 if (agent_.loggingEnabled()) { 840 agent_.logWriter_.traceEntry(this, "getResultSet"); 841 } 842 checkForClosedStatement(); if (agent_.loggingEnabled()) { 844 agent_.logWriter_.traceExit(this, "getResultSet", resultSet_); 845 } 846 return resultSet_; 847 } 848 } 849 catch ( SqlException se ) 850 { 851 throw se.getSQLException(); 852 } 853 } 854 855 public int getUpdateCount() throws SQLException { 856 try 857 { 858 synchronized (connection_) { 859 if (agent_.loggingEnabled()) { 860 agent_.logWriter_.traceEntry(this, "getUpdateCount"); 861 } 862 checkForClosedStatement(); if (agent_.loggingEnabled()) { 864 agent_.logWriter_.traceExit(this, "getUpdateCount", updateCount_); 865 } 866 return updateCount_; 867 } 868 } 869 catch ( SqlException se ) 870 { 871 throw se.getSQLException(); 872 } 873 } 874 875 public boolean getMoreResults() throws SQLException { 876 try 877 { 878 synchronized (connection_) { 879 if (agent_.loggingEnabled()) { 880 agent_.logWriter_.traceEntry(this, "getMoreResults"); 881 } 882 boolean resultIsResultSet = getMoreResultsX(CLOSE_ALL_RESULTS); 883 if (agent_.loggingEnabled()) { 884 agent_.logWriter_.traceExit(this, "getMoreResults", resultIsResultSet); 885 } 886 return resultIsResultSet; 887 } 888 } 889 catch ( SqlException se ) 890 { 891 throw se.getSQLException(); 892 } 893 } 894 895 897 public void setFetchDirection(int direction) throws SQLException { 898 try 899 { 900 synchronized (connection_) { 901 if (agent_.loggingEnabled()) { 902 agent_.logWriter_.traceEntry(this, "setFetchDirection", direction); 903 } 904 checkForClosedStatement(); switch (direction) { 906 case java.sql.ResultSet.FETCH_FORWARD: 907 case java.sql.ResultSet.FETCH_REVERSE: 908 case java.sql.ResultSet.FETCH_UNKNOWN: 909 fetchDirection_ = direction; 910 break; 911 default: 912 throw new SqlException(agent_.logWriter_, 913 new ClientMessageId(SQLState.INVALID_FETCH_DIRECTION), 914 new Integer (direction)); 915 } 916 } 917 } 918 catch ( SqlException se ) 919 { 920 throw se.getSQLException(); 921 } 922 } 923 924 public int getFetchDirection() throws SQLException { 925 try 926 { 927 checkForClosedStatement(); 928 if (agent_.loggingEnabled()) { 929 agent_.logWriter_.traceExit(this, "getFetchDirection", fetchDirection_); 930 } 931 return fetchDirection_; 932 } 933 catch ( SqlException se ) 934 { 935 throw se.getSQLException(); 936 } 937 } 938 939 public void setFetchSize(int rows) throws SQLException { 940 try 941 { 942 synchronized (connection_) { 943 if (agent_.loggingEnabled()) { 944 agent_.logWriter_.traceEntry(this, "setFetchSize", rows); 945 } 946 checkForClosedStatement(); 948 if (rows < 0 || (maxRows_ != 0 && rows > maxRows_)) { 949 throw new SqlException(agent_.logWriter_, 950 new ClientMessageId(SQLState.INVALID_ST_FETCH_SIZE), 951 new Integer (rows)).getSQLException(); 952 } 953 fetchSize_ = rows; 954 } 955 } 956 catch ( SqlException se ) 957 { 958 throw se.getSQLException(); 959 } 960 } 961 962 public int getFetchSize() throws SQLException { 963 try 964 { 965 checkForClosedStatement(); 966 if (agent_.loggingEnabled()) { 967 agent_.logWriter_.traceExit(this, "getFetchSize", fetchSize_); 968 } 969 return fetchSize_; 970 } 971 catch ( SqlException se ) 972 { 973 throw se.getSQLException(); 974 } 975 } 976 977 public int getResultSetConcurrency() throws SQLException { 978 try 979 { 980 checkForClosedStatement(); 981 if (agent_.loggingEnabled()) { 982 agent_.logWriter_.traceExit(this, "getResultSetConcurrency", resultSetConcurrency_); 983 } 984 return resultSetConcurrency_; 985 } 986 catch ( SqlException se ) 987 { 988 throw se.getSQLException(); 989 } 990 } 991 992 public int getResultSetType() throws SQLException { 993 try 994 { 995 checkForClosedStatement(); 996 if (agent_.loggingEnabled()) { 997 agent_.logWriter_.traceExit(this, "getResultSetType", resultSetType_); 998 } 999 return resultSetType_; 1000 } 1001 catch ( SqlException se ) 1002 { 1003 throw se.getSQLException(); 1004 } 1005 } 1006 1007 public void addBatch(String sql) throws SQLException { 1008 try 1009 { 1010 synchronized (connection_) { 1011 if (agent_.loggingEnabled()) { 1012 agent_.logWriter_.traceEntry(this, "addBatch", sql); 1013 } 1014 checkForClosedStatement(); 1015 sql = connection_.nativeSQLX(sql); 1016 batch_.add(sql); 1017 } 1018 } 1019 catch ( SqlException se ) 1020 { 1021 throw se.getSQLException(); 1022 } 1023 } 1024 1025 public void clearBatch() throws SQLException { 1026 try 1027 { 1028 synchronized (connection_) { 1029 if (agent_.loggingEnabled()) { 1030 agent_.logWriter_.traceEntry(this, "clearBatch"); 1031 } 1032 checkForClosedStatement(); 1033 batch_.clear(); 1034 } 1035 } 1036 catch ( SqlException se ) 1037 { 1038 throw se.getSQLException(); 1039 } 1040 } 1041 1042 public int[] executeBatch() throws SQLException , BatchUpdateException { 1043 try 1044 { 1045 synchronized (connection_) { 1046 if (agent_.loggingEnabled()) { 1047 agent_.logWriter_.traceEntry(this, "executeBatch"); 1048 } 1049 int[] updateCounts = executeBatchX(); 1050 if (agent_.loggingEnabled()) { 1051 agent_.logWriter_.traceExit(this, "executeBatch", updateCounts); 1052 } 1053 return updateCounts; 1054 } 1055 } 1056 catch ( SqlException se ) 1057 { 1058 throw se.getSQLException(); 1059 } 1060 } 1061 1062 private int[] executeBatchX() throws SqlException, BatchUpdateException { 1063 checkForClosedStatement(); clearWarningsX(); resultSetList_ = null; 1066 int[] updateCounts = new int[batch_.size()]; 1070 for (int i = 0; i < batch_.size(); i++) { 1071 updateCounts[i] = -3; 1072 } 1073 flowExecuteBatch(updateCounts); 1074 return updateCounts; 1075 } 1076 1077 public java.sql.Connection getConnection() throws SQLException { 1078 try 1079 { 1080 checkForClosedStatement(); 1081 if (agent_.loggingEnabled()) { 1082 agent_.logWriter_.traceExit(this, "getConnection", connection_); 1083 } 1084 return connection_; 1085 } 1086 catch ( SqlException se ) 1087 { 1088 throw se.getSQLException(); 1089 } 1090 } 1091 1092 1094 public boolean getMoreResults(int current) throws SQLException { 1095 try 1096 { 1097 synchronized (connection_) { 1098 if (agent_.loggingEnabled()) { 1099 agent_.logWriter_.traceEntry(this, "getMoreResults", current); 1100 } 1101 boolean resultIsResultSet = getMoreResultsX(current); 1102 if (agent_.loggingEnabled()) { 1103 agent_.logWriter_.traceExit(this, "getMoreResults", resultIsResultSet); 1104 } 1105 return resultIsResultSet; 1106 } 1107 } 1108 catch ( SqlException se ) 1109 { 1110 throw se.getSQLException(); 1111 } 1112 } 1113 1114 private boolean getMoreResultsX(int current) throws SqlException { 1115 checkForClosedStatement(); boolean resultIsResultSet; 1117 updateCount_ = -1; 1118 if (resultSetList_ == null) { 1119 if (resultSet_ != null) { 1120 if (current != KEEP_CURRENT_RESULT) { 1121 resultSet_.closeX(); 1122 } 1123 resultSet_ = null; 1124 } 1125 resultIsResultSet = false; 1126 } else { 1127 if (numInvisibleRS_ == 0 && 1128 current == CLOSE_CURRENT_RESULT && 1129 resultSetList_[indexOfCurrentResultSet_] != null) { 1130 resultSetList_[indexOfCurrentResultSet_].closeX(); 1131 } 1132 resultIsResultSet = indexOfCurrentResultSet_ + 1 < resultSetList_.length; 1133 } 1134 if ((current == CLOSE_ALL_RESULTS) && (numInvisibleRS_ == 0)) { 1135 int numberOfResultSetsToClose = (resultSetList_ == null) ? 0 : indexOfCurrentResultSet_ + 1; 1136 boolean willTickleServer = willTickleServer(numberOfResultSetsToClose, false); 1137 if (willTickleServer) { 1138 flowCloseRetrievedResultSets(); 1139 } else { 1140 flowCloseRetrievedResultSetsOutsideUOW(); 1141 } 1142 } 1143 if (resultIsResultSet) { 1144 resultSet_ = resultSetList_[++indexOfCurrentResultSet_]; 1145 } else { 1146 resultSet_ = null; 1147 } 1148 1149 return resultIsResultSet; 1150 } 1151 1152 public java.sql.ResultSet getGeneratedKeys() throws SQLException { 1153 try 1154 { 1155 if (agent_.loggingEnabled()) { 1156 agent_.logWriter_.traceEntry(this, "getGeneratedKeys"); 1157 } 1158 checkForClosedStatement(); 1159 if (agent_.loggingEnabled()) { 1160 agent_.logWriter_.traceExit(this, "getGeneratedKeys", generatedKeysResultSet_); 1161 } 1162 return generatedKeysResultSet_; 1163 } 1164 catch ( SqlException se ) 1165 { 1166 throw se.getSQLException(); 1167 } 1168 } 1169 1170 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 1171 try 1172 { 1173 synchronized (connection_) { 1174 if (agent_.loggingEnabled()) { 1175 agent_.logWriter_.traceEntry(this, "executeUpdate", sql, autoGeneratedKeys); 1176 } 1177 autoGeneratedKeys_ = autoGeneratedKeys; 1178 int updateValue = executeUpdateX(sql); 1179 if (agent_.loggingEnabled()) { 1180 agent_.logWriter_.traceExit(this, "executeUpdate", updateValue); 1181 } 1182 return updateValue; 1183 } 1184 } 1185 catch ( SqlException se ) 1186 { 1187 throw se.getSQLException(); 1188 } 1189 } 1190 1191 public int executeUpdate(String sql, int columnIndexes[]) throws SQLException { 1192 try 1193 { 1194 if (agent_.loggingEnabled()) { 1195 agent_.logWriter_.traceEntry(this, "executeUpdate", sql, columnIndexes); 1196 } 1197 checkForClosedStatement(); 1198 throw new SqlException(agent_.logWriter_, 1199 new ClientMessageId(SQLState.NOT_IMPLEMENTED), 1200 "executeUpdate(String, int[])"); 1201 } 1202 catch ( SqlException se ) 1203 { 1204 throw se.getSQLException(); 1205 } 1206 } 1207 1208 public int executeUpdate(String sql, String columnNames[]) throws SQLException { 1209 try 1210 { 1211 synchronized (connection_) { 1212 if (agent_.loggingEnabled()) { 1213 agent_.logWriter_.traceEntry(this, "executeUpdate", sql, columnNames); 1214 } 1215 generatedKeysColumnNames_ = columnNames; 1216 int updateValue = executeUpdateX(sql); 1217 if (agent_.loggingEnabled()) { 1218 agent_.logWriter_.traceExit(this, "executeUpdate", updateValue); 1219 } 1220 return updateValue; 1221 } 1222 } 1223 catch ( SqlException se ) 1224 { 1225 throw se.getSQLException(); 1226 } 1227 } 1228 1229 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 1230 try 1231 { 1232 synchronized (connection_) { 1233 if (agent_.loggingEnabled()) { 1234 agent_.logWriter_.traceEntry(this, "execute", sql, autoGeneratedKeys); 1235 } 1236 autoGeneratedKeys_ = autoGeneratedKeys; 1237 boolean b = executeX(sql); 1238 if (agent_.loggingEnabled()) { 1239 agent_.logWriter_.traceExit(this, "execute", b); 1240 } 1241 return b; 1242 } 1243 } 1244 catch ( SqlException se ) 1245 { 1246 throw se.getSQLException(); 1247 } 1248 } 1249 1250 public boolean execute(String sql, int columnIndexes[]) throws SQLException { 1251 try 1252 { 1253 if (agent_.loggingEnabled()) { 1254 agent_.logWriter_.traceEntry(this, "execute", sql, columnIndexes); 1255 } 1256 checkForClosedStatement(); 1257 throw new SqlException(agent_.logWriter_, 1258 new ClientMessageId(SQLState.NOT_IMPLEMENTED), 1259 "execute(String, int[])"); 1260 } 1261 catch ( SqlException se ) 1262 { 1263 throw se.getSQLException(); 1264 } 1265 } 1266 1267 public boolean execute(String sql, String columnNames[]) throws SQLException { 1268 try 1269 { 1270 synchronized (connection_) { 1271 if (agent_.loggingEnabled()) { 1272 agent_.logWriter_.traceEntry(this, "execute", sql, columnNames); 1273 } 1274 generatedKeysColumnNames_ = columnNames; 1275 boolean b = executeX(sql); 1276 if (agent_.loggingEnabled()) { 1277 agent_.logWriter_.traceExit(this, "execute", b); 1278 } 1279 return b; 1280 } 1281 } 1282 catch ( SqlException se ) 1283 { 1284 throw se.getSQLException(); 1285 } 1286 } 1287 1288 public int getResultSetHoldability() throws SQLException { 1289 try 1290 { 1291 if (agent_.loggingEnabled()) { 1292 agent_.logWriter_.traceEntry(this, "getResultSetHoldability"); 1293 } 1294 checkForClosedStatement(); 1295 return resultSetHoldability_; 1296 } 1297 catch ( SqlException se ) 1298 { 1299 throw se.getSQLException(); 1300 } 1301 } 1302 1303 public void writeSetSpecialRegister(java.util.ArrayList sqlsttList) throws SqlException { 1307 materialStatement_.writeSetSpecialRegister_(sqlsttList); 1308 } 1309 1310 public void readSetSpecialRegister() throws SqlException { 1311 materialStatement_.readSetSpecialRegister_(); 1312 } 1313 1314 public void writeExecuteImmediate(String sql, 1315 Section section) throws SqlException { 1316 materialStatement_.writeExecuteImmediate_(sql, section); 1317 } 1318 1319 public void readExecuteImmediate() throws SqlException { 1320 materialStatement_.readExecuteImmediate_(); 1321 } 1322 1323 public void completeExecuteImmediate(Sqlca sqlca) { 1324 int sqlcode = completeSqlca(sqlca); 1325 if (sqlcode < 0) { 1326 return; 1327 } 1328 if (sqlca != null) { 1329 updateCount_ = sqlca.getUpdateCount(); 1330 } 1331 } 1332 1333 public void readExecuteImmediateForBatch(String sql) throws SqlException { 1334 materialStatement_.readExecuteImmediateForBatch_(sql); 1335 } 1336 1337 public void writePrepareDescribeOutput(String sql, 1338 Section section) throws SqlException { 1339 materialStatement_.writePrepareDescribeOutput_(sql, section); 1340 } 1341 1342 public void readPrepareDescribeOutput() throws SqlException { 1343 materialStatement_.readPrepareDescribeOutput_(); 1344 } 1345 1346 public void completePrepareDescribeOutput(ColumnMetaData resultSetMetaData, 1347 Sqlca sqlca) { 1348 completePrepare(sqlca); 1349 resultSetMetaData_ = resultSetMetaData; 1350 if (agent_.loggingEnabled()) { 1351 agent_.logWriter_.traceResultSetMetaData(this, resultSetMetaData_); 1352 } 1353 } 1354 1355 public void writePrepare(String sql, Section section) throws SqlException { 1357 materialStatement_.writePrepare_(sql, section); 1358 } 1359 1360 public void readPrepare() throws SqlException { 1361 materialStatement_.readPrepare_(); 1362 } 1363 1364 public void completePrepare(Sqlca sqlca) { 1365 int sqlcode = completeSqlca(sqlca); 1366 if (sqlcode < 0) { 1367 return; 1368 } 1369 markPrepared(); 1370 } 1371 1372 public void writeOpenQuery(Section section, 1373 int fetchSize, 1374 int resultSetType) throws SqlException { 1375 materialStatement_.writeOpenQuery_(section, 1376 fetchSize, 1377 resultSetType); 1378 } 1379 1380 public void readOpenQuery() throws SqlException { 1381 materialStatement_.readOpenQuery_(); 1382 } 1383 1384 public void completeOpenQuery(Sqlca sqlca, ResultSet resultSet) { 1385 completeSqlca(sqlca); 1386 resultSet_ = resultSet; 1387 if (resultSet == null) { 1391 return; 1392 } 1393 resultSet.resultSetMetaData_ = resultSetMetaData_; 1394 resultSet.resultSetMetaData_.resultSetConcurrency_ = resultSet.resultSetConcurrency_; 1395 1396 if (cachedCursor_ == null && isPreparedStatement_) { 1399 cachedCursor_ = resultSet_.cursor_; 1400 } 1401 1402 if (sqlca != null && sqlca.getSqlCode() < 0) { 1405 return; 1406 } 1407 openOnServer_ = true; 1408 resultSet.cursor_.rowsRead_ = 0; 1409 1410 if (resultSet_.fetchSize_ == 0 && 1416 (resultSet_.resultSetType_ == java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE || 1417 resultSet_.resultSetType_ == java.sql.ResultSet.TYPE_SCROLL_SENSITIVE)) { 1418 resultSet_.setFetchSize_(org.apache.derby.client.am. 1419 Configuration.defaultFetchSize); 1420 } 1421 } 1422 1423 public void completeExecuteCallOpenQuery(Sqlca sqlca, ResultSet resultSet, ColumnMetaData resultSetMetaData, Section generatedSection) { 1424 resultSet.completeSqlca(sqlca); 1425 resultSet.resultSetMetaData_ = resultSetMetaData; 1427 1428 if (sqlca != null && sqlca.getSqlCode() < 0) { 1431 return; 1432 } 1433 openOnServer_ = true; 1434 resultSet.cursor_.rowsRead_ = 0; 1435 1436 resultSet.generatedSection_ = generatedSection; 1437 1438 if (resultSet.fetchSize_ == 0) { 1443 resultSet.fetchSize_ = org.apache.derby.client.am.Configuration.defaultFetchSize; 1444 } 1445 } 1446 1447 public void writeExecuteCall(boolean outputExpected, 1448 String procedureName, 1449 Section section, 1450 int fetchSize, 1451 boolean suppressResultSets, int resultSetType, 1453 ColumnMetaData parameterMetaData, 1454 Object [] inputs) throws SqlException { 1455 materialStatement_.writeExecuteCall_(outputExpected, 1456 procedureName, 1457 section, 1458 fetchSize, 1459 suppressResultSets, 1460 resultSetType, 1461 parameterMetaData, 1462 inputs); 1463 } 1464 1465 public void readExecuteCall() throws SqlException { 1466 materialStatement_.readExecuteCall_(); 1467 } 1468 1469 public void completeExecuteCall(Sqlca sqlca, Cursor singletonParams, ResultSet[] resultSets) { 1470 completeExecuteCall(sqlca, singletonParams); 1471 resultSetList_ = resultSets; 1472 if (resultSets != null) { 1473 resultSet_ = resultSets[0]; 1474 } 1475 indexOfCurrentResultSet_ = 0; 1476 } 1477 1478 public void completeExecuteCall(Sqlca sqlca, Cursor singletonParams) { 1480 completeExecute(sqlca); 1481 if (sqlca != null && sqlca.getSqlCode() < 0) { 1483 singletonRowData_ = null; 1484 } else { 1485 singletonRowData_ = singletonParams; 1486 if (cachedSingletonRowData_ == null && isPreparedStatement_) { 1487 cachedSingletonRowData_ = singletonRowData_; 1488 } 1489 } 1490 } 1491 1492 public void completeExecute(Sqlca sqlca) { 1494 if (sqlca == null) { 1495 return; 1496 } 1497 1498 int sqlcode = sqlca.getSqlCode(); 1499 if (sqlcode < 0) { 1500 agent_.accumulateReadException(new SqlException(agent_.logWriter_, sqlca)); 1501 returnValueFromProcedure_ = sqlcode; 1502 } else { 1503 updateCount_ = sqlca.getUpdateCount(); 1504 if (sqlMode_ == isCall__) { 1507 updateCount_ = -1; 1508 returnValueFromProcedure_ = sqlca.getSqlErrd()[0]; } 1510 if (sqlcode > 0 && sqlcode != 466 && sqlcode != 100) { 1517 accumulateWarning(new SqlWarning(agent_.logWriter_, sqlca)); 1518 } 1519 } 1520 } 1521 1522 1523 public void setUpdateCount(int updateCount) { 1524 updateCount_ = updateCount; 1525 } 1526 1527 1528 private boolean willTickleServer(int number, boolean allowAutoCommits) throws SqlException { 1529 boolean requiresAutocommit = false; 1530 if (resultSetList_ != null) { 1531 for (int i = 0; i < number; i++) { 1532 if (resultSetList_[i] != null) { 1533 if (resultSetList_[i].openOnServer_) { 1534 return true; } 1536 if (!resultSetList_[i].autoCommitted_ && allowAutoCommits) { 1537 requiresAutocommit = true; } 1539 } 1540 } 1541 } else if (generatedKeysResultSet_ != null && generatedKeysResultSet_.openOnServer_) { 1542 generatedKeysResultSet_.writeClose(); 1543 } else if (resultSet_ != null) { 1544 if (resultSet_.openOnServer_) { 1545 return true; } 1547 if (!resultSet_.autoCommitted_ && allowAutoCommits) { 1548 requiresAutocommit = true; 1549 } 1550 } 1551 if (connection_.autoCommit_ && requiresAutocommit) { if (connection_.isXAConnection_) { 1553 return (connection_.getXAState() == Connection.XA_T0_NOT_ASSOCIATED) ; 1554 } else { 1555 return true; 1556 } 1557 } 1558 return false; 1559 } 1560 1561 private void flowClose() throws SqlException { 1562 agent_.beginWriteChain(this); 1563 writeClose(true); agent_.flow(this); 1565 readClose(true); agent_.endReadChain(); 1567 } 1568 1569 private void flowCloseOutsideUOW() throws SqlException { 1570 agent_.beginWriteChainOutsideUOW(); 1571 writeClose(true); agent_.flowOutsideUOW(); 1573 readClose(true); agent_.endReadChain(); 1575 } 1576 1577 final void writeClose(boolean allowAutoCommits) throws SqlException { 1578 writeCloseResultSets(allowAutoCommits); 1579 } 1580 1581 final void readClose(boolean allowAutoCommits) throws SqlException { 1582 readCloseResultSets(allowAutoCommits); 1583 } 1584 1585 boolean writeCloseResultSets(boolean allowAutoCommits) throws SqlException { 1586 int numberOfResultSetsToClose = (resultSetList_ == null) ? 0 : resultSetList_.length; 1587 return writeCloseResultSets(numberOfResultSetsToClose, allowAutoCommits); 1588 } 1589 1590 boolean writeCloseResultSets(int number, boolean allowAutoCommits) throws SqlException { 1607 boolean requiresAutocommit = false; 1608 if (resultSetList_ != null) { 1609 for (int i = 0; i < number; i++) { 1610 if (resultSetList_[i] != null) { 1611 if (resultSetList_[i].openOnServer_) { 1612 resultSetList_[i].writeClose(); 1613 } 1614 if (!resultSetList_[i].autoCommitted_ && allowAutoCommits) { 1615 requiresAutocommit = true; 1616 } 1617 } 1618 } 1619 } else if (generatedKeysResultSet_ != null && generatedKeysResultSet_.openOnServer_) { 1620 generatedKeysResultSet_.writeClose(); 1621 } else if (resultSet_ != null) { 1622 if (resultSet_.openOnServer_) { 1623 resultSet_.writeClose(); 1624 } 1625 if (!resultSet_.autoCommitted_ && allowAutoCommits) { 1626 requiresAutocommit = true; 1627 } 1628 } 1629 if (connection_.autoCommit_ && requiresAutocommit && isAutoCommittableStatement_) { 1630 connection_.writeAutoCommit(); 1631 if (connection_.isXAConnection_) { 1632 return (connection_.getXAState() == Connection.XA_T0_NOT_ASSOCIATED) ; 1633 } else { 1634 return true; 1635 } 1636 } 1637 return false; 1638 } 1639 1640 void readCloseResultSets(boolean allowAutoCommits) throws SqlException { 1642 int numberOfResultSetsToClose = (resultSetList_ == null) ? 0 : resultSetList_.length; 1643 readCloseResultSets(numberOfResultSetsToClose, allowAutoCommits); 1644 } 1645 1646 void readCloseResultSets(int number, boolean allowAutoCommits) throws SqlException { 1647 boolean requiredAutocommit = false; 1648 if (resultSetList_ != null) { 1649 for (int i = 0; i < number; i++) { 1650 if (resultSetList_[i] != null) { 1651 if (resultSetList_[i].openOnServer_) { 1652 resultSetList_[i].readClose(); 1653 } else { 1654 resultSetList_[i].markClosed(); 1655 } 1656 if (!resultSetList_[i].autoCommitted_ && allowAutoCommits) { 1657 requiredAutocommit = true; 1658 } 1659 } 1660 } 1661 } else if (generatedKeysResultSet_ != null) { 1662 if (generatedKeysResultSet_.openOnServer_) { 1663 generatedKeysResultSet_.readClose(); 1664 } else { 1665 generatedKeysResultSet_.markClosed(); 1666 } 1667 } else if (resultSet_ != null) { 1668 if (resultSet_.openOnServer_) { 1669 resultSet_.readClose(); 1670 } else { 1671 resultSet_.markClosed(); 1672 } 1673 if (!resultSet_.autoCommitted_ && allowAutoCommits) { 1674 requiredAutocommit = true; 1675 } 1676 } 1677 if (connection_.autoCommit_ && requiredAutocommit && isAutoCommittableStatement_) { 1679 connection_.readAutoCommit(); 1680 } 1681 } 1682 1683 private void flowCloseRetrievedResultSets() throws SqlException { 1684 int numberOfResultSetsToClose = (resultSetList_ == null) ? 0 : indexOfCurrentResultSet_ + 1; 1685 agent_.beginWriteChain(this); 1686 writeCloseResultSets(numberOfResultSetsToClose, false); 1691 agent_.flow(this); 1692 readCloseResultSets(numberOfResultSetsToClose, false); agent_.endReadChain(); 1694 } 1695 1696 private void flowCloseRetrievedResultSetsOutsideUOW() throws SqlException { 1697 int numberOfResultSetsToClose = (resultSetList_ == null) ? 0 : indexOfCurrentResultSet_ + 1; 1698 agent_.beginWriteChainOutsideUOW(); 1699 writeCloseResultSets(numberOfResultSetsToClose, false); 1704 agent_.flowOutsideUOW(); 1705 readCloseResultSets(numberOfResultSetsToClose, false); agent_.endReadChain(); 1707 } 1708 1709 public int completeSqlca(Sqlca sqlca) { 1710 if (sqlca == null) { 1711 return 0; 1712 } 1713 int sqlcode = sqlca.getSqlCode(); 1714 if (sqlcode < 0) { 1715 connection_.agent_.accumulateReadException(new SqlException(agent_.logWriter_, sqlca)); 1716 } else if (sqlcode > 0) { 1717 accumulateWarning(new SqlWarning(agent_.logWriter_, sqlca)); 1718 } 1719 return sqlcode; 1720 } 1721 1722 public void completeExecuteSetStatement(Sqlca sqlca) { 1723 } 1724 1725 void markClosedOnServer() { 1726 if (section_ != null) { 1727 section_.free(); 1728 section_ = null; 1729 } 1730 openOnServer_ = false; 1731 if (materialStatement_ != null) { 1735 materialStatement_.markClosedOnServer_(); 1736 } 1737 } 1738 1739 1756 void markClosed() { 1757 markClosed(false); 1758 } 1759 1760 1771 void markClosed(boolean removeListener) { 1772 openOnClient_ = false; 1773 markResultSetsClosed(); 1774 removeClientCursorNameFromCache(); 1779 markPreparedStatementForAutoGeneratedKeysClosed(); 1780 markClosedOnServer(); 1781 1782 if (resultSetMetaData_ != null) { 1784 resultSetMetaData_.markClosed(); 1785 resultSetMetaData_ = null; 1786 } 1787 1788 if(removeListener) 1789 connection_.openStatements_.remove(this); 1790 } 1791 1792 void markPreparedStatementForAutoGeneratedKeysClosed() { 1793 if (preparedStatementForAutoGeneratedKeys_ != null) { 1794 preparedStatementForAutoGeneratedKeys_.markClosed(); 1795 } 1796 } 1797 1798 1804 void markResultSetsClosed() { 1805 markResultSetsClosed(false); 1806 } 1807 1808 1816 void markResultSetsClosed(boolean removeListener) { 1817 if (resultSetList_ != null) { 1818 for (int i = 0; i < resultSetList_.length; i++) { 1819 if (resultSetList_[i] != null) { 1820 resultSetList_[i].markClosed(removeListener); 1821 } 1822 resultSetList_[i] = null; 1823 } 1824 } 1825 if (generatedKeysResultSet_ != null) { 1826 generatedKeysResultSet_.markClosed(removeListener); 1827 } 1828 if (resultSet_ != null) { 1829 resultSet_.markClosed(removeListener); 1830 } 1831 resultSet_ = null; 1832 resultSetList_ = null; 1833 generatedKeysResultSet_ = null; 1834 } 1835 1836 private void flowExecute(int executeType, String sql) throws SqlException { 1837 checkForClosedStatement(); checkAutoGeneratedKeysParameters(); 1839 clearWarningsX(); 1841 sql = escape(sql); 1842 parseSqlAndSetSqlModes(sql); 1843 if (sqlMode_ == isUpdate__) { 1844 updateCount_ = 0; 1845 } else { 1846 updateCount_ = -1; 1847 } 1848 1849 checkForAppropriateSqlMode(executeType, sqlMode_); 1850 1851 1856 if (sqlMode_ == isQuery__) { 1857 checkForDuplicateCursorName(); 1858 } 1859 1860 boolean timeoutSent = false; 1861 1862 agent_.beginWriteChain(this); 1863 boolean piggybackedAutoCommit = writeCloseResultSets(true); 1865 ResultSet scrollableRS = null; 1866 Section newSection = null; 1867 boolean repositionedCursor = false; 1868 1869 if (doWriteTimeout && (timeout_ > 0)) { 1875 timeoutArrayList.set(0, TIMEOUT_STATEMENT + timeout_); 1876 writeSetSpecialRegister(timeoutArrayList); 1877 timeoutSent = true; 1878 } 1879 switch (sqlMode_) { 1880 case isQuery__: 1881 newSection = agent_.sectionManager_.getDynamicSection(resultSetHoldability_); 1882 1883 writePrepareDescribeOutput(sql, newSection); 1884 writeOpenQuery(newSection, 1885 fetchSize_, 1886 resultSetType_); 1887 break; 1888 case isUpdate__: 1889 String cursorName = null; 1890 if (sqlUpdateMode_ == isDeleteSql__ || sqlUpdateMode_ == isUpdateSql__) { 1891 String [] sqlAndCursorName = extractCursorNameFromWhereCurrentOf(sql); 1892 if (sqlAndCursorName != null) { 1893 cursorName = sqlAndCursorName[0]; 1894 sql = sqlAndCursorName[1]; 1895 } 1896 } 1897 if (cursorName != null) { 1898 newSection = agent_.sectionManager_.getPositionedUpdateSection(cursorName, true); if (newSection == null) { 1900 throw new SqlException(agent_.logWriter_, 1901 new ClientMessageId(SQLState.CURSOR_INVALID_NAME), 1902 cursorName); 1903 } 1904 scrollableRS = agent_.sectionManager_.getPositionedUpdateResultSet(cursorName); 1905 if (scrollableRS != null && !scrollableRS.isRowsetCursor_) { 1907 repositionedCursor = 1908 scrollableRS.repositionScrollableResultSetBeforeJDBC1PositionedUpdateDelete(); 1909 if (!repositionedCursor) { 1910 scrollableRS = null; 1911 } 1912 } 1913 1914 if (newSection.getClientCursorName() != null && 1918 cursorName.compareTo(newSection.getClientCursorName()) == 0) { 1919 sql = substituteClientCursorNameWithServerCursorName(sql, newSection); 1921 } 1922 writeExecuteImmediate(sql, newSection); 1923 } 1924 else if (sqlUpdateMode_ == isInsertSql__ && generatedKeysColumnNames_ != null) { 1929 newSection = agent_.sectionManager_.getDynamicSection(resultSetHoldability_); 1930 writePrepareDescribeOutput(constructSelectFromInsertSQL(sql), newSection); 1931 writeOpenQuery(newSection, 1932 fetchSize_, 1933 resultSetType_); 1934 } else { 1935 newSection = agent_.sectionManager_.getDynamicSection(resultSetHoldability_); 1936 1937 writeExecuteImmediate(sql, newSection); 1938 if (sqlUpdateMode_ == isInsertSql__ && autoGeneratedKeys_ == RETURN_GENERATED_KEYS) { 1939 prepareAutoGeneratedKeysStatement(); 1940 writeOpenQuery(preparedStatementForAutoGeneratedKeys_.section_, 1941 preparedStatementForAutoGeneratedKeys_.fetchSize_, 1942 preparedStatementForAutoGeneratedKeys_.resultSetType_); 1943 } 1944 } 1945 1946 if (connection_.autoCommit_) { 1948 connection_.writeAutoCommit(); 1949 } 1950 break; 1951 case isCall__: 1952 newSection = writeExecuteCall(sql, false); 1953 1954 break; 1955 } 1956 1957 agent_.flow(this); 1958 1959 readCloseResultSets(true); 1961 if (timeoutSent) { 1962 readSetSpecialRegister(); } 1964 1965 if (piggybackedAutoCommit) { 1969 connection_.completeTransactionStart(); 1970 } 1971 1972 markResultSetsClosed(true); markClosedOnServer(); 1974 section_ = newSection; 1975 1976 switch (sqlMode_) { 1977 case isQuery__: 1978 readPrepareDescribeOutput(); 1980 readOpenQuery(); 1982 1983 if (resultSet_ != null) { 1988 resultSet_.parseScrollableRowset(); 1989 1990 setupCursorNameCacheAndMappings(); 1996 } 1997 1998 break; 1999 2000 case isUpdate__: 2001 2002 if (scrollableRS != null && !scrollableRS.isRowsetCursor_) { 2004 scrollableRS.readPositioningFetch_(); 2005 } 2006 2007 if (sqlUpdateMode_ == isInsertSql__ && generatedKeysColumnNames_ != null) { 2008 readPrepareDescribeOutput(); 2009 readOpenQuery(); 2010 if (resultSet_ != null) { 2011 generatedKeysResultSet_ = resultSet_; 2012 resultSet_ = null; 2013 updateCount_ = 1; 2014 } 2015 } else { 2016 readExecuteImmediate(); 2017 2018 if (sqlUpdateMode_ == isInsertSql__ && autoGeneratedKeys_ == RETURN_GENERATED_KEYS) { 2019 readPrepareAutoGeneratedKeysStatement(); 2020 preparedStatementForAutoGeneratedKeys_.readOpenQuery(); 2021 generatedKeysResultSet_ = preparedStatementForAutoGeneratedKeys_.resultSet_; 2022 preparedStatementForAutoGeneratedKeys_.resultSet_ = null; 2023 } 2024 } 2025 2026 if (connection_.autoCommit_) { 2027 connection_.readAutoCommit(); 2028 } 2029 break; 2030 2031 case isCall__: 2032 readPrepare(); 2033 readExecuteCall(); 2034 break; 2035 2036 } 2037 2038 agent_.endReadChain(); 2041 2042 if (resultSet_ != null && resultSet_.resultSetHoldability_ != resultSetHoldability_ && sqlMode_ != isCall__) { 2045 throw new SqlException(agent_.logWriter_, 2046 new ClientMessageId(SQLState.UNABLE_TO_OPEN_RS_WITH_REQUESTED_HOLDABILITY), 2047 new Integer (resultSetHoldability_)); 2048 } 2049 2050 if (sqlMode_ == isCall__) { 2052 parseStorProcReturnedScrollableRowset(); 2053 checkForStoredProcResultSetCount(executeType); 2054 if (connection_.autoCommit_ && resultSet_ == null && resultSetList_ == null) { 2056 connection_.flowAutoCommit(); 2057 } 2058 } 2059 2060 if (executeType == executeUpdateMethod__ && updateCount_ < 0) { 2063 updateCount_ = 0; 2064 } 2065 } 2066 2067 void flowExecuteBatch(int[] updateCounts) throws SqlException, BatchUpdateException { 2068 SqlException chainBreaker = null; 2069 boolean isCallCataloguedBestGuess = true; 2070 agent_.beginBatchedWriteChain(this); 2071 for (int i = 0; i < batch_.size(); i++) { 2072 boolean flowSQL = true; 2073 String sql = (String ) batch_.get(i); 2074 parseSqlAndSetSqlModes(sql); 2075 try { 2076 checkForInvalidBatchedSql(sql); 2077 } catch (SqlException e) { 2078 flowSQL = false; 2079 } 2080 2081 if (flowSQL) { 2085 if (section_ != null) { 2086 section_.free(); 2087 } 2088 if (sqlMode_ != isCall__) { 2089 section_ = 2090 agent_.sectionManager_.getDynamicSection(resultSetHoldability_); 2091 writeExecuteImmediate(sql, section_); 2092 } else { 2093 section_ = writeExecuteCall(sql, true); 2094 } 2095 } 2096 } 2097 2098 if (connection_.autoCommit_) { 2099 connection_.writeAutoCommit(); 2100 } 2101 2102 agent_.flowBatch(this, batch_.size()); 2103 2104 try { 2105 for (int i = 0; i < batch_.size(); i++) { 2106 agent_.setBatchedExceptionLabelIndex(i); 2107 SqlException invalidSQLCaughtByClient = null; 2108 String sql = (String ) batch_.get(i); 2109 parseSqlAndSetSqlModes(sql); 2110 try { 2111 checkForInvalidBatchedSql(sql); 2112 } catch (SqlException e) { 2113 invalidSQLCaughtByClient = e; 2114 } 2115 if (invalidSQLCaughtByClient == null) { 2116 updateCount_ = -1; 2117 if (sqlMode_ != isCall__) { 2118 readExecuteImmediateForBatch(sql); 2119 } else { 2120 if (isCallCataloguedBestGuess) { 2121 readPrepare(); 2122 } 2123 readExecuteCall(); 2124 } 2125 } else { 2126 agent_.accumulateReadException(invalidSQLCaughtByClient); 2127 updateCount_ = java.sql.Statement.EXECUTE_FAILED; 2128 invalidSQLCaughtByClient = null; 2129 } 2130 2131 updateCounts[i] = updateCount_; 2132 2133 if (updateCounts[i] == -1) { 2137 updateCounts[i] = 0; 2138 } 2139 } 2140 agent_.disableBatchedExceptionTracking(); if (connection_.autoCommit_) { 2142 connection_.readAutoCommit(); } 2144 } 2145 catch (SqlException e) { 2150 chainBreaker = e; 2151 chainBreaker.setNextException(new SqlException(agent_.logWriter_, 2152 new ClientMessageId(SQLState.BATCH_CHAIN_BREAKING_EXCEPTION))); 2153 } 2154 batch_.clear(); 2156 agent_.endBatchedReadChain(updateCounts, chainBreaker); 2157 } 2158 2159 private Section writeExecuteCall(String sql, 2160 boolean isBatch) throws SqlException { 2161 Section newSection = null; 2162 2163 newSection = agent_.sectionManager_.getDynamicSection(resultSetHoldability_); 2164 writePrepare(sql, newSection); 2166 writeExecuteCall(false, null, newSection, 2169 fetchSize_, 2170 isBatch, resultSetType_, 2172 null, null); 2175 return newSection; 2176 } 2177 2178 2181 public void listenToUnitOfWork() { 2182 } 2184 public void completeLocalCommit(java.util.Iterator listenerIterator) { 2185 } 2187 public void completeLocalRollback(java.util.Iterator listenerIterator) { 2188 } 2190 public void accumulateWarning(SqlWarning e) { 2193 if (warnings_ == null) { 2194 warnings_ = e; 2195 } else { 2196 warnings_.setNextWarning(e); 2197 } 2198 } 2199 2200 private void markPrepared() { 2201 openOnServer_ = true; 2203 listenToUnitOfWork(); 2204 } 2205 2206 2208 2212 protected String getJdbcStatementInterfaceName() { 2213 return "java.sql.Statement"; 2214 } 2215 2216 void parseSqlAndSetSqlModes(String sql) throws SqlException { 2219 String delims = "\t\n\r\f=? ("; 2220 java.util.StringTokenizer tokenizer = null; 2221 String firstToken = null; 2222 2223 if (sql.trim().startsWith("--")) { 2230 2231 int lastEndLine = -1; 2234 String endline = "\n\r\f"; 2235 tokenizer = new java.util.StringTokenizer (sql, endline, true); 2236 while (tokenizer.hasMoreTokens()) { 2237 firstToken = tokenizer.nextToken(); 2238 if (endline.indexOf(firstToken) != -1) 2239 lastEndLine = sql.indexOf(firstToken, lastEndLine+1); 2241 else if (!firstToken.trim().startsWith("--")) 2242 break; 2243 } 2244 2245 if (firstToken.startsWith("--")) { 2246 sqlMode_ = isQuery__; 2249 return; 2250 } 2251 else { 2252 tokenizer = new java.util.StringTokenizer ( 2255 sql.substring(lastEndLine+1), delims); 2256 } 2257 2258 } 2259 else { 2260 tokenizer = new java.util.StringTokenizer (sql, delims); 2263 } 2264 2265 if (!tokenizer.hasMoreTokens()) { 2266 throw new SqlException(agent_.logWriter_, 2267 new ClientMessageId(SQLState.NO_TOKENS_IN_SQL_TEXT), sql); 2268 } 2269 2270 sqlUpdateMode_ = 0; 2271 firstToken = tokenizer.nextToken(); 2272 2273 if (firstToken.equalsIgnoreCase("select") || firstToken.equalsIgnoreCase("values")) { 2276 sqlMode_ = isQuery__; 2277 } else if (firstToken.equalsIgnoreCase("call")) { 2279 sqlMode_ = isCall__; 2280 } else { 2281 parseUpdateSql(firstToken); 2282 } 2283 } 2284 2285 private void parseUpdateSql(String firstToken) throws SqlException { 2286 sqlMode_ = isUpdate__; 2287 if (firstToken.equalsIgnoreCase("insert")) { 2288 sqlUpdateMode_ = isInsertSql__; 2289 } 2290 if (firstToken.equalsIgnoreCase("delete")) { 2291 sqlUpdateMode_ = isDeleteSql__; 2292 } 2293 if (firstToken.equalsIgnoreCase("update")) { 2294 sqlUpdateMode_ = isUpdateSql__; 2295 } 2296 } 2297 2298 String getProcedureName(String sql) throws SqlException { 2300 java.util.StringTokenizer tokenizer = new java.util.StringTokenizer (sql, "\t\n\r\f= (?"); 2301 if (!tokenizer.hasMoreTokens()) { 2302 throw new SqlException(agent_.logWriter_, 2303 new ClientMessageId(SQLState.NO_TOKENS_IN_SQL_TEXT), sql); 2304 } 2305 String firstToken = tokenizer.nextToken(); 2306 if (!firstToken.equalsIgnoreCase("call")) { 2307 throw new SqlException(agent_.logWriter_, 2308 new ClientMessageId(SQLState.LANG_INVALID_CALL_STATEMENT)); 2309 } 2310 if (!tokenizer.hasMoreTokens()) { 2311 throw new SqlException(agent_.logWriter_, 2312 new ClientMessageId(SQLState.LANG_INVALID_CALL_STATEMENT)); 2313 } 2314 return tokenizer.nextToken(); 2315 } 2316 2317 public static String upperCaseProcedureName(String procedureName) throws SqlException { 2319 2321 char[] charArray = null; 2324 if (procedureName.indexOf("\"") == -1) { 2325 return procedureName.toUpperCase(); 2326 } else { 2327 charArray = procedureName.toCharArray(); 2328 boolean inStringLiteral = false; 2329 for (int i = 0; i < charArray.length; i++) { 2330 if (charArray[i] == '"') { 2331 inStringLiteral = !inStringLiteral; 2332 } else if (!inStringLiteral && charArray[i] != '.') { 2333 charArray[i] = Character.toUpperCase(charArray[i]); 2334 } 2335 } 2336 } 2337 return new String (charArray); 2338 } 2339 2340 void checkForAppropriateSqlMode(int executeType, int sqlMode) throws SqlException { 2341 if (executeType == executeQueryMethod__ && sqlMode == isUpdate__) { 2342 throw new SqlException(agent_.logWriter_, 2343 new ClientMessageId(SQLState.CANT_USE_EXEC_QUERY_FOR_UPDATE)); 2344 } 2345 if (executeType == executeUpdateMethod__ && sqlMode == isQuery__) { 2346 throw new SqlException(agent_.logWriter_, 2347 new ClientMessageId(SQLState.LANG_INVALID_CALL_TO_EXECUTE_UPDATE)); 2348 } 2349 } 2350 2351 2363 private void checkResultSetCount(int executeType) throws SqlException { 2364 switch (executeType) { 2365 case executeQueryMethod__: 2366 if (resultSetList_ != null && resultSetList_.length > 1) { 2368 throw new 2369 SqlException(agent_.logWriter_, 2370 new ClientMessageId( 2371 SQLState.MULTIPLE_RESULTS_ON_EXECUTE_QUERY), 2372 getJdbcStatementInterfaceName(), 2373 getJdbcStatementInterfaceName()); 2374 } 2375 if (resultSet_ == null || resultSetList_.length == 0) { 2376 ClientMessageId messageId = 2377 new ClientMessageId( 2378 SQLState.USE_EXECUTE_UPDATE_WITH_NO_RESULTS); 2379 throw new SqlException(agent_.logWriter_, messageId, 2380 getJdbcStatementInterfaceName(), 2381 getJdbcStatementInterfaceName()); 2382 } 2383 break; 2384 case executeUpdateMethod__: 2385 if (resultSet_ != null && resultSetList_.length > 0) { 2387 ClientMessageId messageId = 2388 new ClientMessageId( 2389 SQLState.LANG_INVALID_CALL_TO_EXECUTE_UPDATE); 2390 throw new SqlException(agent_.logWriter_, messageId); 2391 } 2392 break; 2393 } 2394 } 2395 2396 2408 protected final void checkForStoredProcResultSetCount(int executeType) 2409 throws SqlException 2410 { 2411 try { 2412 checkResultSetCount(executeType); 2413 } catch (SqlException se) { 2414 if (connection_.autoCommit_) { 2415 connection_.flowRollback(); 2416 } 2417 throw se; 2418 } 2419 } 2420 2421 void checkForClosedStatement() throws SqlException { 2422 if ( this.connection_ == null || this.connection_.isClosed() ) 2429 throw new SqlException(agent_.logWriter_, 2430 new ClientMessageId(SQLState.NO_CURRENT_CONNECTION)); 2431 2432 if (!openOnClient_) { 2433 agent_.checkForDeferredExceptions(); 2434 throw new SqlException(agent_.logWriter_, 2435 new ClientMessageId(SQLState.ALREADY_CLOSED), "Statement"); 2436 } else { 2437 agent_.checkForDeferredExceptions(); 2438 } 2439 } 2440 2441 void checkForInvalidBatchedSql(String sql) throws SqlException { 2443 if (sql == null) { 2444 throw new SqlException(agent_.logWriter_, 2445 new ClientMessageId(SQLState.NULL_SQL_TEXT)); 2446 } 2447 2448 if (sqlMode_ != isCall__ 2449 && !(sqlMode_ == isUpdate__ 2450 && (sqlUpdateMode_ == isInsertSql__ 2451 || sqlUpdateMode_ == isDeleteSql__ 2452 || sqlUpdateMode_ == isUpdateSql__ 2453 || sqlUpdateMode_ == 0))) { 2455 throw new SqlException(agent_.logWriter_, 2456 new ClientMessageId(SQLState.LANG_INVALID_SQL_IN_BATCH), sql); 2457 } 2458 } 2459 2460 2461 protected void checkForDuplicateCursorName() throws SqlException { 2463 if (cursorName_ != null && (connection_.clientCursorNameCache_. 2464 containsKey(cursorName_))) { 2465 throw new SqlException 2466 (agent_.logWriter_, 2467 new ClientMessageId(SQLState.CURSOR_DUPLICATE_NAME), 2468 cursorName_); 2469 } 2470 } 2471 2472 2473 protected void setupCursorNameCacheAndMappings() { 2476 if (cursorName_ != null) { 2477 agent_.sectionManager_.mapCursorNameToQuerySection 2484 (cursorName_, section_); 2485 section_.setClientCursorName(cursorName_); 2486 2487 connection_.clientCursorNameCache_.put(cursorName_, 2489 cursorName_); 2490 } else { 2491 agent_.sectionManager_.mapCursorNameToQuerySection 2493 (section_.getServerCursorName(), section_); 2494 } 2495 2496 mapCursorNameToResultSet(); 2499 } 2500 2501 2502 String [] extractCursorNameFromWhereCurrentOf(String sql) { 2503 String lowerSql = sql.toLowerCase(); 2504 int currentIndex = lowerSql.lastIndexOf("current"); 2505 if (currentIndex != -1) { 2506 int whereIndex = lowerSql.lastIndexOf("where"); 2507 if (whereIndex != -1) { 2508 String [] whereCurrentOf = {"where", "current", "of"}; 2509 java.util.StringTokenizer st = new java.util.StringTokenizer (sql.substring(whereIndex)); 2510 while (st.hasMoreTokens()) { 2511 if (st.nextToken().equalsIgnoreCase(whereCurrentOf[0]) && 2512 st.nextToken().equalsIgnoreCase(whereCurrentOf[1]) && 2513 st.nextToken().equalsIgnoreCase(whereCurrentOf[2])) { 2514 String cursorName = st.nextToken(); 2515 String oldCursorName = cursorName; 2516 int originalCursorNameLength = cursorName.length(); 2517 int index = sql.lastIndexOf(cursorName); 2518 if (cursorName.charAt(0) == '\"' && cursorName.charAt(cursorName.length() - 1) == '\"') { 2519 cursorName = cursorName.substring(1, cursorName.length() - 1); 2520 } else { 2521 cursorName = cursorName.toUpperCase(); 2522 } 2523 if (sql.length() > index + originalCursorNameLength) { 2526 sql = sql.substring(0, index) + cursorName + sql.substring(index + oldCursorName.length(), sql.length()); 2527 } else { 2528 sql = sql.substring(0, index) + cursorName; 2529 } 2530 return new String []{cursorName, sql}; } 2532 } 2533 } 2534 } 2535 return null; 2536 } 2537 2538 protected String substituteClientCursorNameWithServerCursorName(String sql, 2541 Section section) throws SqlException { 2542 String clientCursorName = section.getClientCursorName(); 2543 int index = sql.lastIndexOf(clientCursorName); 2544 if (sql.length() > index + clientCursorName.length()) { 2545 return sql.substring(0, index) + section.getServerCursorNameForPositionedUpdate() 2546 + sql.substring(index + clientCursorName.length(), sql.length()); 2547 } else { 2548 return sql.substring(0, index) + section.getServerCursorNameForPositionedUpdate(); 2549 } 2550 } 2551 2552 public ConnectionCallbackInterface getConnectionCallbackInterface() { 2553 return connection_; 2554 } 2555 2556 void resetCursorNameAndRemoveFromWhereCurrentOfMappings() { 2562 if (section_ != null) { 2565 agent_.sectionManager_.removeCursorNameToResultSetMapping(cursorName_, 2566 section_.getServerCursorNameForPositionedUpdate()); 2567 2568 agent_.sectionManager_.removeCursorNameToResultSetMapping(cursorName_, 2571 section_.getServerCursorName()); 2572 2573 agent_.sectionManager_.removeCursorNameToQuerySectionMapping(cursorName_, 2576 section_.getServerCursorNameForPositionedUpdate()); 2577 } 2578 2579 } 2583 2584 void mapCursorNameToResultSet() { 2585 if (cursorName_ != null) { 2586 agent_.sectionManager_.mapCursorNameToResultSet(cursorName_, resultSet_); 2587 } else { 2588 agent_.sectionManager_.mapCursorNameToResultSet(section_.getServerCursorName(), resultSet_); 2589 } 2590 } 2591 2592 void parseStorProcReturnedScrollableRowset() throws SqlException { 2593 if (resultSetList_ != null) { 2594 for (int i = 0; i < resultSetList_.length; i++) { 2595 if (resultSetList_[i].scrollable_ && resultSetList_[i].cursor_.dataBufferHasUnprocessedData()) { 2596 resultSetList_[i].parseScrollableRowset(); 2597 if (resultSetList_[i].rowCountIsUnknown()) { 2598 resultSetList_[i].getRowCount(); 2599 } 2600 } 2601 } 2602 } 2603 } 2604 2605 String escape(String sql) throws SqlException { 2606 String nativeSQL = sql; 2607 2608 nativeSQL = connection_.nativeSQLX(sql); 2609 return nativeSQL; 2610 } 2611 2612 String cacheCursorAttributesToSendOnPrepare() throws SqlException { 2617 StringBuffer cursorAttributes = new StringBuffer (); 2618 if (resultSetType_ == java.sql.ResultSet.TYPE_SCROLL_SENSITIVE) { 2619 cursorAttributes.append(Configuration.cursorAttribute_SensitiveStatic); 2621 } else if (resultSetType_ == java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE) { 2622 cursorAttributes.append(Configuration.cursorAttribute_Insensitive); 2627 } 2628 2629 if (resultSetConcurrency_ == java.sql.ResultSet.CONCUR_UPDATABLE) { 2631 cursorAttributes.append(Configuration.cursorAttribute_ForUpdate); } 2633 2634 if ((resultSetHoldability_ == java.sql.ResultSet.HOLD_CURSORS_OVER_COMMIT)) { 2635 cursorAttributes.append(Configuration.cursorAttribute_WithHold); } 2637 2638 return 2639 (cursorAttributes == null || cursorAttributes.toString().equals("")) 2640 ? null 2641 : cursorAttributes.toString(); 2642 } 2643 2644 protected String constructSelectFromInsertSQL(String sql) { 2645 String temp = "select "; 2646 int numOfColumns = generatedKeysColumnNames_.length; 2647 2648 for (int i = 0; i < numOfColumns; i++) { 2649 temp += generatedKeysColumnNames_[i]; 2650 if ((i + 1) < numOfColumns) { 2651 temp += ","; 2652 } 2653 } 2654 temp += (" from final table (" + sql + ")"); 2655 return temp; 2656 } 2657 2658 void getPreparedStatementForAutoGeneratedKeys() throws SqlException { 2659 if (preparedStatementForAutoGeneratedKeys_ == null) { 2660 String s = "select IDENTITY_VAL_LOCAL() from SYSIBM.SYSDUMMY1"; 2661 preparedStatementForAutoGeneratedKeys_ = 2662 connection_.newPreparedStatement_(s, 2663 java.sql.ResultSet.TYPE_FORWARD_ONLY, 2664 java.sql.ResultSet.CONCUR_READ_ONLY, 2665 java.sql.ResultSet.HOLD_CURSORS_OVER_COMMIT, 2666 java.sql.Statement.NO_GENERATED_KEYS, 2667 null); 2668 StringBuffer cursorAttributes = new StringBuffer (); 2674 cursorAttributes.append(Configuration.cursorAttribute_WithHold); 2675 preparedStatementForAutoGeneratedKeys_.cursorAttributesToSendOnPrepare_ = cursorAttributes.toString(); 2676 } 2677 } 2678 2679 void prepareAutoGeneratedKeysStatement() throws SqlException { 2680 getPreparedStatementForAutoGeneratedKeys(); 2681 if (!preparedStatementForAutoGeneratedKeys_.openOnServer_) { 2682 preparedStatementForAutoGeneratedKeys_.materialPreparedStatement_.writePrepareDescribeOutput_(preparedStatementForAutoGeneratedKeys_.sql_, 2683 preparedStatementForAutoGeneratedKeys_.section_); 2684 } 2685 } 2686 2687 void readPrepareAutoGeneratedKeysStatement() throws SqlException { 2688 if (!preparedStatementForAutoGeneratedKeys_.openOnServer_) { 2689 preparedStatementForAutoGeneratedKeys_.materialPreparedStatement_.readPrepareDescribeOutput_(); 2690 } 2691 } 2692 2693 void checkAutoGeneratedKeysParameters() throws SqlException { 2694 if (autoGeneratedKeys_ != java.sql.Statement.NO_GENERATED_KEYS && 2695 autoGeneratedKeys_ != java.sql.Statement.RETURN_GENERATED_KEYS) { 2696 throw new SqlException(agent_.logWriter_, 2697 new ClientMessageId(SQLState.INVALID_API_PARAMETER), 2698 new Integer (autoGeneratedKeys_), "autoGeneratedKeys", 2699 "Statement.execute()/executeQuery()"); 2700 } 2701 2702 if (generatedKeysColumnNames_ != null) { 2703 throw new SqlException(agent_.logWriter_, 2704 new ClientMessageId(SQLState.NOT_IMPLEMENTED), 2705 "Connection.prepareStatement(String sql, String[] columnNames)"); 2706 } 2707 } 2708 2709 public ColumnMetaData getGuessedResultSetMetaData() { 2710 return resultSetMetaData_; 2711 } 2712 2713 public boolean isQueryMode() { 2714 if (this.sqlMode_ == this.isQuery__) { 2715 return true; 2716 } else { 2717 return false; 2718 } 2719 } 2720 2721 protected void removeClientCursorNameFromCache() { 2722 if (cursorName_ != null && 2723 connection_.clientCursorNameCache_.containsKey(cursorName_)) { 2724 connection_.clientCursorNameCache_.remove(cursorName_); 2725 } 2726 } 2727 2728 2735 public void resultSetCommitting(ResultSet closingRS) throws SqlException { 2736 resultSetCommitting(closingRS, false); 2737 } 2738 2739 2748 public boolean resultSetCommitting(ResultSet closingRS, boolean writeChain) throws SqlException { 2749 2750 if (!connection_.autoCommit_ || closingRS.autoCommitted_) 2753 return false; 2754 2755 if (resultSetList_ != null) { 2758 for (int i = 0; i < resultSetList_.length; i++) { 2759 ResultSet crs = resultSetList_[i]; 2760 if (crs == null) 2761 continue; 2762 if (!crs.openOnClient_) 2763 continue; 2764 if (crs == closingRS) 2765 continue; 2766 2767 return false; 2769 } 2770 } 2771 2772 if (writeChain) { 2773 connection_.writeAutoCommit(); 2774 return true; 2775 } else { 2776 if (connection_.flowAutoCommit()) { 2777 markAutoCommitted(); 2778 return true; 2779 } 2780 return false; 2781 } 2782 } 2783 2784 2787 public void markAutoCommitted() { 2788 if (resultSetList_ != null) { 2789 for (int i = 0; i < resultSetList_.length; i++) 2790 if (resultSetList_[i] != null) { 2791 resultSetList_[i].markAutoCommitted(); 2792 } 2793 } else if (resultSet_ != null) { 2794 resultSet_.markAutoCommitted(); 2795 } 2796 } 2797 2798 protected SQLException jdbc3FeatureNotSupported(boolean checkStatement) 2799 throws SQLException 2800 { 2801 try 2802 { 2803 if ( checkStatement ) 2804 checkForClosedStatement(); 2805 2806 throw new SqlException(agent_.logWriter_, 2807 new ClientMessageId(SQLState.JDBC_METHOD_NOT_IMPLEMENTED)); 2808 } 2809 catch ( SqlException se ) 2810 { 2811 throw se.getSQLException(); 2812 } 2813 } 2814 2815 protected SQLException jdbc3FeatureNotSupported() throws SQLException 2816 { 2817 return jdbc3FeatureNotSupported(true); 2818 } 2819} 2820 | Popular Tags |