1 35 36 package com.sqlmagic.tinysql; 37 38 39 import java.sql.SQLException ; 40 import java.sql.SQLWarning ; 41 import java.sql.ResultSet ; 42 import java.sql.Connection ; 43 import java.sql.ResultSetMetaData ; 44 import java.sql.Date ; 45 import java.util.*; 46 import java.math.*; 47 48 53 public class tinySQLPreparedStatement implements java.sql.PreparedStatement { 54 55 59 private String statementString; 60 64 private Vector substitute=(Vector)null; 65 69 private Vector tableList=new Vector(); 70 73 private String invalidIndex = (String )null; 74 78 83 private tinySQLConnection connection; 84 85 90 private tinySQLResultSet result; 91 96 private Vector actions=(Vector)null; 97 98 104 private int max_field_size = 0; 105 106 113 private int max_rows = 65536; 114 115 121 private int timeout = 0; 122 123 126 private int fetchsize = 4096; 127 130 private static boolean debug=false; 131 137 public tinySQLPreparedStatement(tinySQLConnection conn,String inputString) { 138 139 int nextQuestionMark,startAt; 140 connection = conn; 141 startAt = 0; 142 statementString = inputString; 143 while ( (nextQuestionMark=statementString.indexOf("?",startAt)) > -1 ) 144 { 145 if ( substitute == (Vector)null ) substitute = new Vector(); 146 substitute.addElement(new String ("")); 147 startAt = nextQuestionMark + 1; 148 } 149 invalidIndex = " is not in the range 1 to " 150 + Integer.toString(substitute.size()); 151 if ( debug ) System.out.println("Prepare statement has " + substitute.size() 152 + " parameters."); 153 154 } 155 156 165 public synchronized ResultSet executeQuery() 166 throws SQLException { 167 168 result = null; 173 174 if ( debug) System.out.println("executeQuery conn is " + connection.toString()); 178 return new tinySQLResultSet(connection.executetinySQL(this), this); 179 180 } 181 public synchronized ResultSet executeQuery(String sql) 182 throws SQLException { 183 184 result = null; 189 statementString = sql; 190 191 if ( debug) System.out.println("executeQuery conn is " + connection.toString()); 195 return new tinySQLResultSet(connection.executetinySQL(this), this); 196 197 } 198 199 208 public synchronized int executeUpdate(String sql) throws SQLException { 209 210 statementString = sql; 211 return connection.executetinyUpdate(this); 212 213 } 214 public synchronized int executeUpdate() throws SQLException { 215 216 return connection.executetinyUpdate(this); 217 218 } 219 220 230 public boolean execute() throws SQLException { 231 232 tsResultSet r; 235 236 r = connection.executetinySQL(this); 239 240 if( r == null ) { 245 result = null; 246 } else { 247 result = new tinySQLResultSet(r, this); 248 } 249 return (result != null); 250 251 } 252 public boolean execute(String sql) throws SQLException { 253 254 tsResultSet r; 257 statementString = sql; 258 259 r = connection.executetinySQL(this); 262 263 if( r == null ) { 268 result = null; 269 } else { 270 result = new tinySQLResultSet(r, this); 271 } 272 return (result != null); 273 274 } 275 276 279 public String getSQLString () 280 { 281 return statementString; 282 } 283 284 290 public void close() throws SQLException 291 { 292 int i; 293 tinySQLTable nextTable; 294 for ( i = 0; i < tableList.size(); i++ ) 295 { 296 nextTable = (tinySQLTable)tableList.elementAt(i); 297 if ( debug ) System.out.println("Closing " + nextTable.table); 298 nextTable.close(); 299 } 300 } 301 302 309 public ResultSet getResultSet() throws SQLException { 310 311 ResultSet r; 312 313 r = result; result = null; return r; } 317 318 325 public int getUpdateCount() throws SQLException { 326 return -1; 327 } 328 329 337 public boolean getMoreResults() throws SQLException { 338 339 return (result != null); 340 341 } 342 343 350 public int getMaxFieldSize() throws SQLException { 351 return max_field_size; 352 } 353 354 361 public void setMaxFieldSize(int max) throws SQLException { 362 max_field_size = max; 363 } 364 365 372 public int getMaxRows() throws SQLException { 373 return max_rows; 374 } 375 376 383 public void setMaxRows(int max) throws SQLException { 384 max_rows = max; 385 } 386 387 395 public void setEscapeProcessing(boolean enable) 396 throws SQLException { 397 throw new SQLException ("The tinySQL Driver doesn't " + 398 "support escape processing."); 399 } 400 401 409 public int getQueryTimeout() throws SQLException { 410 return timeout; 411 } 412 413 421 public void setQueryTimeout(int x) throws SQLException { 422 timeout = x; 423 } 424 425 432 public void cancel() { 433 } 434 435 442 public final SQLWarning getWarnings() throws SQLException { 443 return null; 444 } 445 446 452 public void clearWarnings() throws SQLException { 453 } 454 455 460 public void setCursorName(String unused) throws SQLException { 461 throw new SQLException ("tinySQL does not support cursors."); 462 } 463 464 466 467 485 public void setFetchDirection(int direction) throws SQLException { 486 throw new SQLException ("tinySQL does not support setFetchDirection."); 487 } 488 489 503 public int getFetchDirection() throws SQLException { 504 throw new SQLException ("tinySQL does not support getFetchDirection."); 505 } 506 507 520 public void setFetchSize(int rows) throws SQLException { 521 if ((rows <= 0) || (rows >= this.getMaxRows ())) 522 throw new SQLException ("Condition 0 <= rows <= this.getMaxRows() is not satisfied"); 523 524 fetchsize = rows; 525 } 526 527 540 public int getFetchSize() throws SQLException { 541 return fetchsize; 542 } 543 544 549 public int getResultSetConcurrency() throws SQLException { 550 throw new SQLException ("tinySQL does not support ResultSet concurrency."); 551 } 552 553 558 public int getResultSetType() throws SQLException { 559 throw new SQLException ("tinySQL does not support getResultSetType."); 560 } 561 562 572 public void addBatch() throws SQLException { 573 throw new SQLException ("tinySQL does not support addBatch."); 574 } 575 public void addBatch( String sql ) throws SQLException { 576 throw new SQLException ("tinySQL does not support addBatch."); 577 } 578 579 588 public void clearBatch() throws SQLException { 589 throw new SQLException ("tinySQL does not support clearBatch."); 590 } 591 592 604 public int[] executeBatch() throws SQLException { 605 throw new SQLException ("tinySQL does not support executeBatch."); 606 } 607 608 616 public Connection getConnection() throws SQLException { 617 throw new SQLException ("tinySQL does not support getConnection."); 618 } 619 622 public void setBoolean(int parameterIndex,boolean inputValue) 623 throws SQLException 624 { 625 if ( inputValue ) setString(parameterIndex,"TRUE"); 626 else setString(parameterIndex,"FALSE"); 627 } 628 public void setInt(int parameterIndex,int inputValue) 629 throws SQLException 630 { 631 setString(parameterIndex,Integer.toString(inputValue)); 632 } 633 public void setDouble(int parameterIndex,double inputValue) 634 throws SQLException 635 { 636 setString(parameterIndex,Double.toString(inputValue)); 637 } 638 public void setBigDecimal(int parameterIndex,BigDecimal inputValue) 639 throws SQLException 640 { 641 setString(parameterIndex,inputValue.toString()); 642 } 643 public void setDate(int parameterIndex,java.sql.Date inputValue, 644 java.util.Calendar inputCalendar) throws SQLException 645 { 646 setString(parameterIndex,inputValue.toString()); 647 } 648 public void setDate(int parameterIndex,java.sql.Date inputValue) 649 throws SQLException 650 { 651 setString(parameterIndex,inputValue.toString()); 652 } 653 public void setTime(int parameterIndex,java.sql.Time inputValue, 654 java.util.Calendar inputCalendar) throws SQLException 655 { 656 setString(parameterIndex,inputValue.toString()); 657 } 658 public void setTime(int parameterIndex,java.sql.Time inputValue) 659 throws SQLException 660 { 661 setString(parameterIndex,inputValue.toString()); 662 } 663 public void setTimestamp(int parameterIndex,java.sql.Timestamp inputValue, 664 java.util.Calendar inputCalendar ) throws SQLException 665 { 666 setString(parameterIndex,inputValue.toString()); 667 } 668 public void setTimestamp(int parameterIndex,java.sql.Timestamp inputValue) 669 throws SQLException 670 { 671 setString(parameterIndex,inputValue.toString()); 672 } 673 public void setAsciiStream(int parameterIndex, 674 java.io.InputStream inputValue,int streamLength) throws SQLException 675 { 676 setString(parameterIndex,inputValue.toString()); 677 } 678 public void setUnicodeStream(int parameterIndex, 679 java.io.InputStream inputValue,int streamLength) throws SQLException 680 { 681 setString(parameterIndex,inputValue.toString()); 682 } 683 public void setBinaryStream(int parameterIndex, 684 java.io.InputStream inputValue,int streamLength) throws SQLException 685 { 686 setString(parameterIndex,inputValue.toString()); 687 } 688 public void setCharacterStream(int parameterIndex, 689 java.io.Reader inputValue,int streamLength) throws SQLException 690 { 691 setString(parameterIndex,inputValue.toString()); 692 } 693 public void setRef(int parameterIndex,java.sql.Ref inputValue) 694 throws SQLException 695 { 696 setString(parameterIndex,inputValue.toString()); 697 } 698 public void setBlob(int parameterIndex,java.sql.Blob inputValue) 699 throws SQLException 700 { 701 setString(parameterIndex,inputValue.toString()); 702 } 703 public void setArray(int parameterIndex,java.sql.Array inputValue) 704 throws SQLException 705 { 706 setString(parameterIndex,inputValue.toString()); 707 } 708 public void setClob(int parameterIndex,java.sql.Clob inputValue) 709 throws SQLException 710 { 711 setString(parameterIndex,inputValue.toString()); 712 } 713 public void setByte(int parameterIndex,byte inputValue) 714 throws SQLException 715 { 716 setString(parameterIndex,Byte.toString(inputValue)); 717 } 718 public void setBytes(int parameterIndex,byte[] inputValue) 719 throws SQLException 720 { 721 setString(parameterIndex,Byte.toString(inputValue[0])); 722 } 723 public void setShort(int parameterIndex,short inputValue) 724 throws SQLException 725 { 726 setString(parameterIndex,Short.toString(inputValue)); 727 } 728 public void setFloat(int parameterIndex,float inputValue) 729 throws SQLException 730 { 731 setString(parameterIndex,Float.toString(inputValue)); 732 } 733 public void setLong(int parameterIndex,long inputValue) 734 throws SQLException 735 { 736 setString(parameterIndex,Long.toString(inputValue)); 737 } 738 public void setObject(int parameterIndex,Object inputValue) 739 throws SQLException 740 { 741 setObject(parameterIndex,inputValue,0,0); 742 } 743 public void setObject(int parameterIndex,Object inputValue, 744 int targetSQLType) throws SQLException 745 { 746 setObject(parameterIndex,inputValue,targetSQLType,0); 747 } 748 public void setObject(int parameterIndex,Object inputValue, 749 int targetSQLType, int scale) throws SQLException 750 { 751 setString(parameterIndex,inputValue.toString()); 752 } 753 public void setNull(int parameterIndex,int sqlType) 754 throws SQLException 755 { 756 setNull(parameterIndex,sqlType,(String )null); 757 } 758 public void setNull(int parameterIndex,int sqlType,String sqlTypeName) 759 throws SQLException 760 { 761 if ( parameterIndex > substitute.size() ) 762 throw new SQLException ("Parameter index " + parameterIndex 763 + invalidIndex); 764 substitute.setElementAt((String )null,parameterIndex-1); 765 } 766 public void setString( int parameterIndex, String setString) 767 throws SQLException 768 { 769 if ( parameterIndex > substitute.size() ) 770 throw new SQLException ("Parameter index " + parameterIndex 771 + invalidIndex); 772 substitute.setElementAt(setString,parameterIndex-1); 773 } 774 public void clearParameters() throws SQLException 775 { 776 substitute.removeAllElements(); 777 } 778 782 public void updateActions(Vector inputActions) throws SQLException 783 { 784 Vector values,originalValues; 785 Hashtable action; 786 String actionType,valueString; 787 int i,j,subCount; 788 if ( actions == (Vector)null ) 789 actions = inputActions; 790 if ( actions == (Vector)null ) return; 791 for ( i = 0; i < actions.size(); i++ ) 792 { 793 action = (Hashtable)actions.elementAt(i); 794 actionType = (String )action.get("TYPE"); 795 if ( actionType.equals("INSERT") | actionType.equals("UPDATE") ) 796 { 797 800 originalValues = (Vector)action.get("ORIGINAL_VALUES"); 801 values = (Vector)action.get("VALUES"); 802 if ( originalValues == (Vector)null ) 803 { 804 originalValues = (Vector)values.clone(); 805 action.put("ORIGINAL_VALUES",originalValues); 806 } 807 subCount = 0; 808 for ( j = 0; j < originalValues.size(); j++ ) 809 { 810 valueString = (String )originalValues.elementAt(j); 811 if ( valueString.equals("?") ) 812 { 813 if ( subCount > substitute.size() - 1 ) 814 throw new SQLException ("Substitution index " + subCount 815 + " not between 0 and " 816 + Integer.toString(substitute.size() - 1)); 817 values.setElementAt(substitute.elementAt(subCount),j); 818 subCount++; 819 } 820 } 821 } 822 } 823 } 824 public void addTable(tinySQLTable inputTable) 825 { 826 int i; 827 tinySQLTable nextTable; 828 for ( i = 0; i < tableList.size(); i++ ) 829 { 830 nextTable = (tinySQLTable)tableList.elementAt(i); 831 if ( nextTable.table.equals(inputTable.table) ) return; 832 } 833 tableList.addElement(inputTable); 834 } 835 836 837 public Vector getActions() 838 { 839 return actions; 840 } 841 public ResultSetMetaData getMetaData() 842 { 843 return (ResultSetMetaData )null; 844 } 845 846 } 847 | Popular Tags |