KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > xml > XmlSqlParser


1 /*
2     Copyright (C) 2003 Together
3     This library is free software; you can redistribute it and/or
4     modify it under the terms of the GNU Lesser General Public
5     License as published by the Free Software Foundation; either
6     version 2.1 of the License, or (at your option) any later version.
7     This library is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10     Lesser General Public License for more details.
11     You should have received a copy of the GNU Lesser General Public
12     License along with this library; if not, write to the Free Software
13     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  */

15
16 package org.webdocwf.util.xml;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Vector JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.io.*;
23
24 /**
25  * This is a simple SQL parser used by the Xml JDBC driver.
26  *
27  * @author Zoran Milakovic
28  */

29 public class XmlSqlParser {
30   /**
31    *Description of the Field
32    *
33    * @since
34    */

35
36   public static final String JavaDoc CREATE_TABLE = "create_table";
37   public static final String JavaDoc DROP_TABLE = "drop_table";
38   public static final String JavaDoc INSERT = "insert";
39   public static final String JavaDoc UPDATE = "update";
40   public static final String JavaDoc SELECT = "select";
41   public static final String JavaDoc DELETE = "delete";
42
43   public static final String JavaDoc quoteEscape = "''";
44   private static final String JavaDoc commaEscape = "~#####1~";
45   public static final String JavaDoc equalEscape = "~EQUAL~";
46   public static final String JavaDoc atEscape = "~AT~";
47   public static final String JavaDoc slashEscape = "~SLASH~";
48
49   public static final String JavaDoc BINARY_STREAM_OBJECT =
50       "XmlDriverBinaryStreamObject";
51
52   private ArrayList JavaDoc binaryStreamObjectList = new ArrayList JavaDoc();
53
54   private HashMap JavaDoc oldValues = new HashMap JavaDoc();
55
56   private boolean isAutoCommit = true;
57
58   private String JavaDoc fileName = "";
59
60   public String JavaDoc tableName;
61
62   public String JavaDoc whereStatement;
63
64   public String JavaDoc sqlType;
65
66   private String JavaDoc sqlStatement = "";
67
68   public String JavaDoc[] columnNames;
69
70   public String JavaDoc[] primaryKeyColumns;
71
72   public String JavaDoc[] notnullColumns;
73
74   public String JavaDoc[] columnValues;
75
76   public String JavaDoc[] columnWhereNames;
77
78   public String JavaDoc[] columnWhereValues;
79
80   public XmlSqlParser() {
81   }
82
83   public XmlSqlParser( String JavaDoc fileName, boolean isAutoCommit ) {
84     this.fileName = fileName;
85     this.isAutoCommit = isAutoCommit;
86   }
87
88   /**
89    *Gets the tableName attribute of the SqlParser object
90    *
91    * @return The tableName value
92    * @since
93    */

94   public String JavaDoc getTableName() {
95     return tableName;
96   }
97
98   /**
99    * Gets columns which can not be NULL
100    *
101    * @return The tableName value
102    * @since
103    */

104   public String JavaDoc[] getNotnullColumns() {
105     return this.notnullColumns;
106   }
107
108   /**
109    *Gets the type of sql statement.INSERT , UPDATE , CREATE , SELECT, DROP_TABLE, CREATE_TABLE
110    *
111    * @return The type of sql statement.
112    * @since
113    */

114   public String JavaDoc getSQLType() {
115     return this.sqlType;
116   }
117
118   /**
119    *Gets the columnNames attribute of the SqlParser object
120    *
121    * @return The columnNames value
122    * @since
123    */

124   public String JavaDoc[] getColumnNames() {
125     return columnNames;
126   }
127
128   public String JavaDoc[] getWhereColumnNames() {
129     return columnWhereNames;
130   }
131
132   /**
133    * Gets array of column values.Used in INSERT , UPDATE statements
134    * @return columnValues as array of string.
135    */

136   public String JavaDoc[] getColumnValues() {
137     return columnValues;
138   }
139
140   /**
141    * Gets array of column which are primarykeys.Used in INSERT statements
142    * @return primarykeys as array of string.
143    */

144   public String JavaDoc[] getPrimaryKeys() {
145     return primaryKeyColumns;
146   }
147
148   public String JavaDoc[] getWhereColumnValues() {
149     return columnWhereValues;
150   }
151
152   public String JavaDoc getSqlStatement() {
153     return this.sqlStatement;
154   }
155
156   /**
157    * Description of the Method
158    *
159    * @param sql Description of Parameter
160    * @exception Exception Description of Exception
161    * @since
162    */

163   public void parse( String JavaDoc sql ) throws Exception JavaDoc {
164     this.sqlStatement = sql;
165     tableName = null;
166     columnNames = new String JavaDoc[0];
167     columnValues = new String JavaDoc[0];
168     columnWhereNames = new String JavaDoc[0];
169     columnWhereValues = new String JavaDoc[0];
170     whereStatement = null;
171     sqlType = null;
172
173     //removing comments
174
if ( sql.indexOf( "/*" ) != -1 ) {
175       StringBuffer JavaDoc buf = new StringBuffer JavaDoc( sql );
176       buf.delete( sql.indexOf( "/*" ), sql.indexOf( "*/" ) + 2 );
177       sql = buf.toString();
178     }
179     sql = sql.trim();
180
181     oldValues.clear();
182 // int startIndex = 0;
183
//replace special characters between single quotes
184
StringTokenizer JavaDoc tokQuote = new StringTokenizer JavaDoc( sql.toString(), "'", true );
185     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
186     boolean openParent1 = false;
187     while ( tokQuote.hasMoreTokens() ) {
188 // startIndex++;
189
String JavaDoc next = tokQuote.nextToken();
190       if ( openParent1 ) {
191         next = Utils.replaceAll( next, ",", commaEscape );
192         next = Utils.replaceAll( next, "=", equalEscape );
193         next = Utils.replaceAll( next, "@", atEscape );
194         next = Utils.replaceAll( next, "/", slashEscape );
195         next = Utils.replaceKeywords(next, oldValues );
196       }
197       sb.append( next );
198       if ( next.equalsIgnoreCase( "'" ) ) {
199         if ( openParent1 == true )
200           openParent1 = false;
201         else
202           openParent1 = true;
203       }
204     }
205
206     sql = sb.toString();
207     String JavaDoc upperSql = sql.toUpperCase();
208
209 XmlDriver.log("sql = "+sql);
210
211 //handle unsupported statements
212
if ( upperSql.startsWith( "ALTER " ) )
213       throw new Exception JavaDoc( "ALTER TABLE statements are not supported." );
214
215 //DROP TABLE
216
if ( upperSql.startsWith( "DROP TABLE" ) ) {
217       sqlType = DROP_TABLE;
218       int dropPos = upperSql.indexOf( "DROP TABLE" );
219       tableName = sql.substring( dropPos + 10 ).trim().toUpperCase();
220     }
221
222 //DELETE
223
else if ( upperSql.startsWith( "DELETE " ) ) {
224       sqlType = DELETE;
225       int deletePos = upperSql.indexOf( "DELETE" );
226       int wherePos = upperSql.indexOf( " WHERE " );
227       int fromPos = upperSql.lastIndexOf( " FROM " );
228       if ( wherePos == -1 )
229         tableName = sql.substring( fromPos + 6 ).trim().toUpperCase();
230       else
231         tableName = sql.substring( fromPos + 6, wherePos ).trim().toUpperCase();
232
233       if ( wherePos != -1 ) {
234         String JavaDoc strWhere = sql.substring( wherePos + 6 ).trim();
235         Vector JavaDoc whereCols = new Vector JavaDoc();
236         Vector JavaDoc whereValues = new Vector JavaDoc();
237         StringTokenizer JavaDoc tokenizerWhere = new StringTokenizer JavaDoc( strWhere, "," );
238         while ( tokenizerWhere.hasMoreTokens() ) {
239           String JavaDoc strToken = tokenizerWhere.nextToken();
240           if ( strToken.toUpperCase().indexOf( " AND " ) != -1 ) {
241             String JavaDoc temp = strToken;
242             int andPos = 0;
243             out:
244             do {
245               andPos = temp.toUpperCase().indexOf( " AND " );
246               String JavaDoc strTokenAdd;
247               if ( andPos != -1 )
248                 strTokenAdd = temp.substring( 0, andPos ).trim();
249               else
250                 strTokenAdd = temp.trim();
251               int delimiter2 = strTokenAdd.indexOf( "=" );
252               if ( delimiter2 != -1 ) {
253                 String JavaDoc valueAdd = strTokenAdd.substring( delimiter2 + 1 ).trim();
254                 valueAdd = Utils.handleQuotedString(valueAdd);
255                 whereCols.add( strTokenAdd.substring( 0, delimiter2 ).trim().toUpperCase() );
256                 valueAdd = Utils.replaceAll( valueAdd, commaEscape, "," );
257                 valueAdd = Utils.replaceAll( valueAdd, quoteEscape, "'" );
258                 valueAdd = Utils.replaceKeywordsBack(valueAdd, oldValues);
259                 whereValues.add( valueAdd );
260               }
261               else {
262                 int delimiter3 = strTokenAdd.toLowerCase().indexOf( " is " );
263                 whereCols.add( strTokenAdd.substring( 0, delimiter3 ).trim().
264                                toUpperCase() );
265                 whereValues.add( null );
266               }
267               temp = temp.substring( andPos + 5 );
268               if ( temp.toUpperCase().indexOf( " AND " ) == -1 ) {
269                 strTokenAdd = temp.trim();
270                 int delimiter4 = strTokenAdd.indexOf( "=" );
271                 if ( delimiter4 != -1 ) {
272                   String JavaDoc valueAdd = strTokenAdd.substring( delimiter4 + 1 ).
273                       trim();
274                   valueAdd = Utils.handleQuotedString(valueAdd);
275                   whereCols.add( strTokenAdd.substring( 0, delimiter4 ).trim().toUpperCase() );
276                   valueAdd = Utils.replaceAll( valueAdd, commaEscape, "," );
277                   valueAdd = Utils.replaceAll( valueAdd, quoteEscape, "'" );
278                   valueAdd = Utils.replaceKeywordsBack(valueAdd, oldValues);
279                   whereValues.add( valueAdd );
280                 }
281                 else {
282                   int delimiter3 = strTokenAdd.toLowerCase().indexOf( " is " );
283                   whereCols.add( strTokenAdd.substring( 0, delimiter3 ).trim().
284                                  toUpperCase() );
285                   whereValues.add( null );
286                 }
287                 break out;
288               }
289
290             }
291             while ( true );
292
293           }
294           else {
295             int delimiter = strToken.indexOf( "=" );
296             if ( delimiter != -1 ) {
297               String JavaDoc value = strToken.substring( delimiter + 1 ).trim();
298               value = Utils.handleQuotedString(value);
299               whereCols.add( strToken.substring( 0, delimiter ).trim().toUpperCase() );
300               value = Utils.replaceAll( value, commaEscape, "," );
301               value = Utils.replaceAll( value, quoteEscape, "'" );
302               value = Utils.replaceKeywordsBack(value, oldValues);
303               whereValues.add( value );
304             }
305             else {
306               int delimiter1 = strToken.toLowerCase().indexOf( " is " );
307               whereCols.add( strToken.substring( 0, delimiter1 ).trim().
308                              toUpperCase() );
309               whereValues.add( null );
310             }
311           }
312         }
313         columnWhereNames = new String JavaDoc[whereCols.size()];
314         columnWhereValues = new String JavaDoc[whereValues.size()];
315         whereCols.copyInto( columnWhereNames );
316         whereValues.copyInto( columnWhereValues );
317       }
318     }
319
320 //SELECT
321
else if ( upperSql.startsWith( "SELECT " ) ) {
322
323       if ( upperSql.lastIndexOf( " FROM " ) == -1 ) {
324         throw new Exception JavaDoc( "Malformed SQL. Missing FROM statement." );
325       }
326
327       sqlType = SELECT;
328       int fromPos = upperSql.lastIndexOf( " FROM " );
329       int wherePos = upperSql.lastIndexOf( " WHERE " );
330       if ( wherePos == -1 )
331         tableName = sql.substring( fromPos + 6 ).trim().toUpperCase();
332       else
333         tableName = sql.substring( fromPos + 6, wherePos ).trim().toUpperCase();
334       String JavaDoc columnNamesSql = sql.substring( 0, fromPos );
335       if ( columnNamesSql.indexOf( "*" ) == -1 ) {
336         Vector JavaDoc cols = new Vector JavaDoc();
337         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( upperSql.substring( 7,
338             fromPos ), "," );
339
340         while ( tokenizer.hasMoreTokens() ) {
341           cols.add( tokenizer.nextToken().trim() );
342         }
343
344         columnNames = new String JavaDoc[cols.size()];
345         cols.copyInto( columnNames );
346       }
347       else {
348         columnNames = ( String JavaDoc[] ) ( new XmlWriter( this.fileName,
349             this.isAutoCommit ).getTableProperties( this.tableName ).get( 0 ) );
350       }
351       if ( wherePos != -1 ) {
352         String JavaDoc strWhere = sql.substring( wherePos + 7 );
353         Vector JavaDoc whereCols = new Vector JavaDoc();
354         Vector JavaDoc whereValues = new Vector JavaDoc();
355         String JavaDoc strToken = strWhere;
356         if ( strToken.toUpperCase().indexOf( " AND " ) != -1 ) {
357           String JavaDoc temp = strToken;
358           int andPos = 0;
359           out:
360           do {
361             andPos = temp.toUpperCase().indexOf( " AND " );
362             String JavaDoc strTokenAdd;
363             if ( andPos != -1 )
364               strTokenAdd = temp.substring( 0, andPos ).trim();
365             else
366               strTokenAdd = temp.trim();
367             int delimiter2 = strTokenAdd.indexOf( "=" );
368             if ( delimiter2 != -1 ) {
369               String JavaDoc valueAdd = strTokenAdd.substring( delimiter2 + 1 ).trim();
370               valueAdd = Utils.handleQuotedString(valueAdd);
371               whereCols.add( strTokenAdd.substring( 0, delimiter2 ).trim().
372                              toUpperCase() );
373               valueAdd = Utils.replaceAll( valueAdd, commaEscape, "," );
374               valueAdd = Utils.replaceAll( valueAdd, quoteEscape, "'" );
375               valueAdd = Utils.replaceKeywordsBack(valueAdd, oldValues);
376               whereValues.add( valueAdd );
377             }
378             else {
379               int delimiter3 = strTokenAdd.toLowerCase().indexOf( " is " );
380               whereCols.add( strTokenAdd.substring( 0, delimiter3 ).trim().
381                              toUpperCase() );
382               whereValues.add( null );
383             }
384             temp = temp.substring( andPos + 5 );
385             if ( temp.toUpperCase().indexOf( " AND " ) == -1 ) {
386               strTokenAdd = temp.trim();
387               int delimiter4 = strTokenAdd.indexOf( "=" );
388               if ( delimiter4 != -1 ) {
389                 String JavaDoc valueAdd = strTokenAdd.substring( delimiter4 + 1 ).trim();
390                 valueAdd = Utils.handleQuotedString(valueAdd);
391                 whereCols.add( strTokenAdd.substring( 0, delimiter4 ).trim().toUpperCase() );
392                 valueAdd = Utils.replaceAll( valueAdd, commaEscape, "," );
393                 valueAdd = Utils.replaceAll( valueAdd, quoteEscape, "'" );
394                 valueAdd = Utils.replaceKeywordsBack(valueAdd, oldValues);
395                 whereValues.add( valueAdd );
396               }
397               else {
398                 int delimiter3 = strTokenAdd.toLowerCase().indexOf( " is " );
399                 whereCols.add( strTokenAdd.substring( 0, delimiter3 ).trim().
400                                toUpperCase() );
401                 whereValues.add( null );
402               }
403               break out;
404             }
405
406           }
407           while ( true );
408
409         }
410         else {
411           int delimiter = strToken.indexOf( "=" );
412           if ( delimiter != -1 ) {
413             String JavaDoc value = strToken.substring( delimiter + 1 ).trim();
414             value = Utils.handleQuotedString(value);
415             whereCols.add( strToken.substring( 0, delimiter ).trim().toUpperCase() );
416             value = Utils.replaceAll( value, commaEscape, "," );
417             value = Utils.replaceAll( value, quoteEscape, "'" );
418             value = Utils.replaceKeywordsBack(value, oldValues);
419             whereValues.add( value );
420           }
421           else {
422             int delimiter1 = strToken.toLowerCase().indexOf( " is " );
423             whereCols.add( strToken.substring( 0, delimiter1 ).trim().
424                            toUpperCase() );
425             whereValues.add( null );
426           }
427         }
428
429         columnWhereNames = new String JavaDoc[whereCols.size()];
430         columnWhereValues = new String JavaDoc[whereValues.size()];
431         whereCols.copyInto( columnWhereNames );
432         whereValues.copyInto( columnWhereValues );
433       }
434     }
435
436 //INSERT
437
else if ( upperSql.startsWith( "INSERT " ) ) {
438       if ( upperSql.lastIndexOf( " VALUES" ) == -1 ) {
439         throw new Exception JavaDoc( "Malformed SQL. Missing VALUES statement." );
440       }
441       sqlType = INSERT;
442       int intoPos = 0;
443       if ( upperSql.indexOf( " INTO " ) != -1 )
444         intoPos = upperSql.indexOf( " INTO " ) + 6;
445       else
446         intoPos = upperSql.indexOf( "INSERT " ) + 7;
447       int bracketPos = upperSql.indexOf( "(" );
448       int lastBracketPos = upperSql.indexOf( ")" );
449       tableName = sql.substring( intoPos, bracketPos ).trim().toUpperCase();
450       Vector JavaDoc cols = new Vector JavaDoc();
451       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( upperSql.substring(
452           bracketPos + 1, lastBracketPos ), "," );
453       while ( tokenizer.hasMoreTokens() ) {
454         cols.add( tokenizer.nextToken().trim() );
455       }
456       columnNames = new String JavaDoc[cols.size()];
457       cols.copyInto( columnNames );
458
459       int valuesPos = upperSql.indexOf( "VALUES" );
460       String JavaDoc endStatement = sql.substring( valuesPos + 6 ).trim();
461       bracketPos = endStatement.indexOf( "(" );
462       lastBracketPos = endStatement.lastIndexOf( ")" );
463       Vector JavaDoc values = new Vector JavaDoc();
464
465       StringTokenizer JavaDoc tokenizer2 = new StringTokenizer JavaDoc( endStatement.substring(
466           bracketPos + 1, lastBracketPos ), "," );
467       while ( tokenizer2.hasMoreTokens() ) {
468         String JavaDoc value = tokenizer2.nextToken().trim();
469           value = Utils.handleQuotedString( value );
470         value = Utils.replaceAll( value, commaEscape, "," );
471         value = Utils.replaceAll( value, quoteEscape, "'" );
472         value = Utils.replaceKeywordsBack(value, oldValues);
473         value = Utils.handleBinaryString(value, this.binaryStreamObjectList);
474         values.add( value );
475         }
476
477       columnValues = new String JavaDoc[values.size()];
478       values.copyInto( columnValues );
479     }
480
481 //UPDATE
482
else if ( upperSql.startsWith( "UPDATE " ) ) {
483       if ( upperSql.lastIndexOf( " SET " ) == -1 )
484         throw new Exception JavaDoc( "Malformed SQL. Missing SET statement." );
485       sqlType = UPDATE;
486       int updatePos = upperSql.indexOf( "UPDATE" );
487       int setPos = upperSql.indexOf( " SET " );
488       int equalPos = upperSql.indexOf( "=" );
489       int wherePos = upperSql.indexOf( " WHERE " );
490       tableName = sql.substring( updatePos + 6, setPos ).trim().toUpperCase();
491       String JavaDoc setString = "";
492       //to handle situation when no where clause in sql statement
493
if ( wherePos != -1 ) {
494         setString = sql.substring( setPos + 5, wherePos );
495       }
496       else {
497         setString = sql.substring( setPos + 5 );
498       }
499
500       StringTokenizer JavaDoc tokenizerSet = new StringTokenizer JavaDoc( setString, "," );
501       Vector JavaDoc setNames = new Vector JavaDoc();
502       Vector JavaDoc setValues = new Vector JavaDoc();
503
504       while ( tokenizerSet.hasMoreTokens() ) {
505         String JavaDoc strToken = tokenizerSet.nextToken();
506         int delimiter = strToken.indexOf( "=" );
507         setNames.add( strToken.substring( 0, delimiter ).trim().toUpperCase() );
508         String JavaDoc value = strToken.substring( delimiter + 1 ).trim();
509         value = Utils.handleQuotedString(value);
510         value = Utils.replaceAll( value, commaEscape, "," );
511         value = Utils.replaceAll( value, quoteEscape, "'" );
512         value = Utils.replaceKeywordsBack(value, oldValues);
513         value = Utils.handleBinaryString(value, this.binaryStreamObjectList);
514         setValues.add( value );
515       }
516       columnNames = new String JavaDoc[setNames.size()];
517       columnValues = new String JavaDoc[setValues.size()];
518       setNames.copyInto( columnNames );
519       setValues.copyInto( columnValues );
520
521       if ( wherePos != -1 ) {
522         String JavaDoc strWhere = sql.substring( wherePos + 6 ).trim();
523         Vector JavaDoc whereCols = new Vector JavaDoc();
524         Vector JavaDoc whereValues = new Vector JavaDoc();
525         StringTokenizer JavaDoc tokenizerWhere = new StringTokenizer JavaDoc( strWhere, "," );
526
527         while ( tokenizerWhere.hasMoreTokens() ) {
528           String JavaDoc strToken = tokenizerWhere.nextToken();
529           if ( strToken.toUpperCase().indexOf( " AND " ) != -1 ) {
530             String JavaDoc temp = strToken;
531             int andPos = 0;
532             out:
533             do {
534               andPos = temp.toUpperCase().indexOf( " AND " );
535               String JavaDoc strTokenAdd;
536               if ( andPos != -1 )
537                 strTokenAdd = temp.substring( 0, andPos ).trim();
538               else
539                 strTokenAdd = temp.trim();
540               int delimiter2 = strTokenAdd.indexOf( "=" );
541               if ( delimiter2 != -1 ) {
542                 String JavaDoc valueAdd = strTokenAdd.substring( delimiter2 + 1 ).trim();
543                 valueAdd = Utils.handleQuotedString(valueAdd);
544                 whereCols.add( strTokenAdd.substring( 0, delimiter2 ).trim().toUpperCase() );
545                 valueAdd = Utils.replaceAll( valueAdd, commaEscape, "," );
546                 valueAdd = Utils.replaceAll( valueAdd, quoteEscape, "'" );
547                 valueAdd = Utils.replaceKeywordsBack(valueAdd, oldValues);
548                 whereValues.add( valueAdd );
549               }
550               else {
551                 int delimiter3 = strTokenAdd.toLowerCase().indexOf( " is " );
552                 whereCols.add( strTokenAdd.substring( 0, delimiter3 ).trim().
553                                toUpperCase() );
554                 whereValues.add( null );
555               }
556               temp = temp.substring( andPos + 5 );
557               if ( temp.toUpperCase().indexOf( " AND " ) == -1 ) {
558                 strTokenAdd = temp.trim();
559                 int delimiter4 = strTokenAdd.indexOf( "=" );
560                 if ( delimiter4 != -1 ) {
561                   String JavaDoc valueAdd = strTokenAdd.substring( delimiter4 + 1 ).
562                       trim();
563                   valueAdd = Utils.handleQuotedString(valueAdd);
564                   whereCols.add( strTokenAdd.substring( 0, delimiter4 ).trim().toUpperCase() );
565                   valueAdd = Utils.replaceAll( valueAdd, commaEscape, "," );
566                   valueAdd = Utils.replaceAll( valueAdd, quoteEscape, "'" );
567                   valueAdd = Utils.replaceKeywordsBack(valueAdd, oldValues);
568                   whereValues.add( valueAdd );
569                 }
570                 else {
571                   int delimiter3 = strTokenAdd.toLowerCase().indexOf( " is " );
572                   whereCols.add( strTokenAdd.substring( 0, delimiter3 ).trim().
573                                  toUpperCase() );
574                   whereValues.add( null );
575                 }
576                 break out;
577               }
578
579             }
580             while ( true );
581
582           }
583           else {
584             int delimiter = strToken.indexOf( "=" );
585             if ( delimiter != -1 ) {
586               String JavaDoc value = strToken.substring( delimiter + 1 ).trim();
587               value = Utils.handleQuotedString(value);
588               whereCols.add( strToken.substring( 0, delimiter ).trim().toUpperCase() );
589               value = Utils.replaceAll( value, commaEscape, "," );
590               value = Utils.replaceAll( value, quoteEscape, "'" );
591               value = Utils.replaceKeywordsBack(value, oldValues);
592               whereValues.add( value );
593             }
594             else {
595               int delimiter1 = strToken.toLowerCase().indexOf( " is " );
596               whereCols.add( strToken.substring( 0, delimiter1 ).trim().toUpperCase() );
597               whereValues.add( null );
598             }
599           }
600         }
601         columnWhereNames = new String JavaDoc[whereCols.size()];
602         columnWhereValues = new String JavaDoc[whereValues.size()];
603         whereCols.copyInto( columnWhereNames );
604         whereValues.copyInto( columnWhereValues );
605       }
606     }
607
608 //CREATE TABLE
609
else if ( upperSql.startsWith( "CREATE TABLE " ) ) {
610       //removing line brakes
611
sql = Utils.replaceAll( sql, "\n", "" );
612       upperSql = sql.toUpperCase();
613       sqlType = CREATE_TABLE;
614       int createPos = upperSql.indexOf( "CREATE TABLE" );
615       int tableStartPos = upperSql.indexOf( "(" );
616       int tableEndPos = upperSql.lastIndexOf( ")" );
617
618       tableName = sql.substring( createPos + 12, tableStartPos ).trim().
619           toUpperCase();
620       String JavaDoc createString = sql.substring( tableStartPos + 1, tableEndPos ).trim();
621       ArrayList JavaDoc setColumnNames = new ArrayList JavaDoc();
622       ArrayList JavaDoc setPrimaryKeys = new ArrayList JavaDoc();
623       ArrayList JavaDoc setNotnullColumns = new ArrayList JavaDoc();
624       String JavaDoc token = ( ( String JavaDoc ) upperSql.substring( tableStartPos + 1,tableEndPos ) ).trim();
625       StringTokenizer JavaDoc comma = new StringTokenizer JavaDoc( token, "," );
626       while ( comma.hasMoreTokens() ) {
627         String JavaDoc nextComma = comma.nextToken().trim();
628         StringTokenizer JavaDoc space = new StringTokenizer JavaDoc( nextComma, " " );
629         String JavaDoc nextSpace = space.nextToken();
630         setColumnNames.add( nextSpace );
631         if ( nextComma.indexOf( "PRIMARYKEY" ) != -1 ) {
632           setPrimaryKeys.add( nextSpace );
633           setNotnullColumns.add( nextSpace );
634         }
635         if ( nextComma.indexOf( "NOT NULL" ) != -1 ) {
636           setNotnullColumns.add( nextSpace );
637         }
638       }
639       columnNames = new String JavaDoc[setColumnNames.size()];
640       setColumnNames.toArray( columnNames );
641       this.primaryKeyColumns = new String JavaDoc[setPrimaryKeys.size()];
642       setPrimaryKeys.toArray( this.primaryKeyColumns );
643       this.notnullColumns = new String JavaDoc[setNotnullColumns.size()];
644       setNotnullColumns.toArray( this.notnullColumns );
645
646     }
647     else {
648       throw new Exception JavaDoc( "Malformed SQL. Wrong SQL statement." );
649     }
650   }
651
652   /**
653    * Set setBinaryStreamList.
654    * @param list
655    */

656   public void setBinaryStreamList( ArrayList JavaDoc list ) {
657     this.binaryStreamObjectList = list;
658   }
659
660 }
661
Popular Tags