KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > relique > jdbc > csv > CsvStatement


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 JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Vector JavaDoc;
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 JavaDoc resultSets = new Vector JavaDoc();
37     private CsvWriter writeCsv;
38     private String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e)
335             {
336                 throw new SQLException("Syntax Error. " + e.getMessage());
337             }
338
339     String JavaDoc fileName = connection.getPath() + parser.getTableName() + connection.getExtension();
340     File JavaDoc checkFile = new File JavaDoc(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                                        );
361       String JavaDoc[] xxx = parser.getColumnNames();
362       String JavaDoc[] 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 JavaDoc e)
381     {
382 // if( CsvDriver.DEBUG )
383
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     /**
396      *Description of the Method
397      *
398      * @param sql Description of Parameter
399      * @return Description of the Returned Value
400      * @exception SQLException Description of Exception
401      * @since
402      */

403     public int executeUpdate(String JavaDoc 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 JavaDoc 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 JavaDoc fileName = connection.getPath() + parser.getTableName() + connection.getExtension();
424             File JavaDoc checkFile = new File JavaDoc(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 // CsvWriter writeCsv;
436
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 JavaDoc[] xxx = parser.getColumnNames();
455                 String JavaDoc[] 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 JavaDoc 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 JavaDoc fileName = connection.getPath() + parser.getTableName() + connection.getExtension();
484             File JavaDoc checkFile = new File JavaDoc(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 // CsvWriter writeCsv;
496
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 JavaDoc[] xxx = parser.getColumnNames();
514                 String JavaDoc[] 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 JavaDoc 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     /**
547      * Releases this <code>Statement</code> object's database
548      * and JDBC resources immediately instead of waiting for
549      * this to happen when it is automatically closed.
550      * It is generally good practice to release resources as soon as
551      * you are finished with them to avoid tying up database
552      * resources.
553      * <P>
554      * Calling the method <code>close</code> on a <code>Statement</code>
555      * object that is already closed has no effect.
556      * <P>
557      * <B>Note:</B> A <code>Statement</code> object is automatically closed
558      * when it is garbage collected. When a <code>Statement</code> object is
559      * closed, its current <code>ResultSet</code> object, if one exists, is
560      * also closed.
561      *
562      * @exception SQLException if a database access error occurs
563      */

564     public void close() throws SQLException {
565       // close all result sets
566
for(Enumeration JavaDoc 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 // this.writeCsv.output.close();
574
}
575       catch(Exception JavaDoc e) {
576         throw new SQLException(e.getMessage());
577       }
578
579     }
580
581     /**
582      *Description of the Method
583      *
584      * @exception SQLException Description of Exception
585      * @since
586      */

587     public void cancel() throws SQLException
588     {
589         throw new SQLException("Not Supported !");
590     }
591
592
593     /**
594      *Description of the Method
595      *
596      * @exception SQLException Description of Exception
597      * @since
598      */

599     public void clearWarnings() throws SQLException
600     {
601         throw new SQLException("Not Supported !");
602     }
603
604
605     /**
606      *Description of the Method
607      *
608      * @param sql Description of Parameter
609      * @return Description of the Returned Value
610      * @exception SQLException Description of Exception
611      * @since
612      */

613     public boolean execute(String JavaDoc 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 JavaDoc 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 JavaDoc fileName = connection.getPath() + parser.getTableName() + connection.getExtension();
632             File JavaDoc checkFile = new File JavaDoc(fileName);
633
634             if (checkFile.exists())
635                     {
636                       throw new SQLException("Data file '" + fileName + "'already exists !");
637                     }
638
639               // CsvWriter writeCsv;
640
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 JavaDoc e)
659             {
660             throw new SQLException("Error reading data file. Message was: " + e);
661             }
662         }
663         return true;
664
665   }
666
667
668
669     /**
670      *Adds a feature to the Batch attribute of the CsvStatement object
671      *
672      * @param p0 The feature to be added to the Batch attribute
673      * @exception SQLException Description of Exception
674      * @since
675      */

676     public void addBatch(String JavaDoc p0) throws SQLException
677     {
678         throw new SQLException("Not Supported !");
679     }
680
681
682     /**
683      *Description of the Method
684      *
685      * @exception SQLException Description of Exception
686      * @since
687      */

688     public void clearBatch() throws SQLException
689     {
690         throw new SQLException("Not Supported !");
691     }
692
693
694     /**
695      *Description of the Method
696      *
697      * @return Description of the Returned Value
698      * @exception SQLException Description of Exception
699      * @since
700      */

701     public int[] executeBatch() throws SQLException
702     {
703         throw new SQLException("Not Supported !");
704     }
705
706     //---------------------------------------------------------------------
707
// JDBC 3.0
708
//---------------------------------------------------------------------
709

710     public boolean getMoreResults(int current) throws SQLException {
711         throw new UnsupportedOperationException JavaDoc("Statement.getMoreResults(int) unsupported");
712     }
713
714     public ResultSet getGeneratedKeys() throws SQLException {
715         throw new UnsupportedOperationException JavaDoc("Statement.getGeneratedKeys() unsupported");
716     }
717
718     public int executeUpdate(String JavaDoc sql, int autoGeneratedKeys) throws SQLException {
719         throw new UnsupportedOperationException JavaDoc("Statement.executeUpdate(String,int) unsupported");
720     }
721
722     public int executeUpdate(String JavaDoc sql, int[] columnIndexes) throws SQLException {
723         throw new UnsupportedOperationException JavaDoc("Statement.executeUpdate(String,int[]) unsupported");
724     }
725
726     public int executeUpdate(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException {
727         throw new UnsupportedOperationException JavaDoc("Statement.executeUpdate(String,String[]) unsupported");
728     }
729
730     public boolean execute(String JavaDoc sql, int autoGeneratedKeys) throws SQLException {
731         throw new UnsupportedOperationException JavaDoc("Statement.execute(String,int) unsupported");
732     }
733
734     public boolean execute(String JavaDoc sql, int[] columnIndexes) throws SQLException {
735         throw new UnsupportedOperationException JavaDoc("Statement.execute(String,int[]) unsupported");
736     }
737
738     public boolean execute(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException {
739         throw new UnsupportedOperationException JavaDoc("Statement.execute(String,String[]) unsupported");
740     }
741
742     public int getResultSetHoldability() throws SQLException {
743         throw new UnsupportedOperationException JavaDoc("Statement.getResultSetHoldability() unsupported");
744     }
745
746     public String JavaDoc getSqlStatement() {
747       return sql;
748     }
749 }
750
751
Popular Tags