| 1 19 20 21 package org.webdocwf.util.i18njdbc; 22 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.InputStream ; 26 import java.io.Reader ; 27 import java.math.BigDecimal ; 28 import java.net.URL ; 29 import java.sql.Array ; 30 import java.sql.Blob ; 31 import java.sql.Clob ; 32 import java.sql.Connection ; 33 import java.sql.Date ; 34 import java.sql.DriverManager ; 35 import java.sql.ParameterMetaData ; 36 import java.sql.PreparedStatement ; 37 import java.sql.Ref ; 38 import java.sql.ResultSet ; 39 import java.sql.ResultSetMetaData ; 40 import java.sql.SQLException ; 41 import java.sql.SQLWarning ; 42 import java.sql.Time ; 43 import java.sql.Timestamp ; 44 import java.util.ArrayList ; 45 import java.util.Calendar ; 46 import java.util.Enumeration ; 47 import java.util.Vector ; 48 49 55 public class I18nPreparedStatement implements PreparedStatement { 56 57 private I18nConnection connection; 58 private Vector resultSets = new Vector (); 59 private String sqlForPrepare = ""; 60 private String sqlPrepared = ""; 61 private int paramCount = 0; 62 private ArrayList parameters = new ArrayList (); 63 private ArrayList binaryStreamParameters = new ArrayList (); 64 65 public static String PREPARE_SEPARATOR = "~@#NEXTPARAMETER#@~"; 66 private String sql; 67 68 public I18nPreparedStatement(I18nConnection connection, String preparedSql) { 69 DriverManager.println("I18nJdbc - I18nStatement() - connection=" + connection); 70 this.sqlForPrepare = preparedSql; 71 this.sqlPrepared = preparedSql; 72 this.paramCount = countParameters(preparedSql); 73 for ( int i = 0; i < paramCount; i++ ) 74 parameters.add(null); 75 this.connection = connection; 76 } 77 78 private int countParameters(String sql) { 79 int count = 0; 80 int index = sql.indexOf(PREPARE_SEPARATOR); 81 while( index != -1 ) { 82 count++; 83 sql = sql.substring(index+1); 84 index = sql.indexOf(PREPARE_SEPARATOR); 85 } 86 return count; 87 } 88 89 private boolean prepareSql() throws SQLException { 90 boolean retVal = true; 91 for (int i = 0; i < parameters.size(); i++) { 92 int index = sqlPrepared.indexOf(PREPARE_SEPARATOR); 93 String val; 94 if (index != -1) { 95 if( parameters.get(i) == null ) 96 val = "null"; 97 else 98 val = parameters.get(i).toString(); 99 sqlPrepared = sqlPrepared.substring(0, index) + 100 val + 101 sqlPrepared.substring(index + PREPARE_SEPARATOR.length()); 102 } 103 } 104 if (sqlPrepared.indexOf(PREPARE_SEPARATOR) != -1) 105 throw new SQLException ( 106 "All ? in prepared query has to be replaced with values."); 107 else if (parameters.size() < this.paramCount) 108 throw new SQLException ( 109 "Number of setted parameters is less than number of parameters in statement."); 110 else if (parameters.size() > this.paramCount) 111 throw new SQLException ( 112 "Number of setted parameters is greater than number of parameters in statement."); 113 return retVal; 114 } 115 116 123 public void setMaxFieldSize(int p0) throws SQLException  124 { 125 throw new SQLException ("Not Supported !"); 126 } 127 128 129 136 public void setMaxRows(int p0) throws SQLException  137 { 138 throw new SQLException ("Not Supported !"); 139 } 140 141 142 149 public void setEscapeProcessing(boolean p0) throws SQLException  150 { 151 throw new SQLException ("Not Supported !"); 152 } 153 154 155 162 public void setQueryTimeout(int p0) throws SQLException  163 { 164 throw new SQLException ("Not Supported !"); 165 } 166 167 168 175 public void setCursorName(String p0) throws SQLException  176 { 177 throw new SQLException ("Not Supported !"); 178 } 179 180 181 188 public void setFetchDirection(int p0) throws SQLException  189 { 190 throw new SQLException ("Not Supported !"); 191 } 192 193 194 201 public void setFetchSize(int p0) throws SQLException  202 { 203 throw new SQLException ("Not Supported !"); 204 } 205 206 207 214 public int getMaxFieldSize() throws SQLException  215 { 216 throw new SQLException ("Not Supported !"); 217 } 218 219 220 227 public int getMaxRows() throws SQLException  228 { 229 throw new SQLException ("Not Supported !"); 230 } 231 232 233 240 public int getQueryTimeout() throws SQLException  241 { 242 throw new SQLException ("Not Supported !"); 243 } 244 245 246 253 public SQLWarning getWarnings() throws SQLException  254 { 255 throw new SQLException ("Not Supported !"); 256 } 257 258 259 266 public ResultSet getResultSet() throws SQLException  267 { 268 throw new SQLException ("Not Supported !"); 269 } 270 271 272 279 public int getUpdateCount() throws SQLException  280 { 281 throw new SQLException ("Not Supported !"); 282 } 283 284 285 292 public boolean getMoreResults() throws SQLException  293 { 294 throw new SQLException ("Not Supported !"); 295 } 296 297 298 305 public int getFetchDirection() throws SQLException  306 { 307 throw new SQLException ("Not Supported !"); 308 } 309 310 311 318 public int getFetchSize() throws SQLException  319 { 320 throw new SQLException ("Not Supported !"); 321 } 322 323 324 331 public int getResultSetConcurrency() throws SQLException  332 { 333 throw new SQLException ("Not Supported !"); 334 } 335 336 337 344 public int getResultSetType() throws SQLException  345 { 346 throw new SQLException ("Not Supported !"); 347 } 348 349 350 357 public Connection getConnection() throws SQLException  358 { 359 return connection; 360 } 361 362 363 371 public ResultSet executeQuery(String sql) throws SQLException  372 { 373 DriverManager.println("I18nJdbc - I18nStatement:executeQuery() - sql= " + sql); 374 I18nSqlParser parser = new I18nSqlParser(); 375 this.sql = sql; 376 try 377 { 378 parser.parse(this); 379 } 380 catch (Exception e) 381 { 382 throw new SQLException ("Syntax Error. " + e.getMessage()); 383 } 384 385 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 386 File checkFile = new File (fileName); 387 if (!checkFile.exists()) 388 { 389 throw new SQLException ("Cannot open data file '" + fileName + "' !"); 390 } 391 392 if (!checkFile.canRead()) 393 { 394 throw new SQLException ("Data file '" + fileName + "' not readable !"); 395 } 396 397 try 398 { 399 400 String [] xxx = parser.getColumnNames(); 401 String [] yyy = connection.getColumnNames(); 402 boolean isOK = true; 403 for(int i=0; i< xxx.length; i++) { 404 if(!xxx[i].endsWith("*")) { 405 out: 406 for(int j=0; j< yyy.length; j++) { 407 if(xxx[i].equalsIgnoreCase(yyy[j])) { 408 isOK=true; 409 break out; 410 } 411 else 412 isOK=false; 413 } 414 if(!isOK) 415 throw new SQLException ("Column '" + xxx[i] + "' not found."); 416 } 417 } 418 } 419 catch (Exception e) 420 { 421 if( I18nDriver.DEBUG ) 422 e.printStackTrace(); 423 throw new SQLException ("Error reading data file. Message was: " + e); 424 } 425 426 this.connection.setCurrentTableName(parser.getTableName()); 427 428 try { 430 if(connection.getAutoCommit()) { 431 connection.getProperties().load(new FileInputStream (checkFile)); 432 } 433 } catch (Exception e) { 434 throw new SQLException ("Error while loading properties"); 435 } 436 437 I18nResultSet resultSet = new I18nResultSet(this, 438 parser.getTableName(), parser.getColumnNames(), parser.getWhereColumnNames(), 439 parser.getWhereColumnValues()); 440 resultSets.add(resultSet); 441 return resultSet; 442 } 443 444 445 446 454 public int executeUpdate(String sql) throws SQLException  455 { 456 int updated=0; 457 DriverManager.println("I18nJdbc - I18nStatement:executeUpdate() - sql= " + sql); 458 I18nSqlParser parser = new I18nSqlParser(); 459 this.sql = sql; 460 try 461 { 462 parser.parse(this); 463 } 464 catch (Exception e) 465 { 466 throw new SQLException ("Syntax Error. " + e.getMessage()); 467 } 468 if(parser.sqlType.equals(I18nSqlParser.SELECT)) 469 throw new SQLException ("Not supported SELECT statement - use executeQuery method"); 470 else if (parser.sqlType.equals(I18nSqlParser.CREATE_TABLE)) { 471 throw new SQLException ("Not supported CREATE TABLE statement - use execute method"); 472 } 473 else if (parser.sqlType.equals(I18nSqlParser.INSERT)) { 474 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 475 File checkFile = new File (fileName); 476 477 if (!checkFile.exists()) 478 { 479 throw new SQLException ("Cannot open data file '" + fileName + "' !"); 480 } 481 482 if (!checkFile.canWrite()) 483 { 484 throw new SQLException ("Data file '" + fileName + "' is read only !"); 485 } 486 try 487 { 488 String [] xxx = parser.getColumnNames(); 489 String [] yyy = connection.getColumnNames(); 490 boolean isOK = true; 491 for(int i=0; i< xxx.length; i++) { 492 if(!xxx[i].endsWith("*")) { 493 out: 494 for(int j=0; j< yyy.length; j++) { 495 if(xxx[i].equalsIgnoreCase(yyy[j])) { 496 isOK=true; 497 break out; 498 } 499 else 500 isOK=false; 501 } 502 if(!isOK) 503 throw new SQLException ("Column '" + xxx[i] + "' not found."); 504 } 505 } 506 try { 507 String [] colValues = parser.columnValues; 508 String [] colNames = parser.columnNames; 509 String keyColumn=connection.getNameColumn(); 510 String key; 511 String value; 512 if (colNames.length!=colValues.length){ 513 throw new Exception ("Number of columns and number of values are different!"); 514 } 515 if (colNames[0].equalsIgnoreCase(keyColumn)){ 516 key = colValues[0]; 517 value = colValues[1]; 518 }else{ 519 key = colValues[1]; 520 value = colValues[0]; 521 } 522 523 connection.setCurrentTableName(parser.getTableName()); 524 525 if(connection.getAutoCommit()) { 526 connection.getProperties().load(new FileInputStream (checkFile)); 527 } 528 if (connection.getProperties().containsKey(key)==true){ 529 throw new Exception ("Key already exist in the property file. Key must be unique!"); 530 }else{ 531 connection.getProperties().put(key,value); 532 if(connection.getAutoCommit()) { 533 connection.getProperties().store(checkFile); 534 } 535 updated++; 536 } 537 538 } catch (Exception e) { 539 throw new SQLException ("Error writing data to property file."+e.getMessage()); 540 } 541 } 542 catch (Exception e) 543 { 544 throw new SQLException ("Error writing data file. Message was: " + e); 545 } 546 547 } 548 else if (parser.sqlType.equals(I18nSqlParser.UPDATE)) { 549 550 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 551 File checkFile = new File (fileName); 552 553 if (!checkFile.exists()) 554 { 555 throw new SQLException ("Cannot open data file '" + fileName + "' !"); 556 } 557 558 if (!checkFile.canWrite()) 559 { 560 throw new SQLException ("Data file '" + fileName + "' is read only !"); 561 } 562 563 try 564 { 565 String [] xxx = parser.getColumnNames(); 566 String [] yyy = connection.getColumnNames(); 567 boolean isOK = true; 568 for(int i=0; i< xxx.length; i++) { 569 if(!xxx[i].endsWith("*")) { 570 out: 571 for(int j=0; j< yyy.length; j++) { 572 if(xxx[i].equalsIgnoreCase(yyy[j])) { 573 isOK=true; 574 break out; 575 } 576 else 577 isOK=false; 578 } 579 if(!isOK) 580 throw new SQLException ("Column '" + xxx[i] + "' not found."); 581 } 582 } 583 584 try { 585 String [] colValues = parser.columnValues; 586 String [] colNames = parser.columnNames; 587 String [] colWhereNames = parser.columnWhereNames; 588 String [] colWhereValues = parser.columnWhereValues; 589 String keyColumn=connection.getNameColumn(); 590 String valueColumn=connection.getValueColumn(); 591 String key = ""; 592 String value = ""; 593 boolean paramsOK = true; 594 if (colNames[0].equalsIgnoreCase(keyColumn)){ 597 if (colValues.length==1){ 598 key = colValues[0]; 599 }else if (colValues.length==2){ 600 key = colValues[0]; 601 value = colValues[1]; 602 } 603 }else if (colNames[0].equalsIgnoreCase(valueColumn)){ 606 if (colValues.length==1){ 607 value = colValues[0]; 608 }else if (colValues.length==2){ 609 value = colValues[0]; 610 key = colValues[1]; 611 } 612 } 613 614 connection.setCurrentTableName(parser.getTableName()); 615 616 if(connection.getAutoCommit()) { 617 connection.getProperties().load(new FileInputStream (checkFile)); 618 } 619 if (!(colWhereNames[0].equalsIgnoreCase(keyColumn) || colWhereNames[0].equalsIgnoreCase(valueColumn))){ 620 throw new SQLException ("Error in sql statement. Wrong column name!"); 621 } 622 if ((!key.equalsIgnoreCase("")) && (!value.equalsIgnoreCase(""))){ 623 Enumeration en = connection.getProperties().keys(); 624 while(en.hasMoreElements()){ 625 String oldKey = en.nextElement().toString(); 626 String oldValue = connection.getProperties().getProperty(oldKey); 627 if ((colWhereValues[0].equalsIgnoreCase(oldKey) && keyColumn.equals(colWhereNames[0])) 630 ||(colWhereValues[0].equalsIgnoreCase(oldValue) && valueColumn.equals(colWhereNames[0]))){ 631 connection.getProperties().remove(oldKey); 632 connection.getProperties().put(key, value); 633 updated++; 634 } 635 } 636 637 }else if (!value.equalsIgnoreCase("")){ 638 639 Enumeration en = connection.getProperties().keys(); 640 while(en.hasMoreElements()){ 641 String oldKey = en.nextElement().toString(); 642 String oldValue = connection.getProperties().getProperty(oldKey); 643 if ((colWhereValues[0].equalsIgnoreCase(oldKey) && keyColumn.equals(colWhereNames[0])) 646 ||(colWhereValues[0].equalsIgnoreCase(oldValue) && valueColumn.equals(colWhereNames[0]))){ 647 connection.getProperties().put(oldKey,value); 648 updated++; 649 } 650 } 651 }else if (!key.equalsIgnoreCase("")){ 652 653 Enumeration en = connection.getProperties().keys(); 654 while(en.hasMoreElements()){ 655 String oldKey = en.nextElement().toString(); 656 String oldValue = connection.getProperties().getProperty(oldKey); 657 if ((colWhereValues[0].equalsIgnoreCase(oldKey) && keyColumn.equals(colWhereNames[0])) 660 &nbs
|