Source Code Cross Referenced for TransferDb.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.sql.Connection;
034:        import java.sql.DatabaseMetaData;
035:        import java.sql.PreparedStatement;
036:        import java.sql.ResultSet;
037:        import java.sql.ResultSetMetaData;
038:        import java.sql.SQLException;
039:        import java.sql.Statement;
040:        import java.sql.Types;
041:        import java.util.Vector;
042:
043:        // fredt@users 20020215 - patch 516309 by Nicolas Bazin - enhancements
044:        // sqlbob@users 20020401 - patch 1.7.0 - reengineering
045:        // nicolas BAZIN 20020430 - add support of Catalog and mckoi db helper
046:        // Stephan Frind 20040508 - speed improvements
047:
048:        /**
049:         * Conversions between different databases
050:         *
051:         * @version 1.7.0
052:         */
053:        class TransferDb extends DataAccessPoint {
054:
055:            Connection conn;
056:            DatabaseMetaData meta;
057:            protected Statement srcStatement = null;
058:
059:            TransferDb(Connection c, Traceable t)
060:                    throws DataAccessPointException {
061:
062:                super (t);
063:
064:                conn = c;
065:
066:                if (c != null) {
067:                    String productLowerName;
068:
069:                    try {
070:                        meta = c.getMetaData();
071:                        databaseToConvert = c.getCatalog();
072:                        productLowerName = meta.getDatabaseProductName();
073:
074:                        if (productLowerName == null) {
075:                            productLowerName = "";
076:                        } else {
077:                            productLowerName = productLowerName.toLowerCase();
078:                        }
079:
080:                        helper = HelperFactory.getHelper(productLowerName);
081:
082:                        helper.set(this , t, meta.getIdentifierQuoteString());
083:                    } catch (SQLException e) {
084:                        throw new DataAccessPointException(e.getMessage());
085:                    }
086:                }
087:            }
088:
089:            boolean isConnected() {
090:                return (conn != null);
091:            }
092:
093:            boolean getAutoCommit() throws DataAccessPointException {
094:
095:                boolean result = false;
096:
097:                try {
098:                    result = conn.getAutoCommit();
099:                } catch (SQLException e) {
100:                    throw new DataAccessPointException(e.getMessage());
101:                }
102:
103:                return result;
104:            }
105:
106:            void commit() throws DataAccessPointException {
107:
108:                if (srcStatement != null) {
109:                    try {
110:                        srcStatement.close();
111:                    } catch (SQLException e) {
112:                    }
113:
114:                    srcStatement = null;
115:                }
116:
117:                try {
118:                    conn.commit();
119:                } catch (SQLException e) {
120:                    throw new DataAccessPointException(e.getMessage());
121:                }
122:            }
123:
124:            void rollback() throws DataAccessPointException {
125:
126:                if (srcStatement != null) {
127:                    try {
128:                        srcStatement.close();
129:                    } catch (SQLException e) {
130:                    }
131:
132:                    srcStatement = null;
133:                }
134:
135:                try {
136:                    conn.rollback();
137:                } catch (SQLException e) {
138:                    throw new DataAccessPointException(e.getMessage());
139:                }
140:            }
141:
142:            void setAutoCommit(boolean flag) throws DataAccessPointException {
143:
144:                try {
145:                    conn.setAutoCommit(flag);
146:                } catch (SQLException e) {
147:                    throw new DataAccessPointException(e.getMessage());
148:                }
149:            }
150:
151:            boolean execute(String statement) throws DataAccessPointException {
152:
153:                boolean result = false;
154:                Statement stmt = null;
155:
156:                try {
157:                    stmt = conn.createStatement();
158:                    result = stmt.execute(statement);
159:                } catch (SQLException e) {
160:                    throw new DataAccessPointException(e.getMessage());
161:                } finally {
162:                    if (stmt != null) {
163:                        try {
164:                            stmt.close();
165:                        } catch (SQLException e) {
166:                        }
167:                    }
168:                }
169:
170:                return result;
171:            }
172:
173:            TransferResultSet getData(String statement)
174:                    throws DataAccessPointException {
175:
176:                ResultSet rsData = null;
177:
178:                try {
179:                    if (srcStatement != null) {
180:                        srcStatement.close();
181:                    }
182:
183:                    srcStatement = conn.createStatement();
184:                    rsData = srcStatement.executeQuery(statement);
185:                } catch (SQLException e) {
186:                    try {
187:                        srcStatement.close();
188:                    } catch (Exception e1) {
189:                    }
190:
191:                    srcStatement = null;
192:                    rsData = null;
193:
194:                    throw new DataAccessPointException(e.getMessage());
195:                }
196:
197:                return new TransferResultSet(rsData);
198:            }
199:
200:            void putData(String statement, TransferResultSet r, int iMaxRows)
201:                    throws DataAccessPointException {
202:
203:                if ((statement == null) || statement.equals("") || (r == null)) {
204:                    return;
205:                }
206:
207:                PreparedStatement destPrep = null;
208:
209:                try {
210:                    destPrep = conn.prepareStatement(statement);
211:
212:                    int i = 0;
213:                    int tmpLength;
214:                    int len = r.getColumnCount();
215:                    int[] tmpTypes = null;
216:
217:                    while (r.next()) {
218:                        if (tmpTypes == null) {
219:                            tmpTypes = new int[len + 1];
220:
221:                            for (int j = 1; j <= len; j++) {
222:                                tmpTypes[j] = r.getColumnType(j);
223:                            }
224:                        }
225:
226:                        transferRow(r, destPrep, len, tmpTypes);
227:
228:                        if (iMaxRows != 0 && i == iMaxRows) {
229:                            break;
230:                        }
231:
232:                        i++;
233:
234:                        if (iMaxRows != 0 || i % 100 == 0) {
235:                            tracer.trace("Transfered " + i + " rows");
236:                        }
237:                    }
238:                } catch (SQLException e) {
239:                    throw new DataAccessPointException(e.getMessage());
240:                } finally {
241:                    if (destPrep != null) {
242:                        try {
243:                            destPrep.close();
244:                        } catch (SQLException e) {
245:                        }
246:                    }
247:                }
248:            }
249:
250:            /*
251:             private void transferRow(TransferResultSet r,
252:             PreparedStatement p)
253:             throws DataAccessPointException, SQLException {
254:             // TODO
255:             // what is this never used variable for?
256:             // looks like missing debug flags because constructing these strings consumes a lot
257:             // of time
258:             String sLast = "";
259:
260:             if (p != null) {
261:             p.clearParameters();
262:             }
263:
264:             int len = r.getColumnCount();
265:
266:             for (int i = 0; i < len; i++) {
267:             int t = r.getColumnType(i + 1);
268:
269:             sLast = "column=" + r.getColumnName(i + 1) + " datatype="
270:             + (String) helper.getSupportedTypes().get(new Integer(t));
271:
272:             Object o = r.getObject(i + 1);
273:
274:             if (o == null) {
275:             if (p != null) {
276:             p.setNull(i + 1, t);
277:             }
278:
279:             sLast += " value=<null>";
280:             } else {
281:             o = helper.convertColumnValue(o, i + 1, t);
282:
283:             p.setObject(i + 1, o);
284:
285:             sLast += " value=\'" + o.toString() + "\'";
286:             }
287:             }
288:
289:             if (p != null) {
290:             p.execute();
291:             }
292:
293:             sLast = "";
294:             }
295:             */
296:            Vector getSchemas() throws DataAccessPointException {
297:
298:                Vector ret = new Vector();
299:                ResultSet result = null;
300:
301:                try {
302:                    result = meta.getSchemas();
303:                } catch (SQLException e) {
304:                    result = null;
305:                }
306:
307:                try {
308:                    if (result != null) {
309:                        while (result.next()) {
310:                            ret.addElement(result.getString(1));
311:                        }
312:
313:                        result.close();
314:                    }
315:                } catch (SQLException e) {
316:                    throw new DataAccessPointException(e.getMessage());
317:                }
318:
319:                return (ret);
320:            }
321:
322:            Vector getCatalog() throws DataAccessPointException {
323:
324:                Vector ret = new Vector();
325:                ResultSet result = null;
326:
327:                if (databaseToConvert != null && databaseToConvert.length() > 0) {
328:                    ret.addElement(databaseToConvert);
329:
330:                    return (ret);
331:                }
332:
333:                try {
334:                    result = meta.getCatalogs();
335:                } catch (SQLException e) {
336:                    result = null;
337:                }
338:
339:                try {
340:                    if (result != null) {
341:                        while (result.next()) {
342:                            ret.addElement(result.getString(1));
343:                        }
344:
345:                        result.close();
346:                    }
347:                } catch (SQLException e) {
348:                    throw new DataAccessPointException(e.getMessage());
349:                }
350:
351:                return (ret);
352:            }
353:
354:            void setCatalog(String sCatalog) throws DataAccessPointException {
355:
356:                if (sCatalog != null && sCatalog.length() > 0) {
357:                    try {
358:                        conn.setCatalog(sCatalog);
359:                    } catch (SQLException e) {
360:                        throw new DataAccessPointException(e.getMessage());
361:                    }
362:                }
363:            }
364:
365:            Vector getTables(String sCatalog, String[] sSchemas)
366:                    throws DataAccessPointException {
367:
368:                Vector tTable = new Vector();
369:                ResultSet result = null;
370:
371:                tracer.trace("Reading source tables");
372:
373:                int nbloops = 1;
374:
375:                if (sSchemas != null) {
376:                    nbloops = sSchemas.length;
377:                }
378:
379:                try {
380:
381:                    // variations return null or emtpy result sets with informix JDBC driver 2.2
382:                    for (int SchemaIdx = 0; SchemaIdx < nbloops; SchemaIdx++) {
383:                        if (sSchemas != null && sSchemas[SchemaIdx] != null) {
384:                            result = meta.getTables(sCatalog,
385:                                    sSchemas[SchemaIdx], null, null);
386:                        } else {
387:                            try {
388:                                result = meta.getTables(sCatalog, "", null,
389:                                        null);
390:                            } catch (SQLException e) {
391:                                result = meta.getTables(sCatalog, null, null,
392:                                        null);
393:                            }
394:                        }
395:
396:                        while (result.next()) {
397:                            String name = result.getString(3);
398:                            String type = result.getString(4);
399:                            String schema = "";
400:
401:                            if (sSchemas != null && sSchemas[SchemaIdx] != null) {
402:                                schema = sSchemas[SchemaIdx];
403:                            }
404:
405:                            /*
406:                             ** we ignore the following table types:
407:                             **    "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY"
408:                             **    "ALIAS", "SYNONYM"
409:                             */
410:                            if ((type.compareTo("TABLE") == 0)
411:                                    || (type.compareTo("VIEW") == 0)) {
412:                                TransferTable t = new TransferTable(this , name,
413:                                        schema, type, tracer);
414:
415:                                tTable.addElement(t);
416:                            } else {
417:                                tracer.trace("Found table of type :" + type
418:                                        + " - this type is ignored");
419:                            }
420:                        }
421:                    }
422:                } catch (SQLException e) {
423:                    throw new DataAccessPointException(e.getMessage());
424:                } finally {
425:                    if (result != null) {
426:                        try {
427:                            result.close();
428:                        } catch (SQLException e) {
429:                        }
430:                    }
431:                }
432:
433:                return (tTable);
434:            }
435:
436:            void getTableStructure(TransferTable TTable, DataAccessPoint Dest)
437:                    throws DataAccessPointException {
438:
439:                String create = "CREATE " + TTable.Stmts.sType + " "
440:                        + Dest.helper.formatName(TTable.Stmts.sDestTable);
441:                String insert = "";
442:                ResultSet ImportedKeys = null;
443:                boolean importedkeys = false;
444:                String alterCreate = new String("");
445:                String alterDrop = new String("");
446:                String ConstraintName = new String("");
447:                String RefTableName = new String("");
448:                String foreignKeyName = new String("");
449:                String columnName = new String("");
450:
451:                TTable.Stmts.sDestDrop = "DROP " + TTable.Stmts.sType + " "
452:                        + Dest.helper.formatName(TTable.Stmts.sDestTable) + ";";
453:
454:                if (TTable.Stmts.sType.compareTo("TABLE") == 0) {
455:                    TTable.Stmts.sDestDelete = "DELETE FROM "
456:                            + Dest.helper.formatName(TTable.Stmts.sDestTable)
457:                            + ";";
458:                    create += "(";
459:                } else if (TTable.Stmts.sType.compareTo("VIEW") == 0) {
460:                    TTable.Stmts.bDelete = false;
461:                    TTable.Stmts.sDestDelete = "";
462:                    create += " AS SELECT ";
463:                }
464:
465:                if (TTable.Stmts.sType.compareTo("TABLE") == 0) {
466:                    insert = "INSERT INTO "
467:                            + Dest.helper.formatName(TTable.Stmts.sDestTable)
468:                            + " VALUES(";
469:                } else if (TTable.Stmts.sType.compareTo("VIEW") == 0) {
470:                    TTable.Stmts.bInsert = false;
471:                    insert = "";
472:                }
473:
474:                if (TTable.Stmts.sType.compareTo("VIEW") == 0) {
475:                    /*
476:                     ** Don't know how to retrieve the underlying select so we leave here.
477:                     ** The user will have to edit the rest of the create statement.
478:                     */
479:                    TTable.Stmts.bTransfer = false;
480:                    TTable.Stmts.bCreate = true;
481:                    TTable.Stmts.bDelete = false;
482:                    TTable.Stmts.bDrop = true;
483:                    TTable.Stmts.bCreateIndex = false;
484:                    TTable.Stmts.bDropIndex = false;
485:                    TTable.Stmts.bInsert = false;
486:                    TTable.Stmts.bAlter = false;
487:
488:                    return;
489:                }
490:
491:                ImportedKeys = null;
492:
493:                try {
494:                    ImportedKeys = meta.getImportedKeys(
495:                            TTable.Stmts.sDatabaseToConvert,
496:                            TTable.Stmts.sSchema, TTable.Stmts.sSourceTable);
497:                } catch (SQLException e) {
498:                    ImportedKeys = null;
499:                }
500:
501:                try {
502:                    if (ImportedKeys != null) {
503:                        while (ImportedKeys.next()) {
504:                            importedkeys = true;
505:
506:                            if (!ImportedKeys.getString(12).equals(
507:                                    ConstraintName)) {
508:                                if (!ConstraintName.equals("")) {
509:                                    alterCreate += Dest.helper
510:                                            .formatIdentifier(columnName
511:                                                    .substring(0, columnName
512:                                                            .length() - 1))
513:                                            + ") REFERENCES "
514:                                            + Dest.helper
515:                                                    .formatName(RefTableName);
516:
517:                                    if (foreignKeyName.length() > 0) {
518:                                        alterCreate += " ("
519:                                                + Dest.helper
520:                                                        .formatIdentifier(foreignKeyName
521:                                                                .substring(
522:                                                                        0,
523:                                                                        foreignKeyName
524:                                                                                .length() - 1))
525:                                                + ")";
526:                                    }
527:
528:                                    alterCreate += ";";
529:                                    alterDrop = alterDrop.substring(0,
530:                                            alterDrop.length() - 1)
531:                                            + ";";
532:                                    foreignKeyName = "";
533:                                    columnName = "";
534:                                }
535:
536:                                RefTableName = ImportedKeys.getString(3);
537:                                ConstraintName = ImportedKeys.getString(12);
538:                                alterCreate += "ALTER TABLE "
539:                                        + Dest.helper
540:                                                .formatName(TTable.Stmts.sDestTable)
541:                                        + " ADD CONSTRAINT ";
542:
543:                                if ((TTable.Stmts.bFKForced)
544:                                        && (!ConstraintName.startsWith("FK_"))) {
545:                                    alterCreate += Dest.helper
546:                                            .formatIdentifier("FK_"
547:                                                    + ConstraintName)
548:                                            + " ";
549:                                } else {
550:                                    alterCreate += Dest.helper
551:                                            .formatIdentifier(ConstraintName)
552:                                            + " ";
553:                                }
554:
555:                                alterCreate += "FOREIGN KEY (";
556:                                alterDrop += "ALTER TABLE "
557:                                        + Dest.helper
558:                                                .formatName(TTable.Stmts.sDestTable)
559:                                        + " DROP CONSTRAINT ";
560:
561:                                if ((TTable.Stmts.bFKForced)
562:                                        && (!ConstraintName.startsWith("FK_"))) {
563:                                    alterDrop += Dest.helper
564:                                            .formatIdentifier("FK_"
565:                                                    + ConstraintName)
566:                                            + " ";
567:                                } else {
568:                                    alterDrop += Dest.helper
569:                                            .formatIdentifier(ConstraintName)
570:                                            + " ";
571:                                }
572:                            }
573:
574:                            columnName += ImportedKeys.getString(8) + ",";
575:                            foreignKeyName += ImportedKeys.getString(4) + ",";
576:                        }
577:
578:                        ImportedKeys.close();
579:                    }
580:
581:                    if (importedkeys) {
582:                        alterCreate += columnName.substring(0, columnName
583:                                .length() - 1)
584:                                + ") REFERENCES "
585:                                + Dest.helper.formatName(RefTableName);
586:
587:                        if (foreignKeyName.length() > 0) {
588:                            alterCreate += " ("
589:                                    + Dest.helper
590:                                            .formatIdentifier(foreignKeyName
591:                                                    .substring(
592:                                                            0,
593:                                                            foreignKeyName
594:                                                                    .length() - 1))
595:                                    + ")";
596:                        }
597:
598:                        alterCreate += ";";
599:                        alterDrop = alterDrop.substring(0,
600:                                alterDrop.length() - 1)
601:                                + ";";
602:                        TTable.Stmts.sDestDrop = alterDrop
603:                                + TTable.Stmts.sDestDrop;
604:                    }
605:                } catch (SQLException e) {
606:                    throw new DataAccessPointException(e.getMessage());
607:                }
608:
609:                boolean primarykeys = false;
610:                String PrimaryKeysConstraint = new String();
611:
612:                PrimaryKeysConstraint = "";
613:
614:                ResultSet PrimaryKeys = null;
615:
616:                try {
617:                    PrimaryKeys = meta.getPrimaryKeys(
618:                            TTable.Stmts.sDatabaseToConvert,
619:                            TTable.Stmts.sSchema, TTable.Stmts.sSourceTable);
620:                } catch (SQLException e) {
621:                    PrimaryKeys = null;
622:                }
623:
624:                try {
625:                    if (PrimaryKeys != null) {
626:                        while (PrimaryKeys.next()) {
627:                            if (primarykeys) {
628:                                PrimaryKeysConstraint += ", ";
629:                            } else {
630:                                if (PrimaryKeys.getString(6) != null) {
631:                                    PrimaryKeysConstraint = " CONSTRAINT "
632:                                            + Dest.helper
633:                                                    .formatIdentifier(PrimaryKeys
634:                                                            .getString(6));
635:                                }
636:
637:                                PrimaryKeysConstraint += " PRIMARY KEY (";
638:                            }
639:
640:                            PrimaryKeysConstraint += Dest.helper
641:                                    .formatIdentifier(PrimaryKeys.getString(4));
642:                            primarykeys = true;
643:                        }
644:
645:                        PrimaryKeys.close();
646:
647:                        if (primarykeys) {
648:                            PrimaryKeysConstraint += ") ";
649:                        }
650:                    }
651:                } catch (SQLException e) {
652:                    throw new DataAccessPointException(e.getMessage());
653:                }
654:
655:                boolean indices = false;
656:                ResultSet Indices = null;
657:                String IndiceName = new String("");
658:                String CreateIndex = new String("");
659:                String DropIndex = new String("");
660:
661:                try {
662:                    Indices = meta.getIndexInfo(
663:                            TTable.Stmts.sDatabaseToConvert,
664:                            TTable.Stmts.sSchema, TTable.Stmts.sSourceTable,
665:                            false, false);
666:                } catch (SQLException e) {
667:                    Indices = null;
668:                }
669:
670:                try {
671:                    if (Indices != null) {
672:                        while (Indices.next()) {
673:                            String tmpIndexName = null;
674:
675:                            try {
676:                                tmpIndexName = Indices.getString(6);
677:                            } catch (SQLException e) {
678:                                tmpIndexName = null;
679:                            }
680:
681:                            if (tmpIndexName == null) {
682:                                continue;
683:                            }
684:
685:                            if (!tmpIndexName.equals(IndiceName)) {
686:                                if (!IndiceName.equals("")) {
687:                                    CreateIndex = CreateIndex.substring(0,
688:                                            CreateIndex.length() - 1)
689:                                            + ");";
690:                                    DropIndex += ";";
691:                                }
692:
693:                                IndiceName = tmpIndexName;
694:                                DropIndex += "DROP INDEX ";
695:
696:                                if ((TTable.Stmts.bIdxForced)
697:                                        && (!IndiceName.startsWith("Idx_"))) {
698:                                    DropIndex += Dest.helper
699:                                            .formatIdentifier("Idx_"
700:                                                    + IndiceName);
701:                                } else {
702:                                    DropIndex += Dest.helper
703:                                            .formatIdentifier(IndiceName);
704:                                }
705:
706:                                CreateIndex += "CREATE ";
707:
708:                                if (!Indices.getBoolean(4)) {
709:                                    CreateIndex += "UNIQUE ";
710:                                }
711:
712:                                CreateIndex += "INDEX ";
713:
714:                                if ((TTable.Stmts.bIdxForced)
715:                                        && (!IndiceName.startsWith("Idx_"))) {
716:                                    CreateIndex += Dest.helper
717:                                            .formatIdentifier("Idx_"
718:                                                    + IndiceName);
719:                                } else {
720:                                    CreateIndex += Dest.helper
721:                                            .formatIdentifier(IndiceName);
722:                                }
723:
724:                                CreateIndex += " ON "
725:                                        + Dest.helper
726:                                                .formatName(TTable.Stmts.sDestTable)
727:                                        + "(";
728:                            }
729:
730:                            CreateIndex += Dest.helper.formatIdentifier(Indices
731:                                    .getString(9))
732:                                    + ",";
733:                            indices = true;
734:                        }
735:
736:                        Indices.close();
737:
738:                        if (indices) {
739:                            CreateIndex = CreateIndex.substring(0, CreateIndex
740:                                    .length() - 1)
741:                                    + ");";
742:                            DropIndex += ";";
743:                        }
744:                    }
745:                } catch (SQLException e) {
746:                    throw new DataAccessPointException(e.getMessage());
747:                }
748:
749:                Vector v = new Vector();
750:
751:                tracer.trace("Reading source columns for table "
752:                        + TTable.Stmts.sSourceTable);
753:
754:                ResultSet col = null;
755:                int colnum = 1;
756:                Statement stmt = null;
757:                ResultSet select_rs = null;
758:                ResultSetMetaData select_rsmdata = null;
759:
760:                try {
761:                    stmt = conn.createStatement();
762:                    select_rs = stmt.executeQuery(TTable.Stmts.sSourceSelect);
763:                    select_rsmdata = select_rs.getMetaData();
764:                    col = meta.getColumns(TTable.Stmts.sDatabaseToConvert,
765:                            TTable.Stmts.sSchema, TTable.Stmts.sSourceTable,
766:                            null);
767:                } catch (SQLException eSchema) {
768:
769:                    // fredt - second try with null schema
770:                    if (TTable.Stmts.sSchema.equals("")) {
771:                        try {
772:                            col = meta.getColumns(
773:                                    TTable.Stmts.sDatabaseToConvert, null,
774:                                    TTable.Stmts.sSourceTable, null);
775:                        } catch (SQLException eSchema1) {
776:                        }
777:                    }
778:                }
779:
780:                try {
781:                    while (col.next()) {
782:                        String name = Dest.helper.formatIdentifier(col
783:                                .getString(4));
784:                        int type = col.getShort(5);
785:                        String source = col.getString(6);
786:                        int column_size = col.getInt(7);
787:                        String DefaultVal = col.getString(13);
788:                        boolean rsmdata_NoNulls = (select_rsmdata
789:                                .isNullable(colnum) == java.sql.DatabaseMetaData.columnNoNulls);
790:                        boolean rsmdata_isAutoIncrement = false;
791:
792:                        try {
793:                            rsmdata_isAutoIncrement = select_rsmdata
794:                                    .isAutoIncrement(colnum);
795:                        } catch (SQLException e) {
796:                            rsmdata_isAutoIncrement = false;
797:                        }
798:
799:                        int rsmdata_precision = select_rsmdata
800:                                .getPrecision(colnum);
801:                        int rsmdata_scale = select_rsmdata.getScale(colnum);
802:
803:                        type = helper.convertFromType(type);
804:                        type = Dest.helper.convertToType(type);
805:
806:                        Integer inttype = new Integer(type);
807:                        String datatype = (String) TTable.hTypes.get(inttype);
808:
809:                        if (datatype == null) {
810:                            datatype = source;
811:
812:                            tracer.trace("No mapping for type: " + name
813:                                    + " type: " + type + " source: " + source);
814:                        }
815:
816:                        if (type == Types.NUMERIC) {
817:                            datatype += "("
818:                                    + Integer.toString(rsmdata_precision);
819:
820:                            if (rsmdata_scale > 0) {
821:                                datatype += ","
822:                                        + Integer.toString(rsmdata_scale);
823:                            }
824:
825:                            datatype += ")";
826:                        } else if (type == Types.CHAR) {
827:                            datatype += "(" + Integer.toString(column_size)
828:                                    + ")";
829:                        } else if (rsmdata_isAutoIncrement) {
830:                            datatype = "SERIAL";
831:                        }
832:
833:                        if (DefaultVal != null) {
834:                            if (type == Types.CHAR || type == Types.VARCHAR
835:                                    || type == Types.LONGVARCHAR
836:                                    || type == Types.BINARY
837:                                    || type == Types.DATE || type == Types.TIME
838:                                    || type == Types.TIMESTAMP) {
839:                                DefaultVal = "\'" + DefaultVal + "\'";
840:                            }
841:
842:                            datatype += " DEFAULT " + DefaultVal;
843:                        }
844:
845:                        if (rsmdata_NoNulls) {
846:                            datatype += " NOT NULL ";
847:                        }
848:
849:                        v.addElement(inttype);
850:
851:                        datatype = helper.fixupColumnDefRead(TTable,
852:                                select_rsmdata, datatype, col, colnum);
853:                        datatype = Dest.helper.fixupColumnDefWrite(TTable,
854:                                select_rsmdata, datatype, col, colnum);
855:                        create += name + " " + datatype + ",";
856:                        insert += "?,";
857:
858:                        colnum++;
859:                    }
860:
861:                    select_rs.close();
862:                    stmt.close();
863:                    col.close();
864:                } catch (SQLException e) {
865:                    throw new DataAccessPointException(e.getMessage());
866:                }
867:
868:                if (primarykeys) {
869:                    create += PrimaryKeysConstraint + ",";
870:                }
871:
872:                TTable.Stmts.sDestCreate = create.substring(0,
873:                        create.length() - 1)
874:                        + ")";
875:                TTable.Stmts.sDestInsert = insert.substring(0,
876:                        insert.length() - 1)
877:                        + ")";
878:
879:                if (importedkeys) {
880:                    TTable.Stmts.bAlter = true;
881:                    TTable.Stmts.sDestAlter = alterCreate;
882:                } else {
883:                    TTable.Stmts.bAlter = false;
884:                }
885:
886:                if (indices) {
887:                    TTable.Stmts.bCreateIndex = true;
888:                    TTable.Stmts.bDropIndex = true;
889:                    TTable.Stmts.sDestCreateIndex = CreateIndex;
890:                    TTable.Stmts.sDestDropIndex = DropIndex;
891:                } else {
892:                    TTable.Stmts.bCreateIndex = false;
893:                    TTable.Stmts.bDropIndex = false;
894:                }
895:
896:                //iColumnType = new int[v.size()];
897:                //for (int j = 0; j < v.size(); j++) {
898:                //    iColumnType[j] = ((Integer) v.elementAt(j)).intValue();
899:                //}
900:            }
901:
902:            void close() throws DataAccessPointException {
903:
904:                if (srcStatement != null) {
905:                    try {
906:                        srcStatement.close();
907:                    } catch (SQLException e) {
908:                    }
909:
910:                    srcStatement = null;
911:                }
912:
913:                if (conn != null) {
914:                    try {
915:                        conn.close();
916:                    } catch (SQLException e) {
917:                    }
918:
919:                    conn = null;
920:                }
921:            }
922:
923:            /**
924:             * Method declaration
925:             *
926:             *
927:             * @param type
928:             * @param r
929:             * @param p
930:             *
931:             * @throws SQLException
932:             */
933:            private void transferRow(TransferResultSet r, PreparedStatement p,
934:                    int len, int[] types) throws DataAccessPointException,
935:                    SQLException {
936:
937:                for (int i = 1; i <= len; i++) {
938:                    int t = types[i];
939:                    Object o = r.getObject(i);
940:
941:                    if (o == null) {
942:                        if (p != null) {
943:                            p.setNull(i, t);
944:                        }
945:                    } else {
946:                        o = helper.convertColumnValue(o, i, t);
947:
948:                        p.setObject(i, o);
949:                    }
950:                }
951:
952:                if (p != null) {
953:                    p.execute();
954:                }
955:            }
956:
957:            /**
958:             * @return Returns the meta.
959:             */
960:            public DatabaseMetaData getMeta() {
961:                return meta;
962:            }
963:
964:            /**
965:             * @return Returns the conn.
966:             */
967:            public Connection getConn() {
968:                return conn;
969:            }
970:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.