001: /**
002: Copyright (C) 2002-2003 Together
003:
004: This library is free software; you can redistribute it and/or
005: modify it under the terms of the GNU Lesser General Public
006: License as published by the Free Software Foundation; either
007: version 2.1 of the License, or (at your option) any later version.
008:
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public
015: License along with this library; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: */package org.relique.jdbc.csv;
019:
020: import java.sql.*;
021: import java.io.File;
022: import java.util.Enumeration;
023: import java.util.Vector;
024:
025: /**
026: * This class implements the Statement interface for the CsvJdbc driver.
027: *
028: * @author Zoran Milakovic
029: */
030:
031: public class CsvStatement implements Statement {
032: private CsvConnection connection;
033: private Vector resultSets = new Vector();
034: private CsvWriter writeCsv;
035: private String sql;
036:
037: /**
038: *Constructor for the CsvStatement object
039: *
040: * @param connection Description of Parameter
041: * @since
042: */
043: protected CsvStatement(CsvConnection connection) {
044: DriverManager.println("CsvJdbc - CsvStatement() - connection="
045: + connection);
046: this .connection = connection;
047:
048: try {
049: if (!connection.getAutoCommit())
050: writeCsv = new CsvWriter(null, connection
051: .getSeperator(), connection.getExtension(),
052: connection.getMaxFileSize(), connection
053: .getCharset(), connection
054: .getUseQuotes(), connection
055: .getUseQuotesEscape());
056: } catch (Exception ex) {
057: ex.printStackTrace();
058: }
059:
060: }
061:
062: /**
063: *Sets the maxFieldSize attribute of the CsvStatement object
064: *
065: * @param p0 The new maxFieldSize value
066: * @exception SQLException Description of Exception
067: * @since
068: */
069: public void setMaxFieldSize(int p0) throws SQLException {
070: throw new SQLException("Not Supported !");
071: }
072:
073: /**
074: *Sets the maxRows attribute of the CsvStatement object
075: *
076: * @param p0 The new maxRows value
077: * @exception SQLException Description of Exception
078: * @since
079: */
080: public void setMaxRows(int p0) throws SQLException {
081: throw new SQLException("Not Supported !");
082: }
083:
084: /**
085: *Sets the escapeProcessing attribute of the CsvStatement object
086: *
087: * @param p0 The new escapeProcessing value
088: * @exception SQLException Description of Exception
089: * @since
090: */
091: public void setEscapeProcessing(boolean p0) throws SQLException {
092: throw new SQLException("Not Supported !");
093: }
094:
095: /**
096: *Sets the queryTimeout attribute of the CsvStatement object
097: *
098: * @param p0 The new queryTimeout value
099: * @exception SQLException Description of Exception
100: * @since
101: */
102: public void setQueryTimeout(int p0) throws SQLException {
103: throw new SQLException("Not Supported !");
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: throw new SQLException("Not Supported !");
115: }
116:
117: /**
118: *Sets the fetchDirection attribute of the CsvStatement object
119: *
120: * @param p0 The new fetchDirection value
121: * @exception SQLException Description of Exception
122: * @since
123: */
124: public void setFetchDirection(int p0) throws SQLException {
125: throw new SQLException("Not Supported !");
126: }
127:
128: /**
129: *Sets the fetchSize attribute of the CsvStatement object
130: *
131: * @param p0 The new fetchSize value
132: * @exception SQLException Description of Exception
133: * @since
134: */
135: public void setFetchSize(int p0) throws SQLException {
136: throw new SQLException("Not Supported !");
137: }
138:
139: /**
140: *Gets the maxFieldSize attribute of the CsvStatement object
141: *
142: * @return The maxFieldSize value
143: * @exception SQLException Description of Exception
144: * @since
145: */
146: public int getMaxFieldSize() throws SQLException {
147: throw new SQLException("Not Supported !");
148: }
149:
150: /**
151: *Gets the maxRows attribute of the CsvStatement object
152: *
153: * @return The maxRows value
154: * @exception SQLException Description of Exception
155: * @since
156: */
157: public int getMaxRows() throws SQLException {
158: throw new SQLException("Not Supported !");
159: }
160:
161: /**
162: *Gets the queryTimeout attribute of the CsvStatement object
163: *
164: * @return The queryTimeout value
165: * @exception SQLException Description of Exception
166: * @since
167: */
168: public int getQueryTimeout() throws SQLException {
169: throw new SQLException("Not Supported !");
170: }
171:
172: /**
173: *Gets the warnings attribute of the CsvStatement object
174: *
175: * @return The warnings value
176: * @exception SQLException Description of Exception
177: * @since
178: */
179: public SQLWarning getWarnings() throws SQLException {
180: throw new SQLException("Not Supported !");
181: }
182:
183: /**
184: *Gets the resultSet attribute of the CsvStatement object
185: *
186: * @return The resultSet value
187: * @exception SQLException Description of Exception
188: * @since
189: */
190: public ResultSet getResultSet() throws SQLException {
191: throw new SQLException("Not Supported !");
192: }
193:
194: /**
195: *Gets the updateCount attribute of the CsvStatement object
196: *
197: * @return The updateCount value
198: * @exception SQLException Description of Exception
199: * @since
200: */
201: public int getUpdateCount() throws SQLException {
202: throw new SQLException("Not Supported !");
203: }
204:
205: /**
206: *Gets the moreResults attribute of the CsvStatement object
207: *
208: * @return The moreResults value
209: * @exception SQLException Description of Exception
210: * @since
211: */
212: public boolean getMoreResults() throws SQLException {
213: throw new SQLException("Not Supported !");
214: }
215:
216: /**
217: *Gets the fetchDirection attribute of the CsvStatement object
218: *
219: * @return The fetchDirection value
220: * @exception SQLException Description of Exception
221: * @since
222: */
223: public int getFetchDirection() throws SQLException {
224: throw new SQLException("Not Supported !");
225: }
226:
227: /**
228: *Gets the fetchSize attribute of the CsvStatement object
229: *
230: * @return The fetchSize value
231: * @exception SQLException Description of Exception
232: * @since
233: */
234: public int getFetchSize() throws SQLException {
235: throw new SQLException("Not Supported !");
236: }
237:
238: /**
239: *Gets the resultSetConcurrency attribute of the CsvStatement object
240: *
241: * @return The resultSetConcurrency value
242: * @exception SQLException Description of Exception
243: * @since
244: */
245: public int getResultSetConcurrency() throws SQLException {
246: throw new SQLException("Not Supported !");
247: }
248:
249: /**
250: *Gets the resultSetType attribute of the CsvStatement object
251: *
252: * @return The resultSetType value
253: * @exception SQLException Description of Exception
254: * @since
255: */
256: public int getResultSetType() throws SQLException {
257: throw new SQLException("Not Supported !");
258: }
259:
260: /**
261: *Gets the connection attribute of the CsvStatement object
262: *
263: * @return The connection value
264: * @exception SQLException Description of Exception
265: * @since
266: */
267: public Connection getConnection() throws SQLException {
268: return connection;
269: }
270:
271: /**
272: *Description of the Method
273: *
274: * @param sql Description of Parameter
275: * @return Description of the Returned Value
276: * @exception SQLException Description of Exception
277: * @since
278: */
279: public ResultSet executeQuery(String sql) throws SQLException {
280: DriverManager
281: .println("CsvJdbc - CsvStatement:executeQuery() - sql= "
282: + sql);
283: CsvSqlParser parser = new CsvSqlParser();
284: this .sql = sql;
285: try {
286: parser.parse(this );
287: } catch (Exception e) {
288: throw new SQLException("Syntax Error. " + e.getMessage());
289: }
290:
291: String fileName = connection.getPath() + parser.getTableName()
292: + connection.getExtension();
293: File checkFile = new File(fileName);
294: if (!checkFile.exists()) {
295: throw new SQLException("Cannot open data file '" + fileName
296: + "' !");
297: }
298:
299: if (!checkFile.canRead()) {
300: throw new SQLException("Data file '" + fileName
301: + "' not readable !");
302: }
303: CsvReader reader;
304: try {
305: reader = new CsvReader(fileName, connection.getSeperator(),
306: connection.isSuppressHeaders(), connection
307: .getCharset(), connection.getExtension(),
308: connection.getLineBreakEscape(),
309: // connection.getDoubleQuotesEscape(),
310: connection.getCarriageReturnEscape(), connection
311: .isTrimString());
312: String[] xxx = parser.getColumnNames();
313: String[] yyy = reader.getColumnNames();
314: boolean isOK = true;
315: for (int i = 0; i < xxx.length; i++) {
316: if (!xxx[i].endsWith("*")) {
317: out: for (int j = 0; j < yyy.length; j++) {
318: if (xxx[i].equalsIgnoreCase(yyy[j])) {
319: isOK = true;
320: break out;
321: } else
322: isOK = false;
323: }
324: if (!isOK)
325: throw new SQLException("Column '" + xxx[i]
326: + "' not found.");
327: }
328: }
329: } catch (Exception e) {
330: // if( CsvDriver.DEBUG )
331: e.printStackTrace();
332: throw new SQLException(
333: "Error reading data file. Message was: " + e);
334: }
335: CsvResultSet resultSet = new CsvResultSet(this , reader, parser
336: .getTableName(), parser.getColumnNames(), parser
337: .getWhereColumnNames(), parser.getWhereColumnValues(),
338: reader.getColumnTypes());
339: resultSets.add(resultSet);
340: return resultSet;
341: }
342:
343: /**
344: *Description of the Method
345: *
346: * @param sql Description of Parameter
347: * @return Description of the Returned Value
348: * @exception SQLException Description of Exception
349: * @since
350: */
351: public int executeUpdate(String sql) throws SQLException {
352: int updated = 0;
353: DriverManager
354: .println("CsvJdbc - CsvStatement:executeUpdate() - sql= "
355: + sql);
356: CsvSqlParser parser = new CsvSqlParser();
357: this .sql = sql;
358: try {
359: parser.parse(this );
360: } catch (Exception e) {
361: throw new SQLException("Syntax Error. " + e.getMessage());
362: }
363: if (parser.sqlType.equals(parser.SELECT))
364: throw new SQLException(
365: "Not supported SELECT statement - use executeQuery method");
366: else if (parser.sqlType.equals(parser.CREATE_TABLE)) {
367: throw new SQLException(
368: "Not supported CREATE TABLE statement - use execute method");
369: } else if (parser.sqlType.equals(parser.INSERT)) {
370: String fileName = connection.getPath()
371: + parser.getTableName() + connection.getExtension();
372: File checkFile = new File(fileName);
373:
374: if (!checkFile.exists()) {
375: throw new SQLException("Cannot open data file '"
376: + fileName + "' !");
377: }
378:
379: if (!checkFile.canWrite()) {
380: throw new SQLException("Data file '" + fileName
381: + "' is read only !");
382: }
383: // CsvWriter writeCsv;
384: try {
385: if (connection.getAutoCommit())
386: writeCsv = new CsvWriter(fileName, connection
387: .getSeperator(), connection.getExtension(),
388: connection.getMaxFileSize(), connection
389: .getCharset(), connection
390: .getUseQuotes(), connection
391: .getUseQuotesEscape());
392:
393: else {
394: writeCsv.setFileName(fileName);
395: writeCsv.fillTableColumnNames();
396: }
397:
398: String[] xxx = parser.getColumnNames();
399: String[] yyy = writeCsv.getColumnNames();
400: boolean isOK = true;
401: for (int i = 0; i < xxx.length; i++) {
402: if (!xxx[i].endsWith("*")) {
403: out: for (int j = 0; j < yyy.length; j++) {
404: if (xxx[i].equalsIgnoreCase(yyy[j])) {
405: isOK = true;
406: break out;
407: } else
408: isOK = false;
409: }
410: if (!isOK)
411: throw new SQLException("Column '" + xxx[i]
412: + "' not found.");
413: }
414: }
415: writeCsv.newLine(parser.columnNames,
416: parser.columnValues);
417:
418: } catch (Exception e) {
419: throw new SQLException(
420: "Error reading data file. Message was: " + e);
421: }
422:
423: } else if (parser.sqlType.equals(parser.UPDATE)) {
424:
425: String fileName = connection.getPath()
426: + parser.getTableName() + connection.getExtension();
427: File checkFile = new File(fileName);
428:
429: if (!checkFile.exists()) {
430: throw new SQLException("Cannot open data file '"
431: + fileName + "' !");
432: }
433:
434: if (!checkFile.canWrite()) {
435: throw new SQLException("Data file '" + fileName
436: + "' is read only !");
437: }
438: // CsvWriter writeCsv;
439: try {
440: if (connection.getAutoCommit())
441: writeCsv = new CsvWriter(fileName, connection
442: .getSeperator(), connection.getExtension(),
443: connection.getMaxFileSize(), connection
444: .getCharset(), connection
445: .getUseQuotes(), connection
446: .getUseQuotesEscape());
447: else {
448: writeCsv.setFileName(fileName);
449: writeCsv.fillTableColumnNames();
450: }
451:
452: String[] xxx = parser.getColumnNames();
453: String[] yyy = writeCsv.getColumnNames();
454: boolean isOK = true;
455: for (int i = 0; i < xxx.length; i++) {
456: if (!xxx[i].endsWith("*")) {
457: out: for (int j = 0; j < yyy.length; j++) {
458: if (xxx[i].equalsIgnoreCase(yyy[j])) {
459: isOK = true;
460: break out;
461: } else
462: isOK = false;
463: }
464: if (!isOK)
465: throw new SQLException("Column '" + xxx[i]
466: + "' not found.");
467: }
468: }
469: if (!writeCsv.updateFields(parser.columnNames,
470: parser.columnValues, parser.columnWhereNames,
471: parser.columnWhereValues))
472: updated = -1;
473: } catch (Exception e) {
474: e.printStackTrace();
475: throw new SQLException(
476: "Error reading data file. Message was: " + e);
477: }
478:
479: }
480: return updated;
481:
482: }
483:
484: /**
485: * Releases this <code>Statement</code> object's database
486: * and JDBC resources immediately instead of waiting for
487: * this to happen when it is automatically closed.
488: * It is generally good practice to release resources as soon as
489: * you are finished with them to avoid tying up database
490: * resources.
491: * <P>
492: * Calling the method <code>close</code> on a <code>Statement</code>
493: * object that is already closed has no effect.
494: * <P>
495: * <B>Note:</B> A <code>Statement</code> object is automatically closed
496: * when it is garbage collected. When a <code>Statement</code> object is
497: * closed, its current <code>ResultSet</code> object, if one exists, is
498: * also closed.
499: *
500: * @exception SQLException if a database access error occurs
501: */
502: public void close() throws SQLException {
503: // close all result sets
504: for (Enumeration i = resultSets.elements(); i.hasMoreElements();) {
505: CsvResultSet resultSet = (CsvResultSet) i.nextElement();
506: resultSet.close();
507: }
508: try {
509: if (this .writeCsv != null)
510: this .writeCsv.close();
511: // this.writeCsv.output.close();
512: } catch (Exception e) {
513: throw new SQLException(e.getMessage());
514: }
515:
516: }
517:
518: /**
519: *Description of the Method
520: *
521: * @exception SQLException Description of Exception
522: * @since
523: */
524: public void cancel() throws SQLException {
525: throw new SQLException("Not Supported !");
526: }
527:
528: /**
529: *Description of the Method
530: *
531: * @exception SQLException Description of Exception
532: * @since
533: */
534: public void clearWarnings() throws SQLException {
535: throw new SQLException("Not Supported !");
536: }
537:
538: /**
539: *Description of the Method
540: *
541: * @param sql Description of Parameter
542: * @return Description of the Returned Value
543: * @exception SQLException Description of Exception
544: * @since
545: */
546: public boolean execute(String sql) throws SQLException {
547:
548: CsvSqlParser parser = new CsvSqlParser();
549: this .sql = sql;
550: try {
551: parser.parse(this );
552: } catch (Exception e) {
553: throw new SQLException("Syntax Error. " + e.getMessage());
554: }
555: if (parser.sqlType.equals(parser.SELECT))
556: throw new SQLException(
557: "Not supported SELECT statement - use executeQuery method");
558: else if (parser.sqlType.equals(parser.INSERT))
559: executeUpdate(sql);
560: else if (parser.sqlType.equals(parser.CREATE_TABLE)) {
561: String fileName = connection.getPath()
562: + parser.getTableName() + connection.getExtension();
563: File checkFile = new File(fileName);
564:
565: if (checkFile.exists()) {
566: throw new SQLException("Data file '" + fileName
567: + "'already exists !");
568: }
569:
570: // CsvWriter writeCsv;
571: try {
572: if (connection.getAutoCommit())
573: writeCsv = new CsvWriter(fileName, connection
574: .getSeperator(), connection.getExtension(),
575: connection.getMaxFileSize(), connection
576: .getCharset(), connection
577: .getUseQuotes(), connection
578: .getUseQuotesEscape());
579: else {
580: writeCsv.setFileName(fileName);
581: writeCsv.fillTableColumnNames();
582: }
583:
584: writeCsv.createTable(parser.columnNames, fileName);
585: } catch (Exception e) {
586: throw new SQLException(
587: "Error reading data file. Message was: " + e);
588: }
589: }
590: return true;
591:
592: }
593:
594: /**
595: *Adds a feature to the Batch attribute of the CsvStatement object
596: *
597: * @param p0 The feature to be added to the Batch attribute
598: * @exception SQLException Description of Exception
599: * @since
600: */
601: public void addBatch(String p0) throws SQLException {
602: throw new SQLException("Not Supported !");
603: }
604:
605: /**
606: *Description of the Method
607: *
608: * @exception SQLException Description of Exception
609: * @since
610: */
611: public void clearBatch() throws SQLException {
612: throw new SQLException("Not Supported !");
613: }
614:
615: /**
616: *Description of the Method
617: *
618: * @return Description of the Returned Value
619: * @exception SQLException Description of Exception
620: * @since
621: */
622: public int[] executeBatch() throws SQLException {
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(
632: "Statement.getMoreResults(int) unsupported");
633: }
634:
635: public ResultSet getGeneratedKeys() throws SQLException {
636: throw new UnsupportedOperationException(
637: "Statement.getGeneratedKeys() unsupported");
638: }
639:
640: public int executeUpdate(String sql, int autoGeneratedKeys)
641: throws SQLException {
642: throw new UnsupportedOperationException(
643: "Statement.executeUpdate(String,int) unsupported");
644: }
645:
646: public int executeUpdate(String sql, int[] columnIndexes)
647: throws SQLException {
648: throw new UnsupportedOperationException(
649: "Statement.executeUpdate(String,int[]) unsupported");
650: }
651:
652: public int executeUpdate(String sql, String[] columnNames)
653: throws SQLException {
654: throw new UnsupportedOperationException(
655: "Statement.executeUpdate(String,String[]) unsupported");
656: }
657:
658: public boolean execute(String sql, int autoGeneratedKeys)
659: throws SQLException {
660: throw new UnsupportedOperationException(
661: "Statement.execute(String,int) unsupported");
662: }
663:
664: public boolean execute(String sql, int[] columnIndexes)
665: throws SQLException {
666: throw new UnsupportedOperationException(
667: "Statement.execute(String,int[]) unsupported");
668: }
669:
670: public boolean execute(String sql, String[] columnNames)
671: throws SQLException {
672: throw new UnsupportedOperationException(
673: "Statement.execute(String,String[]) unsupported");
674: }
675:
676: public int getResultSetHoldability() throws SQLException {
677: throw new UnsupportedOperationException(
678: "Statement.getResultSetHoldability() unsupported");
679: }
680:
681: public String getSqlStatement() {
682: return sql;
683: }
684:
685: public boolean isClosed() throws SQLException {
686: // TODO Auto-generated method stub
687: return false;
688: }
689:
690: public boolean isPoolable() throws SQLException {
691: // TODO Auto-generated method stub
692: return false;
693: }
694:
695: public void setPoolable(boolean poolable) throws SQLException {
696: // TODO Auto-generated method stub
697: throw new UnsupportedOperationException(
698: "Statement.setPoolable(boolean) unsupported");
699:
700: }
701:
702: public boolean isWrapperFor(Class<?> iface) throws SQLException {
703: // TODO Auto-generated method stub
704: return false;
705: }
706:
707: public <T> T unwrap(Class<T> iface) throws SQLException {
708: // TODO Auto-generated method stub
709: return null;
710: }
711: }
|