View Javadoc
1 /* 2 Copyright (C) 2003 Together 3 4 This library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Lesser General Public 6 License as published by the Free Software Foundation; either 7 version 2.1 of the License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 14 You should have received a copy of the GNU Lesser General Public 15 License along with this library; if not, write to the Free Software 16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 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.Vector; 26 import java.util.ArrayList; 27 28 /*** 29 * Class implements the JDBC Statement interface for the XmlJdbc driver. 30 * 31 * @author Zoran Milakovic 32 */ 33 34 public class XmlStatement implements Statement 35 { 36 private XmlConnection connection; 37 private String fileName; 38 39 40 /*** 41 *Constructor for the XmlStatement object 42 * 43 * @param connection Description of Parameter 44 * @since 45 */ 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 /*** 55 *Sets the maxFieldSize attribute of the SmlStatement object 56 * 57 * @param p0 The new maxFieldSize value 58 * @exception SQLException Description of Exception 59 * @since 60 */ 61 public void setMaxFieldSize(int p0) throws SQLException 62 { 63 throw new SQLException("Not Supported !"); 64 } 65 66 67 /*** 68 *Sets the maxRows attribute of the XmlStatement object 69 * 70 * @param p0 The new maxRows value 71 * @exception SQLException Description of Exception 72 * @since 73 */ 74 public void setMaxRows(int p0) throws SQLException 75 { 76 throw new SQLException("Not Supported !"); 77 } 78 79 80 /*** 81 *Sets the escapeProcessing attribute of the XmlStatement object 82 * 83 * @param p0 The new escapeProcessing value 84 * @exception SQLException Description of Exception 85 * @since 86 */ 87 public void setEscapeProcessing(boolean p0) throws SQLException 88 { 89 throw new SQLException("Not Supported !"); 90 } 91 92 93 /*** 94 *Sets the queryTimeout attribute of the XmlStatement object 95 * 96 * @param p0 The new queryTimeout value 97 * @exception SQLException Description of Exception 98 * @since 99 */ 100 public void setQueryTimeout(int p0) throws SQLException 101 { 102 throw new SQLException("Not Supported !"); 103 } 104 105 106 /*** 107 *Sets the cursorName attribute of the XmlStatement object 108 * 109 * @param p0 The new cursorName value 110 * @exception SQLException Description of Exception 111 * @since 112 */ 113 public void setCursorName(String p0) throws SQLException 114 { 115 throw new SQLException("Not Supported !"); 116 } 117 118 119 /*** 120 *Sets the fetchDirection attribute of the XmlStatement object 121 * 122 * @param p0 The new fetchDirection value 123 * @exception SQLException Description of Exception 124 * @since 125 */ 126 public void setFetchDirection(int p0) throws SQLException 127 { 128 throw new SQLException("Not Supported !"); 129 } 130 131 132 /*** 133 *Sets the fetchSize attribute of the XmlStatement object 134 * 135 * @param p0 The new fetchSize value 136 * @exception SQLException Description of Exception 137 * @since 138 */ 139 public void setFetchSize(int p0) throws SQLException 140 { 141 throw new SQLException("Not Supported !"); 142 } 143 144 145 /*** 146 *Gets the maxFieldSize attribute of the XmlStatement object 147 * 148 * @return The maxFieldSize value 149 * @exception SQLException Description of Exception 150 * @since 151 */ 152 public int getMaxFieldSize() throws SQLException 153 { 154 throw new SQLException("Not Supported !"); 155 } 156 157 158 /*** 159 *Gets the maxRows attribute of the XmlStatement object 160 * 161 * @return The maxRows value 162 * @exception SQLException Description of Exception 163 * @since 164 */ 165 public int getMaxRows() throws SQLException 166 { 167 throw new SQLException("Not Supported !"); 168 } 169 170 171 /*** 172 *Gets the queryTimeout attribute of the XmlStatement object 173 * 174 * @return The queryTimeout value 175 * @exception SQLException Description of Exception 176 * @since 177 */ 178 public int getQueryTimeout() throws SQLException 179 { 180 throw new SQLException("Not Supported !"); 181 } 182 183 184 /*** 185 *Gets the warnings attribute of the XmlStatement object 186 * 187 * @return The warnings value 188 * @exception SQLException Description of Exception 189 * @since 190 */ 191 public SQLWarning getWarnings() throws SQLException 192 { 193 throw new SQLException("Not Supported !"); 194 } 195 196 197 /*** 198 *Gets the resultSet attribute of the XmlStatement object 199 * 200 * @return The resultSet value 201 * @exception SQLException Description of Exception 202 * @since 203 */ 204 public ResultSet getResultSet() throws SQLException 205 { 206 throw new SQLException("Not Supported !"); 207 } 208 209 210 /*** 211 *Gets the updateCount attribute of the XmlStatement object 212 * 213 * @return The updateCount value 214 * @exception SQLException Description of Exception 215 * @since 216 */ 217 public int getUpdateCount() throws SQLException 218 { 219 throw new SQLException("Not Supported !"); 220 } 221 222 223 /*** 224 *Gets the moreResults attribute of the XmlStatement object 225 * 226 * @return The moreResults value 227 * @exception SQLException Description of Exception 228 * @since 229 */ 230 public boolean getMoreResults() throws SQLException 231 { 232 throw new SQLException("Not Supported !"); 233 } 234 235 236 /*** 237 *Gets the fetchDirection attribute of the XmlStatement object 238 * 239 * @return The fetchDirection value 240 * @exception SQLException Description of Exception 241 * @since 242 */ 243 public int getFetchDirection() throws SQLException 244 { 245 throw new SQLException("Not Supported !"); 246 } 247 248 249 /*** 250 *Gets the fetchSize attribute of the XmlStatement object 251 * 252 * @return The fetchSize value 253 * @exception SQLException Description of Exception 254 * @since 255 */ 256 public int getFetchSize() throws SQLException 257 { 258 throw new SQLException("Not Supported !"); 259 } 260 261 262 /*** 263 *Gets the resultSetConcurrency attribute of the XmlStatement object 264 * 265 * @return The resultSetConcurrency value 266 * @exception SQLException Description of Exception 267 * @since 268 */ 269 public int getResultSetConcurrency() throws SQLException 270 { 271 throw new SQLException("Not Supported !"); 272 } 273 274 275 /*** 276 *Gets the resultSetType attribute of the XmlStatement object 277 * 278 * @return The resultSetType value 279 * @exception SQLException Description of Exception 280 * @since 281 */ 282 public int getResultSetType() throws SQLException 283 { 284 throw new SQLException("Not Supported !"); 285 } 286 287 288 /*** 289 *Gets the connection attribute of the XmlStatement object 290 * 291 * @return The connection value 292 * @exception SQLException Description of Exception 293 * @since 294 */ 295 public Connection getConnection() throws SQLException 296 { 297 return connection; 298 } 299 300 301 /*** 302 *Description of the Method 303 * 304 * @param sql Sql statement 305 * @return ResultSet 306 * @exception SQLException 307 * @since 308 */ 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 /*** 386 * Insert data into XML database 387 * 388 * @param sql Description of Parameter 389 * @return Description of the Returned Value 390 * @exception SQLException Description of Exception 391 * @since 392 */ 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 //check for existance of columns 424 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 //check if NOTNULL columns is set 443 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 /*** 549 * Releases this <code>Statement</code> object's database 550 * and JDBC resources immediately instead of waiting for 551 * this to happen when it is automatically closed. 552 * It is generally good practice to release resources as soon as 553 * you are finished with them to avoid tying up database 554 * resources. 555 * <P> 556 * Calling the method <code>close</code> on a <code>Statement</code> 557 * object that is already closed has no effect. 558 * <P> 559 * <B>Note:</B> A <code>Statement</code> object is automatically closed 560 * when it is garbage collected. When a <code>Statement</code> object is 561 * closed, its current <code>ResultSet</code> object, if one exists, is 562 * also closed. 563 * 564 * @exception SQLException if a database access error occurs 565 */ 566 public void close() throws SQLException { 567 568 } 569 570 571 /*** 572 *Description of the Method 573 * 574 * @exception SQLException Description of Exception 575 * @since 576 */ 577 public void cancel() throws SQLException 578 { 579 throw new SQLException("Not Supported !"); 580 } 581 582 583 /*** 584 *Description of the Method 585 * 586 * @exception SQLException Description of Exception 587 * @since 588 */ 589 public void clearWarnings() throws SQLException 590 { 591 throw new SQLException("Not Supported !"); 592 } 593 594 595 /*** 596 *Description of the Method 597 * 598 * @param sql Description of Parameter 599 * @return Description of the Returned Value 600 * @exception SQLException Description of Exception 601 * @since 602 */ 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 /*** 655 *Adds a feature to the Batch attribute of the XmlStatement object 656 * 657 * @param p0 The feature to be added to the Batch attribute 658 * @exception SQLException Description of Exception 659 * @since 660 */ 661 public void addBatch(String p0) throws SQLException 662 { 663 throw new SQLException("Not Supported !"); 664 } 665 666 667 /*** 668 *Description of the Method 669 * 670 * @exception SQLException Description of Exception 671 * @since 672 */ 673 public void clearBatch() throws SQLException 674 { 675 throw new SQLException("Not Supported !"); 676 } 677 678 679 /*** 680 *Description of the Method 681 * 682 * @return Description of the Returned Value 683 * @exception SQLException Description of Exception 684 * @since 685 */ 686 public int[] executeBatch() throws SQLException 687 { 688 throw new SQLException("Not Supported !"); 689 } 690 691 //--------------------------------------------------------------------- 692 // JDBC 3.0 693 //--------------------------------------------------------------------- 694 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 }

This page was automatically generated by Maven