Source Code Cross Referenced for XmlSqlParser.java in  » Database-JDBC-Connection-Pool » octopus » org » webdocwf » util » xml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » octopus » org.webdocwf.util.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:            Copyright (C) 2003  Together
003:            This library is free software; you can redistribute it and/or
004:            modify it under the terms of the GNU Lesser General Public
005:            License as published by the Free Software Foundation; either
006:            version 2.1 of the License, or (at your option) any later version.
007:            This library is distributed in the hope that it will be useful,
008:            but WITHOUT ANY WARRANTY; without even the implied warranty of
009:            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
010:            Lesser General Public License for more details.
011:            You should have received a copy of the GNU Lesser General Public
012:            License along with this library; if not, write to the Free Software
013:            Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
014:         */
015:
016:        package org.webdocwf.util.xml;
017:
018:        import java.util.ArrayList;
019:        import java.util.Vector;
020:        import java.util.StringTokenizer;
021:        import java.util.HashMap;
022:        import java.io.*;
023:
024:        /**
025:         * This is a simple SQL parser used by the Xml JDBC driver.
026:         *
027:         * @author     Zoran Milakovic
028:         */
029:        public class XmlSqlParser {
030:            /**
031:             *Description of the Field
032:             *
033:             * @since
034:             */
035:
036:            public static final String CREATE_TABLE = "create_table";
037:            public static final String DROP_TABLE = "drop_table";
038:            public static final String INSERT = "insert";
039:            public static final String UPDATE = "update";
040:            public static final String SELECT = "select";
041:            public static final String DELETE = "delete";
042:
043:            public static final String quoteEscape = "''";
044:            private static final String commaEscape = "~#####1~";
045:            public static final String equalEscape = "~EQUAL~";
046:            public static final String atEscape = "~AT~";
047:            public static final String slashEscape = "~SLASH~";
048:
049:            public static final String BINARY_STREAM_OBJECT = "XmlDriverBinaryStreamObject";
050:
051:            private ArrayList binaryStreamObjectList = new ArrayList();
052:
053:            private HashMap oldValues = new HashMap();
054:
055:            private boolean isAutoCommit = true;
056:
057:            private String fileName = "";
058:
059:            public String tableName;
060:
061:            public String whereStatement;
062:
063:            public String sqlType;
064:
065:            private String sqlStatement = "";
066:
067:            public String[] columnNames;
068:
069:            public String[] primaryKeyColumns;
070:
071:            public String[] notnullColumns;
072:
073:            public String[] columnValues;
074:
075:            public String[] columnWhereNames;
076:
077:            public String[] columnWhereValues;
078:
079:            public XmlSqlParser() {
080:            }
081:
082:            public XmlSqlParser(String fileName, boolean isAutoCommit) {
083:                this .fileName = fileName;
084:                this .isAutoCommit = isAutoCommit;
085:            }
086:
087:            /**
088:             *Gets the tableName attribute of the SqlParser object
089:             *
090:             * @return    The tableName value
091:             * @since
092:             */
093:            public String getTableName() {
094:                return tableName;
095:            }
096:
097:            /**
098:             * Gets columns which can not be NULL
099:             *
100:             * @return    The tableName value
101:             * @since
102:             */
103:            public String[] getNotnullColumns() {
104:                return this .notnullColumns;
105:            }
106:
107:            /**
108:             *Gets the type of sql statement.INSERT , UPDATE , CREATE , SELECT, DROP_TABLE, CREATE_TABLE
109:             *
110:             * @return    The type of sql statement.
111:             * @since
112:             */
113:            public String getSQLType() {
114:                return this .sqlType;
115:            }
116:
117:            /**
118:             *Gets the columnNames attribute of the SqlParser object
119:             *
120:             * @return    The columnNames value
121:             * @since
122:             */
123:            public String[] getColumnNames() {
124:                return columnNames;
125:            }
126:
127:            public String[] getWhereColumnNames() {
128:                return columnWhereNames;
129:            }
130:
131:            /**
132:             * Gets array of column values.Used in INSERT , UPDATE statements
133:             * @return columnValues as array of string.
134:             */
135:            public String[] getColumnValues() {
136:                return columnValues;
137:            }
138:
139:            /**
140:             * Gets array of column which are primarykeys.Used in INSERT statements
141:             * @return primarykeys as array of string.
142:             */
143:            public String[] getPrimaryKeys() {
144:                return primaryKeyColumns;
145:            }
146:
147:            public String[] getWhereColumnValues() {
148:                return columnWhereValues;
149:            }
150:
151:            public String getSqlStatement() {
152:                return this .sqlStatement;
153:            }
154:
155:            /**
156:             * Description of the Method
157:             *
158:             * @param  sql            Description of Parameter
159:             * @exception  Exception  Description of Exception
160:             * @since
161:             */
162:            public void parse(String sql) throws Exception {
163:                this .sqlStatement = sql;
164:                tableName = null;
165:                columnNames = new String[0];
166:                columnValues = new String[0];
167:                columnWhereNames = new String[0];
168:                columnWhereValues = new String[0];
169:                whereStatement = null;
170:                sqlType = null;
171:
172:                //removing comments
173:                if (sql.indexOf("/*") != -1) {
174:                    StringBuffer buf = new StringBuffer(sql);
175:                    buf.delete(sql.indexOf("/*"), sql.indexOf("*/") + 2);
176:                    sql = buf.toString();
177:                }
178:                sql = sql.trim();
179:
180:                oldValues.clear();
181:                //  	int startIndex = 0;
182:                //replace special characters between single quotes
183:                StringTokenizer tokQuote = new StringTokenizer(sql.toString(),
184:                        "'", true);
185:                StringBuffer sb = new StringBuffer();
186:                boolean openParent1 = false;
187:                while (tokQuote.hasMoreTokens()) {
188:                    //      startIndex++;
189:                    String 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 upperSql = sql.toUpperCase();
208:
209:                XmlDriver.log("sql = " + sql);
210:
211:                //handle unsupported statements
212:                if (upperSql.startsWith("ALTER "))
213:                    throw new Exception(
214:                            "ALTER TABLE statements are not supported.");
215:
216:                //DROP TABLE
217:                if (upperSql.startsWith("DROP TABLE")) {
218:                    sqlType = DROP_TABLE;
219:                    int dropPos = upperSql.indexOf("DROP TABLE");
220:                    tableName = sql.substring(dropPos + 10).trim()
221:                            .toUpperCase();
222:                }
223:
224:                //DELETE
225:                else if (upperSql.startsWith("DELETE ")) {
226:                    sqlType = DELETE;
227:                    int deletePos = upperSql.indexOf("DELETE");
228:                    int wherePos = upperSql.indexOf(" WHERE ");
229:                    int fromPos = upperSql.lastIndexOf(" FROM ");
230:                    if (wherePos == -1)
231:                        tableName = sql.substring(fromPos + 6).trim()
232:                                .toUpperCase();
233:                    else
234:                        tableName = sql.substring(fromPos + 6, wherePos).trim()
235:                                .toUpperCase();
236:
237:                    if (wherePos != -1) {
238:                        String strWhere = sql.substring(wherePos + 6).trim();
239:                        Vector whereCols = new Vector();
240:                        Vector whereValues = new Vector();
241:                        StringTokenizer tokenizerWhere = new StringTokenizer(
242:                                strWhere, ",");
243:                        while (tokenizerWhere.hasMoreTokens()) {
244:                            String strToken = tokenizerWhere.nextToken();
245:                            if (strToken.toUpperCase().indexOf(" AND ") != -1) {
246:                                String temp = strToken;
247:                                int andPos = 0;
248:                                out: do {
249:                                    andPos = temp.toUpperCase()
250:                                            .indexOf(" AND ");
251:                                    String strTokenAdd;
252:                                    if (andPos != -1)
253:                                        strTokenAdd = temp.substring(0, andPos)
254:                                                .trim();
255:                                    else
256:                                        strTokenAdd = temp.trim();
257:                                    int delimiter2 = strTokenAdd.indexOf("=");
258:                                    if (delimiter2 != -1) {
259:                                        String valueAdd = strTokenAdd
260:                                                .substring(delimiter2 + 1)
261:                                                .trim();
262:                                        valueAdd = Utils
263:                                                .handleQuotedString(valueAdd);
264:                                        whereCols.add(strTokenAdd.substring(0,
265:                                                delimiter2).trim()
266:                                                .toUpperCase());
267:                                        valueAdd = Utils.replaceAll(valueAdd,
268:                                                commaEscape, ",");
269:                                        valueAdd = Utils.replaceAll(valueAdd,
270:                                                quoteEscape, "'");
271:                                        valueAdd = Utils.replaceKeywordsBack(
272:                                                valueAdd, oldValues);
273:                                        whereValues.add(valueAdd);
274:                                    } else {
275:                                        int delimiter3 = strTokenAdd
276:                                                .toLowerCase().indexOf(" is ");
277:                                        whereCols.add(strTokenAdd.substring(0,
278:                                                delimiter3).trim()
279:                                                .toUpperCase());
280:                                        whereValues.add(null);
281:                                    }
282:                                    temp = temp.substring(andPos + 5);
283:                                    if (temp.toUpperCase().indexOf(" AND ") == -1) {
284:                                        strTokenAdd = temp.trim();
285:                                        int delimiter4 = strTokenAdd
286:                                                .indexOf("=");
287:                                        if (delimiter4 != -1) {
288:                                            String valueAdd = strTokenAdd
289:                                                    .substring(delimiter4 + 1)
290:                                                    .trim();
291:                                            valueAdd = Utils
292:                                                    .handleQuotedString(valueAdd);
293:                                            whereCols.add(strTokenAdd
294:                                                    .substring(0, delimiter4)
295:                                                    .trim().toUpperCase());
296:                                            valueAdd = Utils.replaceAll(
297:                                                    valueAdd, commaEscape, ",");
298:                                            valueAdd = Utils.replaceAll(
299:                                                    valueAdd, quoteEscape, "'");
300:                                            valueAdd = Utils
301:                                                    .replaceKeywordsBack(
302:                                                            valueAdd, oldValues);
303:                                            whereValues.add(valueAdd);
304:                                        } else {
305:                                            int delimiter3 = strTokenAdd
306:                                                    .toLowerCase().indexOf(
307:                                                            " is ");
308:                                            whereCols.add(strTokenAdd
309:                                                    .substring(0, delimiter3)
310:                                                    .trim().toUpperCase());
311:                                            whereValues.add(null);
312:                                        }
313:                                        break out;
314:                                    }
315:
316:                                } while (true);
317:
318:                            } else {
319:                                int delimiter = strToken.indexOf("=");
320:                                if (delimiter != -1) {
321:                                    String value = strToken.substring(
322:                                            delimiter + 1).trim();
323:                                    value = Utils.handleQuotedString(value);
324:                                    whereCols.add(strToken.substring(0,
325:                                            delimiter).trim().toUpperCase());
326:                                    value = Utils.replaceAll(value,
327:                                            commaEscape, ",");
328:                                    value = Utils.replaceAll(value,
329:                                            quoteEscape, "'");
330:                                    value = Utils.replaceKeywordsBack(value,
331:                                            oldValues);
332:                                    whereValues.add(value);
333:                                } else {
334:                                    int delimiter1 = strToken.toLowerCase()
335:                                            .indexOf(" is ");
336:                                    whereCols.add(strToken.substring(0,
337:                                            delimiter1).trim().toUpperCase());
338:                                    whereValues.add(null);
339:                                }
340:                            }
341:                        }
342:                        columnWhereNames = new String[whereCols.size()];
343:                        columnWhereValues = new String[whereValues.size()];
344:                        whereCols.copyInto(columnWhereNames);
345:                        whereValues.copyInto(columnWhereValues);
346:                    }
347:                }
348:
349:                //SELECT
350:                else if (upperSql.startsWith("SELECT ")) {
351:
352:                    if (upperSql.lastIndexOf(" FROM ") == -1) {
353:                        throw new Exception(
354:                                "Malformed SQL. Missing FROM statement.");
355:                    }
356:
357:                    sqlType = SELECT;
358:                    int fromPos = upperSql.lastIndexOf(" FROM ");
359:                    int wherePos = upperSql.lastIndexOf(" WHERE ");
360:                    if (wherePos == -1)
361:                        tableName = sql.substring(fromPos + 6).trim()
362:                                .toUpperCase();
363:                    else
364:                        tableName = sql.substring(fromPos + 6, wherePos).trim()
365:                                .toUpperCase();
366:                    String columnNamesSql = sql.substring(0, fromPos);
367:                    if (columnNamesSql.indexOf("*") == -1) {
368:                        Vector cols = new Vector();
369:                        StringTokenizer tokenizer = new StringTokenizer(
370:                                upperSql.substring(7, fromPos), ",");
371:
372:                        while (tokenizer.hasMoreTokens()) {
373:                            cols.add(tokenizer.nextToken().trim());
374:                        }
375:
376:                        columnNames = new String[cols.size()];
377:                        cols.copyInto(columnNames);
378:                    } else {
379:                        columnNames = (String[]) (new XmlWriter(this .fileName,
380:                                this .isAutoCommit)
381:                                .getTableProperties(this .tableName).get(0));
382:                    }
383:                    if (wherePos != -1) {
384:                        String strWhere = sql.substring(wherePos + 7);
385:                        Vector whereCols = new Vector();
386:                        Vector whereValues = new Vector();
387:                        String strToken = strWhere;
388:                        if (strToken.toUpperCase().indexOf(" AND ") != -1) {
389:                            String temp = strToken;
390:                            int andPos = 0;
391:                            out: do {
392:                                andPos = temp.toUpperCase().indexOf(" AND ");
393:                                String strTokenAdd;
394:                                if (andPos != -1)
395:                                    strTokenAdd = temp.substring(0, andPos)
396:                                            .trim();
397:                                else
398:                                    strTokenAdd = temp.trim();
399:                                int delimiter2 = strTokenAdd.indexOf("=");
400:                                if (delimiter2 != -1) {
401:                                    String valueAdd = strTokenAdd.substring(
402:                                            delimiter2 + 1).trim();
403:                                    valueAdd = Utils
404:                                            .handleQuotedString(valueAdd);
405:                                    whereCols.add(strTokenAdd.substring(0,
406:                                            delimiter2).trim().toUpperCase());
407:                                    valueAdd = Utils.replaceAll(valueAdd,
408:                                            commaEscape, ",");
409:                                    valueAdd = Utils.replaceAll(valueAdd,
410:                                            quoteEscape, "'");
411:                                    valueAdd = Utils.replaceKeywordsBack(
412:                                            valueAdd, oldValues);
413:                                    whereValues.add(valueAdd);
414:                                } else {
415:                                    int delimiter3 = strTokenAdd.toLowerCase()
416:                                            .indexOf(" is ");
417:                                    whereCols.add(strTokenAdd.substring(0,
418:                                            delimiter3).trim().toUpperCase());
419:                                    whereValues.add(null);
420:                                }
421:                                temp = temp.substring(andPos + 5);
422:                                if (temp.toUpperCase().indexOf(" AND ") == -1) {
423:                                    strTokenAdd = temp.trim();
424:                                    int delimiter4 = strTokenAdd.indexOf("=");
425:                                    if (delimiter4 != -1) {
426:                                        String valueAdd = strTokenAdd
427:                                                .substring(delimiter4 + 1)
428:                                                .trim();
429:                                        valueAdd = Utils
430:                                                .handleQuotedString(valueAdd);
431:                                        whereCols.add(strTokenAdd.substring(0,
432:                                                delimiter4).trim()
433:                                                .toUpperCase());
434:                                        valueAdd = Utils.replaceAll(valueAdd,
435:                                                commaEscape, ",");
436:                                        valueAdd = Utils.replaceAll(valueAdd,
437:                                                quoteEscape, "'");
438:                                        valueAdd = Utils.replaceKeywordsBack(
439:                                                valueAdd, oldValues);
440:                                        whereValues.add(valueAdd);
441:                                    } else {
442:                                        int delimiter3 = strTokenAdd
443:                                                .toLowerCase().indexOf(" is ");
444:                                        whereCols.add(strTokenAdd.substring(0,
445:                                                delimiter3).trim()
446:                                                .toUpperCase());
447:                                        whereValues.add(null);
448:                                    }
449:                                    break out;
450:                                }
451:
452:                            } while (true);
453:
454:                        } else {
455:                            int delimiter = strToken.indexOf("=");
456:                            if (delimiter != -1) {
457:                                String value = strToken
458:                                        .substring(delimiter + 1).trim();
459:                                value = Utils.handleQuotedString(value);
460:                                whereCols.add(strToken.substring(0, delimiter)
461:                                        .trim().toUpperCase());
462:                                value = Utils.replaceAll(value, commaEscape,
463:                                        ",");
464:                                value = Utils.replaceAll(value, quoteEscape,
465:                                        "'");
466:                                value = Utils.replaceKeywordsBack(value,
467:                                        oldValues);
468:                                whereValues.add(value);
469:                            } else {
470:                                int delimiter1 = strToken.toLowerCase()
471:                                        .indexOf(" is ");
472:                                whereCols.add(strToken.substring(0, delimiter1)
473:                                        .trim().toUpperCase());
474:                                whereValues.add(null);
475:                            }
476:                        }
477:
478:                        columnWhereNames = new String[whereCols.size()];
479:                        columnWhereValues = new String[whereValues.size()];
480:                        whereCols.copyInto(columnWhereNames);
481:                        whereValues.copyInto(columnWhereValues);
482:                    }
483:                }
484:
485:                //INSERT
486:                else if (upperSql.startsWith("INSERT ")) {
487:                    if (upperSql.lastIndexOf(" VALUES") == -1) {
488:                        throw new Exception(
489:                                "Malformed SQL. Missing VALUES statement.");
490:                    }
491:                    sqlType = INSERT;
492:                    int intoPos = 0;
493:                    if (upperSql.indexOf(" INTO ") != -1)
494:                        intoPos = upperSql.indexOf(" INTO ") + 6;
495:                    else
496:                        intoPos = upperSql.indexOf("INSERT ") + 7;
497:                    int bracketPos = upperSql.indexOf("(");
498:                    int lastBracketPos = upperSql.indexOf(")");
499:                    tableName = sql.substring(intoPos, bracketPos).trim()
500:                            .toUpperCase();
501:                    Vector cols = new Vector();
502:                    StringTokenizer tokenizer = new StringTokenizer(upperSql
503:                            .substring(bracketPos + 1, lastBracketPos), ",");
504:                    while (tokenizer.hasMoreTokens()) {
505:                        cols.add(tokenizer.nextToken().trim());
506:                    }
507:                    columnNames = new String[cols.size()];
508:                    cols.copyInto(columnNames);
509:
510:                    int valuesPos = upperSql.indexOf("VALUES");
511:                    String endStatement = sql.substring(valuesPos + 6).trim();
512:                    bracketPos = endStatement.indexOf("(");
513:                    lastBracketPos = endStatement.lastIndexOf(")");
514:                    Vector values = new Vector();
515:
516:                    StringTokenizer tokenizer2 = new StringTokenizer(
517:                            endStatement.substring(bracketPos + 1,
518:                                    lastBracketPos), ",");
519:                    while (tokenizer2.hasMoreTokens()) {
520:                        String value = tokenizer2.nextToken().trim();
521:                        value = Utils.handleQuotedString(value);
522:                        value = Utils.replaceAll(value, commaEscape, ",");
523:                        value = Utils.replaceAll(value, quoteEscape, "'");
524:                        value = Utils.replaceKeywordsBack(value, oldValues);
525:                        value = Utils.handleBinaryString(value,
526:                                this .binaryStreamObjectList);
527:                        values.add(value);
528:                    }
529:
530:                    columnValues = new String[values.size()];
531:                    values.copyInto(columnValues);
532:                }
533:
534:                //UPDATE
535:                else if (upperSql.startsWith("UPDATE ")) {
536:                    if (upperSql.lastIndexOf(" SET ") == -1)
537:                        throw new Exception(
538:                                "Malformed SQL. Missing SET statement.");
539:                    sqlType = UPDATE;
540:                    int updatePos = upperSql.indexOf("UPDATE");
541:                    int setPos = upperSql.indexOf(" SET ");
542:                    int equalPos = upperSql.indexOf("=");
543:                    int wherePos = upperSql.indexOf(" WHERE ");
544:                    tableName = sql.substring(updatePos + 6, setPos).trim()
545:                            .toUpperCase();
546:                    String setString = "";
547:                    //to handle situation when no where clause in sql statement
548:                    if (wherePos != -1) {
549:                        setString = sql.substring(setPos + 5, wherePos);
550:                    } else {
551:                        setString = sql.substring(setPos + 5);
552:                    }
553:
554:                    StringTokenizer tokenizerSet = new StringTokenizer(
555:                            setString, ",");
556:                    Vector setNames = new Vector();
557:                    Vector setValues = new Vector();
558:
559:                    while (tokenizerSet.hasMoreTokens()) {
560:                        String strToken = tokenizerSet.nextToken();
561:                        int delimiter = strToken.indexOf("=");
562:                        setNames.add(strToken.substring(0, delimiter).trim()
563:                                .toUpperCase());
564:                        String value = strToken.substring(delimiter + 1).trim();
565:                        value = Utils.handleQuotedString(value);
566:                        value = Utils.replaceAll(value, commaEscape, ",");
567:                        value = Utils.replaceAll(value, quoteEscape, "'");
568:                        value = Utils.replaceKeywordsBack(value, oldValues);
569:                        value = Utils.handleBinaryString(value,
570:                                this .binaryStreamObjectList);
571:                        setValues.add(value);
572:                    }
573:                    columnNames = new String[setNames.size()];
574:                    columnValues = new String[setValues.size()];
575:                    setNames.copyInto(columnNames);
576:                    setValues.copyInto(columnValues);
577:
578:                    if (wherePos != -1) {
579:                        String strWhere = sql.substring(wherePos + 6).trim();
580:                        Vector whereCols = new Vector();
581:                        Vector whereValues = new Vector();
582:                        StringTokenizer tokenizerWhere = new StringTokenizer(
583:                                strWhere, ",");
584:
585:                        while (tokenizerWhere.hasMoreTokens()) {
586:                            String strToken = tokenizerWhere.nextToken();
587:                            if (strToken.toUpperCase().indexOf(" AND ") != -1) {
588:                                String temp = strToken;
589:                                int andPos = 0;
590:                                out: do {
591:                                    andPos = temp.toUpperCase()
592:                                            .indexOf(" AND ");
593:                                    String strTokenAdd;
594:                                    if (andPos != -1)
595:                                        strTokenAdd = temp.substring(0, andPos)
596:                                                .trim();
597:                                    else
598:                                        strTokenAdd = temp.trim();
599:                                    int delimiter2 = strTokenAdd.indexOf("=");
600:                                    if (delimiter2 != -1) {
601:                                        String valueAdd = strTokenAdd
602:                                                .substring(delimiter2 + 1)
603:                                                .trim();
604:                                        valueAdd = Utils
605:                                                .handleQuotedString(valueAdd);
606:                                        whereCols.add(strTokenAdd.substring(0,
607:                                                delimiter2).trim()
608:                                                .toUpperCase());
609:                                        valueAdd = Utils.replaceAll(valueAdd,
610:                                                commaEscape, ",");
611:                                        valueAdd = Utils.replaceAll(valueAdd,
612:                                                quoteEscape, "'");
613:                                        valueAdd = Utils.replaceKeywordsBack(
614:                                                valueAdd, oldValues);
615:                                        whereValues.add(valueAdd);
616:                                    } else {
617:                                        int delimiter3 = strTokenAdd
618:                                                .toLowerCase().indexOf(" is ");
619:                                        whereCols.add(strTokenAdd.substring(0,
620:                                                delimiter3).trim()
621:                                                .toUpperCase());
622:                                        whereValues.add(null);
623:                                    }
624:                                    temp = temp.substring(andPos + 5);
625:                                    if (temp.toUpperCase().indexOf(" AND ") == -1) {
626:                                        strTokenAdd = temp.trim();
627:                                        int delimiter4 = strTokenAdd
628:                                                .indexOf("=");
629:                                        if (delimiter4 != -1) {
630:                                            String valueAdd = strTokenAdd
631:                                                    .substring(delimiter4 + 1)
632:                                                    .trim();
633:                                            valueAdd = Utils
634:                                                    .handleQuotedString(valueAdd);
635:                                            whereCols.add(strTokenAdd
636:                                                    .substring(0, delimiter4)
637:                                                    .trim().toUpperCase());
638:                                            valueAdd = Utils.replaceAll(
639:                                                    valueAdd, commaEscape, ",");
640:                                            valueAdd = Utils.replaceAll(
641:                                                    valueAdd, quoteEscape, "'");
642:                                            valueAdd = Utils
643:                                                    .replaceKeywordsBack(
644:                                                            valueAdd, oldValues);
645:                                            whereValues.add(valueAdd);
646:                                        } else {
647:                                            int delimiter3 = strTokenAdd
648:                                                    .toLowerCase().indexOf(
649:                                                            " is ");
650:                                            whereCols.add(strTokenAdd
651:                                                    .substring(0, delimiter3)
652:                                                    .trim().toUpperCase());
653:                                            whereValues.add(null);
654:                                        }
655:                                        break out;
656:                                    }
657:
658:                                } while (true);
659:
660:                            } else {
661:                                int delimiter = strToken.indexOf("=");
662:                                if (delimiter != -1) {
663:                                    String value = strToken.substring(
664:                                            delimiter + 1).trim();
665:                                    value = Utils.handleQuotedString(value);
666:                                    whereCols.add(strToken.substring(0,
667:                                            delimiter).trim().toUpperCase());
668:                                    value = Utils.replaceAll(value,
669:                                            commaEscape, ",");
670:                                    value = Utils.replaceAll(value,
671:                                            quoteEscape, "'");
672:                                    value = Utils.replaceKeywordsBack(value,
673:                                            oldValues);
674:                                    whereValues.add(value);
675:                                } else {
676:                                    int delimiter1 = strToken.toLowerCase()
677:                                            .indexOf(" is ");
678:                                    whereCols.add(strToken.substring(0,
679:                                            delimiter1).trim().toUpperCase());
680:                                    whereValues.add(null);
681:                                }
682:                            }
683:                        }
684:                        columnWhereNames = new String[whereCols.size()];
685:                        columnWhereValues = new String[whereValues.size()];
686:                        whereCols.copyInto(columnWhereNames);
687:                        whereValues.copyInto(columnWhereValues);
688:                    }
689:                }
690:
691:                //CREATE TABLE
692:                else if (upperSql.startsWith("CREATE TABLE ")) {
693:                    //removing line brakes
694:                    sql = Utils.replaceAll(sql, "\n", "");
695:                    upperSql = sql.toUpperCase();
696:                    sqlType = CREATE_TABLE;
697:                    int createPos = upperSql.indexOf("CREATE TABLE");
698:                    int tableStartPos = upperSql.indexOf("(");
699:                    int tableEndPos = upperSql.lastIndexOf(")");
700:
701:                    tableName = sql.substring(createPos + 12, tableStartPos)
702:                            .trim().toUpperCase();
703:                    String createString = sql.substring(tableStartPos + 1,
704:                            tableEndPos).trim();
705:                    ArrayList setColumnNames = new ArrayList();
706:                    ArrayList setPrimaryKeys = new ArrayList();
707:                    ArrayList setNotnullColumns = new ArrayList();
708:                    String token = ((String) upperSql.substring(
709:                            tableStartPos + 1, tableEndPos)).trim();
710:                    StringTokenizer comma = new StringTokenizer(token, ",");
711:                    while (comma.hasMoreTokens()) {
712:                        String nextComma = comma.nextToken().trim();
713:                        StringTokenizer space = new StringTokenizer(nextComma,
714:                                " ");
715:                        String nextSpace = space.nextToken();
716:                        setColumnNames.add(nextSpace);
717:                        if (nextComma.indexOf("PRIMARYKEY") != -1) {
718:                            setPrimaryKeys.add(nextSpace);
719:                            setNotnullColumns.add(nextSpace);
720:                        }
721:                        if (nextComma.indexOf("NOT NULL") != -1) {
722:                            setNotnullColumns.add(nextSpace);
723:                        }
724:                    }
725:                    columnNames = new String[setColumnNames.size()];
726:                    setColumnNames.toArray(columnNames);
727:                    this .primaryKeyColumns = new String[setPrimaryKeys.size()];
728:                    setPrimaryKeys.toArray(this .primaryKeyColumns);
729:                    this .notnullColumns = new String[setNotnullColumns.size()];
730:                    setNotnullColumns.toArray(this .notnullColumns);
731:
732:                } else {
733:                    throw new Exception("Malformed SQL. Wrong SQL statement.");
734:                }
735:            }
736:
737:            /**
738:             * Set setBinaryStreamList.
739:             * @param list
740:             */
741:            public void setBinaryStreamList(ArrayList list) {
742:                this.binaryStreamObjectList = list;
743:            }
744:
745:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.