1 33 34 35 49 package com.internetcds.jdbc.tds; 50 51 import java.sql.*; 52 53 54 public class Statement implements java.sql.Statement 55 { 56 public static final String cvsVersion = "$Id: Statement.java,v 1.1 2006/06/23 10:39:30 sinisa Exp $"; 57 58 59 private java.sql.Connection connection; protected SQLWarningChain warningChain; protected int timeout = 0; 64 protected Tds tds = null; 65 66 protected java.sql.ResultSet results = null; 67 private java.sql.ResultSetMetaData metaResults = null; 68 69 private boolean escapeProcessing = true; 70 71 protected int updateCount = -1; 72 73 private int maxFieldSize = (1<<31)-1; 74 private int maxRows = 0; 75 private boolean isClosed = false; 76 77 78 85 public Statement( 86 Object connection_, 87 Tds tds_) 88 throws SQLException 89 { 90 tds = tds_; 91 connection = (java.sql.Connection )connection_; 92 warningChain = new SQLWarningChain(); 93 } 94 95 96 private void NotImplemented() throws java.sql.SQLException 97 { 98 throw new SQLException("Not Implemented"); 99 } 100 101 protected void finalize() 102 throws Throwable 103 { 104 super.finalize(); 105 106 if (tds != null) 107 { 108 close(); 109 } 110 } 111 112 113 114 121 public java.sql.ResultSet executeQuery(String sql) throws SQLException 122 { 123 124 125 if (execute(sql)) 126 { 127 startResultSet(); 128 } 129 130 return results; 131 } 132 133 134 135 149 150 public int executeUpdate(String sql) throws SQLException 151 { 152 if (execute(sql)) 153 { 154 startResultSet(); 155 closeResults(); 156 throw new SQLException("executeUpdate can't return a result set"); 157 } 158 else 159 { 160 return getUpdateCount(); 161 } 162 } 163 164 protected void closeResults() 165 throws java.sql.SQLException 166 { 167 168 if (results != null) 169 { 170 results.close(); 171 results = null; 172 } 173 } 174 175 private void skipToEnd() 176 throws java.sql.SQLException , java.io.IOException , 177 com.internetcds.jdbc.tds.TdsUnknownPacketSubType, 178 com.internetcds.jdbc.tds.TdsException 179 { 180 boolean done; 181 PacketResult tmp; 182 183 do 184 { 185 tmp = tds.processSubPacket(); 186 done = (tmp instanceof PacketEndTokenResult) 187 && (! ((PacketEndTokenResult)tmp).moreResults()); 188 } while (! done); 189 } 190 191 192 public void commit() 193 throws java.sql.SQLException , java.io.IOException , com.internetcds.jdbc.tds.TdsUnknownPacketSubType, com.internetcds.jdbc.tds.TdsException 194 { 195 String sql = "IF @@TRANCOUNT > 0 COMMIT TRAN "; 196 197 if (tds == null) 198 { 199 throw new SQLException("Statement is closed"); 200 } 201 202 executeQuery(sql); 203 skipToEnd(); 204 } 205 206 public void rollback() 207 throws java.sql.SQLException , java.io.IOException , com.internetcds.jdbc.tds.TdsUnknownPacketSubType, com.internetcds.jdbc.tds.TdsException 208 { 209 String sql = "IF @@TRANCOUNT > 0 ROLLBACK TRAN "; 210 211 if (tds == null) 212 { 213 throw new SQLException("Statement is closed"); 214 } 215 216 executeQuery(sql); 217 skipToEnd(); 218 } 219 220 232 public void close() throws SQLException 233 { 234 closeResults(); 235 236 257 if (tds != null) 259 { 260 Tds tmpTds = tds; 261 tds = null; 262 try 263 { 264 ((ConnectionHelper)connection).relinquish(tmpTds); 265 } 266 catch(TdsException e) 267 { 268 throw new SQLException("Internal Error: " + e.getMessage()); 269 } 270 } 271 try 272 { 273 ((ConnectionHelper)connection).markAsClosed(this); 274 } 275 catch(TdsException e) 276 { 277 throw new SQLException(e.getMessage()); 278 } 279 this.isClosed = true; 280 281 } 282 283 293 294 public int getMaxFieldSize() throws SQLException 295 { 296 return maxFieldSize; 297 } 298 299 305 306 public void setMaxFieldSize(int max) throws SQLException 307 { 308 maxFieldSize = max; 309 } 310 311 319 320 public int getMaxRows() throws SQLException 321 { 322 return maxRows; 323 } 324 325 332 333 public void setMaxRows(int max) throws SQLException 334 { 335 if (maxRows < 0) 336 { 337 throw new SQLException("Negative row count"); 338 } 339 maxRows = max; 340 341 this.executeUpdate("set rowcount " + maxRows); 342 } 343 344 351 352 public void setEscapeProcessing(boolean enable) throws SQLException 353 { 354 escapeProcessing = enable; 355 } 356 357 365 366 public int getQueryTimeout() throws SQLException 367 { 368 return timeout; 369 } 370 371 377 378 public void setQueryTimeout(int seconds) throws SQLException 379 { 380 timeout = seconds; 381 } 382 383 387 public void cancel() throws SQLException 388 { 389 if (tds == null) 390 { 391 throw new SQLException("Statement is closed"); 392 } 393 394 try 395 { 396 tds.cancel(); 397 } 398 catch(com.internetcds.jdbc.tds.TdsException e) 399 { 400 throw new SQLException(e.getMessage()); 401 } 402 catch(java.io.IOException e) 403 { 404 throw new SQLException(e.getMessage()); 405 } 406 } 407 408 409 425 public SQLWarning getWarnings() throws SQLException 426 { 427 return warningChain.getWarnings(); 428 } 429 430 431 437 public void clearWarnings() throws SQLException 438 { 439 warningChain.clearWarnings(); 440 } 441 442 454 public void setCursorName(String name) throws SQLException 455 { 456 NotImplemented(); 457 } 458 459 465 public boolean execute(String sql) throws SQLException 466 { 467 468 SQLException exception = null; 469 470 if (tds == null) 471 { 472 throw new SQLException("Statement is closed"); 473 } 474 475 476 closeResults(); 477 clearWarnings(); 478 updateCount = -1; 479 try 480 { 481 if (escapeProcessing) 482 { 483 sql = Tds.toNativeSql(sql, tds.getServerType()); 484 } 485 tds.executeQuery(sql, this, timeout); 486 } 487 catch(java.io.IOException e) 488 { 489 throw new SQLException("Network error- " + e.getMessage()); 490 } 491 catch(com.internetcds.jdbc.tds.TdsException e) 492 { 493 throw new SQLException("TDS error- " + e.getMessage()); 494 } 495 return getMoreResults(); 496 } 498 499 506 public java.sql.ResultSet getResultSet() throws SQLException 507 { 508 try 509 { 510 if (tds == null) 511 { 512 throw new SQLException("Statement is closed"); 513 } 514 closeResults(); 515 516 if (tds.peek()==TdsDefinitions.TDS_DONEINPROC) 517 { 518 PacketResult tmp = tds.processSubPacket(); 519 } 520 521 if (tds.isResultSet()) { 523 startResultSet(); 524 } 525 else if (updateCount!=-1) 526 { 527 if (! tds.isEndOfResults()) 528 { 529 throw new SQLException("Internal error. "+ 531 " expected EndOfResults, found 0x" 532 + Integer.toHexString(tds.peek()&0xff)); 533 } 534 PacketEndTokenResult end = 535 (PacketEndTokenResult) tds.processSubPacket(); 536 updateCount = end.getRowCount(); 537 results = null; 538 } 539 else 540 { 541 throw new SQLException("Internal error. Confused"); 544 } 545 } 546 catch(java.io.IOException e) 547 { 548 throw new SQLException(e.getMessage()); 549 } 550 catch(TdsException e) 551 { 552 throw new SQLException(e.getMessage()); 553 } 554 555 return results; 556 } 557 558 566 public int getUpdateCount() throws SQLException 567 { 568 return updateCount; 576 } 577 578 585 public boolean getMoreResults() throws SQLException 586 { 587 SQLException exception = null; 588 if (tds == null) 589 { 590 throw new SQLException("Statement is closed"); 591 } 592 593 updateCount = -1; 595 if (!tds.moreResults()) 596 { 597 return false; 598 } 599 600 closeResults(); 602 try 603 { 604 tds.isResultSet(); 605 606 while (!tds.isResultSet() && !tds.isEndOfResults()) 608 { 609 if (tds.isProcId()) 610 { 611 tds.processSubPacket(); 612 } 613 else if (tds.isDoneInProc()) 614 { 615 PacketDoneInProcResult tmp = 616 (PacketDoneInProcResult)tds.processSubPacket(); 617 } 618 else if (tds.isTextUpdate()) 619 { 620 PacketResult tmp1 = 621 (PacketResult)tds.processSubPacket(); 622 } 623 else if (tds.isMessagePacket() || tds.isErrorPacket()) 624 { 625 PacketMsgResult tmp = (PacketMsgResult)tds.processSubPacket(); 626 exception = warningChain.addOrReturn(tmp); 627 } 628 else if (tds.isReturnStatus()) 629 { 630 tds.processSubPacket(); 631 } 633 else 634 { 635 throw new SQLException("Protocol confusion. " 636 + "Got a 0x" 637 + Integer.toHexString((tds.peek() & 0xff)) 638 + " packet"); 639 } 640 } 642 if (exception != null) 643 { 644 try 645 { 646 tds.discardResultSet(null); 647 } 648 catch(java.io.IOException e) 649 { 650 throw new SQLException("Error discarding result set while processing sql error- " + 651 exception.getMessage() + 652 "\nIOException was " + 653 e.getMessage()); 654 } 655 catch(com.internetcds.jdbc.tds.TdsException e) 656 { 657 throw new SQLException("Error discarding result set while processing sql error- " + 658 exception.getMessage() + 659 "\nIOException was " + 660 e.getMessage()); 661 } 662 throw exception; 663 } 664 665 if (tds.isEndOfResults()) 666 { 667 PacketEndTokenResult end = 668 (PacketEndTokenResult)tds.processSubPacket(); 669 updateCount = end.getRowCount(); 670 return false; 671 672 } 673 else if (tds.isResultSet()) 674 { 675 return true; 676 } 677 else 678 { 679 throw new SQLException("Protocol confusion. " 680 + "Got a 0x" 681 + Integer.toHexString((tds.peek() & 0xff)) 682 + " packet"); 683 } 684 } 685 686 catch(java.io.IOException e) 687 { 688 throw new SQLException("Network error- " + e.getMessage()); 689 } 690 catch(com.internetcds.jdbc.tds.TdsException e) 691 { 692 throw new SQLException("TDS error- " + e.getMessage()); 693 } 694 } 695 696 697 698 protected void startResultSet() 699 throws SQLException 700 { 701 702 Columns names = null; 703 Columns info = null; 704 SQLException exception = null; 705 706 707 try 708 { 709 while (!tds.isResultRow() && !tds.isEndOfResults()) 710 { 711 PacketResult tmp = tds.processSubPacket(); 712 713 if (tmp.getPacketType() == TdsDefinitions.TDS_DONEINPROC) 714 { 715 } 717 else if (tmp instanceof PacketColumnNamesResult) 718 { 719 names = ((PacketColumnNamesResult)tmp).getColumnNames(); 720 } 721 else if (tmp instanceof PacketColumnInfoResult) 722 { 723 info = ((PacketColumnInfoResult)tmp).getColumnInfo(); 724 } 725 else if (tmp instanceof PacketColumnOrderResult) 726 { 727 } 730 else if (tmp instanceof PacketTabNameResult) 731 { 732 } 735 else if (tmp instanceof PacketControlResult) 736 { 737 } 740 else if (tmp instanceof PacketMsgResult) 741 { 742 exception = warningChain.addOrReturn((PacketMsgResult)tmp); 743 } 744 else if (tmp instanceof PacketUnknown) 745 { 746 } 748 else 749 { 750 throw new SQLException("Trying to get a result set. Found a " 751 + tmp.getClass().getName()); 752 } 753 } 754 755 if (exception != null) 756 { 757 throw exception; 758 } 759 else if (!tds.isResultRow() && !tds.isEndOfResults()) 760 { 761 throw new SQLException("Confused. Was expecting a result row. " 763 + "Got a 0x" + Integer.toHexString(tds.peek() & 0xff)); 764 } 765 766 if (info != null) 768 names.merge(info); 769 results = Constructors.newResultSet(tds, this, names); 770 } 771 catch(com.internetcds.jdbc.tds.TdsException e) 772 { 773 e.printStackTrace(); 774 throw new SQLException(e.getMessage()); 775 } 776 catch( java.io.IOException e) 777 { 778 e.printStackTrace(); 779 throw new SQLException(e.getMessage()); 780 } 781 } 782 783 784 785 786 788 789 807 public void setFetchDirection(int direction) throws SQLException 808 { 809 NotImplemented(); 810 } 811 812 813 827 public int getFetchDirection() throws SQLException 828 { 829 NotImplemented(); 830 return 0; 831 } 832 833 834 847 public void setFetchSize(int rows) throws SQLException 848 { 849 NotImplemented(); 850 } 851 852 853 866 public int getFetchSize() throws SQLException 867 { 868 NotImplemented(); 869 return 0; 870 } 871 872 873 878 public int getResultSetConcurrency() throws SQLException 879 { 880 NotImplemented(); 881 return 0; 882 } 883 884 885 890 public int getResultSetType() throws SQLException 891 { 892 NotImplemented(); 893 return 0; 894 } 895 896 897 907 public void addBatch( String sql ) throws SQLException 908 { 909 NotImplemented(); 910 } 911 912 921 public void clearBatch() throws SQLException 922 { 923 NotImplemented(); 924 } 925 926 927 939 public int[] executeBatch() throws SQLException 940 { 941 NotImplemented(); 942 return null; 943 } 944 945 946 954 public java.sql.Connection getConnection() throws SQLException 955 { 956 return connection; 957 } 958 959 960 961 965 public boolean getMoreResults(int current) throws SQLException { 966 throw new UnsupportedOperationException ("Statement.getMoreResults(int) unsupported"); 967 } 968 969 public ResultSet getGeneratedKeys() throws SQLException { 970 throw new UnsupportedOperationException ("Statement.getGeneratedKeys() unsupported"); 971 } 972 973 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 974 throw new UnsupportedOperationException ("Statement.executeUpdate(String,int) unsupported"); 975 } 976 977 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 978 throw new UnsupportedOperationException ("Statement.executeUpdate(String,int[]) unsupported"); 979 } 980 981 public int executeUpdate(String sql, String [] columnNames) throws SQLException { 982 throw new UnsupportedOperationException ("Statement.executeUpdate(String,String[]) unsupported"); 983 } 984 985 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 986 throw new UnsupportedOperationException ("Statement.execute(String,int) unsupported"); 987 } 988 989 public boolean execute(String sql, int[] columnIndexes) throws SQLException { 990 throw new UnsupportedOperationException ("Statement.execute(String,int[]) unsupported"); 991 } 992 993 public boolean execute(String sql, String [] columnNames) throws SQLException { 994 throw new UnsupportedOperationException ("Statement.execute(String,String[]) unsupported"); 995 } 996 997 public int getResultSetHoldability() throws SQLException { 998 throw new UnsupportedOperationException ("Statement.getResultSetHoldability() unsupported"); 999 } 1000 1001 1002 1003 1004 static public void main(String args[]) 1005 throws java.lang.ClassNotFoundException , 1006 java.lang.IllegalAccessException , 1007 java.lang.InstantiationException , 1008 SQLException 1009 { 1010 1011 String query = null; 1012 1013 String url = url = "" 1014 + "jdbc:freetds:" 1015 + "//" 1016 + "kap" 1017 + "/" 1018 + "pubs"; 1019 1020 Class.forName("com.internetcds.jdbc.tds.Driver").newInstance(); 1021 java.sql.Connection connection; 1022 connection = DriverManager.getConnection(url, 1023 "testuser", 1024 "password"); 1025 java.sql.Statement stmt = connection.createStatement(); 1026 1027 query = "" 1028 + "update titles " 1029 + " set price=price+1.00 " 1030 + " where title_id='MC3021' or title_id = 'BU1032' "; 1031 int count = stmt.executeUpdate(query); 1032 System.out.println("Updated " + count + " rows."); 1033 1034 query = 1035 "" 1036 +"select price, title_id, title, price*ytd_sales gross from titles" 1037 +" where title like 'The%'"; 1038 java.sql.ResultSet rs = stmt.executeQuery(query); 1039 1040 while(rs.next()) 1041 { 1042 float price = rs.getFloat("price"); 1043 if (rs.wasNull()) 1044 { 1045 System.out.println("price: null"); 1046 } 1047 else 1048 { 1049 System.out.println("price: " + price); 1050 } 1051 1052 String title_id = rs.getString("title_id"); 1053 String title = rs.getString("title"); 1054 float gross = rs.getFloat("gross"); 1055 1056 1057 System.out.println("id: " + title_id); 1058 System.out.println("name: " + title); 1059 System.out.println("gross: " + gross); 1060 System.out.println(""); 1061 } 1062 } 1063 1064 public boolean isClosed() { 1065 return isClosed; 1066 } 1067 1068} 1069 1070 1071 | Popular Tags |