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