1 18 19 package org.webdocwf.util.xml; 20 21 import java.sql.*; 22 import java.io.File ; 23 import java.io.RandomAccessFile ; 24 import java.util.Enumeration ; 25 import java.util.ArrayList ; 27 28 33 34 public class XmlStatement implements Statement 35 { 36 private XmlConnection connection; 37 private String fileName; 38 39 40 46 protected XmlStatement(XmlConnection connection) 47 { 48 DriverManager.println("XmlJdbc - XmlStatement() - connection=" + connection); 49 this.connection = connection; 50 this.fileName = connection.getPath() + connection.getExtension(); 51 } 52 53 54 61 public void setMaxFieldSize(int p0) throws SQLException 62 { 63 throw new SQLException("Not Supported !"); 64 } 65 66 67 74 public void setMaxRows(int p0) throws SQLException 75 { 76 throw new SQLException("Not Supported !"); 77 } 78 79 80 87 public void setEscapeProcessing(boolean p0) throws SQLException 88 { 89 throw new SQLException("Not Supported !"); 90 } 91 92 93 100 public void setQueryTimeout(int p0) throws SQLException 101 { 102 throw new SQLException("Not Supported !"); 103 } 104 105 106 113 public void setCursorName(String p0) throws SQLException 114 { 115 throw new SQLException("Not Supported !"); 116 } 117 118 119 126 public void setFetchDirection(int p0) throws SQLException 127 { 128 throw new SQLException("Not Supported !"); 129 } 130 131 132 139 public void setFetchSize(int p0) throws SQLException 140 { 141 throw new SQLException("Not Supported !"); 142 } 143 144 145 152 public int getMaxFieldSize() throws SQLException 153 { 154 throw new SQLException("Not Supported !"); 155 } 156 157 158 165 public int getMaxRows() throws SQLException 166 { 167 throw new SQLException("Not Supported !"); 168 } 169 170 171 178 public int getQueryTimeout() throws SQLException 179 { 180 throw new SQLException("Not Supported !"); 181 } 182 183 184 191 public SQLWarning getWarnings() throws SQLException 192 { 193 throw new SQLException("Not Supported !"); 194 } 195 196 197 204 public ResultSet getResultSet() throws SQLException 205 { 206 throw new SQLException("Not Supported !"); 207 } 208 209 210 217 public int getUpdateCount() throws SQLException 218 { 219 throw new SQLException("Not Supported !"); 220 } 221 222 223 230 public boolean getMoreResults() throws SQLException 231 { 232 throw new SQLException("Not Supported !"); 233 } 234 235 236 243 public int getFetchDirection() throws SQLException 244 { 245 throw new SQLException("Not Supported !"); 246 } 247 248 249 256 public int getFetchSize() throws SQLException 257 { 258 throw new SQLException("Not Supported !"); 259 } 260 261 262 269 public int getResultSetConcurrency() throws SQLException 270 { 271 throw new SQLException("Not Supported !"); 272 } 273 274 275 282 public int getResultSetType() throws SQLException 283 { 284 throw new SQLException("Not Supported !"); 285 } 286 287 288 295 public Connection getConnection() throws SQLException 296 { 297 return connection; 298 } 299 300 301 309 public ResultSet executeQuery(String sql) throws SQLException 310 { 311 DriverManager.println("XmlJdbc - XmlStatement:executeQuery() - sql= " + sql); 312 XmlWriter writer; 313 if( this.connection.getAutoCommit() ) 314 writer = new XmlWriter(fileName,true); 315 else 316 writer = new XmlWriter(fileName,false); 317 XmlSqlParser parser = new XmlSqlParser( this.fileName, this.connection.getAutoCommit() ); 318 try 319 { 320 parser.parse(sql); 321 } 322 catch (Exception e) 323 { 324 throw new SQLException("Syntax Error. " + e.getMessage()); 325 } 326 if (parser.sqlType.equals(parser.DROP_TABLE)) { 327 execute(sql); 328 return null; 329 } 330 if (parser.sqlType.equals(parser.DELETE)) { 331 executeUpdate(sql); 332 return null; 333 } 334 if (parser.sqlType.equals(parser.UPDATE)) { 335 executeUpdate(sql); 336 return null; 337 } 338 else if (parser.sqlType.equals(parser.INSERT)) { 339 executeUpdate(sql); 340 return null; 341 } 342 else if (parser.sqlType.equals(parser.CREATE_TABLE)) { 343 execute( sql ); 344 return null; 345 } else if(parser.sqlType.equals(parser.SELECT) ) { 346 347 String fileName = connection.getPath() + connection.getExtension(); 348 File checkFile = new File (fileName); 349 XmlReader reader; 350 try 351 { 352 reader = new XmlReader(fileName); 353 String [] xxx = parser.getColumnNames(); 354 String [] yyy = (String [])writer.getTableProperties( parser.getTableName() ).get(0); 355 boolean isOK = true; 356 for(int i=0; i< xxx.length; i++) { 357 out: 358 for(int j=0; j< yyy.length; j++) { 359 if(xxx[i].equalsIgnoreCase(yyy[j])) { 360 isOK=true; 361 break out; 362 } 363 else 364 isOK=false; 365 } 366 if(!isOK) 367 throw new SQLException("Column '" + xxx[i] + "' not found in table "+parser.getTableName()); 368 } 369 } 370 catch (Exception e) 371 { 372 throw new SQLException("Error reading data file. Message was: " + e); 373 } 374 XmlResultSet resultSet = new XmlResultSet(this, reader, 375 parser.getTableName(), parser.getColumnNames() , parser.getWhereColumnNames() , parser.getWhereColumnValues()); 376 resultSet.select(); 377 return resultSet; 378 } else { 379 throw new SQLException("Syntax error.Not supported sql statement."); 380 } 381 } 382 383 384 385 393 public int executeUpdate(String sql) throws SQLException 394 { 395 DriverManager.println("XmlJdbc - XmlStatement:executeUpdate() - sql= " + sql); 396 XmlSqlParser parser = new XmlSqlParser(); 397 try 398 { 399 parser.parse(sql); 400 } 401 catch (Exception e) 402 { 403 throw new SQLException("Syntax Error. " + e.getMessage()); 404 } 405 if(parser.sqlType.equals(parser.SELECT)) 406 throw new SQLException("Not supported SELECT statement - use executeQuery() method"); 407 else if (parser.sqlType.equals(parser.CREATE_TABLE)) { 408 throw new SQLException("Not supported CREATE_TABLE statement - use execute() method"); 409 } 410 else if (parser.sqlType.equals(parser.DROP_TABLE)) { 411 throw new SQLException("Not supported DROP_TABLE statement - use execute() method"); 412 } 413 else if (parser.sqlType.equals(parser.INSERT)) { 414 String fileName = connection.getPath() + connection.getExtension(); 415 XmlWriter writeXml; 416 try 417 { 418 if( this.connection.getAutoCommit() ) 419 writeXml = new XmlWriter(fileName,true); 420 else 421 writeXml = new XmlWriter(fileName,false); 422 ArrayList properties = writeXml.getTableProperties( parser.tableName ); 423 String [] xxx = parser.getColumnNames(); 425 String [] yyy = (String [])properties.get(0); 426 boolean isOK = true; 427 for(int i=0; i< xxx.length; i++) { 428 if(!xxx[i].endsWith("*")) { 429 out: 430 for(int j=0; j< yyy.length; j++) { 431 if(xxx[i].equalsIgnoreCase(yyy[j])) { 432 isOK=true; 433 break out; 434 } 435 else 436 isOK=false; 437 } 438 if(!isOK) 439 throw new SQLException("Column '" + xxx[i] + "' not found in table "+parser.getTableName()); 440 } 441 } 442 if( !properties.get(1).toString().equalsIgnoreCase("NO CREATE TABLE") ) { 444 yyy = parser.getColumnNames(); 445 xxx = (String [])writeXml.getTableProperties( parser.tableName ).get(2); 446 isOK = true; 447 for(int i=0; i< xxx.length; i++) { 448 out: 449 for(int j=0; j< yyy.length; j++) { 450 if(xxx[i].equalsIgnoreCase(yyy[j])) { 451 isOK=true; 452 break out; 453 } 454 else 455 isOK=false; 456 } 457 if(!isOK) 458 throw new SQLException("Column '" + xxx[i] + "' can not be NULL."); 459 } 460 } 461 writeXml.insert( parser.getTableName() , parser.getColumnNames() , parser.getColumnValues() ); 462 463 } 464 catch (Exception e) 465 { 466 throw new SQLException("Error reading data file. Message was: " + e); 467 } 468 469 } 470 else if (parser.sqlType.equals(parser.UPDATE)) { 471 String fileName = connection.getPath() + connection.getExtension(); 472 XmlWriter writeXml; 473 try 474 { 475 if( this.connection.getAutoCommit() ) 476 writeXml = new XmlWriter(fileName,true); 477 else 478 writeXml = new XmlWriter(fileName,false); 479 String [] xxx = parser.getColumnNames(); 480 String [] yyy = (String [])writeXml.getTableProperties( parser.tableName ).get(0); 481 boolean isOK = true; 482 for(int i=0; i< xxx.length; i++) { 483 if(!xxx[i].endsWith("*")) { 484 out: 485 for(int j=0; j< yyy.length; j++) { 486 if(xxx[i].equalsIgnoreCase(yyy[j])) { 487 isOK=true; 488 break out; 489 } 490 else 491 isOK=false; 492 } 493 if(!isOK) 494 throw new SQLException("Column '" + xxx[i] + "' not found in table "+parser.getTableName()); 495 } 496 } 497 writeXml.update( parser.getTableName() , parser.getColumnNames() , parser.getColumnValues() , parser.getWhereColumnNames() , parser.getWhereColumnValues() ); 498 } 499 catch (Exception e) 500 { 501 throw new SQLException("Error reading data file. Message was: " + e); 502 } 503 504 } 505 else if (parser.sqlType.equals(parser.DELETE)) { 506 String fileName = connection.getPath() + connection.getExtension(); 507 XmlWriter writeXml; 508 try 509 { 510 if( this.connection.getAutoCommit() ) 511 writeXml = new XmlWriter(fileName,true); 512 else 513 writeXml = new XmlWriter(fileName,false); 514 String [] xxx = parser.getWhereColumnNames(); 515 String [] yyy = (String [])writeXml.getTableProperties( parser.tableName ).get(0); 516 boolean isOK = true; 517 for(int i=0; i< xxx.length; i++) { 518 if(!xxx[i].endsWith("*")) { 519 out: 520 for(int j=0; j< yyy.length; j++) { 521 if(xxx[i].equalsIgnoreCase(yyy[j])) { 522 isOK=true; 523 break out; 524 } 525 else 526 isOK=false; 527 } 528 if(!isOK) 529 throw new SQLException("Column '" + xxx[i] + "' not found in table "+parser.getTableName()); 530 } 531 } 532 writeXml.delete( parser.getTableName() , parser.getWhereColumnNames() , parser.getWhereColumnValues() ); 533 } 534 catch (Exception e) 535 { 536 throw new SQLException("Error reading data file. Message was: " + e); 537 } 538 539 } 540 else if (parser.sqlType.equals(parser.DROP_TABLE)) { 541 } 542 return 0; 543 544 545 } 546 547 548 566 public void close() throws SQLException { 567 568 } 569 570 571 577 public void cancel() throws SQLException 578 { 579 throw new SQLException("Not Supported !"); 580 } 581 582 583 589 public void clearWarnings() throws SQLException 590 { 591 throw new SQLException("Not Supported !"); 592 } 593 594 595 603 public boolean execute(String sql) throws SQLException 604 { 605 XmlWriter writeXml; 606 if( this.connection.getAutoCommit() ) 607 writeXml = new XmlWriter(fileName,true); 608 else 609 writeXml = new XmlWriter(fileName,false); 610 XmlSqlParser parser = new XmlSqlParser( this.fileName, this.connection.getAutoCommit() ); 611 try 612 { 613 parser.parse(sql); 614 } 615 catch (Exception e) 616 { 617 throw new SQLException("Syntax Error. " + e.getMessage()); 618 } 619 if(parser.sqlType.equals(parser.SELECT)) 620 throw new SQLException("Use executeQuery() for SELECT statement"); 621 else if (parser.sqlType.equals(parser.UPDATE)) 622 executeUpdate(sql); 623 else if (parser.sqlType.equals(parser.INSERT)) 624 executeUpdate(sql); 625 else if (parser.sqlType.equals(parser.DELETE)) 626 executeUpdate(sql); 627 else if (parser.sqlType.equals(parser.CREATE_TABLE)) { 628 String fileName = connection.getPath() + connection.getExtension(); 629 try 630 { 631 writeXml.createTable( parser.getSqlStatement() , parser.getTableName() ); 632 } 633 catch (Exception e) 634 { 635 throw new SQLException("Error reading data file. Message was: " + e); 636 } 637 } else if (parser.sqlType.equals(parser.DROP_TABLE)) { 638 String fileName = connection.getPath() + connection.getExtension(); 639 try 640 { 641 writeXml.dropTable( parser.getTableName() ); 642 } 643 catch (Exception e) 644 { 645 throw new SQLException("Error reading data file. Message was: " + e); 646 } 647 } 648 return true; 649 650 } 651 652 653 654 661 public void addBatch(String p0) throws SQLException 662 { 663 throw new SQLException("Not Supported !"); 664 } 665 666 667 673 public void clearBatch() throws SQLException 674 { 675 throw new SQLException("Not Supported !"); 676 } 677 678 679 686 public int[] executeBatch() throws SQLException 687 { 688 throw new SQLException("Not Supported !"); 689 } 690 691 695 public boolean getMoreResults(int current) throws SQLException { 696 throw new UnsupportedOperationException ("Statement.getMoreResults(int) unsupported"); 697 } 698 699 public ResultSet getGeneratedKeys() throws SQLException { 700 throw new UnsupportedOperationException ("Statement.getGeneratedKeys() unsupported"); 701 } 702 703 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 704 throw new UnsupportedOperationException ("Statement.executeUpdate(String,int) unsupported"); 705 } 706 707 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 708 throw new UnsupportedOperationException ("Statement.executeUpdate(String,int[]) unsupported"); 709 } 710 711 public int executeUpdate(String sql, String [] columnNames) throws SQLException { 712 throw new UnsupportedOperationException ("Statement.executeUpdate(String,String[]) unsupported"); 713 } 714 715 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 716 throw new UnsupportedOperationException ("Statement.execute(String,int) unsupported"); 717 } 718 719 public boolean execute(String sql, int[] columnIndexes) throws SQLException { 720 throw new UnsupportedOperationException ("Statement.execute(String,int[]) unsupported"); 721 } 722 723 public boolean execute(String sql, String [] columnNames) throws SQLException { 724 throw new UnsupportedOperationException ("Statement.execute(String,String[]) unsupported"); 725 } 726 727 public int getResultSetHoldability() throws SQLException { 728 throw new UnsupportedOperationException ("Statement.getResultSetHoldability() unsupported"); 729 } 730 731 } 732 | Popular Tags |