001: /*
002: * Copyright (C) 2006 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library 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 GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; 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 net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
022: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
023: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
024: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
025:
026: import java.sql.SQLException;
027: import java.util.List;
028:
029: import org.hibernate.HibernateException;
030:
031: /**
032: * An interface for methods implemented by database dialects for the purpose of
033: * handling standard and non-standard SQL and database types.
034: */
035: public interface HibernateDialect {
036:
037: /**
038: * Get the name of the database type associated with the given
039: * <tt>java.sql.Types</tt> typecode.
040: * @param code <tt>java.sql.Types</tt> typecode
041: * @param length the length or precision of the column
042: * @param precision the precision of the column
043: * @param scale the scale of the column
044: *
045: * @return the database type name
046: * @throws HibernateException
047: */
048: String getTypeName(int code, int length, int precision, int scale)
049: throws HibernateException;
050:
051: /**
052: * Returns a boolean indicating whether or not the specified database object
053: * can be pasted into for this database dialect. Some databases support the
054: * notion of schemas where tables live, and in those cases pasting to a
055: * database object in the object tree is not really appropriate. However,
056: * other databases don't support schemas (like Axion, Firebird)
057: *
058: * @param info
059: * @return
060: */
061: boolean canPasteTo(IDatabaseObjectInfo info);
062:
063: /**
064: * Returns a boolean value indicating whether or not this database dialect
065: * supports table creation statements where the table name is prefixed by
066: * the schema.
067: *
068: * @return
069: */
070: boolean supportsSchemasInTableDefinition();
071:
072: /**
073: * Returns the string that should be appended to a column declaration when
074: * nulls are allowed in the column.
075: *
076: * @return the "nulls are allowed in this column" string for a table
077: * declaration
078: */
079: String getNullColumnString();
080:
081: /**
082: * Returns the name of the aggregate function that determines the max value
083: * of an expression.
084: *
085: * @return the name of the function to be applied to a set to determine the
086: * element with the highest numeric value.
087: */
088: String getMaxFunction();
089:
090: /**
091: * Returns the name of the function that measures the length of a character
092: * string.
093: *
094: * @param dataType the java.sql.Types data type. Some databases have
095: * different length functions for different data types.
096: *
097: * @return the name of the function to be applied to a column to determine
098: * the length of that column in a particular record.
099: */
100: String getLengthFunction(int dataType);
101:
102: /**
103: * Returns the maximum precision allowed by the database for number type
104: * fields that specify the length of the number to the left of the decimal
105: * point in digits. If the HibernateDialect implementation doesn't ever
106: * use $p in any call to registerColumnType(), then this maximum precsision
107: * will not be used.
108: *
109: * @param dataType the java.sql.Types data type.
110: *
111: * @return the maximum number that can be used in a column declaration for
112: * precision for the specified type.
113: */
114: int getMaxPrecision(int dataType);
115:
116: /**
117: * Returns the maximum scale allowed by the database for number type
118: * fields that specify the length of the number to the right of the decimal
119: * point in digits. If the HibernateDialect implementation doesn't ever
120: * use $s in any call to registerColumnType(), then this maximum scale
121: * will not be used.
122: *
123: * @param dataType the java.sql.Types data type.
124: *
125: * @return the maximum number that can be used in a column declaration for
126: * scale for the specified type.
127: */
128: int getMaxScale(int dataType);
129:
130: /**
131: * Returns the number of digits of precision is represented by the specifed
132: * columnSize for the specified dataType. Some DBs represent precision as
133: * the total number of digits on the right or left of the decimal. That is
134: * what we want. Others (like PostgreSQL) give the number of bytes of
135: * storage a column can use - less than useful, since the SQL-92 says
136: * "number of digits" and this is what most other DBs use.
137: *
138: * @param columnSize the size of the column as reported by the driver.
139: * @param dataType the java.sql.Types data type.
140: *
141: * @return a number indicating the total number of digits (includes both
142: * sides of the decimal point) the column can represent.
143: */
144: int getPrecisionDigits(int columnSize, int dataType);
145:
146: /**
147: * Some jdbc drivers are hopelessly broken with regard to reporting the
148: * COLUMN_SIZE. For example, MaxDB has a "long byte" data type which can
149: * store up to 2G of data, yet the driver reports that the column size is
150: * "8" - real helpful. So for drivers that have this problem, return the
151: * "proper" maximum column length for the specified dataType. If the driver
152: * doesn't have this problem, just return the columnSize.
153: *
154: * @param columnSize the size of the column as reported by the jdbc driver
155: * @param dataType the type of the column.
156: *
157: * @return the specified columnSize if the jdbc driver isn't broken;
158: * otherwise, the maximum column size for the specified dataType if
159: * the driver is broken.
160: */
161: int getColumnLength(int columnSize, int dataType);
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: boolean supportsProduct(String databaseProductName,
175: String databaseProductVersion);
176:
177: /**
178: * The string which identifies this dialect in the dialect chooser.
179: *
180: * @return a descriptive name that tells the user what database this dialect
181: * is design to work with.
182: */
183: String getDisplayName();
184:
185: /**
186: * Returns the SQL statement to use to add a column to the specified table
187: * using the information about the new column specified by info.
188: * @param info information about the new column such as type, name, etc.
189: *
190: * @return
191: * @throws UnsupportedOperationException if the database doesn't support
192: * adding columns after a table has already been created.
193: * @throws HibernateException if the type in the specified info isn't
194: * supported by this dialect.
195: */
196: String[] getColumnAddSQL(TableColumnInfo info)
197: throws HibernateException, UnsupportedOperationException;
198:
199: /**
200: * Returns a boolean value indicating whether or not this dialect supports
201: * adding comments to columns.
202: *
203: * @return true if column comments are supported; false otherwise.
204: */
205: boolean supportsColumnComment();
206:
207: /**
208: * Returns the SQL statement to use to add a comment to the specified
209: * column of the specified table.
210: * @param info information about the column such as type, name, etc.
211: * @return
212: * @throws UnsupportedOperationException if the database doesn't support
213: * annotating columns with a comment.
214: */
215: public String getColumnCommentAlterSQL(TableColumnInfo info)
216: throws UnsupportedOperationException;
217:
218: /**
219: * Returns a boolean value indicating whether or not this database dialect
220: * supports dropping columns from tables.
221: *
222: * @return true if the database supports dropping columns; false otherwise.
223: */
224: boolean supportsDropColumn();
225:
226: /**
227: * Returns a boolean value indicating whether or not this database dialect
228: * supports changing a column from null to not-null and vice versa.
229: *
230: * @return true if the database supports dropping columns; false otherwise.
231: */
232: boolean supportsAlterColumnNull();
233:
234: /**
235: * Returns the SQL that forms the command to drop the specified colum in the
236: * specified table.
237: *
238: * @param tableName the name of the table that has the column
239: * @param columnName the name of the column to drop.
240: * @return
241: * @throw UnsupportedOperationException if the database doesn't support
242: * dropping columns.
243: */
244: String getColumnDropSQL(String tableName, String columnName)
245: throws UnsupportedOperationException;
246:
247: /**
248: * Returns the SQL that forms the command to drop the specified table. If
249: * cascade contraints is supported by the dialect and cascadeConstraints is
250: * true, then a drop statement with cascade constraints clause will be
251: * formed.
252: *
253: * @param iTableInfo the table to drop
254: * @param cascadeConstraints whether or not to drop any FKs that may
255: * reference the specified table.
256: * @param isMaterializedView TODO
257: * @return the drop SQL command.
258: */
259: List<String> getTableDropSQL(ITableInfo iTableInfo,
260: boolean cascadeConstraints, boolean isMaterializedView);
261:
262: /**
263: * Returns the SQL that forms the command to add a primary key to the
264: * specified table composed of the given column names.
265: *
266: * @param pkName the name of the constraint
267: * @param ti TODO
268: * @param columnNames the columns that form the key
269: * @return
270: */
271: String[] getAddPrimaryKeySQL(String pkName,
272: TableColumnInfo[] colInfos, ITableInfo ti);
273:
274: /**
275: * Returns the SQL fragment for adding a column in an alter table statment.
276: * @return
277: */
278: String getAddColumnString();
279:
280: /**
281: * Returns the SQL used to alter the nullability of the specified column
282: *
283: * @param info the column to modify
284: * @return the SQL to execute
285: */
286: String getColumnNullableAlterSQL(TableColumnInfo info);
287:
288: /**
289: * Returns a boolean value indicating whether or not this database dialect
290: * supports renaming columns.
291: *
292: * @return true if the database supports changing the name of columns;
293: * false otherwise.
294: */
295: boolean supportsRenameColumn();
296:
297: /**
298: * Returns the SQL that is used to change the column name.
299: *
300: * @param from the TableColumnInfo as it is
301: * @param to the TableColumnInfo as it wants to be
302: *
303: * @return the SQL to make the change
304: */
305: String getColumnNameAlterSQL(TableColumnInfo from,
306: TableColumnInfo to);
307:
308: /**
309: * Returns a boolean value indicating whether or not this dialect supports
310: * modifying a columns type.
311: *
312: * @return true if supported; false otherwise
313: */
314: boolean supportsAlterColumnType();
315:
316: /**
317: * Returns the SQL that is used to change the column type.
318: *
319: * @param from the TableColumnInfo as it is
320: * @param to the TableColumnInfo as it wants to be
321: *
322: * @return the SQL to make the change
323: * @throw UnsupportedOperationException if the database doesn't support
324: * modifying column types.
325: */
326: List<String> getColumnTypeAlterSQL(TableColumnInfo from,
327: TableColumnInfo to) throws UnsupportedOperationException;
328:
329: /**
330: * Returns a boolean value indicating whether or not this database dialect
331: * supports changing a column's default value.
332: *
333: * @return true if the database supports modifying column defaults; false
334: * otherwise
335: */
336: boolean supportsAlterColumnDefault();
337:
338: /**
339: * Returns the SQL command to change the specified column's default value
340: *
341: * @param info the column to modify and it's default value.
342: * @return SQL to make the change
343: */
344: String getColumnDefaultAlterSQL(TableColumnInfo info);
345:
346: /**
347: * Returns the SQL command to drop the specified table's primary key.
348: *
349: * @param pkName the name of the primary key that should be dropped
350: * @param tableName the name of the table whose primary key should be
351: * dropped
352: * @return
353: */
354: String getDropPrimaryKeySQL(String pkName, String tableName);
355:
356: /**
357: * Returns the SQL command to drop the specified table's foreign key
358: * constraint.
359: *
360: * @param fkName the name of the foreign key that should be dropped
361: * @param tableName the name of the table whose foreign key should be
362: * dropped
363: * @return
364: */
365: String getDropForeignKeySQL(String fkName, String tableName);
366:
367: /**
368: * Returns the SQL command to create the specified table.
369: *
370: * @param tables the tables to get create statements for
371: * @param md the metadata from the ISession
372: * @param prefs preferences about how the resultant SQL commands should be
373: * formed.
374: * @param isJdbcOdbc whether or not the connection is via JDBC-ODBC bridge.
375: *
376: * @return the SQL that is used to create the specified table
377: */
378: List<String> getCreateTableSQL(List<ITableInfo> tables,
379: ISQLDatabaseMetaData md, CreateScriptPreferences prefs,
380: boolean isJdbcOdbc) throws SQLException;
381:
382: }
|