1 package org.webdocwf.util.xml; 2 3 import java.sql.PreparedStatement ; 4 import java.sql.ResultSet ; 5 import java.sql.SQLException ; 6 import java.math.BigDecimal ; 7 import java.sql.Date ; 8 import java.sql.Time ; 9 import java.sql.Timestamp ; 10 import java.io.InputStream ; 11 import java.io.Reader ; 12 import java.sql.Ref ; 13 import java.sql.Blob ; 14 import java.sql.Clob ; 15 import java.sql.Array ; 16 import java.sql.ResultSetMetaData ; 17 import java.util.Calendar ; 18 import java.net.URL ; 19 import java.sql.ParameterMetaData ; 20 import java.sql.SQLWarning ; 21 import java.sql.Connection ; 22 import java.sql.DriverManager ; 23 import java.io.File ; 24 import java.util.*; 25 26 31 32 public class XmlPreparedStatement implements PreparedStatement { 33 34 private XmlConnection connection; 35 private Vector resultSets = new Vector(); 36 private String sqlForPrepare = ""; 37 private String sqlPrepared = ""; 38 private int paramCount = 0; 39 private ArrayList parameters = new ArrayList(); 40 private ArrayList binaryStreamParameters = new ArrayList(); 41 private String fileName; 42 public static String PREPARE_SEPARATOR = "~@#NEXTPARAMETER#@~"; 43 44 public XmlPreparedStatement(XmlConnection connection, String preparedSql) { 45 DriverManager.println("XmlJdbc - XmlStatement() - connection=" + connection); 46 this.sqlForPrepare = preparedSql; 47 this.sqlPrepared = preparedSql; 48 this.paramCount = countParameters(preparedSql); 49 for(int i = 0; i < paramCount; i++) 50 parameters.add("null"); 51 this.connection = connection; 52 this.fileName = connection.getPath() + connection.getExtension(); 53 } 54 55 private int countParameters(String sql) { 56 int count = 0; 57 int index = sql.indexOf(PREPARE_SEPARATOR); 58 while(index != -1) { 59 count++; 60 sql = sql.substring(index + 1); 61 index = sql.indexOf(PREPARE_SEPARATOR); 62 } 63 return count; 64 } 65 66 private boolean prepareSql() throws SQLException { 67 boolean retVal = true; 68 69 for(int i = 0; i < parameters.size(); i++) { 70 int index = sqlPrepared.indexOf(PREPARE_SEPARATOR); 71 if(index != -1) { 72 sqlPrepared = sqlPrepared.substring(0, index) + 73 parameters.get(i) + 74 sqlPrepared.substring(index + PREPARE_SEPARATOR.length()); 75 } 76 } 77 78 if(sqlPrepared.indexOf(PREPARE_SEPARATOR) != -1) 79 throw new SQLException ("All ? in prepared query has to be replaced with values."); 80 else if(parameters.size() < this.paramCount) 81 throw new SQLException ( 82 "Number of setted parameters is less than number of parameters in statement."); 83 else if(parameters.size() > this.paramCount) 84 throw new SQLException ( 85 "Number of setted parameters is greater than number of parameters in statement."); 86 87 return retVal; 88 } 89 90 97 public void setMaxFieldSize(int p0) throws SQLException { 98 throw new SQLException ("Not Supported !"); 99 } 100 101 108 public void setMaxRows(int p0) throws SQLException { 109 throw new SQLException ("Not Supported !"); 110 } 111 112 119 public void setEscapeProcessing(boolean p0) throws SQLException { 120 throw new SQLException ("Not Supported !"); 121 } 122 123 130 public void setQueryTimeout(int p0) throws SQLException { 131 throw new SQLException ("Not Supported !"); 132 } 133 134 141 public void setCursorName(String p0) throws SQLException { 142 throw new SQLException ("Not Supported !"); 143 } 144 145 152 public void setFetchDirection(int p0) throws SQLException { 153 throw new SQLException ("Not Supported !"); 154 } 155 156 163 public void setFetchSize(int p0) throws SQLException { 164 throw new SQLException ("Not Supported !"); 165 } 166 167 174 public int getMaxFieldSize() throws SQLException { 175 throw new SQLException ("Not Supported !"); 176 } 177 178 185 public int getMaxRows() throws SQLException { 186 throw new SQLException ("Not Supported !"); 187 } 188 189 196 public int getQueryTimeout() throws SQLException { 197 throw new SQLException ("Not Supported !"); 198 } 199 200 207 public SQLWarning getWarnings() throws SQLException { 208 throw new SQLException ("Not Supported !"); 209 } 210 211 218 public ResultSet getResultSet() throws SQLException { 219 throw new SQLException ("Not Supported !"); 220 } 221 222 229 public int getUpdateCount() throws SQLException { 230 throw new SQLException ("Not Supported !"); 231 } 232 233 240 public boolean getMoreResults() throws SQLException { 241 throw new SQLException ("Not Supported !"); 242 } 243 244 251 public int getFetchDirection() throws SQLException { 252 throw new SQLException ("Not Supported !"); 253 } 254 255 262 public int getFetchSize() throws SQLException { 263 throw new SQLException ("Not Supported !"); 264 } 265 266 273 public int getResultSetConcurrency() throws SQLException { 274 throw new SQLException ("Not Supported !"); 275 } 276 277 284 public int getResultSetType() throws SQLException { 285 throw new SQLException ("Not Supported !"); 286 } 287 288 295 public Connection getConnection() throws SQLException { 296 return connection; 297 } 298 299 public ResultSet executeQuery(String sql) throws SQLException { 300 DriverManager.println("XmlJdbc - XmlStatement:executeQuery() - sql= " + sql); 301 XmlWriter writer; 302 if(this.connection.getAutoCommit()) 303 writer = new XmlWriter(fileName,true); 304 else 305 writer = new XmlWriter(fileName, false); 306 XmlSqlParser parser = new XmlSqlParser(this.fileName, this.connection.getAutoCommit() ); 307 if( binaryStreamParameters.size() != 0 ) 308 parser.setBinaryStreamList( binaryStreamParameters ); 309 try { 310 parser.parse(sql); 311 } 312 catch(Exception e) { 313 throw new SQLException ("Syntax Error. " + e.getMessage()); 314 } 315 if(parser.sqlType.equals(parser.DROP_TABLE)) { 316 execute(sql); 317 return null; 318 } 319 if(parser.sqlType.equals(parser.DELETE)) { 320 executeUpdate(sql); 321 return null; 322 } 323 if(parser.sqlType.equals(parser.UPDATE)) { 324 executeUpdate(sql); 325 return null; 326 } else if(parser.sqlType.equals(parser.INSERT)) { 327 executeUpdate(sql); 328 return null; 329 } else if(parser.sqlType.equals(parser.CREATE_TABLE)) { 330 execute(sql); 331 return null; 332 } else if(parser.sqlType.equals(parser.SELECT)) { 333 334 String fileName = connection.getPath() + connection.getExtension(); 335 File checkFile = new File (fileName); 336 XmlReader reader; 337 try { 338 reader = new XmlReader(fileName); 339 String [] xxx = parser.getColumnNames(); 340 String [] yyy = (String []) writer.getTableProperties(parser.getTableName()).get(0); 341 boolean isOK = true; 342 for(int i = 0; i < xxx.length; i++) { 343 out: 344 for(int j = 0; j < yyy.length; j++) { 345 if(xxx[i].equalsIgnoreCase(yyy[j])) { 346 isOK = true; 347 break out; 348 } else 349 isOK = false; 350 } 351 if(!isOK) 352 throw new SQLException ("Column '" + xxx[i] + "' not found in table " + 353 parser.getTableName()); 354 } 355 } 356 catch(Exception e) { 357 throw new SQLException ("Error reading data file. Message was: " + e); 358 } 359 XmlResultSet resultSet = new XmlResultSet(this, reader, 360 parser.getTableName(), parser.getColumnNames(), 361 parser.getWhereColumnNames(), parser.getWhereColumnValues()); 362 resultSet.select(); 363 return resultSet; 364 } else { 365 throw new SQLException ("Syntax error.Not supported sql statement."); 366 } 367 } 368 369 377 public int executeUpdate(String sql) throws SQLException { 378 DriverManager.println("XmlJdbc - XmlStatement:executeUpdate() - sql= " + sql); 379 XmlSqlParser parser = new XmlSqlParser(); 380 if( binaryStreamParameters.size() != 0 ) 381 parser.setBinaryStreamList( binaryStreamParameters ); 382 try { 383 parser.parse(sql); 384 } 385 catch(Exception e) { 386 throw new SQLException ("Syntax Error. " + e.getMessage()); 387 } 388 if(parser.sqlType.equals(parser.SELECT)) 389 throw new SQLException ("Not supported SELECT statement - use executeQuery() method"); 390 else if(parser.sqlType.equals(parser.CREATE_TABLE)) { 391 throw new SQLException ("Not supported CREATE_TABLE statement - use execute() method"); 392 } else if(parser.sqlType.equals(parser.DROP_TABLE)) { 393 throw new SQLException ("Not supported DROP_TABLE statement - use execute() method"); 394 } else if(parser.sqlType.equals(parser.INSERT)) { 395 String fileName = connection.getPath() + connection.getExtension(); 396 XmlWriter writeXml; 397 try { 398 if(this.connection.getAutoCommit()) 399 writeXml = new XmlWriter(fileName,true); 400 else 401 writeXml = new XmlWriter(fileName, false); 402 ArrayList properties = writeXml.getTableProperties(parser.tableName); 403 String [] xxx = parser.getColumnNames(); 405 String [] yyy = (String []) properties.get(0); 406 boolean isOK = true; 407 for(int i = 0; i < xxx.length; i++) { 408 if(!xxx[i].endsWith("*")) { 409 out: 410 for(int j = 0; j < yyy.length; j++) { 411 if(xxx[i].equalsIgnoreCase(yyy[j])) { 412 isOK = true; 413 break out; 414 } else 415 isOK = false; 416 } 417 if(!isOK) 418 throw new SQLException ("Column '" + xxx[i] + "' not found in table " + 419 parser.getTableName()); 420 } 421 } 422 if(!properties.get(1).toString().equalsIgnoreCase("NO CREATE TABLE")) { 424 yyy = parser.getColumnNames(); 425 xxx = (String []) writeXml.getTableProperties(parser.tableName).get(2); 426 isOK = true; 427 for(int i = 0; i < xxx.length; i++) { 428 out: 429 for(int j = 0; j < yyy.length; j++) { 430 if(xxx[i].equalsIgnoreCase(yyy[j])) { 431 isOK = true; 432 break out; 433 } else 434 isOK = false; 435 } 436 if(!isOK) 437 throw new SQLException ("Column '" + xxx[i] + "' can not be NULL."); 438 } 439 } 440 writeXml.insert(parser.getTableName(), parser.getColumnNames(), parser.getColumnValues()); 441 } 442 catch(Exception e) { 443 throw new SQLException ("Error reading data file. Message was: " + e); 444 } 445 446 } else if(parser.sqlType.equals(parser.UPDATE)) { 447 String fileName = connection.getPath() + connection.getExtension(); 448 XmlWriter writeXml; 449 try { 450 if(this.connection.getAutoCommit()) 451 writeXml = new XmlWriter(fileName,true); 452 else 453 writeXml = new XmlWriter(fileName, false); 454 String [] xxx = parser.getColumnNames(); 455 String [] yyy = (String []) writeXml.getTableProperties(parser.tableName).get(0); 456 boolean isOK = true; 457 for(int i = 0; i < xxx.length; i++) { 458 if(!xxx[i].endsWith("*")) { 459 out: 460 for(int j = 0; j < yyy.length; j++) { 461 if(xxx[i].equalsIgnoreCase(yyy[j])) { 462 isOK = true; 463 break out; 464 } else 465 isOK = false; 466 } 467 if(!isOK) 468 throw new SQLException ("Column '" + xxx[i] + "' not found in table " + 469 parser.getTableName()); 470 } 471 } 472 writeXml.update(parser.getTableName(), parser.getColumnNames(), parser.getColumnValues(), 473 parser.getWhereColumnNames(), parser.getWhereColumnValues()); 474 } 475 catch(Exception e) { 476 throw new SQLException ("Error reading data file. Message was: " + e); 477 } 478 479 } else if(parser.sqlType.equals(parser.DELETE)) { 480 String fileName = connection.getPath() + connection.getExtension(); 481 XmlWriter writeXml; 482 try { 483 if(this.connection.getAutoCommit()) 484 writeXml = new XmlWriter(fileName,true); 485 else 486 writeXml = new XmlWriter(fileName, false); 487 String [] xxx = parser.getWhereColumnNames(); 488 String [] yyy = (String []) writeXml.getTableProperties(parser.tableName).get(0); 489 boolean isOK = true; 490 for(int i = 0; i < xxx.length; i++) { 491 if(!xxx[i].endsWith("*")) { 492 out: 493 for(int j = 0; j < yyy.length; j++) { 494 if(xxx[i].equalsIgnoreCase(yyy[j])) { 495 isOK = true; 496 break out; 497 } else 498 isOK = false; 499 } 500 if(!isOK) 501 throw new SQLException ("Column '" + xxx[i] + "' not found in table " + 502 parser.getTableName()); 503 } 504 } 505 writeXml.delete(parser.getTableName(), parser.getWhereColumnNames(), 506 parser.getWhereColumnValues()); 507 } 508 catch(Exception e) { 509 throw new SQLException ("Error reading data file. Message was: " + e); 510 } 511 512 } else if(parser.sqlType.equals(parser.DROP_TABLE)) { 513 } 514 return 0; 515 516 } 517 518 536 public void close() throws SQLException { 537 } 539 540 546 public void cancel() throws SQLException { 547 throw new SQLException ("Not Supported !"); 548 } 549 550 556 public void clearWarnings() throws SQLException { 557 throw new SQLException ("Not Supported !"); 558 } 559 560 568 public boolean execute(String sql) throws SQLException { 569 XmlWriter writeXml; 570 if(this.connection.getAutoCommit()) 571 writeXml = new XmlWriter(fileName,true); 572 else 573 writeXml = new XmlWriter(fileName, false); 574 XmlSqlParser parser = new XmlSqlParser(this.fileName, this.connection.getAutoCommit() ); 575 if( binaryStreamParameters.size() != 0 ) 576 parser.setBinaryStreamList( binaryStreamParameters ); 577 try { 578 parser.parse(sql); 579 } 580 catch(Exception e) { 581 throw new SQLException ("Syntax Error. " + e.getMessage()); 582 } 583 if(parser.sqlType.equals(parser.SELECT)) 584 throw new SQLException ("Use executeQuery() for SELECT statement"); 585 else if(parser.sqlType.equals(parser.UPDATE)) 586 executeUpdate(sql); 587 else if(parser.sqlType.equals(parser.INSERT)) 588 executeUpdate(sql); 589 else if(parser.sqlType.equals(parser.DELETE)) 590 executeUpdate(sql); 591 else if(parser.sqlType.equals(parser.CREATE_TABLE)) { 592 String fileName = connection.getPath() + connection.getExtension(); 593 try { 594 writeXml.createTable(parser.getSqlStatement(), parser.getTableName()); 595 } 596 catch(Exception e) { 597 throw new SQLException ("Error reading data file. Message was: " + e); 598 } 599 } else if(parser.sqlType.equals(parser.DROP_TABLE)) { 600 String fileName = connection.getPath() + connection.getExtension(); 601 try { 602 writeXml.dropTable(parser.getTableName()); 603 } 604 catch(Exception e) { 605 throw new SQLException ("Error reading data file. Message was: " + e); 606 } 607 } 608 return true; 609 610 } 611 612 618 public void setString(int parameterIndex, String value) throws SQLException { 619 if( value == null ) 620 value = "null"; 621 value = Utils.replaceAll( value, "'", XmlSqlParser.quoteEscape); 622 623 XmlDriver.log("XmlPreparedStatement.setString(), value = "+value); 624 625 parameters.set(parameterIndex - 1, "'" + value + "'"); 626 } 627 628 634 public void setBinaryStream(int parameterIndex, InputStream value, 635 int length) throws SQLException { 636 throw new SQLException ("Not Supported !"); 637 } 638 639 645 public void setBytes(int parameterIndex, byte[] value) throws SQLException { 646 binaryStreamParameters.add(Utils.bytesToHexString(value)); 647 String paramName = XmlSqlParser.BINARY_STREAM_OBJECT+binaryStreamParameters.size(); 648 parameters.set(parameterIndex-1, paramName); 649 } 650 651 652 658 public void setBlob(int parameterIndex, Blob value) throws SQLException { 659 throw new SQLException ("Not Supported !"); 660 } 661 662 669 public void addBatch(String p0) throws SQLException { 670 throw new SQLException ("Not Supported !"); 671 } 672 673 679 public void clearBatch() throws SQLException { 680 throw new SQLException ("Not Supported !"); 681 } 682 683 690 public int[] executeBatch() throws SQLException { 691 throw new SQLException ("Not Supported !"); 692 } 693 694 698 public boolean getMoreResults(int current) throws SQLException { 699 throw new UnsupportedOperationException ("Statement.getMoreResults(int) unsupported"); 700 } 701 702 public ResultSet getGeneratedKeys() throws SQLException { 703 throw new UnsupportedOperationException ("Statement.getGeneratedKeys() unsupported"); 704 } 705 706 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 707 throw new UnsupportedOperationException ("Statement.executeUpdate(String,int) unsupported"); 708 } 709 710 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 711 throw new UnsupportedOperationException ("Statement.executeUpdate(String,int[]) unsupported"); 712 } 713 714 public int executeUpdate(String sql, String [] columnNames) throws SQLException { 715 throw new UnsupportedOperationException ("Statement.executeUpdate(String,String[]) unsupported"); 716 } 717 718 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 719 throw new UnsupportedOperationException ("Statement.execute(String,int) unsupported"); 720 } 721 722 public boolean execute(String sql, int[] columnIndexes) throws SQLException { 723 throw new UnsupportedOperationException ("Statement.execute(String,int[]) unsupported"); 724 } 725 726 public boolean execute(String sql, String [] columnNames) throws SQLException { 727 throw new UnsupportedOperationException ("Statement.execute(String,String[]) unsupported"); 728 } 729 730 public int getResultSetHoldability() throws SQLException { 731 throw new UnsupportedOperationException ("Statement.getResultSetHoldability() unsupported"); 732 } 733 734 public ResultSet executeQuery() throws SQLException { 735 if(!prepareSql()) 736 throw new SQLException ("Error with prepared statement !"); 737 return executeQuery(this.sqlPrepared); 738 739 } 740 741 public int executeUpdate() throws SQLException { 742 if(!prepareSql()) 743 throw new SQLException ("Error with prepared statement !"); 744 executeUpdate(this.sqlPrepared); 745 return 0; 746 } 747 748 public void setNull(int parameterIndex, int sqlType) throws SQLException { 749 this.setString(parameterIndex, "null" ); 750 } 751 752 public void setBoolean(int parameterIndex, boolean value) throws SQLException { 753 this.setString(parameterIndex, String.valueOf(value) ); 754 } 755 756 public void setByte(int parameterIndex, byte value) throws SQLException { 757 this.setString(parameterIndex, String.valueOf(value) ); 758 } 759 760 public void setShort(int parameterIndex, short value) throws SQLException { 761 this.setString(parameterIndex, String.valueOf(value) ); 762 } 763 764 public void setInt(int parameterIndex, int value) throws SQLException { 765 this.setString(parameterIndex, String.valueOf(value) ); 766 } 767 768 public void setLong(int parameterIndex, long value) throws SQLException { 769 this.setString(parameterIndex, String.valueOf(value) ); 770 } 771 772 public void setFloat(int parameterIndex, float value) throws SQLException { 773 this.setString(parameterIndex, String.valueOf(value) ); 774 } 775 776 public void setDouble(int parameterIndex, double value) throws SQLException { 777 this.setString(parameterIndex, String.valueOf(value) ); 778 } 779 780 public void setBigDecimal(int parameterIndex, BigDecimal value) throws SQLException { 781 if(value == null) { 782 this.setString( parameterIndex, value.toString() ); 783 } else { 784 this.setString( parameterIndex, "" ); 785 } 786 } 787 788 public void setDate(int parameterIndex, Date value) throws SQLException { 789 if(value == null) { 790 this.setString( parameterIndex, value.toString() ); 791 } else { 792 this.setString( parameterIndex, "" ); 793 } 794 } 795 796 public void setTime(int parameterIndex, Time value) throws SQLException { 797 if(value == null) { 798 this.setString( parameterIndex, value.toString() ); 799 } else { 800 this.setString( parameterIndex, "" ); 801 } 802 } 803 804 public void setTimestamp(int parameterIndex, Timestamp value) throws SQLException { 805 if(value == null) { 806 this.setString( parameterIndex, value.toString() ); 807 } else { 808 this.setString( parameterIndex, "" ); 809 } 810 } 811 812 public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException { 813 814 throw new java.lang.UnsupportedOperationException ( 815 "Method setAsciiStream() not yet implemented."); 816 } 817 818 public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException { 819 820 throw new java.lang.UnsupportedOperationException ( 821 "Method setUnicodeStream() not yet implemented."); 822 } 823 824 public void clearParameters() throws SQLException { 825 this.sqlPrepared = this.sqlForPrepare; 826 for(int i = 0; i < parameters.size(); i++) 828 parameters.set(i, null); 829 this.binaryStreamParameters.clear(); 830 } 831 832 public void setObject(int parameterIndex, Object x, int targetSqlType, 833 int scale) throws SQLException { 834 835 throw new java.lang.UnsupportedOperationException ("Method setObject() not yet implemented."); 836 } 837 838 public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { 839 840 throw new java.lang.UnsupportedOperationException ("Method setObject() not yet implemented."); 841 } 842 843 public void setObject(int parameterIndex, Object x) throws SQLException { 844 if(x == null) 845 x = new String ("null"); 846 setString(parameterIndex, x.toString()); 847 } 848 849 public boolean execute() throws SQLException { 850 851 throw new java.lang.UnsupportedOperationException ("Method execute() not yet implemented."); 852 } 853 854 public void addBatch() throws SQLException { 855 856 throw new java.lang.UnsupportedOperationException ("Method addBatch() not yet implemented."); 857 } 858 859 public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { 860 861 throw new java.lang.UnsupportedOperationException ( 862 "Method setCharacterStream() not yet implemented."); 863 } 864 865 public void setRef(int i, Ref x) throws SQLException { 866 867 throw new java.lang.UnsupportedOperationException ("Method setRef() not yet implemented."); 868 } 869 870 public void setClob(int i, Clob x) throws SQLException { 871 872 throw new java.lang.UnsupportedOperationException ("Method setClob() not yet implemented."); 873 } 874 875 public void setArray(int i, Array x) throws SQLException { 876 877 throw new java.lang.UnsupportedOperationException ("Method setArray() not yet implemented."); 878 } 879 880 public ResultSetMetaData getMetaData() throws SQLException { 881 882 throw new java.lang.UnsupportedOperationException ("Method getMetaData() not yet implemented."); 883 } 884 885 public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { 886 887 throw new java.lang.UnsupportedOperationException ("Method setDate() not yet implemented."); 888 } 889 890 public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { 891 892 throw new java.lang.UnsupportedOperationException ("Method setTime() not yet implemented."); 893 } 894 895 public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { 896 897 throw new java.lang.UnsupportedOperationException ("Method setTimestamp() not yet implemented."); 898 } 899 900 public void setNull(int paramIndex, int sqlType, String typeName) throws SQLException { 901 this.setString(paramIndex, "null" ); 902 } 903 904 public void setURL(int parameterIndex, URL x) throws SQLException { 905 906 throw new java.lang.UnsupportedOperationException ("Method setURL() not yet implemented."); 907 } 908 909 public ParameterMetaData getParameterMetaData() throws SQLException { 910 911 throw new java.lang.UnsupportedOperationException ( 912 "Method getParameterMetaData() not yet implemented."); 913 } 914 915 } | Popular Tags |