1 19 20 package org.relique.jdbc.csv; 21 22 import java.sql.*; 23 import java.io.File ; 24 import java.util.Enumeration ; 25 import java.util.Vector ; 26 27 32 33 public class CsvStatement implements Statement 34 { 35 private CsvConnection connection; 36 private Vector resultSets = new Vector (); 37 private CsvWriter writeCsv; 38 private String sql; 39 40 46 protected CsvStatement(CsvConnection connection) 47 { 48 DriverManager.println("CsvJdbc - CsvStatement() - connection=" + connection); 49 this.connection = connection; 50 51 try { 52 if(!connection.getAutoCommit()) 53 writeCsv = new CsvWriter( 54 null, 55 connection.getSeperator(), 56 connection.getExtension(), 57 connection.getMaxFileSize(), 58 connection.getCharset(), 59 connection.getUseQuotes(), 60 connection.getUseQuotesEscape() 61 ); 62 } 63 catch(Exception ex) { 64 ex.printStackTrace(); 65 } 66 67 } 68 69 70 77 public void setMaxFieldSize(int p0) throws SQLException 78 { 79 throw new SQLException("Not Supported !"); 80 } 81 82 83 90 public void setMaxRows(int p0) throws SQLException 91 { 92 throw new SQLException("Not Supported !"); 93 } 94 95 96 103 public void setEscapeProcessing(boolean p0) throws SQLException 104 { 105 throw new SQLException("Not Supported !"); 106 } 107 108 109 116 public void setQueryTimeout(int p0) throws SQLException 117 { 118 throw new SQLException("Not Supported !"); 119 } 120 121 122 129 public void setCursorName(String p0) throws SQLException 130 { 131 throw new SQLException("Not Supported !"); 132 } 133 134 135 142 public void setFetchDirection(int p0) throws SQLException 143 { 144 throw new SQLException("Not Supported !"); 145 } 146 147 148 155 public void setFetchSize(int p0) throws SQLException 156 { 157 throw new SQLException("Not Supported !"); 158 } 159 160 161 168 public int getMaxFieldSize() throws SQLException 169 { 170 throw new SQLException("Not Supported !"); 171 } 172 173 174 181 public int getMaxRows() throws SQLException 182 { 183 throw new SQLException("Not Supported !"); 184 } 185 186 187 194 public int getQueryTimeout() throws SQLException 195 { 196 throw new SQLException("Not Supported !"); 197 } 198 199 200 207 public SQLWarning getWarnings() throws SQLException 208 { 209 throw new SQLException("Not Supported !"); 210 } 211 212 213 220 public ResultSet getResultSet() throws SQLException 221 { 222 throw new SQLException("Not Supported !"); 223 } 224 225 226 233 public int getUpdateCount() throws SQLException 234 { 235 throw new SQLException("Not Supported !"); 236 } 237 238 239 246 public boolean getMoreResults() throws SQLException 247 { 248 throw new SQLException("Not Supported !"); 249 } 250 251 252 259 public int getFetchDirection() throws SQLException 260 { 261 throw new SQLException("Not Supported !"); 262 } 263 264 265 272 public int getFetchSize() throws SQLException 273 { 274 throw new SQLException("Not Supported !"); 275 } 276 277 278 285 public int getResultSetConcurrency() throws SQLException 286 { 287 throw new SQLException("Not Supported !"); 288 } 289 290 291 298 public int getResultSetType() throws SQLException 299 { 300 throw new SQLException("Not Supported !"); 301 } 302 303 304 311 public Connection getConnection() throws SQLException 312 { 313 return connection; 314 } 315 316 317 325 public ResultSet executeQuery(String sql) throws SQLException 326 { 327 DriverManager.println("CsvJdbc - CsvStatement:executeQuery() - sql= " + sql); 328 CsvSqlParser parser = new CsvSqlParser(); 329 this.sql = sql; 330 try 331 { 332 parser.parse(this); 333 } 334 catch (Exception e) 335 { 336 throw new SQLException("Syntax Error. " + e.getMessage()); 337 } 338 339 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 340 File checkFile = new File (fileName); 341 if (!checkFile.exists()) 342 { 343 throw new SQLException("Cannot open data file '" + fileName + "' !"); 344 } 345 346 if (!checkFile.canRead()) 347 { 348 throw new SQLException("Data file '" + fileName + "' not readable !"); 349 } 350 CsvReader reader; 351 try 352 { 353 reader = new CsvReader(fileName, connection.getSeperator(), 354 connection.isSuppressHeaders(), 355 connection.getCharset(), 356 connection.getExtension(), 357 connection.getLineBreakEscape(), 358 connection.getCarriageReturnEscape() 360 ); 361 String [] xxx = parser.getColumnNames(); 362 String [] yyy = reader.getColumnNames(); 363 boolean isOK = true; 364 for(int i=0; i< xxx.length; i++) { 365 if(!xxx[i].endsWith("*")) { 366 out: 367 for(int j=0; j< yyy.length; j++) { 368 if(xxx[i].equalsIgnoreCase(yyy[j])) { 369 isOK=true; 370 break out; 371 } 372 else 373 isOK=false; 374 } 375 if(!isOK) 376 throw new SQLException("Column '" + xxx[i] + "' not found."); 377 } 378 } 379 } 380 catch (Exception e) 381 { 382 e.printStackTrace(); 384 throw new SQLException("Error reading data file. Message was: " + e); 385 } 386 CsvResultSet resultSet = new CsvResultSet(this, reader, 387 parser.getTableName(), parser.getColumnNames(), parser.getWhereColumnNames(), 388 parser.getWhereColumnValues(),reader.getColumnTypes()); 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("CsvJdbc - CsvStatement:executeUpdate() - sql= " + sql); 407 CsvSqlParser parser = new CsvSqlParser(); 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(parser.SELECT)) 418 throw new SQLException("Not supported SELECT statement - use executeQuery method"); 419 else if (parser.sqlType.equals(parser.CREATE_TABLE)) { 420 throw new SQLException("Not supported CREATE TABLE statement - use execute method"); 421 } 422 else if (parser.sqlType.equals(parser.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 437 { 438 if(connection.getAutoCommit()) 439 writeCsv = new CsvWriter( 440 fileName, 441 connection.getSeperator(), 442 connection.getExtension(), 443 connection.getMaxFileSize(), 444 connection.getCharset(), 445 connection.getUseQuotes(), 446 connection.getUseQuotesEscape() 447 ); 448 449 else{ 450 writeCsv.setFileName(fileName); 451 writeCsv.fillTableColumnNames(); 452 } 453 454 String [] xxx = parser.getColumnNames(); 455 String [] yyy = writeCsv.getColumnNames(); 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 } 465 else 466 isOK=false; 467 } 468 if(!isOK) 469 throw new SQLException("Column '" + xxx[i] + "' not found."); 470 } 471 } 472 writeCsv.newLine(parser.columnNames, parser.columnValues); 473 474 } 475 catch (Exception e) 476 { 477 throw new SQLException("Error reading data file. Message was: " + e); 478 } 479 480 } 481 else if (parser.sqlType.equals(parser.UPDATE)) { 482 483 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 484 File checkFile = new File (fileName); 485 486 if (!checkFile.exists()) 487 { 488 throw new SQLException("Cannot open data file '" + fileName + "' !"); 489 } 490 491 if (!checkFile.canWrite()) 492 { 493 throw new SQLException("Data file '" + fileName + "' is read only !"); 494 } 495 try 497 { 498 if(connection.getAutoCommit()) 499 writeCsv = new CsvWriter( 500 fileName, 501 connection.getSeperator(), 502 connection.getExtension(), 503 connection.getMaxFileSize(), 504 connection.getCharset(), 505 connection.getUseQuotes(), 506 connection.getUseQuotesEscape() 507 ); 508 else{ 509 writeCsv.setFileName(fileName); 510 writeCsv.fillTableColumnNames(); 511 } 512 513 String [] xxx = parser.getColumnNames(); 514 String [] yyy = writeCsv.getColumnNames(); 515 boolean isOK = true; 516 for(int i=0; i< xxx.length; i++) { 517 if(!xxx[i].endsWith("*")) { 518 out: 519 for(int j=0; j< yyy.length; j++) { 520 if(xxx[i].equalsIgnoreCase(yyy[j])) { 521 isOK=true; 522 break out; 523 } 524 else 525 isOK=false; 526 } 527 if(!isOK) 528 throw new SQLException("Column '" + xxx[i] + "' not found."); 529 } 530 } 531 if(!writeCsv.updateFields(parser.columnNames, parser.columnValues, parser.columnWhereNames, parser.columnWhereValues)) 532 updated = -1; 533 } 534 catch (Exception e) 535 { 536 e.printStackTrace(); 537 throw new SQLException("Error reading data file. Message was: " + e); 538 } 539 540 } 541 return updated; 542 543 } 544 545 546 564 public void close() throws SQLException { 565 for(Enumeration i = resultSets.elements(); i.hasMoreElements(); ) { 567 CsvResultSet resultSet = (CsvResultSet) i.nextElement(); 568 resultSet.close(); 569 } 570 try { 571 if(this.writeCsv!=null) 572 this.writeCsv.close(); 573 } 575 catch(Exception e) { 576 throw new SQLException(e.getMessage()); 577 } 578 579 } 580 581 587 public void cancel() throws SQLException 588 { 589 throw new SQLException("Not Supported !"); 590 } 591 592 593 599 public void clearWarnings() throws SQLException 600 { 601 throw new SQLException("Not Supported !"); 602 } 603 604 605 613 public boolean execute(String sql) throws SQLException 614 { 615 616 CsvSqlParser parser = new CsvSqlParser(); 617 this.sql = sql; 618 try 619 { 620 parser.parse(this); 621 } 622 catch (Exception e) 623 { 624 throw new SQLException("Syntax Error. " + e.getMessage()); 625 } 626 if(parser.sqlType.equals(parser.SELECT)) 627 throw new SQLException("Not supported SELECT statement - use executeQuery method"); 628 else if (parser.sqlType.equals(parser.INSERT)) 629 executeUpdate(sql); 630 else if (parser.sqlType.equals(parser.CREATE_TABLE)) { 631 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 632 File checkFile = new File (fileName); 633 634 if (checkFile.exists()) 635 { 636 throw new SQLException("Data file '" + fileName + "'already exists !"); 637 } 638 639 try { 641 if (connection.getAutoCommit()) 642 writeCsv = new CsvWriter( 643 fileName, 644 connection.getSeperator(), 645 connection.getExtension(), 646 connection.getMaxFileSize(), 647 connection.getCharset(), 648 connection.getUseQuotes(), 649 connection.getUseQuotesEscape() 650 ); 651 else { 652 writeCsv.setFileName(fileName); 653 writeCsv.fillTableColumnNames(); 654 } 655 656 writeCsv.createTable(parser.columnNames, fileName); 657 } 658 catch (Exception e) 659 { 660 throw new SQLException("Error reading data file. Message was: " + e); 661 } 662 } 663 return true; 664 665 } 666 667 668 669 676 public void addBatch(String p0) throws SQLException 677 { 678 throw new SQLException("Not Supported !"); 679 } 680 681 682 688 public void clearBatch() throws SQLException 689 { 690 throw new SQLException("Not Supported !"); 691 } 692 693 694 701 public int[] executeBatch() throws SQLException 702 { 703 throw new SQLException("Not Supported !"); 704 } 705 706 710 public boolean getMoreResults(int current) throws SQLException { 711 throw new UnsupportedOperationException ("Statement.getMoreResults(int) unsupported"); 712 } 713 714 public ResultSet getGeneratedKeys() throws SQLException { 715 throw new UnsupportedOperationException ("Statement.getGeneratedKeys() unsupported"); 716 } 717 718 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 719 throw new UnsupportedOperationException ("Statement.executeUpdate(String,int) unsupported"); 720 } 721 722 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 723 throw new UnsupportedOperationException ("Statement.executeUpdate(String,int[]) unsupported"); 724 } 725 726 public int executeUpdate(String sql, String [] columnNames) throws SQLException { 727 throw new UnsupportedOperationException ("Statement.executeUpdate(String,String[]) unsupported"); 728 } 729 730 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 731 throw new UnsupportedOperationException ("Statement.execute(String,int) unsupported"); 732 } 733 734 public boolean execute(String sql, int[] columnIndexes) throws SQLException { 735 throw new UnsupportedOperationException ("Statement.execute(String,int[]) unsupported"); 736 } 737 738 public boolean execute(String sql, String [] columnNames) throws SQLException { 739 throw new UnsupportedOperationException ("Statement.execute(String,String[]) unsupported"); 740 } 741 742 public int getResultSetHoldability() throws SQLException { 743 throw new UnsupportedOperationException ("Statement.getResultSetHoldability() unsupported"); 744 } 745 746 public String getSqlStatement() { 747 return sql; 748 } 749 } 750 751 | Popular Tags |