Source Code Cross Referenced for TransferSQLText.java in  » Database-DBMS » hsql » org » hsqldb » util » 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 DBMS » hsql » org.hsqldb.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright (c) 2001-2005, The HSQL Development Group
002:         * All rights reserved.
003:         *
004:         * Redistribution and use in source and binary forms, with or without
005:         * modification, are permitted provided that the following conditions are met:
006:         *
007:         * Redistributions of source code must retain the above copyright notice, this
008:         * list of conditions and the following disclaimer.
009:         *
010:         * Redistributions in binary form must reproduce the above copyright notice,
011:         * this list of conditions and the following disclaimer in the documentation
012:         * and/or other materials provided with the distribution.
013:         *
014:         * Neither the name of the HSQL Development Group nor the names of its
015:         * contributors may be used to endorse or promote products derived from this
016:         * software without specific prior written permission.
017:         *
018:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019:         * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020:         * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021:         * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022:         * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024:         * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025:         * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028:         * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029:         */
030:
031:        package org.hsqldb.util;
032:
033:        import java.io.BufferedReader;
034:        import java.io.BufferedWriter;
035:        import java.io.FileReader;
036:        import java.io.FileWriter;
037:        import java.io.IOException;
038:        import java.sql.SQLException;
039:        import java.util.Enumeration;
040:        import java.util.Hashtable;
041:        import java.util.NoSuchElementException;
042:        import java.util.StringTokenizer;
043:        import java.util.Vector;
044:
045:        /**
046:         * @author Nicolas BAZIN, INGENICO
047:         * @version 1.7.0
048:         */
049:        class TransferSQLText extends DataAccessPoint {
050:
051:            String sFileName = null;
052:            BufferedWriter WTextWrite = null;
053:            BufferedReader WTextRead = null;
054:            protected boolean StructureAlreadyParsed = false;
055:            Hashtable DbStmts = null;
056:            protected JDBCTypes JDBCT = null;
057:
058:            TransferSQLText(String _FileName, Traceable t)
059:                    throws DataAccessPointException {
060:
061:                super (t);
062:
063:                sFileName = _FileName;
064:                JDBCT = new JDBCTypes();
065:
066:                if (sFileName == null) {
067:                    throw new DataAccessPointException(
068:                            "File name not initialized");
069:                }
070:            }
071:
072:            boolean execute(String statement) throws DataAccessPointException {
073:
074:                if (WTextWrite == null) {
075:                    try {
076:                        WTextWrite = new BufferedWriter(new FileWriter(
077:                                sFileName));
078:                    } catch (IOException e) {
079:                        throw new DataAccessPointException(e.getMessage());
080:                    }
081:                }
082:
083:                try {
084:                    WTextWrite.write(statement + "\n");
085:                    WTextWrite.flush();
086:                } catch (IOException e) {
087:                    throw new DataAccessPointException(e.getMessage());
088:                }
089:
090:                return true;
091:            }
092:
093:            void putData(String statement, TransferResultSet r, int iMaxRows)
094:                    throws DataAccessPointException {
095:
096:                int i = 0;
097:
098:                if (r == null) {
099:                    return;
100:                }
101:
102:                if (WTextWrite == null) {
103:                    try {
104:                        WTextWrite = new BufferedWriter(new FileWriter(
105:                                sFileName));
106:                    } catch (IOException e) {
107:                        throw new DataAccessPointException(e.getMessage());
108:                    }
109:                }
110:
111:                try {
112:                    while (r.next()) {
113:                        if (i == 0) {
114:                            WTextWrite.write(statement + "\n");
115:                            WTextWrite.flush();
116:                        }
117:
118:                        transferRow(r);
119:
120:                        if (iMaxRows != 0 && i == iMaxRows) {
121:                            break;
122:                        }
123:
124:                        i++;
125:
126:                        if (iMaxRows != 0 || i % 100 == 0) {
127:                            tracer.trace("Transfered " + i + " rows");
128:                        }
129:                    }
130:                } catch (Exception e) {
131:                    throw new DataAccessPointException(e.getMessage());
132:                } finally {
133:                    try {
134:                        if (i > 0) {
135:                            WTextWrite.write("\tNumber of Rows=" + i + "\n\n");
136:                            WTextWrite.flush();
137:                        }
138:                    } catch (IOException e) {
139:                        throw new DataAccessPointException(e.getMessage());
140:                    }
141:                }
142:            }
143:
144:            void close() throws DataAccessPointException {
145:
146:                if (WTextWrite != null) {
147:                    try {
148:                        WTextWrite.flush();
149:                        WTextWrite.close();
150:                    } catch (IOException e) {
151:                    }
152:                }
153:            }
154:
155:            /**
156:             * Method declaration
157:             *
158:             *
159:             * @param type
160:             * @param r
161:             * @param p
162:             *
163:             * @throws SQLException
164:             */
165:            private void transferRow(TransferResultSet r) throws Exception {
166:
167:                String sLast = "";
168:                int len = r.getColumnCount();
169:
170:                if (WTextWrite == null) {
171:                    try {
172:                        WTextWrite = new BufferedWriter(new FileWriter(
173:                                sFileName));
174:                    } catch (IOException e) {
175:                        throw new DataAccessPointException(e.getMessage());
176:                    }
177:                }
178:
179:                for (int i = 0; i < len; i++) {
180:                    int t = r.getColumnType(i + 1);
181:
182:                    sLast = "column="
183:                            + r.getColumnName(i + 1)
184:                            + " datatype="
185:                            + (String) helper.getSupportedTypes().get(
186:                                    new Integer(t));
187:
188:                    Object o = r.getObject(i + 1);
189:
190:                    if (o == null) {
191:                        sLast += " value=<null>";
192:                    } else {
193:                        o = helper.convertColumnValue(o, i + 1, t);
194:                        sLast += " value=\'" + o.toString() + "\'";
195:                    }
196:
197:                    WTextWrite.write("\t" + sLast + "\n");
198:                    WTextWrite.flush();
199:                }
200:
201:                WTextWrite.write("\n");
202:                WTextWrite.flush();
203:
204:                sLast = "";
205:            }
206:
207:            class ColumnDef {
208:
209:                String columnName;
210:                String columnType;
211:                String options;
212:                int start;
213:                int len;
214:
215:                public ColumnDef() {
216:
217:                    columnName = "";
218:                    columnType = "";
219:                    options = "";
220:                    start = 0;
221:                    len = 0;
222:                }
223:            }
224:
225:            ColumnDef getColumnDef(String ColumnsDesc, int curPos) {
226:
227:                int nextPos = 0;
228:                ColumnDef columnDef = new TransferSQLText.ColumnDef();
229:
230:                columnDef.start = curPos;
231:
232:                if ((ColumnsDesc == null) || (ColumnsDesc.length() == 0)
233:                        || (curPos >= ColumnsDesc.length())) {
234:                    return new TransferSQLText.ColumnDef();
235:                }
236:
237:                String stbuff = ColumnsDesc.substring(curPos);
238:
239:                try {
240:                    int i = 0;
241:
242:                    for (; i < stbuff.length(); i++) {
243:                        int c = stbuff.charAt(i);
244:
245:                        if (c == ',' || c == ' ' || c == ')' || c == ';') {
246:                            continue;
247:                        } else {
248:                            break;
249:                        }
250:                    }
251:
252:                    if (i == stbuff.length()) {
253:                        return new TransferSQLText.ColumnDef();
254:                    }
255:
256:                    columnDef.len += i;
257:                    stbuff = stbuff.substring(i);
258:
259:                    while (stbuff.charAt(nextPos) != ' ') {
260:                        nextPos++;
261:                    }
262:
263:                    columnDef.columnName = stbuff.substring(0, nextPos);
264:                    stbuff = stbuff.substring(nextPos);
265:                    columnDef.len += nextPos;
266:                    nextPos = 0;
267:
268:                    if (!columnDef.columnName.toUpperCase()
269:                            .equals("CONSTRAINT")) {
270:                        i = 0;
271:
272:                        for (; i < stbuff.length() && stbuff.charAt(i) == ' '; i++) {
273:                        }
274:
275:                        stbuff = stbuff.substring(i);
276:                        columnDef.len += i;
277:
278:                        while ((stbuff.charAt(nextPos) != '(')
279:                                && (stbuff.charAt(nextPos) != ',')
280:                                && (stbuff.charAt(nextPos) != ')')
281:                                && (stbuff.charAt(nextPos) != ';')
282:                                && (stbuff.charAt(nextPos) != ' ')) {
283:                            nextPos++;
284:                        }
285:
286:                        columnDef.columnType = stbuff.substring(0, nextPos)
287:                                .toUpperCase();
288:                        stbuff = stbuff.substring(nextPos);
289:                        columnDef.len += nextPos;
290:                        nextPos = 0;
291:                    }
292:
293:                    while ((stbuff.charAt(nextPos) != ',')
294:                            && (stbuff.charAt(nextPos) != ';')
295:                            && (nextPos < stbuff.length())
296:                            && (stbuff.charAt(nextPos) != ')')) {
297:                        if (stbuff.charAt(nextPos) == '(') {
298:                            while (stbuff.charAt(nextPos) != ')') {
299:                                nextPos++;
300:                            }
301:                        }
302:
303:                        nextPos++;
304:                    }
305:
306:                    columnDef.options = stbuff.substring(0, nextPos);
307:                    columnDef.len += nextPos;
308:                } catch (Exception e) {
309:                    columnDef = new TransferSQLText.ColumnDef();
310:                }
311:
312:                return columnDef;
313:            }
314:
315:            String translateTypes(String CreateLine, TransferTable TTable,
316:                    DataAccessPoint Dest) throws DataAccessPointException {
317:
318:                String translatedLine = "";
319:                JDBCTypes JDBCT = new JDBCTypes();
320:                int currentPos = 0;
321:                String columnName = "";
322:                String columnType = "";
323:                int colnum = 0;
324:                ColumnDef cDef;
325:
326:                currentPos = CreateLine.indexOf('(') + 1;
327:                translatedLine = CreateLine.substring(0, currentPos);
328:
329:                do {
330:                    cDef = getColumnDef(CreateLine, currentPos);
331:
332:                    if (cDef.len == 0) {
333:                        break;
334:                    }
335:
336:                    columnName = cDef.columnName;
337:                    columnType = cDef.columnType;
338:
339:                    if (columnName.toUpperCase().indexOf("CONSTRAINT") >= 0) {
340:                        translatedLine += CreateLine.substring(currentPos,
341:                                currentPos + cDef.len)
342:                                + ",";
343:                        currentPos += cDef.len + 1;
344:
345:                        colnum++;
346:
347:                        continue;
348:                    }
349:
350:                    columnName = Dest.helper.formatIdentifier(columnName) + " ";
351:
352:                    try {
353:                        Integer inttype = new Integer(Dest.helper
354:                                .convertToType(JDBCT.toInt(columnType)));
355:
356:                        columnType = (String) TTable.hTypes.get(inttype);
357:                    } catch (Exception JDBCtypeEx) {
358:                    }
359:
360:                    if (cDef.options != null) {
361:                        columnType += cDef.options;
362:                    }
363:
364:                    try {
365:                        columnType = Dest.helper.fixupColumnDefWrite(TTable,
366:                                null, columnType, null, colnum);
367:                    } catch (SQLException SQLe) {
368:                        return CreateLine;
369:                    }
370:
371:                    translatedLine += columnName + " " + columnType + ",";
372:                    currentPos += cDef.len + 1;
373:
374:                    colnum++;
375:                } while (true);
376:
377:                return translatedLine.substring(0, translatedLine.length() - 1)
378:                        + ");";
379:            }
380:
381:            void parseFileForTables() throws DataAccessPointException {
382:
383:                StringTokenizer Tokenizer;
384:
385:                if (WTextRead == null) {
386:                    try {
387:                        WTextRead = new BufferedReader(
388:                                new FileReader(sFileName));
389:                    } catch (IOException e) {
390:                        throw new DataAccessPointException(e.getMessage());
391:                    }
392:                }
393:
394:                String currentLine = "";
395:                String Token = "";
396:                String name = "";
397:                TransferTable relatedTable = null;
398:
399:                try {
400:                    while ((currentLine = WTextRead.readLine()) != null) {
401:                        currentLine = currentLine.trim() + ";";
402:                        Tokenizer = new StringTokenizer(currentLine);
403:
404:                        try {
405:                            Token = Tokenizer.nextToken();
406:                        } catch (NoSuchElementException NSE) {
407:                            continue;
408:                        }
409:
410:                        if (Token == null) {
411:                            continue;
412:                        }
413:
414:                        if (!Token.toUpperCase().equals("CREATE")) {
415:                            continue;
416:                        }
417:
418:                        Token = Tokenizer.nextToken().toUpperCase();
419:
420:                        if (Token.equals("TABLE") || Token.equals("VIEW")) {
421:                            try {
422:                                name = Tokenizer.nextToken(" (;");
423:                                relatedTable = new TransferTable(this , name,
424:                                        "", Token, tracer);
425:                                relatedTable.Stmts.bCreate = false;
426:                                relatedTable.Stmts.bDelete = false;
427:                                relatedTable.Stmts.bDrop = false;
428:                                relatedTable.Stmts.bCreateIndex = false;
429:                                relatedTable.Stmts.bDropIndex = false;
430:                                relatedTable.Stmts.bInsert = false;
431:                                relatedTable.Stmts.bAlter = false;
432:
433:                                DbStmts.put(relatedTable.Stmts.sSourceTable,
434:                                        relatedTable);
435:                            } catch (NoSuchElementException NSE) {
436:                                continue;
437:                            }
438:                        }
439:                    }
440:                } catch (Exception IOe) {
441:                    throw new DataAccessPointException(IOe.getMessage());
442:                }
443:            }
444:
445:            void parseFileForTheRest(TransferTable TTable, DataAccessPoint Dest)
446:                    throws DataAccessPointException {
447:
448:                StringTokenizer Tokenizer;
449:
450:                StructureAlreadyParsed = true;
451:
452:                if (WTextRead == null) {
453:                    try {
454:                        WTextRead = new BufferedReader(
455:                                new FileReader(sFileName));
456:                    } catch (IOException e) {
457:                        throw new DataAccessPointException(e.getMessage());
458:                    }
459:                }
460:
461:                String currentLine = "";
462:                String Token = "";
463:                String name = "";
464:                TransferTable relatedTable = null;
465:
466:                try {
467:                    while ((currentLine = WTextRead.readLine()) != null) {
468:                        currentLine = currentLine.trim() + ";";
469:                        Tokenizer = new StringTokenizer(currentLine);
470:
471:                        try {
472:                            Token = Tokenizer.nextToken();
473:                        } catch (NoSuchElementException NSE) {
474:                            continue;
475:                        }
476:
477:                        if (Token == null) {
478:                            continue;
479:                        }
480:
481:                        if (Token.toUpperCase().equals("INSERT")) {
482:                            try {
483:                                if (!Tokenizer.nextToken().toUpperCase()
484:                                        .equals("INTO")) {
485:                                    throw new DataAccessPointException(
486:                                            "Error in INSERT statement: no INTO found");
487:                                }
488:
489:                                Token = Tokenizer.nextToken();
490:
491:                                if ((relatedTable = (TransferTable) DbStmts
492:                                        .get(Token)) != null) {
493:                                    relatedTable.Stmts.bDelete = true;
494:                                    relatedTable.Stmts.bInsert = true;
495:                                    relatedTable.Stmts.sDestInsert = currentLine;
496:                                    relatedTable.Stmts.sDestDelete = "DELETE FROM "
497:                                            + relatedTable.Stmts.sSourceTable
498:                                            + ";";
499:                                }
500:
501:                                continue;
502:                            } catch (NoSuchElementException NSE) {
503:                                continue;
504:                            }
505:                        } else if (Token.toUpperCase().equals("ALTER")) {
506:                            try {
507:                                if (!Tokenizer.nextToken().toUpperCase()
508:                                        .equals("TABLE")) {
509:                                    continue;
510:                                }
511:
512:                                name = Tokenizer.nextToken();
513:                                Token = Tokenizer.nextToken().toUpperCase();
514:
515:                                if (!Token.equals("ADD")) {
516:                                    continue;
517:                                }
518:
519:                                do {
520:                                    Token = Tokenizer.nextToken().toUpperCase();
521:                                } while (!Token.equals("CONSTRAINT"));
522:
523:                                if ((relatedTable = (TransferTable) DbStmts
524:                                        .get(name)) != null) {
525:                                    if (relatedTable.Stmts.sDestAlter == null) {
526:                                        relatedTable.Stmts.sDestAlter = "";
527:                                    }
528:
529:                                    relatedTable.Stmts.bAlter = true;
530:                                    relatedTable.Stmts.sDestAlter += currentLine;
531:                                } else {
532:                                    throw new DataAccessPointException(
533:                                            "table not found");
534:                                }
535:
536:                                Token = Tokenizer.nextToken();
537:
538:                                if (relatedTable.Stmts.sDestDrop == null) {
539:                                    relatedTable.Stmts.sDestDrop = "";
540:                                }
541:
542:                                relatedTable.Stmts.bDrop = true;
543:                                relatedTable.Stmts.sDestDrop = "ALTER TABLE "
544:                                        + name + " DROP CONSTRAINT " + Token
545:                                        + ";" + relatedTable.Stmts.sDestDrop;
546:
547:                                continue;
548:                            } catch (NoSuchElementException NSE) {
549:                                continue;
550:                            }
551:                        } else if (!Token.toUpperCase().equals("CREATE")) {
552:                            continue;
553:                        }
554:
555:                        Token = Tokenizer.nextToken().toUpperCase();
556:
557:                        if (Token.equals("TABLE") || Token.equals("VIEW")) {
558:                            try {
559:                                name = Tokenizer.nextToken(" (;");
560:
561:                                if (!DbStmts.containsKey(name)) {
562:                                    throw new DataAccessPointException(
563:                                            "error: index is created before the table");
564:                                }
565:
566:                                relatedTable = (TransferTable) DbStmts
567:                                        .get(name);
568:                                relatedTable.Stmts.bCreate = true;
569:                                relatedTable.Stmts.bDrop = true;
570:
571:                                //                        relatedTable.Stmts.sDestCreate = currentLine;
572:                                relatedTable.Stmts.sDestCreate = translateTypes(
573:                                        currentLine, TTable, Dest);
574:                                relatedTable.Stmts.sDestDrop = "DROP "
575:                                        + relatedTable.Stmts.sType + " " + name
576:                                        + ";";
577:
578:                                DbStmts.put(relatedTable.Stmts.sSourceTable,
579:                                        relatedTable);
580:                            } catch (NoSuchElementException NSE) {
581:                                continue;
582:                            }
583:                        }
584:
585:                        if (Token.equals("INDEX") || Token.equals("UNIQUE")) {
586:                            try {
587:                                while ((Token = Tokenizer.nextToken())
588:                                        .toUpperCase().equals("INDEX")) {
589:                                    ;
590:                                }
591:
592:                                String IndexdropCommand = "DROP INDEX " + Token
593:                                        + " ;";
594:
595:                                while ((Token = Tokenizer.nextToken(" ("))
596:                                        .toUpperCase().equals("ON")) {
597:                                    ;
598:                                }
599:
600:                                name = Token;
601:
602:                                if (!DbStmts.containsKey(Token)) {
603:                                    throw new DataAccessPointException(
604:                                            "error: index is created before the table");
605:                                }
606:
607:                                relatedTable = (TransferTable) DbStmts
608:                                        .get(Token);
609:
610:                                if (relatedTable.Stmts.sDestCreateIndex == null) {
611:                                    relatedTable.Stmts.sDestCreateIndex = "";
612:                                }
613:
614:                                if (relatedTable.Stmts.sDestDropIndex == null) {
615:                                    relatedTable.Stmts.sDestDropIndex = "";
616:                                }
617:
618:                                relatedTable.Stmts.bCreateIndex = true;
619:                                relatedTable.Stmts.bDropIndex = true;
620:                                relatedTable.Stmts.sDestCreateIndex += currentLine;
621:                                relatedTable.Stmts.sDestDropIndex += IndexdropCommand;
622:                            } catch (NoSuchElementException NSE) {
623:                                continue;
624:                            }
625:                        }
626:                    }
627:                } catch (IOException IOe) {
628:                    throw new DataAccessPointException(IOe.getMessage());
629:                }
630:            }
631:
632:            Vector getTables(String sCatalog, String[] sSchemas)
633:                    throws DataAccessPointException {
634:
635:                Vector AllTables = new Vector();
636:
637:                if (DbStmts == null) {
638:                    DbStmts = new Hashtable();
639:                }
640:
641:                if (WTextRead != null) {
642:                    try {
643:                        WTextRead.close();
644:
645:                        WTextRead = null;
646:                    } catch (IOException e) {
647:                    }
648:                }
649:
650:                this .parseFileForTables();
651:
652:                StructureAlreadyParsed = false;
653:
654:                Enumeration e = DbStmts.elements();
655:
656:                while (e.hasMoreElements()) {
657:                    AllTables.addElement(e.nextElement());
658:                }
659:
660:                return AllTables;
661:            }
662:
663:            void getTableStructure(TransferTable TTable, DataAccessPoint Dest)
664:                    throws DataAccessPointException {
665:
666:                if (!StructureAlreadyParsed) {
667:                    if (WTextRead != null) {
668:                        try {
669:                            WTextRead.close();
670:
671:                            WTextRead = null;
672:                        } catch (IOException e) {
673:                        }
674:                    }
675:
676:                    this .parseFileForTheRest(TTable, Dest);
677:                }
678:            }
679:
680:            TransferResultSet getData(String statement)
681:                    throws DataAccessPointException {
682:
683:                StringTokenizer Tokenizer;
684:                String tableName = "";
685:
686:                try {
687:                    Tokenizer = new StringTokenizer(statement);
688:
689:                    while (!Tokenizer.nextToken().toUpperCase().equals("FROM")) {
690:                        ;
691:                    }
692:
693:                    tableName = Tokenizer.nextToken(" ;");
694:                } catch (NoSuchElementException NSE) {
695:                    throw new DataAccessPointException(
696:                            "Table name not found in statement: " + statement);
697:                }
698:
699:                if (WTextRead != null) {
700:                    try {
701:                        WTextRead.close();
702:
703:                        WTextRead = null;
704:                    } catch (IOException e) {
705:                    }
706:                }
707:
708:                return (this .parseFileForData(tableName));
709:            }
710:
711:            TransferResultSet parseFileForData(String tableName)
712:                    throws DataAccessPointException {
713:
714:                TransferResultSet trsData = new TransferResultSet();
715:                StringTokenizer Tokenizer;
716:
717:                if (WTextRead == null) {
718:                    try {
719:                        WTextRead = new BufferedReader(
720:                                new FileReader(sFileName));
721:                    } catch (IOException e) {
722:                        throw new DataAccessPointException(e.getMessage());
723:                    }
724:                }
725:
726:                String currentLine = "";
727:                String Token;
728:
729:                try {
730:                    while ((currentLine = WTextRead.readLine()) != null) {
731:                        currentLine = currentLine.trim() + ";";
732:                        Tokenizer = new StringTokenizer(currentLine);
733:
734:                        try {
735:                            Token = Tokenizer.nextToken();
736:                        } catch (NoSuchElementException NSE) {
737:                            continue;
738:                        }
739:
740:                        if (Token == null) {
741:                            continue;
742:                        }
743:
744:                        if (!Token.toUpperCase().equals("INSERT")) {
745:                            continue;
746:                        }
747:
748:                        try {
749:                            if (!Tokenizer.nextToken().toUpperCase().equals(
750:                                    "INTO")) {
751:                                throw new DataAccessPointException(
752:                                        "Error in INSERT statement: no INTO found");
753:                            }
754:
755:                            Token = Tokenizer.nextToken();
756:
757:                            if (!Token.equals(tableName)) {
758:                                continue;
759:                            }
760:
761:                            int iParsedRows = 0;
762:                            Vector vColumnNames = new Vector();
763:                            Vector vColumnValues = new Vector();
764:                            Vector vColumnTypes = new Vector();
765:
766:                            while ((currentLine = WTextRead.readLine()) != null) {
767:                                currentLine = currentLine.trim();
768:
769:                                boolean newLine = (currentLine.length() == 0);
770:
771:                                if (newLine) {
772:                                    int iColumnNb = 0;
773:
774:                                    iParsedRows++;
775:
776:                                    iColumnNb = vColumnNames.size();
777:
778:                                    String[] Names = new String[iColumnNb + 1];
779:                                    int[] Types = new int[iColumnNb + 1];
780:                                    Object[] Values = new Object[iColumnNb + 1];
781:
782:                                    for (int Idx = 0; Idx < iColumnNb; Idx++) {
783:                                        Names[Idx + 1] = (String) vColumnNames
784:                                                .elementAt(Idx);
785:                                        Types[Idx + 1] = ((Integer) vColumnTypes
786:                                                .elementAt(Idx)).intValue();
787:                                        Values[Idx + 1] = vColumnValues
788:                                                .elementAt(Idx);
789:                                    }
790:
791:                                    try {
792:                                        trsData.addRow(Names, Types, Values,
793:                                                iColumnNb);
794:                                    } catch (Exception e) {
795:                                        throw new DataAccessPointException(e
796:                                                .getMessage());
797:                                    }
798:
799:                                    iColumnNb = 0;
800:
801:                                    vColumnNames.removeAllElements();
802:                                    vColumnValues.removeAllElements();
803:                                    vColumnTypes.removeAllElements();
804:
805:                                    continue;
806:                                }
807:
808:                                Tokenizer = new StringTokenizer(currentLine);
809:                                Token = Tokenizer.nextToken("=");
810:
811:                                if (Token.equals("Number of Rows")) {
812:                                    int iNbRows = Integer.parseInt(Tokenizer
813:                                            .nextToken());
814:
815:                                    if (iNbRows != iParsedRows) {
816:                                        throw new DataAccessPointException(
817:                                                "Number of parsed rows ("
818:                                                        + iParsedRows
819:                                                        + ") is different from the expected ("
820:                                                        + iNbRows + ")");
821:                                    }
822:
823:                                    return trsData;
824:                                }
825:
826:                                if (Token.equals("column")) {
827:                                    Token = Tokenizer.nextToken(" =");
828:
829:                                    vColumnNames.addElement(Token);
830:                                }
831:
832:                                Token = Tokenizer.nextToken(" =");
833:
834:                                if (Token.equals("datatype")) {
835:                                    int iType;
836:
837:                                    Token = Tokenizer.nextToken(" =");
838:
839:                                    try {
840:                                        iType = JDBCT
841:                                                .toInt(Token.toUpperCase());
842:                                    } catch (Exception e) {
843:                                        throw new DataAccessPointException(
844:                                                "Unknown type: " + Token);
845:                                    }
846:
847:                                    vColumnTypes.addElement(new Integer(iType));
848:                                }
849:
850:                                Token = Tokenizer.nextToken(" =");
851:
852:                                if (Token.equals("value")) {
853:                                    int iStart = currentLine.indexOf("value=") + 6;
854:                                    String sValue = currentLine.substring(
855:                                            iStart).trim();
856:
857:                                    if (sValue.indexOf("<null>") >= 0) {
858:                                        vColumnValues.addElement(null);
859:                                    } else {
860:                                        int i = sValue.indexOf('\'') + 1;
861:                                        String sbToken = sValue.substring(i);
862:
863:                                        i = sbToken.lastIndexOf('\'');
864:                                        sbToken = sbToken.substring(0, i);
865:                                        Token = sbToken;
866:
867:                                        vColumnValues.addElement(Token);
868:                                    }
869:                                }
870:                            }
871:                        } catch (IndexOutOfBoundsException IOBe) {
872:                            continue;
873:                        }
874:                    }
875:                } catch (IOException IOe) {
876:                    throw new DataAccessPointException(IOe.getMessage());
877:                }
878:
879:                return trsData;
880:            }
881:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.