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