1 /***
2 Copyright (C) 2002-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
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 /***
28 * This class implements the Statement interface for the CsvJdbc driver.
29 *
30 * @author Zoran Milakovic
31 */
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 /***
41 *Constructor for the CsvStatement object
42 *
43 * @param connection Description of Parameter
44 * @since
45 */
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 /***
71 *Sets the maxFieldSize attribute of the CsvStatement object
72 *
73 * @param p0 The new maxFieldSize value
74 * @exception SQLException Description of Exception
75 * @since
76 */
77 public void setMaxFieldSize(int p0) throws SQLException
78 {
79 throw new SQLException("Not Supported !");
80 }
81
82
83 /***
84 *Sets the maxRows attribute of the CsvStatement object
85 *
86 * @param p0 The new maxRows value
87 * @exception SQLException Description of Exception
88 * @since
89 */
90 public void setMaxRows(int p0) throws SQLException
91 {
92 throw new SQLException("Not Supported !");
93 }
94
95
96 /***
97 *Sets the escapeProcessing attribute of the CsvStatement object
98 *
99 * @param p0 The new escapeProcessing value
100 * @exception SQLException Description of Exception
101 * @since
102 */
103 public void setEscapeProcessing(boolean p0) throws SQLException
104 {
105 throw new SQLException("Not Supported !");
106 }
107
108
109 /***
110 *Sets the queryTimeout attribute of the CsvStatement object
111 *
112 * @param p0 The new queryTimeout value
113 * @exception SQLException Description of Exception
114 * @since
115 */
116 public void setQueryTimeout(int p0) throws SQLException
117 {
118 throw new SQLException("Not Supported !");
119 }
120
121
122 /***
123 *Sets the cursorName attribute of the CsvStatement object
124 *
125 * @param p0 The new cursorName value
126 * @exception SQLException Description of Exception
127 * @since
128 */
129 public void setCursorName(String p0) throws SQLException
130 {
131 throw new SQLException("Not Supported !");
132 }
133
134
135 /***
136 *Sets the fetchDirection attribute of the CsvStatement object
137 *
138 * @param p0 The new fetchDirection value
139 * @exception SQLException Description of Exception
140 * @since
141 */
142 public void setFetchDirection(int p0) throws SQLException
143 {
144 throw new SQLException("Not Supported !");
145 }
146
147
148 /***
149 *Sets the fetchSize attribute of the CsvStatement object
150 *
151 * @param p0 The new fetchSize value
152 * @exception SQLException Description of Exception
153 * @since
154 */
155 public void setFetchSize(int p0) throws SQLException
156 {
157 throw new SQLException("Not Supported !");
158 }
159
160
161 /***
162 *Gets the maxFieldSize attribute of the CsvStatement object
163 *
164 * @return The maxFieldSize value
165 * @exception SQLException Description of Exception
166 * @since
167 */
168 public int getMaxFieldSize() throws SQLException
169 {
170 throw new SQLException("Not Supported !");
171 }
172
173
174 /***
175 *Gets the maxRows attribute of the CsvStatement object
176 *
177 * @return The maxRows value
178 * @exception SQLException Description of Exception
179 * @since
180 */
181 public int getMaxRows() throws SQLException
182 {
183 throw new SQLException("Not Supported !");
184 }
185
186
187 /***
188 *Gets the queryTimeout attribute of the CsvStatement object
189 *
190 * @return The queryTimeout value
191 * @exception SQLException Description of Exception
192 * @since
193 */
194 public int getQueryTimeout() throws SQLException
195 {
196 throw new SQLException("Not Supported !");
197 }
198
199
200 /***
201 *Gets the warnings attribute of the CsvStatement object
202 *
203 * @return The warnings value
204 * @exception SQLException Description of Exception
205 * @since
206 */
207 public SQLWarning getWarnings() throws SQLException
208 {
209 throw new SQLException("Not Supported !");
210 }
211
212
213 /***
214 *Gets the resultSet attribute of the CsvStatement object
215 *
216 * @return The resultSet value
217 * @exception SQLException Description of Exception
218 * @since
219 */
220 public ResultSet getResultSet() throws SQLException
221 {
222 throw new SQLException("Not Supported !");
223 }
224
225
226 /***
227 *Gets the updateCount attribute of the CsvStatement object
228 *
229 * @return The updateCount value
230 * @exception SQLException Description of Exception
231 * @since
232 */
233 public int getUpdateCount() throws SQLException
234 {
235 throw new SQLException("Not Supported !");
236 }
237
238
239 /***
240 *Gets the moreResults attribute of the CsvStatement object
241 *
242 * @return The moreResults value
243 * @exception SQLException Description of Exception
244 * @since
245 */
246 public boolean getMoreResults() throws SQLException
247 {
248 throw new SQLException("Not Supported !");
249 }
250
251
252 /***
253 *Gets the fetchDirection attribute of the CsvStatement object
254 *
255 * @return The fetchDirection value
256 * @exception SQLException Description of Exception
257 * @since
258 */
259 public int getFetchDirection() throws SQLException
260 {
261 throw new SQLException("Not Supported !");
262 }
263
264
265 /***
266 *Gets the fetchSize attribute of the CsvStatement object
267 *
268 * @return The fetchSize value
269 * @exception SQLException Description of Exception
270 * @since
271 */
272 public int getFetchSize() throws SQLException
273 {
274 throw new SQLException("Not Supported !");
275 }
276
277
278 /***
279 *Gets the resultSetConcurrency attribute of the CsvStatement object
280 *
281 * @return The resultSetConcurrency value
282 * @exception SQLException Description of Exception
283 * @since
284 */
285 public int getResultSetConcurrency() throws SQLException
286 {
287 throw new SQLException("Not Supported !");
288 }
289
290
291 /***
292 *Gets the resultSetType attribute of the CsvStatement object
293 *
294 * @return The resultSetType value
295 * @exception SQLException Description of Exception
296 * @since
297 */
298 public int getResultSetType() throws SQLException
299 {
300 throw new SQLException("Not Supported !");
301 }
302
303
304 /***
305 *Gets the connection attribute of the CsvStatement object
306 *
307 * @return The connection value
308 * @exception SQLException Description of Exception
309 * @since
310 */
311 public Connection getConnection() throws SQLException
312 {
313 return connection;
314 }
315
316
317 /***
318 *Description of the Method
319 *
320 * @param sql Description of Parameter
321 * @return Description of the Returned Value
322 * @exception SQLException Description of Exception
323 * @since
324 */
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.getDoubleQuotesEscape(),
359 connection.getCarriageReturnEscape(),
360 connection.isTrimString()
361 );
362 String[] xxx = parser.getColumnNames();
363 String[] yyy = reader.getColumnNames();
364 boolean isOK = true;
365 for(int i=0; i< xxx.length; i++) {
366 if(!xxx[i].endsWith("*")) {
367 out:
368 for(int j=0; j< yyy.length; j++) {
369 if(xxx[i].equalsIgnoreCase(yyy[j])) {
370 isOK=true;
371 break out;
372 }
373 else
374 isOK=false;
375 }
376 if(!isOK)
377 throw new SQLException("Column '" + xxx[i] + "' not found.");
378 }
379 }
380 }
381 catch (Exception e)
382 {
383 // if( CsvDriver.DEBUG )
384 e.printStackTrace();
385 throw new SQLException("Error reading data file. Message was: " + e);
386 }
387 CsvResultSet resultSet = new CsvResultSet(this, reader,
388 parser.getTableName(), parser.getColumnNames(), parser.getWhereColumnNames(),
389 parser.getWhereColumnValues(),reader.getColumnTypes());
390 resultSets.add(resultSet);
391 return resultSet;
392 }
393
394
395
396 /***
397 *Description of the Method
398 *
399 * @param sql Description of Parameter
400 * @return Description of the Returned Value
401 * @exception SQLException Description of Exception
402 * @since
403 */
404 public int executeUpdate(String sql) throws SQLException
405 {
406 int updated=0;
407 DriverManager.println("CsvJdbc - CsvStatement:executeUpdate() - sql= " + sql);
408 CsvSqlParser parser = new CsvSqlParser();
409 this.sql = sql;
410 try
411 {
412 parser.parse(this);
413 }
414 catch (Exception e)
415 {
416 throw new SQLException("Syntax Error. " + e.getMessage());
417 }
418 if(parser.sqlType.equals(parser.SELECT))
419 throw new SQLException("Not supported SELECT statement - use executeQuery method");
420 else if (parser.sqlType.equals(parser.CREATE_TABLE)) {
421 throw new SQLException("Not supported CREATE TABLE statement - use execute method");
422 }
423 else if (parser.sqlType.equals(parser.INSERT)) {
424 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension();
425 File checkFile = new File(fileName);
426
427 if (!checkFile.exists())
428 {
429 throw new SQLException("Cannot open data file '" + fileName + "' !");
430 }
431
432 if (!checkFile.canWrite())
433 {
434 throw new SQLException("Data file '" + fileName + "' is read only !");
435 }
436 // CsvWriter writeCsv;
437 try
438 {
439 if(connection.getAutoCommit())
440 writeCsv = new CsvWriter(
441 fileName,
442 connection.getSeperator(),
443 connection.getExtension(),
444 connection.getMaxFileSize(),
445 connection.getCharset(),
446 connection.getUseQuotes(),
447 connection.getUseQuotesEscape()
448 );
449
450 else{
451 writeCsv.setFileName(fileName);
452 writeCsv.fillTableColumnNames();
453 }
454
455 String[] xxx = parser.getColumnNames();
456 String[] yyy = writeCsv.getColumnNames();
457 boolean isOK = true;
458 for(int i=0; i< xxx.length; i++) {
459 if(!xxx[i].endsWith("*")) {
460 out:
461 for(int j=0; j< yyy.length; j++) {
462 if(xxx[i].equalsIgnoreCase(yyy[j])) {
463 isOK=true;
464 break out;
465 }
466 else
467 isOK=false;
468 }
469 if(!isOK)
470 throw new SQLException("Column '" + xxx[i] + "' not found.");
471 }
472 }
473 writeCsv.newLine(parser.columnNames, parser.columnValues);
474
475 }
476 catch (Exception e)
477 {
478 throw new SQLException("Error reading data file. Message was: " + e);
479 }
480
481 }
482 else if (parser.sqlType.equals(parser.UPDATE)) {
483
484 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension();
485 File checkFile = new File(fileName);
486
487 if (!checkFile.exists())
488 {
489 throw new SQLException("Cannot open data file '" + fileName + "' !");
490 }
491
492 if (!checkFile.canWrite())
493 {
494 throw new SQLException("Data file '" + fileName + "' is read only !");
495 }
496 // CsvWriter writeCsv;
497 try
498 {
499 if(connection.getAutoCommit())
500 writeCsv = new CsvWriter(
501 fileName,
502 connection.getSeperator(),
503 connection.getExtension(),
504 connection.getMaxFileSize(),
505 connection.getCharset(),
506 connection.getUseQuotes(),
507 connection.getUseQuotesEscape()
508 );
509 else{
510 writeCsv.setFileName(fileName);
511 writeCsv.fillTableColumnNames();
512 }
513
514 String[] xxx = parser.getColumnNames();
515 String[] yyy = writeCsv.getColumnNames();
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.");
530 }
531 }
532 if(!writeCsv.updateFields(parser.columnNames, parser.columnValues, parser.columnWhereNames, parser.columnWhereValues))
533 updated = -1;
534 }
535 catch (Exception e)
536 {
537 e.printStackTrace();
538 throw new SQLException("Error reading data file. Message was: " + e);
539 }
540
541 }
542 return updated;
543
544 }
545
546
547 /***
548 * Releases this <code>Statement</code> object's database
549 * and JDBC resources immediately instead of waiting for
550 * this to happen when it is automatically closed.
551 * It is generally good practice to release resources as soon as
552 * you are finished with them to avoid tying up database
553 * resources.
554 * <P>
555 * Calling the method <code>close</code> on a <code>Statement</code>
556 * object that is already closed has no effect.
557 * <P>
558 * <B>Note:</B> A <code>Statement</code> object is automatically closed
559 * when it is garbage collected. When a <code>Statement</code> object is
560 * closed, its current <code>ResultSet</code> object, if one exists, is
561 * also closed.
562 *
563 * @exception SQLException if a database access error occurs
564 */
565 public void close() throws SQLException {
566 // close all result sets
567 for(Enumeration i = resultSets.elements(); i.hasMoreElements(); ) {
568 CsvResultSet resultSet = (CsvResultSet) i.nextElement();
569 resultSet.close();
570 }
571 try {
572 if(this.writeCsv!=null)
573 this.writeCsv.close();
574 // this.writeCsv.output.close();
575 }
576 catch(Exception e) {
577 throw new SQLException(e.getMessage());
578 }
579
580 }
581
582 /***
583 *Description of the Method
584 *
585 * @exception SQLException Description of Exception
586 * @since
587 */
588 public void cancel() throws SQLException
589 {
590 throw new SQLException("Not Supported !");
591 }
592
593
594 /***
595 *Description of the Method
596 *
597 * @exception SQLException Description of Exception
598 * @since
599 */
600 public void clearWarnings() throws SQLException
601 {
602 throw new SQLException("Not Supported !");
603 }
604
605
606 /***
607 *Description of the Method
608 *
609 * @param sql Description of Parameter
610 * @return Description of the Returned Value
611 * @exception SQLException Description of Exception
612 * @since
613 */
614 public boolean execute(String sql) throws SQLException
615 {
616
617 CsvSqlParser parser = new CsvSqlParser();
618 this.sql = sql;
619 try
620 {
621 parser.parse(this);
622 }
623 catch (Exception e)
624 {
625 throw new SQLException("Syntax Error. " + e.getMessage());
626 }
627 if(parser.sqlType.equals(parser.SELECT))
628 throw new SQLException("Not supported SELECT statement - use executeQuery method");
629 else if (parser.sqlType.equals(parser.INSERT))
630 executeUpdate(sql);
631 else if (parser.sqlType.equals(parser.CREATE_TABLE)) {
632 String fileName = connection.getPath() + parser.getTableName() + connection.getExtension();
633 File checkFile = new File(fileName);
634
635 if (checkFile.exists())
636 {
637 throw new SQLException("Data file '" + fileName + "'already exists !");
638 }
639
640 // CsvWriter writeCsv;
641 try {
642 if (connection.getAutoCommit())
643 writeCsv = new CsvWriter(
644 fileName,
645 connection.getSeperator(),
646 connection.getExtension(),
647 connection.getMaxFileSize(),
648 connection.getCharset(),
649 connection.getUseQuotes(),
650 connection.getUseQuotesEscape()
651 );
652 else {
653 writeCsv.setFileName(fileName);
654 writeCsv.fillTableColumnNames();
655 }
656
657 writeCsv.createTable(parser.columnNames, fileName);
658 }
659 catch (Exception e)
660 {
661 throw new SQLException("Error reading data file. Message was: " + e);
662 }
663 }
664 return true;
665
666 }
667
668
669
670 /***
671 *Adds a feature to the Batch attribute of the CsvStatement object
672 *
673 * @param p0 The feature to be added to the Batch attribute
674 * @exception SQLException Description of Exception
675 * @since
676 */
677 public void addBatch(String p0) throws SQLException
678 {
679 throw new SQLException("Not Supported !");
680 }
681
682
683 /***
684 *Description of the Method
685 *
686 * @exception SQLException Description of Exception
687 * @since
688 */
689 public void clearBatch() throws SQLException
690 {
691 throw new SQLException("Not Supported !");
692 }
693
694
695 /***
696 *Description of the Method
697 *
698 * @return Description of the Returned Value
699 * @exception SQLException Description of Exception
700 * @since
701 */
702 public int[] executeBatch() throws SQLException
703 {
704 throw new SQLException("Not Supported !");
705 }
706
707 //---------------------------------------------------------------------
708 // JDBC 3.0
709 //---------------------------------------------------------------------
710
711 public boolean getMoreResults(int current) throws SQLException {
712 throw new UnsupportedOperationException("Statement.getMoreResults(int) unsupported");
713 }
714
715 public ResultSet getGeneratedKeys() throws SQLException {
716 throw new UnsupportedOperationException("Statement.getGeneratedKeys() unsupported");
717 }
718
719 public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
720 throw new UnsupportedOperationException("Statement.executeUpdate(String,int) unsupported");
721 }
722
723 public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
724 throw new UnsupportedOperationException("Statement.executeUpdate(String,int[]) unsupported");
725 }
726
727 public int executeUpdate(String sql, String[] columnNames) throws SQLException {
728 throw new UnsupportedOperationException("Statement.executeUpdate(String,String[]) unsupported");
729 }
730
731 public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
732 throw new UnsupportedOperationException("Statement.execute(String,int) unsupported");
733 }
734
735 public boolean execute(String sql, int[] columnIndexes) throws SQLException {
736 throw new UnsupportedOperationException("Statement.execute(String,int[]) unsupported");
737 }
738
739 public boolean execute(String sql, String[] columnNames) throws SQLException {
740 throw new UnsupportedOperationException("Statement.execute(String,String[]) unsupported");
741 }
742
743 public int getResultSetHoldability() throws SQLException {
744 throw new UnsupportedOperationException("Statement.getResultSetHoldability() unsupported");
745 }
746
747 public String getSqlStatement() {
748 return sql;
749 }
750 }
751
This page was automatically generated by Maven