001: /*
002: * Copyright (C) 2006 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package net.sourceforge.squirrel_sql.fw.dialects;
020:
021: import java.sql.SQLException;
022: import java.sql.Types;
023: import java.util.List;
024:
025: import org.hibernate.dialect.Dialect;
026:
027: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
028: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
029: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
030: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
031: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
032:
033: /**
034: * An extension to the standard Hibernate Daffodil dialect
035: */
036:
037: public class DaffodilDialect extends Dialect implements
038: HibernateDialect {
039:
040: public DaffodilDialect() {
041: super ();
042: registerColumnType(Types.BIGINT, "bigint");
043: registerColumnType(Types.BINARY, 4192, "binary($l)");
044: registerColumnType(Types.BINARY, 1073741823,
045: "long varbinary($l)");
046: registerColumnType(Types.BINARY, "long varbinary(1073741823)");
047: registerColumnType(Types.BIT, "bit");
048: registerColumnType(Types.BLOB, 1073741823, "blob($l)");
049: registerColumnType(Types.BLOB, "blob(1073741823)");
050: registerColumnType(Types.BOOLEAN, "boolean");
051: registerColumnType(Types.CHAR, 4192, "char($l)");
052: registerColumnType(Types.CHAR, 1073741823, "clob($l)");
053: registerColumnType(Types.CHAR, "clob(1073741823)");
054: registerColumnType(Types.CLOB, 1073741823, "clob($l)");
055: registerColumnType(Types.CLOB, "clob(1073741823)");
056: registerColumnType(Types.DATE, "date");
057: registerColumnType(Types.DECIMAL, "decimal($p,$s)");
058: registerColumnType(Types.DOUBLE, "double precision");
059: registerColumnType(Types.FLOAT, "float($p)");
060: registerColumnType(Types.INTEGER, "integer");
061: registerColumnType(Types.LONGVARBINARY, 1073741823,
062: "long varbinary($l)");
063: registerColumnType(Types.LONGVARBINARY,
064: "long varbinary(1073741823)");
065: registerColumnType(Types.LONGVARCHAR, 1073741823,
066: "long varchar($l)");
067: registerColumnType(Types.LONGVARCHAR,
068: "long varchar(1073741823)");
069: registerColumnType(Types.NUMERIC, "numeric($p,$s)");
070: registerColumnType(Types.REAL, "real");
071: registerColumnType(Types.SMALLINT, "smallint");
072: registerColumnType(Types.TIME, "time");
073: registerColumnType(Types.TIMESTAMP, "timestamp");
074: registerColumnType(Types.TINYINT, "tinyint");
075: registerColumnType(Types.VARBINARY, 4192, "varbinary($l)");
076: registerColumnType(Types.VARBINARY, 1073741823,
077: "long varbinary($l)");
078: registerColumnType(Types.VARBINARY,
079: "long varbinary(1073741823)");
080: registerColumnType(Types.VARCHAR, 4192, "varchar($l)");
081: registerColumnType(Types.VARCHAR, 1073741823, "clob($l)");
082: registerColumnType(Types.VARCHAR, "clob(1073741823)");
083:
084: }
085:
086: /* (non-Javadoc)
087: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#canPasteTo(net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType)
088: */
089: public boolean canPasteTo(IDatabaseObjectInfo info) {
090: boolean result = true;
091: DatabaseObjectType type = info.getDatabaseObjectType();
092: if (type.getName().equalsIgnoreCase("database")) {
093: result = false;
094: }
095: return result;
096: }
097:
098: /* (non-Javadoc)
099: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#supportsSchemasInTableDefinition()
100: */
101: public boolean supportsSchemasInTableDefinition() {
102: return false;
103: }
104:
105: /* (non-Javadoc)
106: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getLengthFunction()
107: */
108: public String getLengthFunction(int dataType) {
109: return "length";
110: }
111:
112: /* (non-Javadoc)
113: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getMaxFunction()
114: */
115: public String getMaxFunction() {
116: return "max";
117: }
118:
119: /* (non-Javadoc)
120: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getMaxPrecision(int)
121: */
122: public int getMaxPrecision(int dataType) {
123: if (dataType == Types.FLOAT) {
124: return 15;
125: }
126: if (dataType == Types.NUMERIC || dataType == Types.DECIMAL) {
127: return 38;
128: }
129: return 0;
130: }
131:
132: /* (non-Javadoc)
133: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getMaxScale(int)
134: */
135: public int getMaxScale(int dataType) {
136: return getMaxPrecision(dataType);
137: }
138:
139: /* (non-Javadoc)
140: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getPrecisionDigits(int, int)
141: */
142: public int getPrecisionDigits(int columnSize, int dataType) {
143: return columnSize;
144: }
145:
146: /* (non-Javadoc)
147: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.dialects.HibernateDialect#getColumnLength(int, int)
148: */
149: public int getColumnLength(int columnSize, int dataType) {
150: return columnSize;
151: }
152:
153: /**
154: * The string which identifies this dialect in the dialect chooser.
155: *
156: * @return a descriptive name that tells the user what database this dialect
157: * is design to work with.
158: */
159: public String getDisplayName() {
160: return "Daffodil";
161: }
162:
163: /**
164: * Returns boolean value indicating whether or not this dialect supports the
165: * specified database product/version.
166: *
167: * @param databaseProductName the name of the database as reported by
168: * DatabaseMetaData.getDatabaseProductName()
169: * @param databaseProductVersion the version of the database as reported by
170: * DatabaseMetaData.getDatabaseProductVersion()
171: * @return true if this dialect can be used for the specified product name
172: * and version; false otherwise.
173: */
174: public boolean supportsProduct(String databaseProductName,
175: String databaseProductVersion) {
176: if (databaseProductName == null) {
177: return false;
178: }
179: if (databaseProductName.trim().startsWith("Daffodil")) {
180: // We don't yet have the need to discriminate by version.
181: return true;
182: }
183: return false;
184: }
185:
186: public String getAddColumnString() {
187: return "ADD COLUMN";
188: }
189:
190: /**
191: * Returns the SQL statement to use to add a column to the specified table
192: * using the information about the new column specified by info.
193: * @param info information about the new column such as type, name, etc.
194: *
195: * @return
196: * @throws UnsupportedOperationException if the database doesn't support
197: * adding columns after a table has already been created.
198: */
199: public String[] getColumnAddSQL(TableColumnInfo info)
200: throws UnsupportedOperationException {
201: return new String[] { DialectUtils.getColumnAddSQL(info, this ,
202: true, false, true) };
203: }
204:
205: /**
206: * Returns a boolean value indicating whether or not this dialect supports
207: * adding comments to columns.
208: *
209: * @return true if column comments are supported; false otherwise.
210: */
211: public boolean supportsColumnComment() {
212: return false;
213: }
214:
215: /**
216: * Returns the SQL statement to use to add a comment to the specified
217: * column of the specified table.
218: *
219: * @param tableName the name of the table to create the SQL for.
220: * @param columnName the name of the column to create the SQL for.
221: * @param comment the comment to add.
222: * @return
223: * @throws UnsupportedOperationException if the database doesn't support
224: * annotating columns with a comment.
225: */
226: @SuppressWarnings("unused")
227: public String getColumnCommentAlterSQL(String tableName,
228: String columnName, String comment)
229: throws UnsupportedOperationException {
230: int featureId = DialectUtils.COLUMN_COMMENT_ALTER_TYPE;
231: String msg = DialectUtils
232: .getUnsupportedMessage(this , featureId);
233: throw new UnsupportedOperationException(msg);
234: }
235:
236: /**
237: * Returns a boolean value indicating whether or not this database dialect
238: * supports dropping columns from tables.
239: *
240: * @return true if the database supports dropping columns; false otherwise.
241: */
242: public boolean supportsDropColumn() {
243: return true;
244: }
245:
246: /**
247: * Returns the SQL that forms the command to drop the specified colum in the
248: * specified table.
249: *
250: * @param tableName the name of the table that has the column
251: * @param columnName the name of the column to drop.
252: * @return
253: * @throws UnsupportedOperationException if the database doesn't support
254: * dropping columns.
255: */
256: public String getColumnDropSQL(String tableName, String columnName)
257: throws UnsupportedOperationException {
258: StringBuffer result = new StringBuffer();
259: result.append("ALTER TABLE ");
260: result.append(tableName);
261: result.append(" DROP COLUMN ");
262: result.append(columnName);
263: result.append(" CASCADE");
264: return result.toString();
265: }
266:
267: /**
268: * Returns the SQL that forms the command to drop the specified table. If
269: * cascade contraints is supported by the dialect and cascadeConstraints is
270: * true, then a drop statement with cascade constraints clause will be
271: * formed.
272: *
273: * @param iTableInfo the table to drop
274: * @param cascadeConstraints whether or not to drop any FKs that may
275: * reference the specified table.
276: * @return the drop SQL command.
277: */
278: public List<String> getTableDropSQL(ITableInfo iTableInfo,
279: boolean cascadeConstraints, boolean isMaterializedView) {
280: return DialectUtils.getTableDropSQL(iTableInfo, true,
281: cascadeConstraints, false, DialectUtils.CASCADE_CLAUSE,
282: false);
283: }
284:
285: /**
286: * Returns the SQL that forms the command to add a primary key to the
287: * specified table composed of the given column names.
288: *
289: * alter table test add constraint pkName primary key (<PK COLS>)
290: *
291: * @param pkName the name of the constraint
292: * @param columnNames the columns that form the key
293: * @return
294: */
295: public String[] getAddPrimaryKeySQL(String pkName,
296: TableColumnInfo[] columns, ITableInfo ti) {
297: return new String[] { DialectUtils.getAddPrimaryKeySQL(ti,
298: pkName, columns, false) };
299: }
300:
301: /**
302: * Returns the SQL statement to use to add a comment to the specified
303: * column of the specified table.
304: * @param info information about the column such as type, name, etc.
305: * @return
306: * @throws UnsupportedOperationException if the database doesn't support
307: * annotating columns with a comment.
308: */
309: public String getColumnCommentAlterSQL(TableColumnInfo info)
310: throws UnsupportedOperationException {
311: int featureId = DialectUtils.COLUMN_COMMENT_ALTER_TYPE;
312: String msg = DialectUtils
313: .getUnsupportedMessage(this , featureId);
314: throw new UnsupportedOperationException(msg);
315: }
316:
317: /**
318: * Returns a boolean value indicating whether or not this database dialect
319: * supports changing a column from null to not-null and vice versa.
320: *
321: * @return true if the database supports dropping columns; false otherwise.
322: */
323: public boolean supportsAlterColumnNull() {
324: return false;
325: }
326:
327: /**
328: * Returns the SQL used to alter the specified column to not allow null
329: * values
330: *
331: * @param info the column to modify
332: * @return the SQL to execute
333: */
334: public String getColumnNullableAlterSQL(TableColumnInfo info) {
335: int featureId = DialectUtils.COLUMN_NULL_ALTER_TYPE;
336: String msg = DialectUtils
337: .getUnsupportedMessage(this , featureId);
338: throw new UnsupportedOperationException(msg);
339: }
340:
341: /**
342: * Returns a boolean value indicating whether or not this database dialect
343: * supports renaming columns.
344: *
345: * @return true if the database supports changing the name of columns;
346: * false otherwise.
347: */
348: public boolean supportsRenameColumn() {
349: return false;
350: }
351:
352: /**
353: * Returns the SQL that is used to change the column name.
354: *
355: *
356: * @param from the TableColumnInfo as it is
357: * @param to the TableColumnInfo as it wants to be
358: *
359: * @return the SQL to make the change
360: */
361: public String getColumnNameAlterSQL(TableColumnInfo from,
362: TableColumnInfo to) {
363: int featureId = DialectUtils.COLUMN_NAME_ALTER_TYPE;
364: String msg = DialectUtils
365: .getUnsupportedMessage(this , featureId);
366: throw new UnsupportedOperationException(msg);
367: }
368:
369: /**
370: * Returns a boolean value indicating whether or not this dialect supports
371: * modifying a columns type.
372: *
373: * @return true if supported; false otherwise
374: */
375: public boolean supportsAlterColumnType() {
376: return false;
377: }
378:
379: /**
380: * Returns the SQL that is used to change the column type.
381: *
382: * @param from the TableColumnInfo as it is
383: * @param to the TableColumnInfo as it wants to be
384: *
385: * @return the SQL to make the change
386: * @throw UnsupportedOperationException if the database doesn't support
387: * modifying column types.
388: */
389: public List<String> getColumnTypeAlterSQL(TableColumnInfo from,
390: TableColumnInfo to) throws UnsupportedOperationException {
391: int featureId = DialectUtils.COLUMN_TYPE_ALTER_TYPE;
392: String msg = DialectUtils
393: .getUnsupportedMessage(this , featureId);
394: throw new UnsupportedOperationException(msg);
395: }
396:
397: /**
398: * Returns a boolean value indicating whether or not this database dialect
399: * supports changing a column's default value.
400: *
401: * @return true if the database supports modifying column defaults; false
402: * otherwise
403: */
404: public boolean supportsAlterColumnDefault() {
405: return true;
406: }
407:
408: /**
409: * Returns the SQL command to change the specified column's default value
410: *
411: * @param info the column to modify and it's default value.
412: * @return SQL to make the change
413: */
414: public String getColumnDefaultAlterSQL(TableColumnInfo info) {
415: String alterClause = DialectUtils.ALTER_COLUMN_CLAUSE;
416: String defaultClause = DialectUtils.SET_DEFAULT_CLAUSE;
417: return DialectUtils.getColumnDefaultAlterSQL(this , info,
418: alterClause, false, defaultClause);
419: }
420:
421: /**
422: * Returns the SQL command to drop the specified table's primary key.
423: *
424: * alter table test drop constraint pk_test cascade
425: *
426: * @param pkName the name of the primary key that should be dropped
427: * @param tableName the name of the table whose primary key should be
428: * dropped
429: * @return
430: */
431: public String getDropPrimaryKeySQL(String pkName, String tableName) {
432: return DialectUtils.getDropPrimaryKeySQL(pkName, tableName,
433: true, true);
434: }
435:
436: /**
437: * Returns the SQL command to drop the specified table's foreign key
438: * constraint.
439: *
440: * @param fkName the name of the foreign key that should be dropped
441: * @param tableName the name of the table whose foreign key should be
442: * dropped
443: * @return
444: */
445: public String getDropForeignKeySQL(String fkName, String tableName) {
446: return DialectUtils.getDropForeignKeySQL(fkName, tableName);
447: }
448:
449: /**
450: * Returns the SQL command to create the specified table.
451: *
452: * @param tables the tables to get create statements for
453: * @param md the metadata from the ISession
454: * @param prefs preferences about how the resultant SQL commands should be
455: * formed.
456: * @param isJdbcOdbc whether or not the connection is via JDBC-ODBC bridge.
457: *
458: * @return the SQL that is used to create the specified table
459: */
460: public List<String> getCreateTableSQL(List<ITableInfo> tables,
461: ISQLDatabaseMetaData md, CreateScriptPreferences prefs,
462: boolean isJdbcOdbc) throws SQLException {
463: return DialectUtils.getCreateTableSQL(tables, md, this, prefs,
464: isJdbcOdbc);
465: }
466:
467: }
|