1 19 20 package org.webdocwf.util.i18njdbc; 21 22 import java.sql.*; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.util.Enumeration ; 26 27 import java.util.Vector ; 28 29 30 31 32 38 39 public class I18nStatement implements Statement 40 { 41 42 private I18nConnection connection; 43 private Vector resultSets = new Vector (); 44 private String sql; 45 46 52 protected I18nStatement(I18nConnection connection) 53 { 54 try { 55 DriverManager.println("I18nJdbc - I18Statement() - connection=" + connection); 56 this.connection = connection; 57 } 58 catch(Exception ex) { 59 ex.printStackTrace(); 60 } 61 62 } 63 64 65 72 public void setMaxFieldSize(int p0) throws SQLException 73 { 74 throw new SQLException("Not Supported !"); 75 } 76 77 78 85 public void setMaxRows(int p0) throws SQLException 86 { 87 throw new SQLException("Not Supported !"); 88 } 89 90 91 98 public void setEscapeProcessing(boolean p0) throws SQLException 99 { 100 throw new SQLException("Not Supported !"); 101 } 102 103 104 111 public void setQueryTimeout(int p0) throws SQLException 112 { 113 throw new SQLException("Not Supported !"); 114 } 115 116 117 124 public void setCursorName(String p0) throws SQLException 125 { 126 throw new SQLException("Not Supported !"); 127 } 128 129 130 137 public void setFetchDirection(int p0) throws SQLException 138 { 139 throw new SQLException("Not Supported !"); 140 } 141 142 143 150 public void setFetchSize(int p0) throws SQLException 151 { 152 throw new SQLException("Not Supported !"); 153 } 154 155 156 163 public int getMaxFieldSize() throws SQLException 164 { 165 throw new SQLException("Not Supported !"); 166 } 167 168 169 176 public int getMaxRows() throws SQLException 177 { 178 throw new SQLException("Not Supported !"); 179 } 180 181 182 189 public int getQueryTimeout() throws SQLException 190 { 191 throw new SQLException("Not Supported !"); 192 } 193 194 195 202 public SQLWarning getWarnings() throws SQLException 203 { 204 throw new SQLException("Not Supported !"); 205 } 206 207 208 215 public ResultSet getResultSet() throws SQLException 216 { 217 throw new SQLException("Not Supported !"); 218 } 219 220 221 228 public int getUpdateCount() throws SQLException 229 { 230 throw new SQLException("Not Supported !"); 231 } 232 233 234 241 public boolean getMoreResults() throws SQLException 242 { 243 throw new SQLException("Not Supported !"); 244 } 245 246 247 254 public int getFetchDirection() throws SQLException 255 { 256 throw new SQLException("Not Supported !"); 257 } 258 259 260 267 public int getFetchSize() throws SQLException 268 { 269 throw new SQLException("Not Supported !"); 270 } 271 272 273 280 public int getResultSetConcurrency() throws SQLException 281 { 282 throw new SQLException("Not Supported !"); 283 } 284 285 286 293 public int getResultSetType() throws SQLException 294 { 295 throw new SQLException("Not Supported !"); 296 } 297 298 299 306 public Connection getConnection() throws SQLException 307 { 308 return connection; 309 } 310 311 312 320 public ResultSet executeQuery(String sql) throws SQLException 321 { 322 DriverManager.println("I18nJdbc - I18nStatement:executeQuery() - sql= " + sql); 323 I18nSqlParser parser = new I18nSqlParser(); 324 this.sql = sql; 325 try 326 { 327 parser.parse(this); 328 } 329 catch (Exception e) 330 { 331 throw new SQLException("Syntax Error. " + e.getMessage()); 332 } 333 334 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 335 File checkFile = new File (fileName); 336 if (!checkFile.exists()) 337 { 338 throw new SQLException("Cannot open data file '" + fileName + "' !"); 339 } 340 341 if (!checkFile.canRead()) 342 { 343 throw new SQLException("Data file '" + fileName + "' not readable !"); 344 } 345 346 try 347 { 348 349 String [] xxx = parser.getColumnNames(); 350 String [] yyy = connection.getColumnNames(); 351 boolean isOK = true; 352 for(int i=0; i< xxx.length; i++) { 353 if(!xxx[i].endsWith("*")) { 354 out: 355 for(int j=0; j< yyy.length; j++) { 356 if(xxx[i].equalsIgnoreCase(yyy[j])) { 357 isOK=true; 358 break out; 359 } 360 else 361 isOK=false; 362 } 363 if(!isOK) 364 throw new SQLException("Column '" + xxx[i] + "' not found."); 365 } 366 } 367 } 368 catch (Exception e) 369 { 370 if( I18nDriver.DEBUG ) 371 e.printStackTrace(); 372 throw new SQLException("Error reading data file. Message was: " + e); 373 } 374 375 this.connection.setCurrentTableName(parser.getTableName()); 376 377 try { 379 if(this.connection.getAutoCommit()) { 380 this.connection.getProperties().load(new FileInputStream (checkFile)); 381 } 382 } catch (Exception e) { 383 throw new SQLException("Error while loading properties"); 384 } 385 386 I18nResultSet resultSet = new I18nResultSet(this, 387 parser.getTableName(), parser.getColumnNames(), parser.getWhereColumnNames(), 388 parser.getWhereColumnValues()); 389 resultSets.add(resultSet); 390 return resultSet; 391 } 392 393 394 395 403 public int executeUpdate(String sql) throws SQLException 404 { 405 int updated=0; 406 DriverManager.println("I18nJdbc - I18nStatement:executeUpdate() - sql= " + sql); 407 I18nSqlParser parser = new I18nSqlParser(); 408 this.sql = sql; 409 try 410 { 411 parser.parse(this); 412 } 413 catch (Exception e) 414 { 415 throw new SQLException("Syntax Error. " + e.getMessage()); 416 } 417 if(parser.sqlType.equals(I18nSqlParser.SELECT)) 418 throw new SQLException("Not supported SELECT statement - use executeQuery method"); 419 else if (parser.sqlType.equals(I18nSqlParser.CREATE_TABLE)) { 420 throw new SQLException("Not supported CREATE TABLE statement - use execute method"); 421 } 422 else if (parser.sqlType.equals(I18nSqlParser.INSERT)) { 423 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 424 File checkFile = new File (fileName); 425 426 if (!checkFile.exists()) 427 { 428 throw new SQLException("Cannot open data file '" + fileName + "' !"); 429 } 430 431 if (!checkFile.canWrite()) 432 { 433 throw new SQLException("Data file '" + fileName + "' is read only !"); 434 } 435 try 436 { 437 String [] xxx = parser.getColumnNames(); 438 String [] yyy = connection.getColumnNames(); 439 boolean isOK = true; 440 for(int i=0; i< xxx.length; i++) { 441 if(!xxx[i].endsWith("*")) { 442 out: 443 for(int j=0; j< yyy.length; j++) { 444 if(xxx[i].equalsIgnoreCase(yyy[j])) { 445 isOK=true; 446 break out; 447 } 448 else 449 isOK=false; 450 } 451 if(!isOK) 452 throw new SQLException("Column '" + xxx[i] + "' not found."); 453 } 454 } 455 try { 456 String [] colValues = parser.columnValues; 457 String [] colNames = parser.columnNames; 458 String keyColumn=connection.getNameColumn(); 459 String key; 460 String value; 461 if (colNames.length!=colValues.length){ 462 throw new Exception ("Number of columns and number of values are different!"); 463 } 464 465 if (colNames[0].equalsIgnoreCase(keyColumn)){ 466 key = colValues[0]; 467 value = colValues[1]; 468 }else{ 469 key = colValues[1]; 470 value = colValues[0]; 471 } 472 473 this.connection.setCurrentTableName(parser.getTableName()); 474 475 if(this.connection.getAutoCommit()) { 476 this.connection.getProperties().load(new FileInputStream (checkFile)); 477 } 478 if (this.connection.getProperties().containsKey(key)==true){ 479 throw new Exception ("Key already exist in the property file. Key must be unique!"); 480 }else{ 481 this.connection.getProperties().put(key,value); 482 if(this.connection.getAutoCommit()) { 483 this.connection.getProperties().store(checkFile); 484 } 485 updated++; 486 } 487 488 } catch (Exception e) { 489 throw new SQLException("Error writing data to property file."+e.getMessage()); 490 } 491 } 492 catch (Exception e) 493 { 494 throw new SQLException("Error writing data file. Message was: " + e); 495 } 496 497 } 498 else if (parser.sqlType.equals(I18nSqlParser.UPDATE)) { 499 500 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 501 File checkFile = new File (fileName); 502 503 if (!checkFile.exists()) 504 { 505 throw new SQLException("Cannot open data file '" + fileName + "' !"); 506 } 507 508 if (!checkFile.canWrite()) 509 { 510 throw new SQLException("Data file '" + fileName + "' is read only !"); 511 } 512 513 try 514 { 515 String [] xxx = parser.getColumnNames(); 516 String [] yyy = connection.getColumnNames(); 517 boolean isOK = true; 518 for(int i=0; i< xxx.length; i++) { 519 if(!xxx[i].endsWith("*")) { 520 out: 521 for(int j=0; j< yyy.length; j++) { 522 if(xxx[i].equalsIgnoreCase(yyy[j])) { 523 isOK=true; 524 break out; 525 } 526 else 527 isOK=false; 528 } 529 if(!isOK) 530 throw new SQLException("Column '" + xxx[i] + "' not found."); 531 } 532 } 533 534 try { 535 String [] colValues = parser.columnValues; 536 String [] colNames = parser.columnNames; 537 String [] colWhereNames = parser.columnWhereNames; 538 String [] colWhereValues = parser.columnWhereValues; 539 String keyColumn=connection.getNameColumn(); 540 String valueColumn=connection.getValueColumn(); 541 String key = ""; 542 String value = ""; 543 boolean paramsOK = true; 544 if (colNames[0].equalsIgnoreCase(keyColumn)){ 547 if (colValues.length==1){ 548 key = colValues[0]; 549 }else if (colValues.length==2){ 550 key = colValues[0]; 551 value = colValues[1]; 552 } 553 }else if (colNames[0].equalsIgnoreCase(valueColumn)){ 556 if (colValues.length==1){ 557 value = colValues[0]; 558 }else if (colValues.length==2){ 559 value = colValues[0]; 560 key = colValues[1]; 561 562 } 563 564 } 565 this.connection.setCurrentTableName(parser.getTableName()); 567 if(this.connection.getAutoCommit()) { 568 this.connection.getProperties().load(new FileInputStream (checkFile)); 569 } 570 if (!(colWhereNames[0].equalsIgnoreCase(keyColumn) || colWhereNames[0].equalsIgnoreCase(valueColumn))){ 572 throw new SQLException("Error in sql statement. Wrong column name!"); 573 } 574 if ((!key.equalsIgnoreCase("")) && (!value.equalsIgnoreCase(""))){ 575 Enumeration en = this.connection.getProperties().keys(); 576 while(en.hasMoreElements()){ 577 String oldKey = en.nextElement().toString(); 578 String oldValue = this.connection.getProperties().getProperty(oldKey); 579 if ((colWhereValues[0].equalsIgnoreCase(oldKey) && keyColumn.equals(colWhereNames[0])) 582 ||(colWhereValues[0].equalsIgnoreCase(oldValue) && valueColumn.equals(colWhereNames[0]))){ 583 this.connection.getProperties().remove(oldKey); 584 this.connection.getProperties().put(key, value); 585 updated++; 586 } 587 } 588 589 }else if (!value.equalsIgnoreCase("")){ 590 591 Enumeration en = this.connection.getProperties().keys(); 592 while(en.hasMoreElements()){ 593 String oldKey = en.nextElement().toString(); 594 String oldValue = this.connection.getProperties().getProperty(oldKey); 595 if ((colWhereValues[0].equalsIgnoreCase(oldKey) && keyColumn.equals(colWhereNames[0])) 598 ||(colWhereValues[0].equalsIgnoreCase(oldValue) && valueColumn.equals(colWhereNames[0]))){ 599 this.connection.getProperties().put(oldKey,value); 600 updated++; 601 } 602 } 603 }else if (!key.equalsIgnoreCase("")){ 604 605 Enumeration en = this.connection.getProperties().keys(); 606 while(en.hasMoreElements()){ 607 String oldKey = en.nextElement().toString(); 608 String oldValue = this.connection.getProperties().getProperty(oldKey); 609 612 if ((colWhereValues[0].equalsIgnoreCase(oldKey) && keyColumn.equals(colWhereNames[0])) 613 ||(colWhereValues[0].equalsIgnoreCase(oldValue) && valueColumn.equals(colWhereNames[0]))){ 614 this.connection.getProperties().remove(oldKey); 615 this.connection.getProperties().put(key, oldValue); 616 updated++; 617 } 618 } 619 } 620 if(this.connection.getAutoCommit()) { 621 this.connection.getProperties().store(checkFile); 622 } 623 624 } catch (Exception e) { 625 throw new SQLException("Error writing data to property file."+e.getMessage()); 626 } 627 628 } 629 catch (Exception e) 630 { 631 throw new SQLException("Error writing data file. Message was: " + e); 632 } 633 634 }else if (parser.sqlType.equals(I18nSqlParser.DELETE)){ 635 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 636 File checkFile = new File (fileName); 637 638 if (!checkFile.exists()) 639 { 640 throw new SQLException("Cannot open data file '" + fileName + "' !"); 641 } 642 643 if (!checkFile.canWrite()) 644 { 645 throw new SQLException("Data file '" + fileName + "' is read only !"); 646 } 647 try 648 { 649 650 651 boolean deleteAll=false; 652 String [] colWhereNames = parser.columnWhereNames; 653 String [] colWhereValues = parser.columnWhereValues; 654 String keyColumn=connection.getNameColumn(); 655 String valueColumn=connection.getValueColumn(); 656 657 this.connection.setCurrentTableName(parser.getTableName()); 658 if(this.connection.getAutoCommit()) { 659 this.connection.getProperties().load(new FileInputStream (checkFile)); 660 } 661 if (!(colWhereNames[0].equalsIgnoreCase(keyColumn) || colWhereNames[0].equalsIgnoreCase(valueColumn))){ 662 throw new SQLException("Error in sql statement. Wrong column name!"); 663 } 664 Enumeration en = this.connection.getProperties().keys(); 665 if (colWhereNames.length==0){ 666 deleteAll=true; 667 } 668 if (!deleteAll){ 669 while(en.hasMoreElements()){ 670 String oldKey = en.nextElement().toString(); 671 String oldValue = this.connection.getProperties().getProperty(oldKey); 672 673 if ((colWhereValues[0].equalsIgnoreCase(oldKey) && keyColumn.equals(colWhereNames[0])) 674 ||(colWhereValues[0].equalsIgnoreCase(oldValue) && valueColumn.equals(colWhereNames[0]))){ 675 this.connection.getProperties().remove(oldKey); 676 if(this.connection.getAutoCommit()) { 677 this.connection.getProperties().store(checkFile); 678 } 679 updated++; 680 } 681 682 } 683 }else{ 684 this.connection.getProperties().clear(); 685 if(this.connection.getAutoCommit()) { 686 this.connection.getProperties().store(checkFile); 687 } 688 } 689 } catch (Exception e) { 690 throw new SQLException("Error deleting data from property file."+e.getMessage()); 691 } 692 693 } 694 return updated; 695 696 } 697 698 699 717 public void close() throws SQLException { 718 for(Enumeration i = resultSets.elements(); i.hasMoreElements(); ) { 720 I18nResultSet resultSet = (I18nResultSet) i.nextElement(); 721 resultSet.close(); 722 } 723 724 725 } 726 727 733 public void cancel() throws SQLException 734 { 735 throw new SQLException("Not Supported !"); 736 } 737 738 739 745 public void clearWarnings() throws SQLException 746 { 747 throw new SQLException("Not Supported !"); 748 } 749 750 751 759 public boolean execute(String sql) throws SQLException 760 { 761 762 I18nSqlParser parser = new I18nSqlParser(); 763 this.sql = sql; 764 try 765 { 766 parser.parse(this); 767 } 768 catch (Exception e) 769 { 770 throw new SQLException("Syntax Error. " + e.getMessage()); 771 } 772 if(parser.sqlType.equals(I18nSqlParser.SELECT)) 773 throw new SQLException("Not supported SELECT statement - use executeQuery method"); 774 else if (parser.sqlType.equals(I18nSqlParser.INSERT)) 775 executeUpdate(sql); 776 else if (parser.sqlType.equals(I18nSqlParser.CREATE_TABLE)) { 777 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 778 File checkFile = new File (fileName); 779 780 if (checkFile.exists()) 781 { 782 throw new SQLException("Data file '" + fileName + "'already exists !"); 783 } 784 785 try { 786 checkFile.createNewFile(); 787 788 } 789 catch (Exception e) 790 { 791 throw new SQLException("Error reading data file. Message was: " + e); 792 } 793 } 794 return true; 795 796 } 797 798 799 800 807 public void addBatch(String p0) throws SQLException 808 { 809 throw new SQLException("Not Supported !"); 810 } 811 812 813 819 public void clearBatch() throws SQLException 820 { 821 throw new SQLException("Not Supported !"); 822 } 823 824 825 832 public int[] executeBatch() throws SQLException 833 { 834 throw new SQLException("Not Supported !"); 835 } 836 837 841 public boolean getMoreResults(int current) throws SQLException { 842 throw new UnsupportedOperationException ("Statement.getMoreResults(int) unsupported"); 843 } 844 845 public ResultSet getGeneratedKeys() throws SQLException { 846 throw new UnsupportedOperationException ("Statement.getGeneratedKeys() unsupported"); 847 } 848 849 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 850 throw new UnsupportedOperationException ("Statement.executeUpdate(String,int) unsupported"); 851 } 852 853 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 854 throw new UnsupportedOperationException ("Statement.executeUpdate(String,int[]) unsupported"); 855 } 856 857 public int executeUpdate(String sql, String [] columnNames) throws SQLException { 858 throw new UnsupportedOperationException ("Statement.executeUpdate(String,String[]) unsupported"); 859 } 860 861 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 862 throw new UnsupportedOperationException ("Statement.execute(String,int) unsupported"); 863 } 864 865 public boolean execute(String sql, int[] columnIndexes) throws SQLException { 866 throw new UnsupportedOperationException ("Statement.execute(String,int[]) unsupported"); 867 } 868 869 public boolean execute(String sql, String [] columnNames) throws SQLException { 870 throw new UnsupportedOperationException ("Statement.execute(String,String[]) unsupported"); 871 } 872 873 public int getResultSetHoldability() throws SQLException { 874 throw new UnsupportedOperationException ("Statement.getResultSetHoldability() unsupported"); 875 } 876 880 public String getSqlStatement() { 881 return sql; 882 } 883 887 public I18nProperties getProperties() { 888 return this.connection.getProperties(); 889 } 890 891 } 892 893 | Popular Tags |