Source Code Cross Referenced for H2Dialect.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:        /*
002:         * Copyright 2004, 2005, 2006 H2 Group.
003:         */
004:        package net.sourceforge.squirrel_sql.fw.dialects;
005:
006:        import java.sql.SQLException;
007:        import java.sql.Types;
008:        import java.util.ArrayList;
009:        import java.util.List;
010:
011:        import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
012:        import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
013:        import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
014:        import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
015:        import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
016:
017:        import org.hibernate.Hibernate;
018:        import org.hibernate.cfg.Environment;
019:        import org.hibernate.dialect.Dialect;
020:        import org.hibernate.dialect.function.NoArgSQLFunction;
021:        import org.hibernate.dialect.function.StandardSQLFunction;
022:        import org.hibernate.dialect.function.VarArgsSQLFunction;
023:        import org.hibernate.exception.TemplatedViolatedConstraintNameExtracter;
024:        import org.hibernate.exception.ViolatedConstraintNameExtracter;
025:
026:        public class H2Dialect extends Dialect implements  HibernateDialect {
027:
028:            static final String DEFAULT_BATCH_SIZE = "15";
029:
030:            public H2Dialect() {
031:                super ();
032:                registerColumnType(Types.ARRAY, "array");
033:                registerColumnType(Types.BIGINT, "bigint");
034:                registerColumnType(Types.BINARY, "binary");
035:                registerColumnType(Types.BIT, "boolean");
036:                registerColumnType(Types.BOOLEAN, "boolean");
037:                registerColumnType(Types.BLOB, "blob");
038:                registerColumnType(Types.CHAR, "varchar($l)");
039:                registerColumnType(Types.CLOB, "clob");
040:                registerColumnType(Types.DATE, "date");
041:                registerColumnType(Types.DECIMAL, "decimal($p,$s)");
042:                registerColumnType(Types.DOUBLE, "double");
043:                registerColumnType(Types.FLOAT, "float");
044:                registerColumnType(Types.INTEGER, "integer");
045:                registerColumnType(Types.LONGVARBINARY, "longvarbinary");
046:                registerColumnType(Types.LONGVARCHAR, "longvarchar");
047:                registerColumnType(Types.NUMERIC, "numeric");
048:                registerColumnType(Types.REAL, "real");
049:                registerColumnType(Types.SMALLINT, "smallint");
050:                registerColumnType(Types.TIME, "time");
051:                registerColumnType(Types.TIMESTAMP, "timestamp");
052:                registerColumnType(Types.TINYINT, "tinyint");
053:                registerColumnType(Types.VARBINARY, "binary($l)");
054:                registerColumnType(Types.VARCHAR, "varchar($l)");
055:
056:                // select topic, syntax from information_schema.help
057:                // where section like 'Function%' order by section, topic
058:
059:                //        registerFunction("abs", new StandardSQLFunction("abs"));
060:                registerFunction("acos", new StandardSQLFunction("acos",
061:                        Hibernate.DOUBLE));
062:                registerFunction("asin", new StandardSQLFunction("asin",
063:                        Hibernate.DOUBLE));
064:                registerFunction("atan", new StandardSQLFunction("atan",
065:                        Hibernate.DOUBLE));
066:                registerFunction("atan2", new StandardSQLFunction("atan2",
067:                        Hibernate.DOUBLE));
068:                registerFunction("bitand", new StandardSQLFunction("bitand",
069:                        Hibernate.INTEGER));
070:                registerFunction("bitor", new StandardSQLFunction("bitor",
071:                        Hibernate.INTEGER));
072:                registerFunction("bitxor", new StandardSQLFunction("bitxor",
073:                        Hibernate.INTEGER));
074:                registerFunction("ceiling", new StandardSQLFunction("ceiling",
075:                        Hibernate.DOUBLE));
076:                registerFunction("cos", new StandardSQLFunction("cos",
077:                        Hibernate.DOUBLE));
078:                registerFunction("cot", new StandardSQLFunction("cot",
079:                        Hibernate.DOUBLE));
080:                registerFunction("degrees", new StandardSQLFunction("degrees",
081:                        Hibernate.DOUBLE));
082:                registerFunction("exp", new StandardSQLFunction("exp",
083:                        Hibernate.DOUBLE));
084:                registerFunction("floor", new StandardSQLFunction("floor",
085:                        Hibernate.DOUBLE));
086:                registerFunction("log", new StandardSQLFunction("log",
087:                        Hibernate.DOUBLE));
088:                registerFunction("log10", new StandardSQLFunction("log10",
089:                        Hibernate.DOUBLE));
090:                //        registerFunction("mod", new StandardSQLFunction("mod", Hibernate.INTEGER));
091:                registerFunction("pi", new NoArgSQLFunction("pi",
092:                        Hibernate.DOUBLE));
093:                registerFunction("power", new StandardSQLFunction("power",
094:                        Hibernate.DOUBLE));
095:                registerFunction("radians", new StandardSQLFunction("radians",
096:                        Hibernate.DOUBLE));
097:                registerFunction("rand", new NoArgSQLFunction("rand",
098:                        Hibernate.DOUBLE));
099:                registerFunction("round", new StandardSQLFunction("round",
100:                        Hibernate.DOUBLE));
101:                registerFunction("roundmagic", new StandardSQLFunction(
102:                        "roundmagic", Hibernate.DOUBLE));
103:                registerFunction("sign", new StandardSQLFunction("sign",
104:                        Hibernate.INTEGER));
105:                registerFunction("sin", new StandardSQLFunction("sin",
106:                        Hibernate.DOUBLE));
107:                //        registerFunction("sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE));
108:                registerFunction("tan", new StandardSQLFunction("tan",
109:                        Hibernate.DOUBLE));
110:                registerFunction("truncate", new StandardSQLFunction(
111:                        "truncate", Hibernate.DOUBLE));
112:
113:                registerFunction("compress", new StandardSQLFunction(
114:                        "compress", Hibernate.BINARY));
115:                registerFunction("expand", new StandardSQLFunction("compress",
116:                        Hibernate.BINARY));
117:                registerFunction("decrypt", new StandardSQLFunction("decrypt",
118:                        Hibernate.BINARY));
119:                registerFunction("encrypt", new StandardSQLFunction("encrypt",
120:                        Hibernate.BINARY));
121:                registerFunction("hash", new StandardSQLFunction("hash",
122:                        Hibernate.BINARY));
123:
124:                registerFunction("ascii", new StandardSQLFunction("ascii",
125:                        Hibernate.INTEGER));
126:                //        registerFunction("bit_length", new StandardSQLFunction("bit_length", Hibernate.INTEGER));
127:                registerFunction("char", new StandardSQLFunction("char",
128:                        Hibernate.CHARACTER));
129:                registerFunction("concat", new VarArgsSQLFunction(
130:                        Hibernate.STRING, "(", "||", ")"));
131:                registerFunction("difference", new StandardSQLFunction(
132:                        "difference", Hibernate.INTEGER));
133:                registerFunction("hextoraw", new StandardSQLFunction(
134:                        "hextoraw", Hibernate.STRING));
135:                registerFunction("lower", new StandardSQLFunction("lower",
136:                        Hibernate.STRING));
137:                registerFunction("insert", new StandardSQLFunction("lower",
138:                        Hibernate.STRING));
139:                registerFunction("left", new StandardSQLFunction("left",
140:                        Hibernate.STRING));
141:                //        registerFunction("length", new StandardSQLFunction("length", Hibernate.INTEGER));
142:                //        registerFunction("locate", new StandardSQLFunction("locate", Hibernate.INTEGER));
143:                //        registerFunction("lower", new StandardSQLFunction("lower", Hibernate.STRING));
144:                registerFunction("lcase", new StandardSQLFunction("lcase",
145:                        Hibernate.STRING));
146:                registerFunction("ltrim", new StandardSQLFunction("ltrim",
147:                        Hibernate.STRING));
148:                registerFunction("octet_length", new StandardSQLFunction(
149:                        "octet_length", Hibernate.INTEGER));
150:                registerFunction("position", new StandardSQLFunction(
151:                        "position", Hibernate.INTEGER));
152:                registerFunction("rawtohex", new StandardSQLFunction(
153:                        "rawtohex", Hibernate.STRING));
154:                registerFunction("repeat", new StandardSQLFunction("repeat",
155:                        Hibernate.STRING));
156:                registerFunction("replace", new StandardSQLFunction("replace",
157:                        Hibernate.STRING));
158:                registerFunction("right", new StandardSQLFunction("right",
159:                        Hibernate.STRING));
160:                registerFunction("rtrim", new StandardSQLFunction("rtrim",
161:                        Hibernate.STRING));
162:                registerFunction("soundex", new StandardSQLFunction("soundex",
163:                        Hibernate.STRING));
164:                registerFunction("space", new StandardSQLFunction("space",
165:                        Hibernate.STRING));
166:                registerFunction("stringencode", new StandardSQLFunction(
167:                        "stringencode", Hibernate.STRING));
168:                registerFunction("stringdecode", new StandardSQLFunction(
169:                        "stringdecode", Hibernate.STRING));
170:                //        registerFunction("substring", new StandardSQLFunction("substring", Hibernate.STRING));
171:                //        registerFunction("upper", new StandardSQLFunction("upper", Hibernate.STRING));
172:                registerFunction("ucase", new StandardSQLFunction("ucase",
173:                        Hibernate.STRING));
174:
175:                registerFunction("stringtoutf8", new StandardSQLFunction(
176:                        "stringtoutf8", Hibernate.BINARY));
177:                registerFunction("utf8tostring", new StandardSQLFunction(
178:                        "utf8tostring", Hibernate.STRING));
179:
180:                registerFunction("current_date", new NoArgSQLFunction(
181:                        "current_date", Hibernate.DATE));
182:                registerFunction("current_time", new NoArgSQLFunction(
183:                        "current_time", Hibernate.TIME));
184:                registerFunction("current_timestamp", new NoArgSQLFunction(
185:                        "current_timestamp", Hibernate.TIMESTAMP));
186:                registerFunction("datediff", new NoArgSQLFunction("datediff",
187:                        Hibernate.INTEGER));
188:                registerFunction("dayname", new StandardSQLFunction("dayname",
189:                        Hibernate.STRING));
190:                registerFunction("dayofmonth", new StandardSQLFunction(
191:                        "dayofmonth", Hibernate.INTEGER));
192:                registerFunction("dayofweek", new StandardSQLFunction(
193:                        "dayofweek", Hibernate.INTEGER));
194:                registerFunction("dayofyear", new StandardSQLFunction(
195:                        "dayofyear", Hibernate.INTEGER));
196:                //        registerFunction("hour", new StandardSQLFunction("hour", Hibernate.INTEGER));
197:                //        registerFunction("minute", new StandardSQLFunction("minute", Hibernate.INTEGER));
198:                //        registerFunction("month", new StandardSQLFunction("month", Hibernate.INTEGER));
199:                registerFunction("monthname", new StandardSQLFunction(
200:                        "monthname", Hibernate.STRING));
201:                registerFunction("quater", new StandardSQLFunction("quater",
202:                        Hibernate.INTEGER));
203:                //        registerFunction("second", new StandardSQLFunction("second", Hibernate.INTEGER));
204:                registerFunction("week", new StandardSQLFunction("week",
205:                        Hibernate.INTEGER));
206:                //        registerFunction("year", new StandardSQLFunction("year", Hibernate.INTEGER));
207:
208:                registerFunction("curdate", new NoArgSQLFunction("curdate",
209:                        Hibernate.DATE));
210:                registerFunction("curtime", new NoArgSQLFunction("curtime",
211:                        Hibernate.TIME));
212:                registerFunction("curtimestamp", new NoArgSQLFunction(
213:                        "curtimestamp", Hibernate.TIME));
214:                registerFunction("now", new NoArgSQLFunction("now",
215:                        Hibernate.TIMESTAMP));
216:
217:                registerFunction("database", new NoArgSQLFunction("database",
218:                        Hibernate.STRING));
219:                registerFunction("user", new NoArgSQLFunction("user",
220:                        Hibernate.STRING));
221:
222:                getDefaultProperties().setProperty(
223:                        Environment.STATEMENT_BATCH_SIZE, DEFAULT_BATCH_SIZE);
224:
225:            }
226:
227:            public String getAddColumnString() {
228:                return "add column";
229:            }
230:
231:            public boolean supportsIdentityColumns() {
232:                return true;
233:            }
234:
235:            public String getIdentityColumnString() {
236:                return "generated by default as identity"; // not null is implicit
237:            }
238:
239:            public String getIdentitySelectString() {
240:                return "call identity()";
241:            }
242:
243:            public String getIdentityInsertString() {
244:                return "null";
245:            }
246:
247:            public String getForUpdateString() {
248:                return " for update";
249:            }
250:
251:            public boolean supportsUnique() {
252:                return true;
253:            }
254:
255:            public boolean supportsLimit() {
256:                return true;
257:            }
258:
259:            public String getLimitString(String sql, boolean hasOffset) {
260:                return new StringBuffer(sql.length() + 20).append(sql).append(
261:                        hasOffset ? " limit ? offset ?" : " limit ?")
262:                        .toString();
263:            }
264:
265:            public boolean bindLimitParametersInReverseOrder() {
266:                return true;
267:            }
268:
269:            public boolean bindLimitParametersFirst() {
270:                return false;
271:            }
272:
273:            public boolean supportsIfExistsAfterTableName() {
274:                return true;
275:            }
276:
277:            public String[] getCreateSequenceStrings(String sequenceName) {
278:                return new String[] { "create sequence " + sequenceName };
279:            }
280:
281:            public String[] getDropSequenceStrings(String sequenceName) {
282:                return new String[] { "drop sequence " + sequenceName };
283:            }
284:
285:            public String getSelectSequenceNextValString(String sequenceName) {
286:                return "next value for " + sequenceName;
287:            }
288:
289:            public String getSequenceNextValString(String sequenceName) {
290:                return "call next value for " + sequenceName;
291:            }
292:
293:            public String getQuerySequencesString() {
294:                return "select name from information_schema.sequences";
295:            }
296:
297:            public boolean supportsSequences() {
298:                return true;
299:            }
300:
301:            public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
302:                return EXTRACTER;
303:            }
304:
305:            private static ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
306:
307:                /**
308:                 * Extract the name of the violated constraint from the given SQLException.
309:                 *
310:                 * @param sqle The exception that was the result of the constraint violation.
311:                 * @return The extracted constraint name.
312:                 */
313:                public String extractConstraintName(SQLException sqle) {
314:                    String constraintName = null;
315:                    // 23000: Check constraint violation: {0}
316:                    // 23001: Unique index or primary key violation: {0}
317:                    if (sqle.getSQLState().startsWith("23")) {
318:                        String message = sqle.getMessage();
319:                        int idx = message.indexOf("violation: ");
320:                        if (idx > 0) {
321:                            constraintName = message.substring(idx
322:                                    + "violation: ".length());
323:                        }
324:                    }
325:                    return constraintName;
326:                }
327:
328:            };
329:
330:            public boolean supportsTemporaryTables() {
331:                return true;
332:            }
333:
334:            public String getCreateTemporaryTableString() {
335:                return "create temporary table if not exists";
336:            }
337:
338:            public boolean supportsCurrentTimestampSelection() {
339:                return true;
340:            }
341:
342:            public boolean isCurrentTimestampSelectStringCallable() {
343:                return false;
344:            }
345:
346:            public String getCurrentTimestampSelectString() {
347:                return "call current_timestamp()";
348:            }
349:
350:            public boolean supportsUnionAll() {
351:                return true;
352:            }
353:
354:            /* (non-Javadoc)
355:             * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#canPasteTo(net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo)
356:             */
357:            public boolean canPasteTo(IDatabaseObjectInfo info) {
358:                boolean result = true;
359:                DatabaseObjectType type = info.getDatabaseObjectType();
360:                if (type.getName().equalsIgnoreCase("database")) {
361:                    result = false;
362:                }
363:                return result;
364:            }
365:
366:            /* (non-Javadoc)
367:             * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getLengthFunction(int)
368:             */
369:            public String getLengthFunction(int dataType) {
370:                return "length";
371:            }
372:
373:            /* (non-Javadoc)
374:             * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getMaxFunction()
375:             */
376:            public String getMaxFunction() {
377:                return "max";
378:            }
379:
380:            /* (non-Javadoc)
381:             * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getMaxPrecision(int)
382:             */
383:            public int getMaxPrecision(int dataType) {
384:                return Integer.MAX_VALUE;
385:            }
386:
387:            /* (non-Javadoc)
388:             * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getMaxScale(int)
389:             */
390:            public int getMaxScale(int dataType) {
391:                return getMaxPrecision(dataType);
392:            }
393:
394:            /* (non-Javadoc)
395:             * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getPrecisionDigits(int, int)
396:             */
397:            public int getPrecisionDigits(int columnSize, int dataType) {
398:                return columnSize;
399:            }
400:
401:            /* (non-Javadoc)
402:             * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#supportsSchemasInTableDefinition()
403:             */
404:            public boolean supportsSchemasInTableDefinition() {
405:                return true;
406:            }
407:
408:            /* (non-Javadoc)
409:             * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getColumnLength(int, int)
410:             */
411:            public int getColumnLength(int columnSize, int dataType) {
412:                return columnSize;
413:            }
414:
415:            /**
416:             * The string which identifies this dialect in the dialect chooser.
417:             * 
418:             * @return a descriptive name that tells the user what database this dialect
419:             *         is design to work with.
420:             */
421:            public String getDisplayName() {
422:                return "H2";
423:            }
424:
425:            /**
426:             * Returns boolean value indicating whether or not this dialect supports the
427:             * specified database product/version.
428:             * 
429:             * @param databaseProductName the name of the database as reported by 
430:             * 							  DatabaseMetaData.getDatabaseProductName()
431:             * @param databaseProductVersion the version of the database as reported by
432:             *                              DatabaseMetaData.getDatabaseProductVersion()
433:             * @return true if this dialect can be used for the specified product name
434:             *              and version; false otherwise.
435:             */
436:            public boolean supportsProduct(String databaseProductName,
437:                    String databaseProductVersion) {
438:                if (databaseProductName == null) {
439:                    return false;
440:                }
441:                if (databaseProductName.trim().startsWith("H2")) {
442:                    // We don't yet have the need to discriminate by version.
443:                    return true;
444:                }
445:                return false;
446:            }
447:
448:            /**
449:             * Returns the SQL statement to use to add a column to the specified table
450:             * using the information about the new column specified by info.
451:             * @param info information about the new column such as type, name, etc.
452:             * 
453:             * @return
454:             * @throws UnsupportedOperationException if the database doesn't support 
455:             *         adding columns after a table has already been created.
456:             */
457:            public String[] getColumnAddSQL(TableColumnInfo info)
458:                    throws UnsupportedOperationException {
459:                ArrayList<String> result = new ArrayList<String>();
460:                result.add(DialectUtils.getColumnAddSQL(info, this , true, true,
461:                        true));
462:                if (info.getRemarks() != null && !"".equals(info.getRemarks())) {
463:                    result.add(getColumnCommentAlterSQL(info));
464:                }
465:                return result.toArray(new String[result.size()]);
466:            }
467:
468:            /**
469:             * Returns a boolean value indicating whether or not this dialect supports
470:             * adding comments to columns.
471:             * 
472:             * @return true if column comments are supported; false otherwise.
473:             */
474:            public boolean supportsColumnComment() {
475:                return true;
476:            }
477:
478:            /**
479:             * Returns a boolean value indicating whether or not this database dialect
480:             * supports dropping columns from tables.
481:             * 
482:             * @return true if the database supports dropping columns; false otherwise.
483:             */
484:            public boolean supportsDropColumn() {
485:                return true;
486:            }
487:
488:            /**
489:             * Returns the SQL that forms the command to drop the specified colum in the
490:             * specified table.
491:             * 
492:             * @param tableName the name of the table that has the column
493:             * @param columnName the name of the column to drop.
494:             * @return
495:             * @throws UnsupportedOperationException if the database doesn't support 
496:             *         dropping columns. 
497:             */
498:            public String getColumnDropSQL(String tableName, String columnName) {
499:                return DialectUtils.getColumnDropSQL(tableName, columnName,
500:                        "DROP COLUMN", false, null);
501:            }
502:
503:            /**
504:             * Returns the SQL that forms the command to drop the specified table.  If
505:             * cascade contraints is supported by the dialect and cascadeConstraints is
506:             * true, then a drop statement with cascade constraints clause will be 
507:             * formed.
508:             * 
509:             * @param iTableInfo the table to drop
510:             * @param cascadeConstraints whether or not to drop any FKs that may 
511:             * reference the specified table.
512:             * @return the drop SQL command.
513:             */
514:            public List<String> getTableDropSQL(ITableInfo iTableInfo,
515:                    boolean cascadeConstraints, boolean isMaterializedView) {
516:                return DialectUtils.getTableDropSQL(iTableInfo, true,
517:                        cascadeConstraints, false, DialectUtils.CASCADE_CLAUSE,
518:                        false);
519:            }
520:
521:            /**
522:             * Returns the SQL that forms the command to add a primary key to the 
523:             * specified table composed of the given column names.
524:             * 
525:             * alter table test alter column mychar char(10) not null 
526:             * 
527:             * alter table test add primary key (mychar)
528:             * 
529:             * alter table pktest add constraint pk_pktest primary key (pkcol)
530:             * 
531:             * @param pkName the name of the constraint
532:             * @param columns the columns that form the key
533:             * @return
534:             */
535:            public String[] getAddPrimaryKeySQL(String pkName,
536:                    TableColumnInfo[] columns, ITableInfo ti) {
537:                ArrayList<String> result = new ArrayList<String>();
538:                StringBuffer addPKSQL = new StringBuffer();
539:                addPKSQL.append("ALTER TABLE ");
540:                addPKSQL.append(ti.getQualifiedName());
541:                addPKSQL.append(" ADD CONSTRAINT ");
542:                addPKSQL.append(pkName);
543:                addPKSQL.append(" PRIMARY KEY (");
544:                for (int i = 0; i < columns.length; i++) {
545:                    TableColumnInfo info = columns[i];
546:                    if (info.isNullable().equals("YES")) {
547:                        result.add(getColumnNullableAlterSQL(info, false));
548:                    }
549:                    addPKSQL.append(info.getColumnName());
550:                    if (i + 1 < columns.length) {
551:                        addPKSQL.append(", ");
552:                    }
553:                }
554:                addPKSQL.append(")");
555:                result.add(addPKSQL.toString());
556:                return result.toArray(new String[result.size()]);
557:            }
558:
559:            /**
560:             * Returns the SQL statement to use to add a comment to the specified 
561:             * column of the specified table.
562:             * @param info information about the column such as type, name, etc.
563:             * @return
564:             * @throws UnsupportedOperationException if the database doesn't support 
565:             *         annotating columns with a comment.
566:             */
567:            public String getColumnCommentAlterSQL(TableColumnInfo info)
568:                    throws UnsupportedOperationException {
569:                return DialectUtils.getColumnCommentAlterSQL(info);
570:            }
571:
572:            /**
573:             * Returns the SQL used to alter the nullability of the specified column 
574:             * 
575:             * ALTER TABLE tableName ALTER COLUMN columnName 
576:             * dataType [DEFAULT expression] [NOT [NULL]]
577:             * 
578:             * @param info the column to modify
579:             * @return the SQL to execute
580:             */
581:            public String getColumnNullableAlterSQL(TableColumnInfo info) {
582:                String alterClause = DialectUtils.ALTER_COLUMN_CLAUSE;
583:                return DialectUtils.getColumnNullableAlterSQL(info, this ,
584:                        alterClause, true);
585:            }
586:
587:            /**
588:             * Returns a boolean value indicating whether or not this database dialect
589:             * supports changing a column from null to not-null and vice versa.
590:             * 
591:             * @return true if the database supports dropping columns; false otherwise.
592:             */
593:            public boolean supportsAlterColumnNull() {
594:                return true;
595:            }
596:
597:            /**
598:             * Returns the SQL used to alter the nullability of the specified column 
599:             * 
600:             * @param info the column to modify
601:             * @return the SQL to execute
602:             */
603:            private String getColumnNullableAlterSQL(TableColumnInfo info,
604:                    boolean isNullable) {
605:                StringBuffer result = new StringBuffer();
606:                result.append("ALTER TABLE ");
607:                result.append(info.getTableName());
608:                result.append(" ALTER COLUMN ");
609:                result.append(info.getColumnName());
610:                result.append(" ");
611:                result.append(DialectUtils.getTypeName(info, this ));
612:                if (isNullable) {
613:                    result.append(" NULL ");
614:                } else {
615:                    result.append(" NOT NULL ");
616:                }
617:                return result.toString();
618:
619:            }
620:
621:            /**
622:             * Returns a boolean value indicating whether or not this database dialect
623:             * supports renaming columns.
624:             * 
625:             * @return true if the database supports changing the name of columns;  
626:             *         false otherwise.
627:             */
628:            public boolean supportsRenameColumn() {
629:                return true;
630:            }
631:
632:            /**
633:             * Returns the SQL that is used to change the column name.
634:             * 
635:             * ALTER TABLE tableName ALTER COLUMN columnName 
636:             * RENAME TO name
637:             * 
638:             * @param from the TableColumnInfo as it is
639:             * @param to the TableColumnInfo as it wants to be
640:             * 
641:             * @return the SQL to make the change
642:             */
643:            public String getColumnNameAlterSQL(TableColumnInfo from,
644:                    TableColumnInfo to) {
645:                String alterClause = DialectUtils.ALTER_COLUMN_CLAUSE;
646:                String renameToClause = DialectUtils.RENAME_TO_CLAUSE;
647:                return DialectUtils.getColumnNameAlterSQL(from, to,
648:                        alterClause, renameToClause);
649:            }
650:
651:            /**
652:             * Returns a boolean value indicating whether or not this dialect supports 
653:             * modifying a columns type.
654:             * 
655:             * @return true if supported; false otherwise
656:             */
657:            public boolean supportsAlterColumnType() {
658:                return true;
659:            }
660:
661:            /**
662:             * Returns the SQL that is used to change the column type.
663:             * 
664:             * ALTER TABLE table_name ALTER COLUMN column_name data_type 
665:             * 
666:             * @param from the TableColumnInfo as it is
667:             * @param to the TableColumnInfo as it wants to be
668:             * 
669:             * @return the SQL to make the change
670:             * @throw UnsupportedOperationException if the database doesn't support 
671:             *         modifying column types. 
672:             */
673:            public List<String> getColumnTypeAlterSQL(TableColumnInfo from,
674:                    TableColumnInfo to) throws UnsupportedOperationException {
675:                String alterClause = DialectUtils.ALTER_COLUMN_CLAUSE;
676:                String setClause = "";
677:                return DialectUtils.getColumnTypeAlterSQL(this , alterClause,
678:                        setClause, false, from, to);
679:            }
680:
681:            /**
682:             * Returns a boolean value indicating whether or not this database dialect
683:             * supports changing a column's default value.
684:             * 
685:             * @return true if the database supports modifying column defaults; false 
686:             *         otherwise
687:             */
688:            public boolean supportsAlterColumnDefault() {
689:                return true;
690:            }
691:
692:            /**
693:             * Returns the SQL command to change the specified column's default value
694:             *
695:             * ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT 'default value'
696:             *   
697:             * @param info the column to modify and it's default value.
698:             * @return SQL to make the change
699:             */
700:            public String getColumnDefaultAlterSQL(TableColumnInfo info) {
701:                String alterClause = DialectUtils.ALTER_COLUMN_CLAUSE;
702:                String defaultClause = DialectUtils.SET_DEFAULT_CLAUSE;
703:                return DialectUtils.getColumnDefaultAlterSQL(this , info,
704:                        alterClause, false, defaultClause);
705:            }
706:
707:            /**
708:             * Returns the SQL command to drop the specified table's primary key.
709:             * 
710:             * @param pkName the name of the primary key that should be dropped
711:             * @param tableName the name of the table whose primary key should be 
712:             *                  dropped
713:             * @return
714:             */
715:            public String getDropPrimaryKeySQL(String pkName, String tableName) {
716:                return DialectUtils.getDropPrimaryKeySQL(pkName, tableName,
717:                        false, false);
718:            }
719:
720:            /**
721:             * Returns the SQL command to drop the specified table's foreign key 
722:             * constraint.
723:             * 
724:             * @param fkName the name of the foreign key that should be dropped
725:             * @param tableName the name of the table whose foreign key should be 
726:             *                  dropped
727:             * @return
728:             */
729:            public String getDropForeignKeySQL(String fkName, String tableName) {
730:                return DialectUtils.getDropForeignKeySQL(fkName, tableName);
731:            }
732:
733:            /**
734:             * Returns the SQL command to create the specified table.
735:             * 
736:             * @param tables the tables to get create statements for
737:             * @param md the metadata from the ISession
738:             * @param prefs preferences about how the resultant SQL commands should be 
739:             *              formed.
740:             * @param isJdbcOdbc whether or not the connection is via JDBC-ODBC bridge.
741:             *  
742:             * @return the SQL that is used to create the specified table
743:             */
744:            public List<String> getCreateTableSQL(List<ITableInfo> tables,
745:                    ISQLDatabaseMetaData md, CreateScriptPreferences prefs,
746:                    boolean isJdbcOdbc) throws SQLException {
747:                return DialectUtils.getCreateTableSQL(tables, md, this, prefs,
748:                        isJdbcOdbc);
749:            }
750:
751:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.