001: package net.sourceforge.squirrel_sql.fw.dialects;
002:
003: import java.sql.SQLException;
004: import java.sql.Types;
005: import java.util.List;
006:
007: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
008: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
009: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
010: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
011:
012: import org.hibernate.HibernateException;
013: import org.hibernate.dialect.Dialect;
014:
015: public class HADBDialect extends Dialect implements HibernateDialect {
016:
017: public HADBDialect() {
018: super ();
019:
020: registerColumnType(Types.BIGINT, "double integer");
021: registerColumnType(Types.BINARY, 8000, "binary($l)");
022: registerColumnType(Types.BINARY, "binary(8000)");
023: registerColumnType(Types.BIT, "smallint");
024: registerColumnType(Types.BOOLEAN, "smallint");
025: registerColumnType(Types.BLOB, "blob");
026: registerColumnType(Types.CHAR, 8000, "char($l)");
027: registerColumnType(Types.CHAR, "char(8000)");
028: registerColumnType(Types.CLOB, "clob");
029: registerColumnType(Types.DATE, "date");
030: registerColumnType(Types.DECIMAL, "decimal($p,$s)");
031: registerColumnType(Types.DOUBLE, "double precision");
032: registerColumnType(Types.FLOAT, "float($p)");
033: registerColumnType(Types.INTEGER, "integer");
034: registerColumnType(Types.LONGVARBINARY, "blob");
035: registerColumnType(Types.LONGVARCHAR, "clob");
036: registerColumnType(Types.NUMERIC, "decimal($p,$s)");
037: registerColumnType(Types.REAL, "real");
038: registerColumnType(Types.SMALLINT, "smallint");
039: registerColumnType(Types.TIME, "time");
040: registerColumnType(Types.TIMESTAMP, "timestamp");
041: registerColumnType(Types.TINYINT, "smallint");
042: registerColumnType(Types.VARBINARY, 8000, "varbinary($l)");
043: registerColumnType(Types.VARBINARY, "varbinary(8000)");
044: registerColumnType(Types.VARCHAR, 8000, "varchar($l)");
045: registerColumnType(Types.VARCHAR, "varchar(8000)");
046:
047: }
048:
049: /**
050: * Returns a boolean indicating whether or not the specified database object
051: * can be pasted into for this database dialect. Some databases support the
052: * notion of schemas where tables live, and in those cases pasting to a
053: * database object in the object tree is not really appropriate. However,
054: * other databases don't support schemas (like Axion, Firebird)
055: *
056: * @param info
057: * @return
058: */
059: public boolean canPasteTo(IDatabaseObjectInfo info) {
060: return true;
061: }
062:
063: public String[] getAddPrimaryKeySQL(String pkName,
064: TableColumnInfo[] colInfos, ITableInfo ti) {
065: // TODO Auto-generated method stub
066: return null;
067: }
068:
069: public String[] getColumnAddSQL(TableColumnInfo info)
070: throws HibernateException, UnsupportedOperationException {
071: // TODO Auto-generated method stub
072: return null;
073: }
074:
075: public String getColumnCommentAlterSQL(TableColumnInfo info)
076: throws UnsupportedOperationException {
077: throw new UnsupportedOperationException("Not yet implemented");
078: }
079:
080: /**
081: * Returns the SQL that forms the command to drop the specified colum in the
082: * specified table.
083: *
084: * @param tableName the name of the table that has the column
085: * @param columnName the name of the column to drop.
086: * @return
087: * @throw UnsupportedOperationException if the database doesn't support
088: * dropping columns.
089: */
090: public String getColumnDropSQL(String tableName, String columnName)
091: throws UnsupportedOperationException {
092: throw new UnsupportedOperationException("Not yet implemented");
093: }
094:
095: /**
096: * Some jdbc drivers are hopelessly broken with regard to reporting the
097: * COLUMN_SIZE. For example, MaxDB has a "long byte" data type which can
098: * store up to 2G of data, yet the driver reports that the column size is
099: * "8" - real helpful. So for drivers that have this problem, return the
100: * "proper" maximum column length for the specified dataType. If the driver
101: * doesn't have this problem, just return the columnSize.
102: *
103: * @param columnSize the size of the column as reported by the jdbc driver
104: * @param dataType the type of the column.
105: *
106: * @return the specified columnSize if the jdbc driver isn't broken;
107: * otherwise, the maximum column size for the specified dataType if
108: * the driver is broken.
109: */
110: public int getColumnLength(int columnSize, int dataType) {
111: // HADB reports "10" for column size of BLOB/CLOB
112: if (dataType == Types.CLOB || dataType == Types.BLOB) {
113: return Integer.MAX_VALUE; // 2GB (2^32)
114: }
115: return columnSize;
116: }
117:
118: /**
119: * Returns a boolean value indicating whether or not this database dialect
120: * supports renaming columns.
121: *
122: * @return true if the database supports changing the name of columns;
123: * false otherwise.
124: */
125: public boolean supportsRenameColumn() {
126: // TODO: need to verify this
127: return true;
128: }
129:
130: /**
131: * Returns the SQL that is used to change the column name.
132: *
133: * alter table test rename column mycol mycol2
134: *
135: * @param from the TableColumnInfo as it is
136: * @param to the TableColumnInfo as it wants to be
137: *
138: * @return the SQL to make the change
139: */
140: public String getColumnNameAlterSQL(TableColumnInfo from,
141: TableColumnInfo to) {
142: StringBuffer result = new StringBuffer();
143: result.append("ALTER TABLE ");
144: result.append(from.getTableName());
145: result.append(" RENAME COLUMN ");
146: result.append(from.getColumnName());
147: result.append(" ");
148: result.append(to.getColumnName());
149: return result.toString();
150: }
151:
152: /**
153: * Returns the SQL used to alter the nullability of the specified column
154: *
155: * @param info the column to modify
156: * @return the SQL to execute
157: */
158: public String getColumnNullableAlterSQL(TableColumnInfo info) {
159: throw new UnsupportedOperationException("Not yet implemented");
160: }
161:
162: /**
163: * Returns a boolean value indicating whether or not this dialect supports
164: * modifying a columns type.
165: *
166: * @return true if supported; false otherwise
167: */
168: public boolean supportsAlterColumnType() {
169: return true;
170: }
171:
172: /**
173: * Returns the SQL that is used to change the column type.
174: *
175: * @param from the TableColumnInfo as it is
176: * @param to the TableColumnInfo as it wants to be
177: *
178: * @return the SQL to make the change
179: * @throw UnsupportedOperationException if the database doesn't support
180: * modifying column types.
181: */
182: public List<String> getColumnTypeAlterSQL(TableColumnInfo from,
183: TableColumnInfo to) throws UnsupportedOperationException {
184: throw new UnsupportedOperationException("Not yet implemented");
185: }
186:
187: /**
188: * The string which identifies this dialect in the dialect chooser.
189: *
190: * @return a descriptive name that tells the user what database this dialect
191: * is design to work with.
192: */
193: public String getDisplayName() {
194: return "Sun HADB";
195: }
196:
197: /**
198: * Returns the name of the function that measures the length of a character
199: * string.
200: *
201: * @param dataType the java.sql.Types data type. Some databases have
202: * different length functions for different data types.
203: *
204: * @return the name of the function to be applied to a column to determine
205: * the length of that column in a particular record.
206: */
207: public String getLengthFunction(int dataType) {
208: return "char_length";
209: }
210:
211: /**
212: * Returns the name of the aggregate function that determines the max value
213: * of an expression.
214: *
215: * @return the name of the function to be applied to a set to determine the
216: * element with the highest numeric value.
217: */
218: public String getMaxFunction() {
219: return "max";
220: }
221:
222: /**
223: * Returns the maximum precision allowed by the database for number type
224: * fields that specify the length of the number to the left of the decimal
225: * point in digits. If the HibernateDialect implementation doesn't ever
226: * use $p in any call to registerColumnType(), then this maximum precsision
227: * will not be used.
228: *
229: * @param dataType the java.sql.Types data type.
230: *
231: * @return the maximum number that can be used in a column declaration for
232: * precision for the specified type.
233: */
234: public int getMaxPrecision(int dataType) {
235: if (dataType == Types.FLOAT) {
236: return 52;
237: }
238: if (dataType == Types.DECIMAL || dataType == Types.NUMERIC) {
239: return 31;
240: }
241: return 0;
242: }
243:
244: /**
245: * Returns the maximum scale allowed by the database for number type
246: * fields that specify the length of the number to the right of the decimal
247: * point in digits. If the HibernateDialect implementation doesn't ever
248: * use $s in any call to registerColumnType(), then this maximum scale
249: * will not be used.
250: *
251: * @param dataType the java.sql.Types data type.
252: *
253: * @return the maximum number that can be used in a column declaration for
254: * scale for the specified type.
255: */
256: public int getMaxScale(int dataType) {
257: return getMaxPrecision(dataType);
258: }
259:
260: /**
261: * Returns the number of digits of precision is represented by the specifed
262: * columnSize for the specified dataType. Some DBs represent precision as
263: * the total number of digits on the right or left of the decimal. That is
264: * what we want. Others (like PostgreSQL) give the number of bytes of
265: * storage a column can use - less than useful, since the SQL-92 says
266: * "number of digits" and this is what most other DBs use.
267: *
268: * @param columnSize the size of the column as reported by the driver.
269: * @param dataType the java.sql.Types data type.
270: *
271: * @return a number indicating the total number of digits (includes both
272: * sides of the decimal point) the column can represent.
273: */
274: public int getPrecisionDigits(int columnSize, int dataType) {
275: return columnSize;
276: }
277:
278: /**
279: * Returns the SQL that forms the command to drop the specified table. If
280: * cascade contraints is supported by the dialect and cascadeConstraints is
281: * true, then a drop statement with cascade constraints clause will be
282: * formed.
283: *
284: * @param iTableInfo the table to drop
285: * @param cascadeConstraints whether or not to drop any FKs that may
286: * reference the specified table.
287: * @return the drop SQL command.
288: */
289: public List<String> getTableDropSQL(ITableInfo iTableInfo,
290: boolean cascadeConstraints, boolean isMaterializedView) {
291: return DialectUtils.getTableDropSQL(iTableInfo, false,
292: cascadeConstraints, false, DialectUtils.CASCADE_CLAUSE,
293: false);
294: }
295:
296: /**
297: * Returns a boolean value indicating whether or not this dialect supports
298: * adding comments to columns.
299: *
300: * @return true if column comments are supported; false otherwise.
301: */
302: public boolean supportsColumnComment() {
303: return false;
304: }
305:
306: /**
307: * Returns a boolean value indicating whether or not this database dialect
308: * supports dropping columns from tables.
309: *
310: * @return true if the database supports dropping columns; false otherwise.
311: */
312: public boolean supportsDropColumn() {
313: return false;
314: }
315:
316: /**
317: * Returns boolean value indicating whether or not this dialect supports the
318: * specified database product/version.
319: *
320: * @param databaseProductName the name of the database as reported by
321: * DatabaseMetaData.getDatabaseProductName()
322: * @param databaseProductVersion the version of the database as reported by
323: * DatabaseMetaData.getDatabaseProductVersion()
324: * @return true if this dialect can be used for the specified product name
325: * and version; false otherwise.
326: */
327: public boolean supportsProduct(String databaseProductName,
328: String databaseProductVersion) {
329: if (databaseProductName == null) {
330: return false;
331: }
332: String prodName = "sun java system high availability";
333: if (databaseProductName.trim().toLowerCase().startsWith(
334: prodName)) {
335: // We don't yet have the need to discriminate by version.
336: return true;
337: }
338: return false;
339: }
340:
341: public boolean supportsSchemasInTableDefinition() {
342: // TODO Auto-generated method stub
343: return false;
344: }
345:
346: /**
347: * Returns a boolean value indicating whether or not this database dialect
348: * supports changing a column from null to not-null and vice versa.
349: *
350: * @return true if the database supports dropping columns; false otherwise.
351: */
352: public boolean supportsAlterColumnNull() {
353: // TODO Auto-generated method stub
354: return false;
355: }
356:
357: /**
358: * Returns a boolean value indicating whether or not this database dialect
359: * supports changing a column's default value.
360: *
361: * @return true if the database supports modifying column defaults; false
362: * otherwise
363: */
364: public boolean supportsAlterColumnDefault() {
365: // TODO Need to verify this
366: return true;
367: }
368:
369: /**
370: * Returns the SQL command to change the specified column's default value
371: *
372: * @param info the column to modify and it's default value.
373: * @return SQL to make the change
374: */
375: public String getColumnDefaultAlterSQL(TableColumnInfo info) {
376: // TODO need to implement or change the message
377: throw new UnsupportedOperationException("Not yet implemented");
378: }
379:
380: /**
381: * Returns the SQL command to drop the specified table's primary key.
382: *
383: * @param pkName the name of the primary key that should be dropped
384: * @param tableName the name of the table whose primary key should be
385: * dropped
386: * @return
387: */
388: public String getDropPrimaryKeySQL(String pkName, String tableName) {
389: return DialectUtils.getDropPrimaryKeySQL(pkName, tableName,
390: false, false);
391: }
392:
393: /**
394: * Returns the SQL command to drop the specified table's foreign key
395: * constraint.
396: *
397: * @param fkName the name of the foreign key that should be dropped
398: * @param tableName the name of the table whose foreign key should be
399: * dropped
400: * @return
401: */
402: public String getDropForeignKeySQL(String fkName, String tableName) {
403: return DialectUtils.getDropForeignKeySQL(fkName, tableName);
404: }
405:
406: /**
407: * Returns the SQL command to create the specified table.
408: *
409: * @param tables the tables to get create statements for
410: * @param md the metadata from the ISession
411: * @param prefs preferences about how the resultant SQL commands should be
412: * formed.
413: * @param isJdbcOdbc whether or not the connection is via JDBC-ODBC bridge.
414: *
415: * @return the SQL that is used to create the specified table
416: */
417: public List<String> getCreateTableSQL(List<ITableInfo> tables,
418: ISQLDatabaseMetaData md, CreateScriptPreferences prefs,
419: boolean isJdbcOdbc) throws SQLException {
420: return DialectUtils.getCreateTableSQL(tables, md, this, prefs,
421: isJdbcOdbc);
422: }
423:
424: }
|