Class SqlBuilder
java.lang.Object
edu.internet2.middleware.grouper.ext.org.apache.ddlutils.platform.SqlBuilder
- Direct Known Subclasses:
AxionBuilder
,CloudscapeBuilder
,Db2Builder
,FirebirdBuilder
,HsqlDbBuilder
,InterbaseBuilder
,MckoiBuilder
,MSSqlBuilder
,MySqlBuilder
,Oracle8Builder
,PostgreSqlBuilder
,SapDbBuilder
,SybaseBuilder
This class is a collection of Strategy methods for creating the DDL required to create and drop
databases and tables.
It is hoped that just a single implementation of this class, for each database should make creating DDL
for each physical database fairly straightforward.
An implementation of this class can always delegate down to some templating technology such as Velocity if
it requires. Though often that can be quite complex when attempting to reuse code across many databases.
Hopefully only a small amount code needs to be changed on a per database basis.
- Version:
- $Revision: 518498 $
-
Field Summary
Modifier and TypeFieldDescriptionprotected final org.apache.commons.logging.Log
The Log to which logging calls will be made.protected static final String
The placeholder for the size value in the native type spec. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
addEscapedCharSequence
(String charSequence, String escapedVersion) Adds a char sequence that needs escaping, and its escaped version.void
alterDatabase
(Database currentModel, Database desiredModel, CreationParameters params) Generates the DDL to modify an existing database so the schema matches the specified database schema by using drops, modifications and additions.protected void
applyForSelectedChanges
(Collection changes, Class[] changeTypes, org.apache.commons.collections.Closure closure) Calls the given closure for all changes that are of one of the given types, and then removes them from the changes collection.protected boolean
Compares the two strings.protected boolean
columnsDiffer
(Column currentColumn, Column desiredColumn) Compares the current column in the database with the desired one.void
createExternalForeignKeys
(Database database) Creates the external foreignkey creation statements for all tables in the database.void
createExternalForeignKeys
(Database database, Table table) Creates external foreignkey creation statements if necessary.void
createTable
(Database database, Table table) Outputs the DDL to create the table along with any non-external constraints as well as with external primary keys and indices (but not foreign keys).void
createTable
(Database database, Table table, Map parameters) Outputs the DDL to create the table along with any non-external constraints as well as with external primary keys and indices (but not foreign keys).void
createTables
(Database database) Outputs the DDL required to drop and (re)create all tables in the database model.void
createTables
(Database database, boolean dropTables) Outputs the DDL required to drop (if requested) and (re)create all tables in the database model.void
createTables
(Database database, CreationParameters params, boolean dropTables) Outputs the DDL required to drop (if requested) and (re)create all tables in the database model.protected void
createTemporaryTable
(Database database, Table table, Map parameters) Outputs the DDL to create the given temporary table.protected String
Creates a reasonably unique identifier only consisting of hexadecimal characters and underscores.void
dropExternalForeignKeys
(Table table) Creates external foreignkey drop statements.void
Outputs the DDL required to drop the given table.void
Outputs the DDL to drop the table.void
dropTables
(Database database) Outputs the DDL required to drop the database.protected void
dropTemporaryTable
(Database database, Table table) Outputs the DDL to drop the given temporary table.protected String
escapeStringValue
(String value) Escapes the necessary characters in given string value.protected ForeignKey
findCorrespondingForeignKey
(Table table, ForeignKey fk) Searches in the given table for a corresponding foreign key.protected String
getBareNativeType
(Column column) Returns the bare database-native type for the given column without any size specifies.protected String
getColumnName
(Column column) Returns the column name.getConstraintName
(String prefix, Table table, String secondPart, String suffix) Returns the constraint name.Returns the default value helper.getDeleteSql
(Table table, Map pkValues, boolean genPlaceholders) Creates the SQL for deleting an object from the specified table.protected String
getDelimitedIdentifier
(String identifier) Returns the delimited version of the identifier (if configured).getForeignKeyName
(Table table, ForeignKey fk) Returns the name to be used for the given foreign key.Returns the string used to indent the SQL.getIndexName
(Index index) Returns the index name.getInsertSql
(Table table, Map columnValues, boolean genPlaceholders) Creates the SQL for inserting an object into the specified table.int
Returns the maximum number of characters that a column name can have.int
Returns the maximum number of characters that a constraint name can have.int
Returns the maximum number of characters that a foreign key name can have.int
Returns the maximum number of characters that a table name can have.protected String
getNativeDefaultValue
(Column column) Returns the native default value for the column.protected String
getNativeType
(Column column) Returns the database-native type for the given column.Returns the platform object.Returns the platform info object.protected Table
getRealTargetTableFor
(Database targetModel, Table sourceTable, Table targetTable) Creates the target table object that differs from the given target table only in the indices.getSelectLastIdentityValues
(Table table) Generates the SQL for querying the id that was created in the last insertion operation.protected String
getSqlType
(Column column) Returns the full SQL type specification (including size and precision/scale) for the given column.getTableName
(Table table) Returns the table name.protected Table
getTemporaryTableFor
(Database targetModel, Table targetTable) Creates a temporary table object that corresponds to the given table.getUpdateSql
(Table table, Map columnValues, boolean genPlaceholders) Creates the SQL for updating an object in the specified table.protected String
getValueAsString
(Column column, Object value) Generates the string representation of the given value.protected DateFormat
Returns the format object for formatting dates in the specified locale.Returns the locale that is used for number and date formatting (when printing default values and in generates insert/update/delete statements).protected NumberFormat
Returns the format object for formatting numbers in the specified locale.protected DateFormat
Returns the format object for formatting times in the specified locale.Returns the writer that the DDL is printed to.protected boolean
isValidDefaultValue
(String defaultSpec, int typeCode) Determines whether the given default spec is a non-empty spec that shall be used in a DEFAULT expression.protected void
Prints some text.protected void
printComment
(String text) Prints an SQL comment to the current stream.protected void
printDefaultValue
(Object defaultValue, int typeCode) Prints the default value of the column.protected void
Prints the end of statement text, which is typically a semi colon followed by a carriage return.protected void
printIdentifier
(String identifier) Prints the given identifier.protected void
Prints the characters used to indent SQL.protected void
println()
Prints a newline.protected void
Prints some text followed by a newline.protected void
printlnIdentifier
(String identifier) Prints the given identifier followed by a newline.protected void
Prints the start of an embedded statement.protected void
processChange
(Database currentModel, Database desiredModel, AddPrimaryKeyChange change) Processes the addition of a primary key to a table.protected void
processChange
(Database currentModel, Database desiredModel, CreationParameters params, AddForeignKeyChange change) Processes the change representing the addition of a foreign key.protected void
processChange
(Database currentModel, Database desiredModel, CreationParameters params, AddIndexChange change) Processes the change representing the addition of an index.protected void
processChange
(Database currentModel, Database desiredModel, CreationParameters params, AddTableChange change) Processes the change representing the addition of a table.protected void
processChange
(Database currentModel, Database desiredModel, CreationParameters params, ModelChange change) This is a fall-through callback which generates a warning because a specific change type wasn't handled.protected void
processChange
(Database currentModel, Database desiredModel, CreationParameters params, RemoveForeignKeyChange change) Processes the change representing the removal of a foreign key.protected void
processChange
(Database currentModel, Database desiredModel, CreationParameters params, RemoveIndexChange change) Processes the change representing the removal of an index.protected void
processChange
(Database currentModel, Database desiredModel, CreationParameters params, RemoveTableChange change) Processes the change representing the removal of a table.protected void
processChanges
(Database currentModel, Database desiredModel, List changes, CreationParameters params) Processes the changes.protected void
processTableStructureChanges
(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, Map parameters, List changes) Allows database-specific implementations to handle changes in a database specific manner.protected void
processTableStructureChanges
(Database currentModel, Database desiredModel, CreationParameters params, Collection changes) Processes the changes to the structure of tables.protected void
processTableStructureChanges
(Database currentModel, Database desiredModel, String tableName, Map parameters, List changes) Processes the changes to the structure of a single table.void
Sets the string used to indent the SQL.protected void
setValueDateFormat
(DateFormat format) Sets the format object for formatting dates in the specified locale.void
setValueLocale
(String localeStr) Sets the locale that is used for number and date formatting (when printing default values and in generates insert/update/delete statements).protected void
setValueNumberFormat
(NumberFormat format) Returns a new date format object for formatting numbers in the specified locale.protected void
setValueTimeFormat
(DateFormat format) Sets the date format object for formatting times in the specified locale.void
Sets the writer for printing the DDL to.shortenName
(String name, int desiredLength) Generates a version of the name that has at most the specified length.protected boolean
shouldGeneratePrimaryKeys
(Column[] primaryKeyColumns) Determines whether we should generate a primary key constraint for the given primary key columns.protected void
writeCastExpression
(Column sourceColumn, Column targetColumn) Writes a cast expression that converts the value of the source column to the data type of the target column.protected void
writeColumn
(Table table, Column column) Outputs the DDL for the specified column.protected void
writeColumnAutoIncrementStmt
(Table table, Column column) Prints that the column is an auto increment column.protected void
writeColumnDefaultValue
(Table table, Column column) Prints the default value of the column.protected void
writeColumnDefaultValueStmt
(Table table, Column column) Prints the default value stmt part for the column.protected void
Prints that a column is not nullable.protected void
Prints that a column is nullable.protected void
writeColumns
(Table table) Writes the columns of the given table.protected void
writeCopyDataStatement
(Table sourceTable, Table targetTable) Writes a statement that copies the data from the source to the target table.protected void
writeEmbeddedForeignKeysStmt
(Database database, Table table) Writes the foreign key constraints inside a create table () clause.protected void
writeEmbeddedIndexCreateStmt
(Table table, Index index) Writes the given embedded index of the table.protected void
writeEmbeddedIndicesStmt
(Table table) Writes the indexes embedded within the create table statement.protected void
Writes the primary key constraints of the table inside its definition.protected void
writeExternalForeignKeyCreateStmt
(Database database, Table table, ForeignKey key) Writes a single foreign key constraint using a alter table statement.protected void
writeExternalForeignKeyDropStmt
(Table table, ForeignKey foreignKey) Generates the statement to drop a foreignkey constraint from the database using an alter table statement.protected void
writeExternalIndexCreateStmt
(Table table, Index index) Writes the given index of the table.void
writeExternalIndexDropStmt
(Table table, Index index) Generates the statement to drop a non-embedded index from the database.protected void
Writes the indexes of the given table.protected void
writeExternalPrimaryKeysCreateStmt
(Table table, Column[] primaryKeyColumns) Writes the primary key constraints of the table as alter table statements.protected void
Writes a list of foreign references for the given foreign key.protected void
Writes a list of local references for the given foreign key.protected void
writePrimaryKeyStmt
(Table table, Column[] primaryKeyColumns) Writes a primary key statement for the given columns.protected void
writeTableAlterStmt
(Table table) Generates the first part of the ALTER TABLE statement including the table name.protected void
writeTableComment
(Table table) Outputs a comment for the table.protected void
writeTableCreationStmt
(Database database, Table table, Map parameters) Writes the table creation statement without the statement end.protected void
writeTableCreationStmtEnding
(Table table, Map parameters) Writes the end of the table creation statement.
-
Field Details
-
SIZE_PLACEHOLDER
The placeholder for the size value in the native type spec.- See Also:
-
_log
protected final org.apache.commons.logging.Log _logThe Log to which logging calls will be made.
-
-
Constructor Details
-
SqlBuilder
Creates a new sql builder.- Parameters:
platform
- The plaftform this builder belongs to
-
-
Method Details
-
getPlatform
Returns the platform object.- Returns:
- The platform
-
getPlatformInfo
Returns the platform info object.- Returns:
- The info object
-
getWriter
Returns the writer that the DDL is printed to.- Returns:
- The writer
-
setWriter
Sets the writer for printing the DDL to.- Parameters:
writer
- The writer
-
getDefaultValueHelper
Returns the default value helper.- Returns:
- The default value helper
-
getIndent
Returns the string used to indent the SQL.- Returns:
- The indentation string
-
setIndent
Sets the string used to indent the SQL.- Parameters:
indent
- The indentation string
-
getValueLocale
Returns the locale that is used for number and date formatting (when printing default values and in generates insert/update/delete statements).- Returns:
- The locale or
null
if default formatting is used
-
setValueLocale
Sets the locale that is used for number and date formatting (when printing default values and in generates insert/update/delete statements).- Parameters:
localeStr
- The new locale ornull
if default formatting should be used; Format is "language[_country[_variant]]"
-
getValueDateFormat
Returns the format object for formatting dates in the specified locale.- Returns:
- The date format object or null if no locale is set
-
setValueDateFormat
Sets the format object for formatting dates in the specified locale.- Parameters:
format
- The date format object
-
getValueTimeFormat
Returns the format object for formatting times in the specified locale.- Returns:
- The time format object or null if no locale is set
-
setValueTimeFormat
Sets the date format object for formatting times in the specified locale.- Parameters:
format
- The time format object
-
getValueNumberFormat
Returns the format object for formatting numbers in the specified locale.- Returns:
- The number format object or null if no locale is set
-
setValueNumberFormat
Returns a new date format object for formatting numbers in the specified locale. Platforms can override this if necessary.- Parameters:
format
- The number format object
-
addEscapedCharSequence
Adds a char sequence that needs escaping, and its escaped version.- Parameters:
charSequence
- The char sequenceescapedVersion
- The escaped version
-
getMaxTableNameLength
public int getMaxTableNameLength()Returns the maximum number of characters that a table name can have. This method is intended to give platform specific builder implementations more control over the maximum length.- Returns:
- The number of characters, or -1 if not limited
-
getMaxColumnNameLength
public int getMaxColumnNameLength()Returns the maximum number of characters that a column name can have. This method is intended to give platform specific builder implementations more control over the maximum length.- Returns:
- The number of characters, or -1 if not limited
-
getMaxConstraintNameLength
public int getMaxConstraintNameLength()Returns the maximum number of characters that a constraint name can have. This method is intended to give platform specific builder implementations more control over the maximum length.- Returns:
- The number of characters, or -1 if not limited
-
getMaxForeignKeyNameLength
public int getMaxForeignKeyNameLength()Returns the maximum number of characters that a foreign key name can have. This method is intended to give platform specific builder implementations more control over the maximum length.- Returns:
- The number of characters, or -1 if not limited
-
createTables
Outputs the DDL required to drop and (re)create all tables in the database model.- Parameters:
database
- The database model- Throws:
IOException
-
createTables
Outputs the DDL required to drop (if requested) and (re)create all tables in the database model.- Parameters:
database
- The databasedropTables
- Whether to drop tables before creating them- Throws:
IOException
-
createTables
public void createTables(Database database, CreationParameters params, boolean dropTables) throws IOException Outputs the DDL required to drop (if requested) and (re)create all tables in the database model.- Parameters:
database
- The databaseparams
- The parameters used in the creationdropTables
- Whether to drop tables before creating them- Throws:
IOException
-
alterDatabase
public void alterDatabase(Database currentModel, Database desiredModel, CreationParameters params) throws IOException Generates the DDL to modify an existing database so the schema matches the specified database schema by using drops, modifications and additions. Database-specific implementations can change aspect of this algorithm by redefining the individual methods that compromise it.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemaparams
- The parameters used in the creation of new tables. Note that for existing tables, the parameters won't be applied- Throws:
IOException
-
applyForSelectedChanges
protected void applyForSelectedChanges(Collection changes, Class[] changeTypes, org.apache.commons.collections.Closure closure) Calls the given closure for all changes that are of one of the given types, and then removes them from the changes collection.- Parameters:
changes
- The changeschangeTypes
- The types to search forclosure
- The closure to invoke
-
processChanges
protected void processChanges(Database currentModel, Database desiredModel, List changes, CreationParameters params) throws IOException Processes the changes. The default argument performs several passes:org.apache.ddlutils.alteration.RemoveForeignKeyChange
andorg.apache.ddlutils.alteration.RemoveIndexChange
come first to allow for e.g. subsequent primary key changes or column removal.org.apache.ddlutils.alteration.RemoveTableChange
comes after the removal of foreign keys and indices.- These are all handled together:
org.apache.ddlutils.alteration.RemovePrimaryKeyChange
org.apache.ddlutils.alteration.AddPrimaryKeyChange
org.apache.ddlutils.alteration.PrimaryKeyChange
org.apache.ddlutils.alteration.RemoveColumnChange
org.apache.ddlutils.alteration.AddColumnChange
org.apache.ddlutils.alteration.ColumnAutoIncrementChange
org.apache.ddlutils.alteration.ColumnDefaultValueChange
org.apache.ddlutils.alteration.ColumnRequiredChange
org.apache.ddlutils.alteration.ColumnDataTypeChange
org.apache.ddlutils.alteration.ColumnSizeChange
The reason for this is that the default algorithm rebuilds the table for these changes and thus their order is irrelevant. org.apache.ddlutils.alteration.AddTableChange
needs to come after the table removal (so that tables of the same name are removed) and before the addition of foreign keys etc.org.apache.ddlutils.alteration.AddForeignKeyChange
andorg.apache.ddlutils.alteration.AddIndexChange
come last after table/column/primary key additions or changes.
- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemachanges
- The changesparams
- The parameters used in the creation of new tables. Note that for existing tables, the parameters won't be applied- Throws:
IOException
-
processChange
protected void processChange(Database currentModel, Database desiredModel, CreationParameters params, ModelChange change) throws IOException This is a fall-through callback which generates a warning because a specific change type wasn't handled.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemaparams
- The parameters used in the creation of new tables. Note that for existing tables, the parameters won't be appliedchange
- The change object- Throws:
IOException
-
processChange
protected void processChange(Database currentModel, Database desiredModel, CreationParameters params, RemoveForeignKeyChange change) throws IOException Processes the change representing the removal of a foreign key.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemaparams
- The parameters used in the creation of new tables. Note that for existing tables, the parameters won't be appliedchange
- The change object- Throws:
IOException
-
processChange
protected void processChange(Database currentModel, Database desiredModel, CreationParameters params, RemoveIndexChange change) throws IOException Processes the change representing the removal of an index.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemaparams
- The parameters used in the creation of new tables. Note that for existing tables, the parameters won't be appliedchange
- The change object- Throws:
IOException
-
processChange
protected void processChange(Database currentModel, Database desiredModel, CreationParameters params, RemoveTableChange change) throws IOException Processes the change representing the removal of a table.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemaparams
- The parameters used in the creation of new tables. Note that for existing tables, the parameters won't be appliedchange
- The change object- Throws:
IOException
-
processChange
protected void processChange(Database currentModel, Database desiredModel, CreationParameters params, AddTableChange change) throws IOException Processes the change representing the addition of a table.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemaparams
- The parameters used in the creation of new tables. Note that for existing tables, the parameters won't be appliedchange
- The change object- Throws:
IOException
-
processChange
protected void processChange(Database currentModel, Database desiredModel, CreationParameters params, AddForeignKeyChange change) throws IOException Processes the change representing the addition of a foreign key.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemaparams
- The parameters used in the creation of new tables. Note that for existing tables, the parameters won't be appliedchange
- The change object- Throws:
IOException
-
processChange
protected void processChange(Database currentModel, Database desiredModel, CreationParameters params, AddIndexChange change) throws IOException Processes the change representing the addition of an index.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemaparams
- The parameters used in the creation of new tables. Note that for existing tables, the parameters won't be appliedchange
- The change object- Throws:
IOException
-
processTableStructureChanges
protected void processTableStructureChanges(Database currentModel, Database desiredModel, CreationParameters params, Collection changes) throws IOException Processes the changes to the structure of tables.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemaparams
- The parameters used in the creation of new tables. Note that for existing tables, the parameters won't be appliedchanges
- The change objects- Throws:
IOException
-
processTableStructureChanges
protected void processTableStructureChanges(Database currentModel, Database desiredModel, String tableName, Map parameters, List changes) throws IOException Processes the changes to the structure of a single table. Database-specific implementations might redefine this method, but it is usually sufficient to redefine theprocessTableStructureChanges(Database, Database, Table, Table, Map, List)
method instead.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schematableName
- The name of the changed tableparameters
- The creation parameters for the desired tablechanges
- The change objects for this table- Throws:
IOException
-
processTableStructureChanges
protected void processTableStructureChanges(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, Map parameters, List changes) throws IOException Allows database-specific implementations to handle changes in a database specific manner. Any handled change should be applied to the given current model (which is a copy of the real original model) and be removed from the list of changes.
In the default implementation, allAddPrimaryKeyChange
changes are applied via anALTER TABLE ADD CONSTRAINT
statement.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemasourceTable
- The original tabletargetTable
- The desired tableparameters
- The creation parameters for the tablechanges
- The change objects for the target table- Throws:
IOException
-
getTemporaryTableFor
Creates a temporary table object that corresponds to the given table. Database-specific implementations may redefine this method if e.g. the database directly supports temporary tables. The default implementation simply appends an underscore to the table name and uses that as the table name.- Parameters:
targetModel
- The target databasetargetTable
- The target table- Returns:
- The temporary table
- Throws:
IOException
-
createTemporaryTable
protected void createTemporaryTable(Database database, Table table, Map parameters) throws IOException Outputs the DDL to create the given temporary table. Per default this is simply a call tocreateTable(Database, Table, Map)
.- Parameters:
database
- The database modeltable
- The tableparameters
- Additional platform-specific parameters for the table creation- Throws:
IOException
-
dropTemporaryTable
Outputs the DDL to drop the given temporary table. Per default this is simply a call todropTable(Table)
.- Parameters:
database
- The database modeltable
- The table- Throws:
IOException
-
getRealTargetTableFor
protected Table getRealTargetTableFor(Database targetModel, Table sourceTable, Table targetTable) throws IOException Creates the target table object that differs from the given target table only in the indices. More specifically, only those indices are used that have not changed.- Parameters:
targetModel
- The target databasesourceTable
- The source tabletargetTable
- The target table- Returns:
- The table
- Throws:
IOException
-
writeCopyDataStatement
Writes a statement that copies the data from the source to the target table. Note that this copies only those columns that are in both tables. Database-specific implementations might redefine this method though they usually it suffices to redefine thewriteCastExpression(Column, Column)
method.- Parameters:
sourceTable
- The source tabletargetTable
- The target table- Throws:
IOException
-
writeCastExpression
Writes a cast expression that converts the value of the source column to the data type of the target column. Per default, simply the name of the source column is written thereby assuming that any casts happen implicitly.- Parameters:
sourceColumn
- The source columntargetColumn
- The target column- Throws:
IOException
-
processChange
protected void processChange(Database currentModel, Database desiredModel, AddPrimaryKeyChange change) throws IOException Processes the addition of a primary key to a table.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemachange
- The change object- Throws:
IOException
-
findCorrespondingForeignKey
Searches in the given table for a corresponding foreign key. If the given key has no name, then a foreign key to the same table with the same columns in the same order is searched. If the given key has a name, then the a corresponding key also needs to have the same name, or no name at all, but not a different one.- Parameters:
table
- The table to search infk
- The original foreign key- Returns:
- The corresponding foreign key if found
-
areEqual
Compares the two strings.- Parameters:
string1
- The first stringstring2
- The second stringcaseMatters
- Whether case matters in the comparison- Returns:
true
if the string are equal
-
createTable
Outputs the DDL to create the table along with any non-external constraints as well as with external primary keys and indices (but not foreign keys).- Parameters:
database
- The database modeltable
- The table- Throws:
IOException
-
createTable
Outputs the DDL to create the table along with any non-external constraints as well as with external primary keys and indices (but not foreign keys).- Parameters:
database
- The database modeltable
- The tableparameters
- Additional platform-specific parameters for the table creation- Throws:
IOException
-
createExternalForeignKeys
Creates the external foreignkey creation statements for all tables in the database.- Parameters:
database
- The database- Throws:
IOException
-
createExternalForeignKeys
Creates external foreignkey creation statements if necessary.- Parameters:
database
- The database modeltable
- The table- Throws:
IOException
-
dropTables
Outputs the DDL required to drop the database.- Parameters:
database
- The database- Throws:
IOException
-
dropTable
Outputs the DDL required to drop the given table. This method also drops foreign keys to the table.- Parameters:
database
- The databasetable
- The table- Throws:
IOException
-
dropTable
Outputs the DDL to drop the table. Note that this method does not drop foreign keys to this table. UsedropTable(Database, Table)
if you want that.- Parameters:
table
- The table to drop- Throws:
IOException
-
dropExternalForeignKeys
Creates external foreignkey drop statements.- Parameters:
table
- The table- Throws:
IOException
-
getInsertSql
Creates the SQL for inserting an object into the specified table. If values are given then a concrete insert statement is created, otherwise an insert statement usable in a prepared statement is build.- Parameters:
table
- The tablecolumnValues
- The columns values indexed by the column namesgenPlaceholders
- Whether to generate value placeholders for a prepared statement- Returns:
- The insertion sql
-
getUpdateSql
Creates the SQL for updating an object in the specified table. If values are given then a concrete update statement is created, otherwise an update statement usable in a prepared statement is build.- Parameters:
table
- The tablecolumnValues
- Contains the values for the columns to update, and should also contain the primary key values to identify the object to update in casegenPlaceholders
isfalse
genPlaceholders
- Whether to generate value placeholders for a prepared statement (both for the pk values and the object values)- Returns:
- The update sql
-
getDeleteSql
Creates the SQL for deleting an object from the specified table. If values are given then a concrete delete statement is created, otherwise an delete statement usable in a prepared statement is build.- Parameters:
table
- The tablepkValues
- The primary key values indexed by the column names, can be emptygenPlaceholders
- Whether to generate value placeholders for a prepared statement- Returns:
- The delete sql
-
getValueAsString
Generates the string representation of the given value.- Parameters:
column
- The columnvalue
- The value- Returns:
- The string representation
-
getSelectLastIdentityValues
Generates the SQL for querying the id that was created in the last insertion operation. This is obviously only useful for pk fields that are auto-incrementing. A database that does not support this, will returnnull
.- Parameters:
table
- The table- Returns:
- The sql, or
null
if the database does not support this
-
shortenName
Generates a version of the name that has at most the specified length.- Parameters:
name
- The original namedesiredLength
- The desired maximum length- Returns:
- The shortened version
-
getTableName
Returns the table name. This method takes care of length limitations imposed by some databases.- Parameters:
table
- The table- Returns:
- The table name
-
writeTableComment
Outputs a comment for the table.- Parameters:
table
- The table- Throws:
IOException
-
writeTableAlterStmt
Generates the first part of the ALTER TABLE statement including the table name.- Parameters:
table
- The table being altered- Throws:
IOException
-
writeTableCreationStmt
protected void writeTableCreationStmt(Database database, Table table, Map parameters) throws IOException Writes the table creation statement without the statement end.- Parameters:
database
- The modeltable
- The tableparameters
- Additional platform-specific parameters for the table creation- Throws:
IOException
-
writeTableCreationStmtEnding
Writes the end of the table creation statement. Per default, only the end of the statement is written, but this can be changed in subclasses.- Parameters:
table
- The tableparameters
- Additional platform-specific parameters for the table creation- Throws:
IOException
-
writeColumns
Writes the columns of the given table.- Parameters:
table
- The table- Throws:
IOException
-
getColumnName
Returns the column name. This method takes care of length limitations imposed by some databases.- Parameters:
column
- The column- Returns:
- The column name
- Throws:
IOException
-
writeColumn
Outputs the DDL for the specified column.- Parameters:
table
- The table containing the columncolumn
- The column- Throws:
IOException
-
getSqlType
Returns the full SQL type specification (including size and precision/scale) for the given column.- Parameters:
column
- The column- Returns:
- The full SQL type string including the size
-
getNativeType
Returns the database-native type for the given column.- Parameters:
column
- The column- Returns:
- The native type
-
getBareNativeType
Returns the bare database-native type for the given column without any size specifies.- Parameters:
column
- The column- Returns:
- The native type
-
getNativeDefaultValue
Returns the native default value for the column.- Parameters:
column
- The column- Returns:
- The native default value
-
escapeStringValue
Escapes the necessary characters in given string value.- Parameters:
value
- The value- Returns:
- The corresponding string with the special characters properly escaped
-
isValidDefaultValue
Determines whether the given default spec is a non-empty spec that shall be used in a DEFAULT expression. E.g. if the spec is an empty string and the type is a numeric type, then it is no valid default value whereas if it is a string type, then it is valid.- Parameters:
defaultSpec
- The default value spectypeCode
- The JDBC type code- Returns:
true
if the default value spec is valid
-
writeColumnDefaultValueStmt
Prints the default value stmt part for the column.- Parameters:
table
- The tablecolumn
- The column- Throws:
IOException
-
writeColumnDefaultValue
Prints the default value of the column.- Parameters:
table
- The tablecolumn
- The column- Throws:
IOException
-
printDefaultValue
Prints the default value of the column.- Parameters:
defaultValue
- The default valuetypeCode
- The type code to write the default value for- Throws:
IOException
-
writeColumnAutoIncrementStmt
Prints that the column is an auto increment column.- Parameters:
table
- The tablecolumn
- The column- Throws:
IOException
-
writeColumnNullableStmt
Prints that a column is nullable.- Throws:
IOException
-
writeColumnNotNullableStmt
Prints that a column is not nullable.- Throws:
IOException
-
columnsDiffer
Compares the current column in the database with the desired one. Type, nullability, size, scale, default value, and precision radix are the attributes checked. Currently default values are compared, and null and empty string are considered equal.- Parameters:
currentColumn
- The current column as it is in the databasedesiredColumn
- The desired column- Returns:
true
if the column specifications differ
-
getForeignKeyName
Returns the name to be used for the given foreign key. If the foreign key has no specified name, this method determines a unique name for it. The name will also be shortened to honor the maximum identifier length imposed by the platform.- Parameters:
table
- The table for whith the foreign key is definedfk
- The foreign key- Returns:
- The name
-
getConstraintName
Returns the constraint name. This method takes care of length limitations imposed by some databases.- Parameters:
prefix
- The constraint prefix, can benull
table
- The table that the constraint belongs tosecondPart
- The second name part, e.g. the name of the constraint columnsuffix
- The constraint suffix, e.g. a counter (can benull
)- Returns:
- The constraint name
-
writeEmbeddedPrimaryKeysStmt
Writes the primary key constraints of the table inside its definition.- Parameters:
table
- The table- Throws:
IOException
-
writeExternalPrimaryKeysCreateStmt
protected void writeExternalPrimaryKeysCreateStmt(Table table, Column[] primaryKeyColumns) throws IOException Writes the primary key constraints of the table as alter table statements.- Parameters:
table
- The tableprimaryKeyColumns
- The primary key columns- Throws:
IOException
-
shouldGeneratePrimaryKeys
Determines whether we should generate a primary key constraint for the given primary key columns.- Parameters:
primaryKeyColumns
- The pk columns- Returns:
true
if a pk statement should be generated for the columns
-
writePrimaryKeyStmt
Writes a primary key statement for the given columns.- Parameters:
table
- The tableprimaryKeyColumns
- The primary columns- Throws:
IOException
-
getIndexName
Returns the index name. This method takes care of length limitations imposed by some databases.- Parameters:
index
- The index- Returns:
- The index name
-
writeExternalIndicesCreateStmt
Writes the indexes of the given table.- Parameters:
table
- The table- Throws:
IOException
-
writeEmbeddedIndicesStmt
Writes the indexes embedded within the create table statement.- Parameters:
table
- The table- Throws:
IOException
-
writeExternalIndexCreateStmt
Writes the given index of the table.- Parameters:
table
- The tableindex
- The index- Throws:
IOException
-
writeEmbeddedIndexCreateStmt
Writes the given embedded index of the table.- Parameters:
table
- The tableindex
- The index- Throws:
IOException
-
writeExternalIndexDropStmt
Generates the statement to drop a non-embedded index from the database.- Parameters:
table
- The table the index is onindex
- The index to drop- Throws:
IOException
-
writeEmbeddedForeignKeysStmt
Writes the foreign key constraints inside a create table () clause.- Parameters:
database
- The database modeltable
- The table- Throws:
IOException
-
writeExternalForeignKeyCreateStmt
protected void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key) throws IOException Writes a single foreign key constraint using a alter table statement.- Parameters:
database
- The database modeltable
- The tablekey
- The foreign key- Throws:
IOException
-
writeLocalReferences
Writes a list of local references for the given foreign key.- Parameters:
key
- The foreign key- Throws:
IOException
-
writeForeignReferences
Writes a list of foreign references for the given foreign key.- Parameters:
key
- The foreign key- Throws:
IOException
-
writeExternalForeignKeyDropStmt
protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey) throws IOException Generates the statement to drop a foreignkey constraint from the database using an alter table statement.- Parameters:
table
- The tableforeignKey
- The foreign key- Throws:
IOException
-
printComment
Prints an SQL comment to the current stream.- Parameters:
text
- The comment text- Throws:
IOException
-
printStartOfEmbeddedStatement
Prints the start of an embedded statement.- Throws:
IOException
-
printEndOfStatement
Prints the end of statement text, which is typically a semi colon followed by a carriage return.- Throws:
IOException
-
println
Prints a newline.- Throws:
IOException
-
print
Prints some text.- Parameters:
text
- The text to print- Throws:
IOException
-
getDelimitedIdentifier
Returns the delimited version of the identifier (if configured).- Parameters:
identifier
- The identifier- Returns:
- The delimited version of the identifier unless the platform is configured to use undelimited identifiers; in that case, the identifier is returned unchanged
-
printIdentifier
Prints the given identifier. For most databases, this will be a delimited identifier.- Parameters:
identifier
- The identifier- Throws:
IOException
-
printlnIdentifier
Prints the given identifier followed by a newline. For most databases, this will be a delimited identifier.- Parameters:
identifier
- The identifier- Throws:
IOException
-
println
Prints some text followed by a newline.- Parameters:
text
- The text to print- Throws:
IOException
-
printIndent
Prints the characters used to indent SQL.- Throws:
IOException
-
createUniqueIdentifier
Creates a reasonably unique identifier only consisting of hexadecimal characters and underscores. It looks liked578271282b42fce__2955b56e_107df3fbc96__8000
and is 48 characters long.- Returns:
- The identifier
-