Source Code Cross Referenced for DialectLiveTestRunner.java in  » Database-Client » squirrel-sql-2.6.5a » net » sourceforge » squirrel_sql » fw » dialects » 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 Client » squirrel sql 2.6.5a » net.sourceforge.squirrel_sql.fw.dialects 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.sourceforge.squirrel_sql.fw.dialects;
002:
003:        /*
004:         * Copyright (C) 2007 Rob Manning
005:         * manningr@users.sourceforge.net
006:         *
007:         * This library is free software; you can redistribute it and/or
008:         * modify it under the terms of the GNU Lesser General Public
009:         * License as published by the Free Software Foundation; either
010:         * version 2.1 of the License, or (at your option) any later version.
011:         *
012:         * This library is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015:         * Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public
018:         * License along with this library; if not, write to the Free Software
019:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020:         */
021:        import java.sql.Connection;
022:        import java.sql.DatabaseMetaData;
023:        import java.sql.SQLException;
024:        import java.sql.Statement;
025:        import java.util.ArrayList;
026:        import java.util.Iterator;
027:        import java.util.List;
028:        import java.util.ResourceBundle;
029:        import java.util.StringTokenizer;
030:
031:        import net.sourceforge.squirrel_sql.client.ApplicationArguments;
032:        import net.sourceforge.squirrel_sql.client.session.ISession;
033:        import net.sourceforge.squirrel_sql.client.session.MockSession;
034:        import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
035:        import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
036:        import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
037:        import net.sourceforge.squirrel_sql.fw.sql.TableInfo;
038:
039:        /**
040:         * The purpose of this class is to hookup to the database(s) specified in 
041:         * dialectLiveTest.properties and test SQL generation parts of the dialect 
042:         * syntatically using the database' native parser.  This is not a JUnit test, 
043:         * as it requires a running database to complete.
044:         * 
045:         * @author manningr
046:         */
047:        public class DialectLiveTestRunner {
048:
049:            ArrayList<ISession> sessions = new ArrayList<ISession>();
050:            ResourceBundle bundle = null;
051:
052:            TableColumnInfo firstCol = null;
053:            TableColumnInfo secondCol = null;
054:            TableColumnInfo thirdCol = null;
055:            TableColumnInfo fourthCol = null;
056:            TableColumnInfo dropCol = null;
057:            TableColumnInfo noDefaultValueVarcharCol = null;
058:            TableColumnInfo noDefaultValueIntegerCol = null;
059:            TableColumnInfo renameCol = null;
060:            TableColumnInfo pkCol = null;
061:            // This column is created in the create script abd unused unless testing DB2 
062:            TableColumnInfo db2pkCol = null;
063:            TableColumnInfo notNullIntegerCol = null;
064:
065:            // two columns to represent a Primary key in the pktest table
066:            TableColumnInfo doubleColumnPKOne = null;
067:            TableColumnInfo doubleColumnPKTwo = null;
068:
069:            private static final String DB2_PK_COLNAME = "db2pkCol";
070:
071:            public DialectLiveTestRunner() throws Exception {
072:                ApplicationArguments.initialize(new String[] {});
073:                bundle = ResourceBundle
074:                        .getBundle("net.sourceforge.squirrel_sql.fw.dialects.dialectLiveTest");
075:                initSessions();
076:            }
077:
078:            private void initSessions() throws Exception {
079:                String dbsToTest = bundle.getString("dbsToTest");
080:                StringTokenizer st = new StringTokenizer(dbsToTest, ",");
081:                ArrayList<String> dbs = new ArrayList<String>();
082:                while (st.hasMoreTokens()) {
083:                    String db = st.nextToken().trim();
084:                    dbs.add(db);
085:                }
086:                for (Iterator<String> iter = dbs.iterator(); iter.hasNext();) {
087:                    String db = iter.next();
088:                    String url = bundle.getString(db + "_jdbcUrl");
089:                    String user = bundle.getString(db + "_jdbcUser");
090:                    String pass = bundle.getString(db + "_jdbcPass");
091:                    String driver = bundle.getString(db + "_jdbcDriver");
092:                    String catalog = bundle.getString(db + "_catalog");
093:                    String schema = bundle.getString(db + "_schema");
094:                    MockSession session = new MockSession(driver, url, user,
095:                            pass);
096:                    session.setDefaultCatalog(catalog);
097:                    session.setDefaultSchema(schema);
098:                    sessions.add(session);
099:                }
100:            }
101:
102:            private void init(ISession session) throws Exception {
103:                createTestTables(session);
104:                firstCol = getIntegerColumn("nullint", fixTableName(session,
105:                        "test1"), true, "0", "An int comment");
106:                secondCol = getIntegerColumn("notnullint", fixTableName(
107:                        session, "test2"), false, "0", "An int comment");
108:                thirdCol = getVarcharColumn("nullvc", fixTableName(session,
109:                        "test3"), true, "defVal", "A varchar comment");
110:                fourthCol = getVarcharColumn("notnullvc", fixTableName(session,
111:                        "test4"), false, "defVal", "A varchar comment");
112:                noDefaultValueVarcharCol = getVarcharColumn(
113:                        "noDefaultVarcharCol", fixTableName(session, "test"),
114:                        true, null, "A varchar column with no default value");
115:                dropCol = getVarcharColumn("dropCol", fixTableName(session,
116:                        "test5"), true, null, "A varchar comment");
117:                noDefaultValueIntegerCol = getIntegerColumn(
118:                        "noDefaultIntgerCol", fixTableName(session, "test5"),
119:                        true, null, "An integer column with no default value");
120:                renameCol = getVarcharColumn("renameCol", fixTableName(session,
121:                        "test"), true, null, "A column to be renamed");
122:                pkCol = getIntegerColumn("pkCol",
123:                        fixTableName(session, "test"), false, "0",
124:                        "primary key column");
125:                notNullIntegerCol = getIntegerColumn("notNullIntegerCol",
126:                        fixTableName(session, "test5"), false, "0",
127:                        "potential pk column");
128:                db2pkCol = getIntegerColumn(DB2_PK_COLNAME, fixTableName(
129:                        session, "test"), false, "0",
130:                        "A DB2 Primary Key column");
131:
132:                // These two columns will be the only ones in the pktest table.  They will 
133:                // start out being nullable, and we will test that the dialect correctly
134:                // converts them to non-null then applies the PK constraint to them.
135:                // This test shall not be run against any database dialect that claims not
136:                // to support changing the nullability of a column.
137:                doubleColumnPKOne = getIntegerColumn("pk_col_1", fixTableName(
138:                        session, "pktest"), true, null,
139:                        "an initially nullable field to be made part of a PK");
140:                doubleColumnPKTwo = getIntegerColumn("pk_col_2", fixTableName(
141:                        session, "pktest"), true, null,
142:                        "an initially nullable field to be made part of a PK");
143:            }
144:
145:            private ITableInfo getTableInfo(ISession session, String tableName)
146:                    throws Exception {
147:                SQLDatabaseMetaData md = session.getSQLConnection()
148:                        .getSQLMetaData();
149:                String catalog = ((MockSession) session).getDefaultCatalog();
150:                String schema = ((MockSession) session).getDefaultSchema();
151:                if (md.storesUpperCaseIdentifiers()) {
152:                    tableName = tableName.toUpperCase();
153:                } else {
154:                    tableName = tableName.toLowerCase();
155:                }
156:                //System.out.println("Looking for table with catalog="+catalog+" schema="+schema+" tableName="+tableName);
157:                ITableInfo[] infos = md.getTables(catalog, schema, tableName,
158:                        new String[] { "TABLE" }, null);
159:                if (infos.length > 1) {
160:                    throw new IllegalStateException(
161:                            "Found more than one table matching name="
162:                                    + tableName);
163:
164:                }
165:                if (infos.length == 0) {
166:                    return null;
167:                }
168:
169:                return infos[0];
170:
171:            }
172:
173:            private void dropTable(ISession session, String tableName)
174:                    throws Exception {
175:                HibernateDialect dialect = DialectFactory.getDialect(
176:                        DialectFactory.DEST_TYPE, session.getApplication()
177:                                .getMainFrame(), session.getMetaData());
178:                try {
179:                    ITableInfo ti = getTableInfo(session, tableName);
180:                    if (ti == null) {
181:                        System.out.println("Table " + tableName
182:                                + " couldn't be dropped - doesn't exist");
183:                        return;
184:                    }
185:                    runSQL(session, dialect.getTableDropSQL(ti, true, false));
186:                } catch (SQLException e) {
187:                    // Do Nothing
188:                }
189:            }
190:
191:            /**
192:             * Setup the test tables.  This used to be only one table but it grew due
193:             * primarily to Ingres' inability to have a table with more than 5-10 
194:             * columns.
195:             * 
196:             * @param session
197:             * @throws Exception
198:             */
199:            private void createTestTables(ISession session) throws Exception {
200:                HibernateDialect dialect = DialectFactory.getDialect(
201:                        DialectFactory.DEST_TYPE, session.getApplication()
202:                                .getMainFrame(), session.getMetaData());
203:
204:                dropTable(session, fixTableName(session, "test"));
205:                dropTable(session, fixTableName(session, "test1"));
206:                dropTable(session, fixTableName(session, "test2"));
207:                dropTable(session, fixTableName(session, "test3"));
208:                dropTable(session, fixTableName(session, "test4"));
209:                dropTable(session, fixTableName(session, "test5"));
210:                dropTable(session, fixTableName(session, "pktest"));
211:                if (DialectFactory.isOracle(session.getMetaData())) {
212:                    dropTable(session, fixTableName(session, "matview"));
213:                }
214:
215:                String pageSizeClause = "";
216:
217:                if (DialectFactory.isIngres(session.getMetaData())) {
218:                    // alterations fail for some reason unless you do this...
219:                    pageSizeClause = " with page_size=4096";
220:                }
221:
222:                if (DialectFactory.isDB2(session.getMetaData())) {
223:                    // db2pkCol is used to create a PK when using DB2.  DB2 doesn't allow
224:                    // you to add a PK to a table after it has been constructed unless the
225:                    // column(s) that comprise the PK were originally there when created
226:                    // *and* created not null.
227:                    runSQL(session, "create table "
228:                            + fixTableName(session, "test")
229:                            + " ( mychar char(10), " + DB2_PK_COLNAME
230:                            + " integer not null)");
231:                } else {
232:                    runSQL(session, "create table "
233:                            + fixTableName(session, "test")
234:                            + " ( mychar char(10))" + pageSizeClause);
235:                }
236:
237:                runSQL(session, "create table "
238:                        + fixTableName(session, "test1")
239:                        + " ( mychar char(10))" + pageSizeClause);
240:                runSQL(session, "create table "
241:                        + fixTableName(session, "test2")
242:                        + " ( mychar char(10))" + pageSizeClause);
243:                runSQL(session, "create table "
244:                        + fixTableName(session, "test3")
245:                        + " ( mychar char(10))" + pageSizeClause);
246:                runSQL(session, "create table "
247:                        + fixTableName(session, "test4")
248:                        + " ( mychar char(10))" + pageSizeClause);
249:                runSQL(session, "create table "
250:                        + fixTableName(session, "test5")
251:                        + " ( mychar char(10))" + pageSizeClause);
252:
253:                if (dialect.supportsAlterColumnNull()) {
254:                    runSQL(session, "create table "
255:                            + fixTableName(session, "pktest")
256:                            + " ( pk_col_1 integer, pk_col_2 integer )"
257:                            + pageSizeClause);
258:                }
259:            }
260:
261:            private void runTests() throws Exception {
262:                for (Iterator<ISession> iter = sessions.iterator(); iter
263:                        .hasNext();) {
264:                    ISession session = iter.next();
265:                    HibernateDialect dialect = DialectFactory.getDialect(
266:                            DialectFactory.DEST_TYPE, session.getApplication()
267:                                    .getMainFrame(), session.getMetaData());
268:
269:                    init(session);
270:                    testAddColumn(session);
271:                    testDropColumn(session);
272:                    testAlterDefaultValue(session);
273:                    testColumnComment(session);
274:                    testAlterNull(session);
275:                    testAlterName(session);
276:                    testAlterColumnlength(session);
277:                    // DB2 cannot alter a column's null attribute directly (only 
278:                    // through constraints).  Not only that, but it's apparently not a
279:                    // valid thing to do to create a primary key using a column that has
280:                    // been made "not null" via a check constraint.  Therefore, the only
281:                    // columns that qualify to be made PKs are those that were declared
282:                    // not null at the time of table creation.
283:                    if (DialectFactory.isDB2(session.getMetaData())) {
284:                        testAddPrimaryKey(session,
285:                                new TableColumnInfo[] { db2pkCol });
286:                        testDropPrimaryKey(session, db2pkCol.getTableName());
287:                    } else {
288:                        try {
289:                            testAddPrimaryKey(session,
290:                                    new TableColumnInfo[] { notNullIntegerCol });
291:                        } catch (UnsupportedOperationException e) {
292:                            System.err
293:                                    .println("doesn't support adding primary keys");
294:                        }
295:                        try {
296:                            testDropPrimaryKey(session, notNullIntegerCol
297:                                    .getTableName());
298:                        } catch (UnsupportedOperationException e) {
299:                            System.err
300:                                    .println("doesn't support dropping primary keys");
301:                        }
302:
303:                        // Test whether or not the dialect correctly converts nullable
304:                        // columns to not-null before applying the primary key - if 
305:                        // necessary
306:                        if (dialect.supportsAlterColumnNull()) {
307:                            try {
308:                                TableColumnInfo[] infos = new TableColumnInfo[] {
309:                                        doubleColumnPKOne, doubleColumnPKTwo };
310:                                testAddPrimaryKey(session, infos);
311:                            } catch (UnsupportedOperationException e) {
312:                                System.err
313:                                        .println("doesn't support adding primary keys");
314:                            }
315:                        }
316:                    }
317:                    testDropMatView(session);
318:                }
319:            }
320:
321:            /**
322:                CREATE MATERIALIZED VIEW matview2
323:                    REFRESH COMPLETE
324:                    NEXT  SYSDATE + 1
325:                    WITH PRIMARY KEY 
326:                    AS SELECT * FROM TEST;
327:
328:             * @param session
329:             * @throws Exception
330:             */
331:            private void testDropMatView(ISession session) throws Exception {
332:                if (!DialectFactory.isOracle(session.getMetaData()))
333:                    return;
334:                HibernateDialect dialect = DialectFactory.getDialect(
335:                        DialectFactory.DEST_TYPE, session.getApplication()
336:                                .getMainFrame(), session.getMetaData());
337:
338:                testAddPrimaryKey(session, new TableColumnInfo[] { pkCol });
339:                String createMatViewSQL = "CREATE MATERIALIZED VIEW MATVIEW "
340:                        + "       REFRESH COMPLETE " + "   NEXT  SYSDATE + 1 "
341:                        + "   WITH PRIMARY KEY " + "   AS SELECT * FROM TEST ";
342:                runSQL(session, createMatViewSQL);
343:                MockSession msession = (MockSession) session;
344:                String cat = msession.getDefaultCatalog();
345:                String schema = msession.getDefaultSchema();
346:                SQLDatabaseMetaData md = session.getSQLConnection()
347:                        .getSQLMetaData();
348:                ITableInfo info = new TableInfo(cat, schema, "MATVIEW",
349:                        "TABLE", "", md);
350:                List<String> dropSQL = dialect.getTableDropSQL(info, true,
351:                        false);
352:                runSQL(session, dropSQL);
353:            }
354:
355:            private void testAlterName(ISession session) throws Exception {
356:                HibernateDialect dialect = DialectFactory.getDialect(
357:                        DialectFactory.DEST_TYPE, session.getApplication()
358:                                .getMainFrame(), session.getMetaData());
359:
360:                TableColumnInfo newNameCol = getVarcharColumn("newNameCol",
361:                        "test", true, null, "A column to be renamed");
362:                if (dialect.supportsRenameColumn()) {
363:                    String sql = dialect.getColumnNameAlterSQL(renameCol,
364:                            newNameCol);
365:                    runSQL(session, sql);
366:                } else {
367:                    try {
368:                        dialect.getColumnNameAlterSQL(renameCol, newNameCol);
369:                    } catch (UnsupportedOperationException e) {
370:                        // this is expected
371:                        System.err.println(e.getMessage());
372:                    }
373:                }
374:            }
375:
376:            private void testDropColumn(ISession session) throws Exception {
377:                HibernateDialect dialect = DialectFactory.getDialect(
378:                        DialectFactory.DEST_TYPE, session.getApplication()
379:                                .getMainFrame(), session.getMetaData());
380:
381:                if (dialect.supportsDropColumn()) {
382:                    dropColumn(session, dropCol);
383:                } else {
384:                    try {
385:                        dropColumn(session, dropCol);
386:                        throw new IllegalStateException(
387:                                "Expected dialect to fail to provide SQL for dropping a column");
388:                    } catch (UnsupportedOperationException e) {
389:                        // This is what we expect
390:                        System.err.println(e.getMessage());
391:                    }
392:                }
393:            }
394:
395:            private void testAlterColumnlength(ISession session)
396:                    throws Exception {
397:                HibernateDialect dialect = DialectFactory.getDialect(
398:                        DialectFactory.DEST_TYPE, session.getApplication()
399:                                .getMainFrame(), session.getMetaData());
400:
401:                //convert nullint into a varchar(100)
402:                /*
403:                 * This won't work on Derby where non-varchar columns cannot be 
404:                 * altered among other restrictions.
405:                 * 
406:                TableColumnInfo nullintVC = 
407:                    getVarcharColumn("nullint", true, "defVal", "A varchar comment");
408:                String alterColTypeSQL = dialect.getColumnTypeAlterSQL(firstCol, nullintVC);
409:                runSQL(session, alterColTypeSQL);
410:                 */
411:                TableColumnInfo thirdColLonger = getVarcharColumn("nullvc",
412:                        "test3", true, "defVal", "A varchar comment", 30);
413:                if (dialect.supportsAlterColumnType()) {
414:                    List<String> alterColLengthSQL = dialect
415:                            .getColumnTypeAlterSQL(thirdCol, thirdColLonger);
416:                    runSQL(session, alterColLengthSQL);
417:                } else {
418:                    try {
419:                        dialect.getColumnTypeAlterSQL(thirdCol, thirdColLonger);
420:                        throw new IllegalStateException(
421:                                "Expected dialect to fail to provide SQL for altering column type");
422:                    } catch (UnsupportedOperationException e) {
423:                        // this is expected
424:                    }
425:                }
426:            }
427:
428:            private void testAlterDefaultValue(ISession session)
429:                    throws Exception {
430:                HibernateDialect dialect = DialectFactory.getDialect(
431:                        DialectFactory.DEST_TYPE, session.getApplication()
432:                                .getMainFrame(), session.getMetaData());
433:
434:                TableColumnInfo varcharColWithDefaultValue = getVarcharColumn(
435:                        "noDefaultVarcharCol", noDefaultValueVarcharCol
436:                                .getTableName(), true, "Default Value",
437:                        "A column with a default value");
438:
439:                TableColumnInfo integerColWithDefaultVal = getIntegerColumn(
440:                        "noDefaultIntgerCol", noDefaultValueIntegerCol
441:                                .getTableName(), true, "0",
442:                        "An integer column with a default value");
443:
444:                if (dialect.supportsAlterColumnDefault()) {
445:                    String defaultValSQL = dialect
446:                            .getColumnDefaultAlterSQL(varcharColWithDefaultValue);
447:                    runSQL(session, defaultValSQL);
448:
449:                    defaultValSQL = dialect
450:                            .getColumnDefaultAlterSQL(integerColWithDefaultVal);
451:                    runSQL(session, defaultValSQL);
452:                } else {
453:                    try {
454:                        dialect
455:                                .getColumnDefaultAlterSQL(noDefaultValueVarcharCol);
456:                        throw new IllegalStateException(
457:                                "Expected dialect to fail to provide SQL for column default alter");
458:                    } catch (UnsupportedOperationException e) {
459:                        // This is what we expect.
460:                        System.err.println(e.getMessage());
461:                    }
462:                }
463:            }
464:
465:            private void testAlterNull(ISession session) throws Exception {
466:                HibernateDialect dialect = DialectFactory.getDialect(
467:                        DialectFactory.DEST_TYPE, session.getApplication()
468:                                .getMainFrame(), session.getMetaData());
469:                TableColumnInfo notNullThirdCol = getVarcharColumn("nullvc",
470:                        "test3", false, "defVal", "A varchar comment");
471:                if (dialect.supportsAlterColumnNull()) {
472:                    String notNullSQL = dialect
473:                            .getColumnNullableAlterSQL(notNullThirdCol);
474:                    runSQL(session, notNullSQL);
475:                } else {
476:                    try {
477:                        dialect.getColumnNullableAlterSQL(notNullThirdCol);
478:                        throw new IllegalStateException(
479:                                "Expected dialect to fail to provide SQL for column nullable alter");
480:                    } catch (UnsupportedOperationException e) {
481:                        // this is expected
482:                        System.err.println(e.getMessage());
483:                    }
484:                }
485:            }
486:
487:            private void testAddColumn(ISession session) throws Exception {
488:                addColumn(session, firstCol);
489:                addColumn(session, secondCol);
490:                addColumn(session, thirdCol);
491:                addColumn(session, fourthCol);
492:                addColumn(session, dropCol);
493:                addColumn(session, noDefaultValueVarcharCol);
494:                addColumn(session, noDefaultValueIntegerCol);
495:                addColumn(session, renameCol);
496:                addColumn(session, pkCol);
497:                addColumn(session, notNullIntegerCol);
498:            }
499:
500:            private void addColumn(ISession session, TableColumnInfo info)
501:                    throws Exception {
502:                HibernateDialect dialect = DialectFactory.getDialect(
503:                        DialectFactory.DEST_TYPE, session.getApplication()
504:                                .getMainFrame(), session.getMetaData());
505:
506:                String[] sqls = dialect.getColumnAddSQL(info);
507:                for (int i = 0; i < sqls.length; i++) {
508:                    String sql = sqls[i];
509:                    runSQL(session, sql);
510:                }
511:
512:            }
513:
514:            private void testColumnComment(ISession session) throws Exception {
515:                HibernateDialect dialect = getDialect(session);
516:                if (dialect.supportsColumnComment()) {
517:                    alterColumnComment(session, firstCol);
518:                    alterColumnComment(session, secondCol);
519:                    alterColumnComment(session, thirdCol);
520:                    alterColumnComment(session, fourthCol);
521:                } else {
522:                    try {
523:                        alterColumnComment(session, firstCol);
524:                    } catch (UnsupportedOperationException e) {
525:                        // This is expected
526:                        System.err.println(e.getMessage());
527:                    }
528:                }
529:            }
530:
531:            private void alterColumnComment(ISession session,
532:                    TableColumnInfo info) throws Exception {
533:                HibernateDialect dialect = DialectFactory.getDialect(
534:                        DialectFactory.DEST_TYPE, session.getApplication()
535:                                .getMainFrame(), session.getMetaData());
536:                String commentSQL = dialect.getColumnCommentAlterSQL(info);
537:                if (commentSQL != null && !commentSQL.equals("")) {
538:                    runSQL(session, commentSQL);
539:                }
540:            }
541:
542:            private String getPKName(String tableName) {
543:                return tableName.toUpperCase() + "_PK";
544:            }
545:
546:            private void testDropPrimaryKey(ISession session, String tableName)
547:                    throws Exception {
548:                HibernateDialect dialect = DialectFactory.getDialect(
549:                        DialectFactory.DEST_TYPE, session.getApplication()
550:                                .getMainFrame(), session.getMetaData());
551:                String pkName = getPKName(tableName);
552:                String sql = dialect.getDropPrimaryKeySQL(pkName, tableName);
553:                runSQL(session, sql);
554:            }
555:
556:            private void testAddPrimaryKey(ISession session,
557:                    TableColumnInfo[] colInfos) throws Exception {
558:                HibernateDialect dialect = DialectFactory.getDialect(
559:                        DialectFactory.DEST_TYPE, session.getApplication()
560:                                .getMainFrame(), session.getMetaData());
561:
562:                String tableName = colInfos[0].getTableName();
563:
564:                if (session.getSQLConnection().getSQLMetaData()
565:                        .storesUpperCaseIdentifiers()) {
566:                    tableName = tableName.toUpperCase();
567:                }
568:
569:                SQLDatabaseMetaData md = session.getSQLConnection()
570:                        .getSQLMetaData();
571:                String catalog = ((MockSession) session).getDefaultCatalog();
572:                String schema = ((MockSession) session).getDefaultSchema();
573:
574:                ITableInfo[] infos = null;
575:                try {
576:                    md.getTables(catalog, schema, tableName,
577:                            new String[] { "TABLE" }, null);
578:                } catch (SQLException e) {
579:                    // Do nothing
580:                }
581:
582:                ITableInfo ti = null;
583:                if (infos != null && infos.length > 0) {
584:                    ti = infos[0];
585:                } else {
586:                    // Couldn't locate the table - just try to fake it.
587:                    ti = new TableInfo(catalog, schema, tableName, "TABLE", "",
588:                            md);
589:                }
590:
591:                String[] pkSQLs = dialect.getAddPrimaryKeySQL(
592:                        getPKName(tableName), colInfos, ti);
593:
594:                for (int i = 0; i < pkSQLs.length; i++) {
595:                    String pkSQL = pkSQLs[i];
596:                    runSQL(session, pkSQL);
597:                }
598:            }
599:
600:            private void dropColumn(ISession session, TableColumnInfo info)
601:                    throws Exception {
602:                HibernateDialect dialect = DialectFactory.getDialect(
603:                        DialectFactory.DEST_TYPE, session.getApplication()
604:                                .getMainFrame(), session.getMetaData());
605:
606:                String sql = dialect.getColumnDropSQL(info.getTableName(), info
607:                        .getColumnName());
608:                runSQL(session, sql);
609:            }
610:
611:            private HibernateDialect getDialect(ISession session)
612:                    throws Exception {
613:                return DialectFactory.getDialect(DialectFactory.DEST_TYPE,
614:                        session.getApplication().getMainFrame(), session
615:                                .getMetaData());
616:            }
617:
618:            private void runSQL(ISession session, List<String> sql)
619:                    throws Exception {
620:                for (String stmt : sql) {
621:                    runSQL(session, stmt);
622:                }
623:            }
624:
625:            private void runSQL(ISession session, String sql) throws Exception {
626:                HibernateDialect dialect = getDialect(session);
627:                Connection con = session.getSQLConnection().getConnection();
628:                Statement stmt = con.createStatement();
629:                System.out.println("Running SQL (" + dialect.getDisplayName()
630:                        + "): " + sql);
631:                stmt.execute(sql);
632:            }
633:
634:            private TableColumnInfo getIntegerColumn(String name,
635:                    String tableName, boolean nullable, String defaultVal,
636:                    String comment) {
637:                return getColumn(java.sql.Types.INTEGER, "INTEGER", name,
638:                        tableName, nullable, defaultVal, comment, 10, 0);
639:            }
640:
641:            private TableColumnInfo getVarcharColumn(String name,
642:                    String tableName, boolean nullable, String defaultVal,
643:                    String comment, int size) {
644:                return getColumn(java.sql.Types.VARCHAR, "VARCHAR", name,
645:                        tableName, nullable, defaultVal, comment, size, 0);
646:            }
647:
648:            private TableColumnInfo getVarcharColumn(String name,
649:                    String tableName, boolean nullable, String defaultVal,
650:                    String comment) {
651:                return getColumn(java.sql.Types.VARCHAR, "VARCHAR", name,
652:                        tableName, nullable, defaultVal, comment, 20, 0);
653:            }
654:
655:            private TableColumnInfo getColumn(int dataType,
656:                    String dataTypeName, String name, String tableName,
657:                    boolean nullable, String defaultVal, String comment,
658:                    int columnSize, int scale) {
659:                String isNullable = "YES";
660:                int isNullAllowed = DatabaseMetaData.columnNullable;
661:                if (!nullable) {
662:                    isNullable = "NO";
663:                    isNullAllowed = DatabaseMetaData.columnNoNulls;
664:                }
665:                TableColumnInfo result = new TableColumnInfo("testCatalog", // catalog 
666:                        "testSchema", // schema
667:                        tableName, // tableName
668:                        name, // columnName
669:                        dataType, // dataType
670:                        dataTypeName, // typeName 
671:                        columnSize, // columnSize
672:                        scale, // decimalDigits
673:                        10, // radix
674:                        isNullAllowed, // isNullAllowed
675:                        comment, // remarks
676:                        defaultVal, // defaultValue
677:                        0, // octet length
678:                        0, // ordinal position
679:                        isNullable); // isNullable 
680:                return result;
681:            }
682:
683:            private String fixTableName(ISession session, String table)
684:                    throws Exception {
685:                String result = null;
686:                SQLDatabaseMetaData md = session.getSQLConnection()
687:                        .getSQLMetaData();
688:                if (md.storesUpperCaseIdentifiers()) {
689:                    result = table.toUpperCase();
690:                } else {
691:                    result = table.toLowerCase();
692:                }
693:                return result;
694:            }
695:
696:            /**
697:             * @param args
698:             */
699:            public static void main(String[] args) throws Exception {
700:                DialectLiveTestRunner runner = new DialectLiveTestRunner();
701:                runner.runTests();
702:            }
703:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.