View Javadoc
1 /* 2 * CsvJdbc - a JDBC driver for CSV files 3 * Copyright (C) 2001 Jonathan Ackerman 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 2.1 of the License, or (at your option) any later version. 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 * You should have received a copy of the GNU Lesser General Public 14 * License along with this library; if not, write to the Free Software 15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 */ 17 package org.relique.jdbc.csv; 18 19 import java.sql.*; 20 import java.io.File; 21 import java.util.Enumeration; 22 import java.util.Vector; 23 24 /*** 25 * This class implements the Statement interface for the CsvJdbc driver. 26 * 27 * @author Jonathan Ackerman 28 * @author Sander Brienen 29 * @author Michael Maraya 30 * @author Tomasz Skutnik 31 * @created 25 November 2001 32 * @version $Id: CsvStatement.html,v 1.1 2003/05/12 16:19:47 sinisa Exp $ 33 */ 34 35 public class CsvStatement implements Statement 36 { 37 private CsvConnection connection; 38 private Vector resultSets = new Vector(); 39 40 41 /*** 42 *Constructor for the CsvStatement object 43 * 44 * @param connection Description of Parameter 45 * @since 46 */ 47 protected CsvStatement(CsvConnection connection) 48 { 49 DriverManager.println("CsvJdbc - CsvStatement() - connection=" + connection); 50 this.connection = connection; 51 } 52 53 54 /*** 55 *Sets the maxFieldSize attribute of the CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 CsvStatement 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 Description of Parameter 305 * @return Description of the Returned Value 306 * @exception SQLException Description of Exception 307 * @since 308 */ 309 public ResultSet executeQuery(String sql) throws SQLException 310 { 311 DriverManager.println("CsvJdbc - CsvStatement:executeQuery() - sql= " + sql); 312 SqlParser parser = new SqlParser(); 313 try 314 { 315 parser.parse(sql); 316 } 317 catch (Exception e) 318 { 319 throw new SQLException("Syntax Error. " + e.getMessage()); 320 } 321 322 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 323 File checkFile = new File(fileName); 324 if (!checkFile.exists()) 325 { 326 throw new SQLException("Cannot open data file '" + fileName + "' !"); 327 } 328 329 if (!checkFile.canRead()) 330 { 331 throw new SQLException("Data file '" + fileName + "' not readable !"); 332 } 333 CsvReader reader; 334 try 335 { 336 reader = new CsvReader(fileName, connection.getSeperator(), 337 connection.isSuppressHeaders(), connection.getCharset()); 338 String[] xxx = parser.getColumnNames(); 339 String[] yyy = reader.getColumnNames(); 340 boolean isOK = true; 341 for(int i=0; i< xxx.length; i++) { 342 if(!xxx[i].endsWith("*")) { 343 out: 344 for(int j=0; j< yyy.length; j++) { 345 if(xxx[i].equalsIgnoreCase(yyy[j])) { 346 isOK=true; 347 break out; 348 } 349 else 350 isOK=false; 351 } 352 if(!isOK) 353 throw new SQLException("Column '" + xxx[i] + "' not found."); 354 } 355 } 356 } 357 catch (Exception e) 358 { 359 throw new SQLException("Error reading data file. Message was: " + e); 360 } 361 CsvResultSet resultSet = new CsvResultSet(this, reader, 362 parser.getTableName(), parser.getColumnNames(), parser.getWhereColumnNames(), 363 parser.getWhereColumnValues()); 364 resultSets.add(resultSet); 365 return resultSet; 366 } 367 368 369 370 /*** 371 *Description of the Method 372 * 373 * @param sql Description of Parameter 374 * @return Description of the Returned Value 375 * @exception SQLException Description of Exception 376 * @since 377 */ 378 public int executeUpdate(String sql) throws SQLException 379 { 380 DriverManager.println("CsvJdbc - CsvStatement:executeUpdate() - sql= " + sql); 381 SqlParser parser = new SqlParser(); 382 try 383 { 384 parser.parse(sql); 385 } 386 catch (Exception e) 387 { 388 throw new SQLException("Syntax Error. " + e.getMessage()); 389 } 390 if(parser.sqlType.equals(parser.SELECT)) 391 throw new SQLException("Not supported SELECT statement - use executeQuery method"); 392 else if (parser.sqlType.equals(parser.CREATE_TABLE)) { 393 throw new SQLException("Not supported SELECT statement - use execute method"); 394 } 395 else if (parser.sqlType.equals(parser.INSERT)) { 396 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 397 File checkFile = new File(fileName); 398 399 if (!checkFile.exists()) 400 { 401 throw new SQLException("Cannot open data file '" + fileName + "' !"); 402 } 403 404 if (!checkFile.canWrite()) 405 { 406 throw new SQLException("Data file '" + fileName + "' is read only !"); 407 } 408 CsvWriter writeCsv; 409 try 410 { 411 writeCsv = new CsvWriter(fileName, connection.getSeperator()); 412 writeCsv.newLine(parser.columnNames, parser.columnValues); 413 String[] xxx = parser.getColumnNames(); 414 String[] yyy = writeCsv.getColumnNames(); 415 boolean isOK = true; 416 for(int i=0; i< xxx.length; i++) { 417 if(!xxx[i].endsWith("*")) { 418 out: 419 for(int j=0; j< yyy.length; j++) { 420 if(xxx[i].equalsIgnoreCase(yyy[j])) { 421 isOK=true; 422 break out; 423 } 424 else 425 isOK=false; 426 } 427 if(!isOK) 428 throw new SQLException("Column '" + xxx[i] + "' not found."); 429 } 430 } 431 432 433 } 434 catch (Exception e) 435 { 436 throw new SQLException("Error reading data file. Message was: " + e); 437 } 438 439 } 440 else if (parser.sqlType.equals(parser.UPDATE)) { 441 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 442 File checkFile = new File(fileName); 443 444 if (!checkFile.exists()) 445 { 446 throw new SQLException("Cannot open data file '" + fileName + "' !"); 447 } 448 449 if (!checkFile.canWrite()) 450 { 451 throw new SQLException("Data file '" + fileName + "' is read only !"); 452 } 453 CsvWriter writeCsv; 454 try 455 { 456 writeCsv = new CsvWriter(fileName, connection.getSeperator()); 457 writeCsv.updateField(parser.columnNames, parser.columnValues, parser.columnWhereNames, parser.columnWhereValues); 458 String[] xxx = parser.getColumnNames(); 459 String[] yyy = writeCsv.getColumnNames(); 460 boolean isOK = true; 461 for(int i=0; i< xxx.length; i++) { 462 if(!xxx[i].endsWith("*")) { 463 out: 464 for(int j=0; j< yyy.length; j++) { 465 if(xxx[i].equalsIgnoreCase(yyy[j])) { 466 isOK=true; 467 break out; 468 } 469 else 470 isOK=false; 471 } 472 if(!isOK) 473 throw new SQLException("Column '" + xxx[i] + "' not found."); 474 } 475 } 476 } 477 catch (Exception e) 478 { 479 throw new SQLException("Error reading data file. Message was: " + e); 480 } 481 482 } 483 return 0; 484 485 } 486 487 488 /*** 489 * Releases this <code>Statement</code> object's database 490 * and JDBC resources immediately instead of waiting for 491 * this to happen when it is automatically closed. 492 * It is generally good practice to release resources as soon as 493 * you are finished with them to avoid tying up database 494 * resources. 495 * <P> 496 * Calling the method <code>close</code> on a <code>Statement</code> 497 * object that is already closed has no effect. 498 * <P> 499 * <B>Note:</B> A <code>Statement</code> object is automatically closed 500 * when it is garbage collected. When a <code>Statement</code> object is 501 * closed, its current <code>ResultSet</code> object, if one exists, is 502 * also closed. 503 * 504 * @exception SQLException if a database access error occurs 505 */ 506 public void close() throws SQLException { 507 // close all result sets 508 for(Enumeration i = resultSets.elements(); i.hasMoreElements(); ) { 509 CsvResultSet resultSet = (CsvResultSet)i.nextElement(); 510 resultSet.close(); 511 } 512 } 513 514 515 /*** 516 *Description of the Method 517 * 518 * @exception SQLException Description of Exception 519 * @since 520 */ 521 public void cancel() throws SQLException 522 { 523 throw new SQLException("Not Supported !"); 524 } 525 526 527 /*** 528 *Description of the Method 529 * 530 * @exception SQLException Description of Exception 531 * @since 532 */ 533 public void clearWarnings() throws SQLException 534 { 535 throw new SQLException("Not Supported !"); 536 } 537 538 539 /*** 540 *Description of the Method 541 * 542 * @param p0 Description of Parameter 543 * @return Description of the Returned Value 544 * @exception SQLException Description of Exception 545 * @since 546 */ 547 public boolean execute(String sql) throws SQLException 548 { 549 550 SqlParser parser = new SqlParser(); 551 try 552 { 553 parser.parse(sql); 554 } 555 catch (Exception e) 556 { 557 throw new SQLException("Syntax Error. " + e.getMessage()); 558 } 559 if(parser.sqlType.equals(parser.SELECT)) 560 throw new SQLException("Not supported SELECT statement - use executeQuery method"); 561 else if (parser.sqlType.equals(parser.INSERT)) 562 executeUpdate(sql); 563 else if (parser.sqlType.equals(parser.CREATE_TABLE)) { 564 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension(); 565 File checkFile = new File(fileName); 566 567 if (checkFile.exists()) 568 { 569 throw new SQLException("Data file '" + fileName + "'already exists !"); 570 } 571 572 CsvWriter writeCsv; 573 try 574 { 575 writeCsv = new CsvWriter(fileName, connection.getSeperator()); 576 writeCsv.createTable(parser.columnNames, fileName); 577 } 578 catch (Exception e) 579 { 580 throw new SQLException("Error reading data file. Message was: " + e); 581 } 582 } 583 return true; 584 585 } 586 587 588 589 /*** 590 *Adds a feature to the Batch attribute of the CsvStatement object 591 * 592 * @param p0 The feature to be added to the Batch attribute 593 * @exception SQLException Description of Exception 594 * @since 595 */ 596 public void addBatch(String p0) throws SQLException 597 { 598 throw new SQLException("Not Supported !"); 599 } 600 601 602 /*** 603 *Description of the Method 604 * 605 * @exception SQLException Description of Exception 606 * @since 607 */ 608 public void clearBatch() throws SQLException 609 { 610 throw new SQLException("Not Supported !"); 611 } 612 613 614 /*** 615 *Description of the Method 616 * 617 * @return Description of the Returned Value 618 * @exception SQLException Description of Exception 619 * @since 620 */ 621 public int[] executeBatch() throws SQLException 622 { 623 throw new SQLException("Not Supported !"); 624 } 625 626 //--------------------------------------------------------------------- 627 // JDBC 3.0 628 //--------------------------------------------------------------------- 629 630 public boolean getMoreResults(int current) throws SQLException { 631 throw new UnsupportedOperationException("Statement.getMoreResults(int) unsupported"); 632 } 633 634 public ResultSet getGeneratedKeys() throws SQLException { 635 throw new UnsupportedOperationException("Statement.getGeneratedKeys() unsupported"); 636 } 637 638 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { 639 throw new UnsupportedOperationException("Statement.executeUpdate(String,int) unsupported"); 640 } 641 642 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { 643 throw new UnsupportedOperationException("Statement.executeUpdate(String,int[]) unsupported"); 644 } 645 646 public int executeUpdate(String sql, String[] columnNames) throws SQLException { 647 throw new UnsupportedOperationException("Statement.executeUpdate(String,String[]) unsupported"); 648 } 649 650 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { 651 throw new UnsupportedOperationException("Statement.execute(String,int) unsupported"); 652 } 653 654 public boolean execute(String sql, int[] columnIndexes) throws SQLException { 655 throw new UnsupportedOperationException("Statement.execute(String,int[]) unsupported"); 656 } 657 658 public boolean execute(String sql, String[] columnNames) throws SQLException { 659 throw new UnsupportedOperationException("Statement.execute(String,String[]) unsupported"); 660 } 661 662 public int getResultSetHoldability() throws SQLException { 663 throw new UnsupportedOperationException("Statement.getResultSetHoldability() unsupported"); 664 } 665 } 666

This page automatically generated by Maven