Package org.jumpmind.db.platform
Class AbstractDdlBuilder
java.lang.Object
org.jumpmind.db.platform.AbstractDdlBuilder
- All Implemented Interfaces:
IDdlBuilder
- Direct Known Subclasses:
AseDdlBuilder
,CassandraDdlBuilder
,Db2DdlBuilder
,DerbyDdlBuilder
,FirebirdDdlBuilder
,GenericJdbcDdlBuilder
,H2DdlBuilder
,HanaDdlBuilder
,HbaseDdlBuilder
,HsqlDb2DdlBuilder
,HsqlDbDdlBuilder
,InformixDdlBuilder
,IngresDdlBuilder
,InterbaseDdlBuilder
,KafkaDdlBuilder
,MsSql2000DdlBuilder
,MySqlDdlBuilder
,NuoDbDdlBuilder
,OracleDdlBuilder
,PostgreSqlDdlBuilder
,RaimaDdlBuilder
,RedshiftDdlBuilder
,SqlAnywhereDdlBuilder
,SqliteDdlBuilder
,VoltDbDdlBuilder
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
protected DatabaseInfo
protected String
protected boolean
protected final org.slf4j.Logger
The Log to which logging calls will be made.protected boolean
protected static final String
The placeholder for the size value in the native type spec.protected boolean
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final void
addEscapedCharSequence
(String charSequence, String escapedVersion) Adds a char sequence that needs escaping, and its escaped version.void
alterDatabase
(Database currentModel, Database desiredModel, StringBuilder ddl, IAlterDatabaseInterceptor... alterDatabaseInterceptors) Generates the DDL to modify an existing database so the schema matches the specified database schema by using drops, modifications and additions.alterDatabase
(Database currentModel, Database desiredModel, IAlterDatabaseInterceptor... alterDatabaseInterceptors) alterTable
(Table currentTable, Table desiredTable, IAlterDatabaseInterceptor... alterDatabaseInterceptors) boolean
areColumnSizesTheSame
(Column sourceColumn, Column targetColumn) protected boolean
Compares the two strings.boolean
areMappedTypesTheSame
(Column sourceColumn, Column targetColumn) protected Database
createBackupTableFor
(Database model, Table sourceTable, StringBuilder ddl) void
createExternalForeignKeys
(Database database, StringBuilder ddl) Creates the external foreignkey creation statements for all tables in the database.void
createExternalForeignKeys
(Database database, Table table, StringBuilder ddl) Creates external foreign key creation statements if necessary.createTable
(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).protected void
createTable
(Table table, StringBuilder ddl, boolean temporary, boolean recreate) 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).createTables
(Database database, boolean dropTables) void
createTables
(Database database, boolean dropTables, StringBuilder ddl) Outputs the DDL required to drop (if requested) and (re)create all tables in the database model.protected void
createTemporaryTable
(Table table, StringBuilder ddl) Outputs the DDL to create the given temporary table.void
dropExternalForeignKeys
(Table table, StringBuilder ddl) Creates external foreign key drop statements.void
dropTable
(Database database, Table table, StringBuilder ddl) Outputs the DDL required to drop the given table.protected void
dropTable
(Table table, StringBuilder ddl, boolean temporary, boolean recreate) Outputs the DDL to drop the table.dropTables
(Database database) void
dropTables
(Database database, StringBuilder ddl) Outputs the DDL required to drop the database.protected void
dropTemporaryTable
(Table table, StringBuilder ddl) Outputs the DDL to drop the given temporary table.protected String
escapeStringValue
(String value) Escapes the necessary characters in given string value.protected void
filterChanges
(Collection<IModelChange> changes) protected void
filterColumnSqlType
(StringBuilder sqlType) protected ForeignKey
findCorrespondingForeignKey
(Table table, ForeignKey fk) Searches in the given table for a corresponding foreign key.protected ForeignKey
findForeignKey
(Table table, ForeignKey fk) protected Table
fixLastIdentityValues
(Table table) Generates the SQL to execute that sets the current sequence number.getBackupTableFor
(Table sourceTable) 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.getColumnTypeDdl
(Table table, Column column) getConstraintName
(String prefix, Table table, String secondPart, String suffix) Returns the constraint name.getCopyDataColumnMapping
(Table sourceTable, Table targetTable) getCopyDataColumnOrderedMapping
(Table sourceTable, Table targetTable) Returns the default value helper.protected String
getDefinitionForGeneratedColumn
(Table table, Column column) getDeleteSql
(Table table, Map<String, Object> 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).getDetectedChanges
(Database currentModel, Database desiredModel, IAlterDatabaseInterceptor... alterDatabaseInterceptors) getForeignKeyName
(Table table, ForeignKey fk) Returns the name to be used for the given foreign key.protected String
getFullyQualifiedIndexNameShorten
(Table table, IIndex index) protected String
The fully qualified table name shortenprotected static int
Get the maximum identity gap size from the property.Returns the string used to indent the SQL.getIndexName
(IIndex index) Returns the index name.getInsertSql
(Table table, Map<String, Object> columnValues, boolean genPlaceholders) Creates the SQL for inserting an object into the specified table.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.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 Integer
getSqlType
(Column column) Returns the full SQL type specification (including size and precision/scale) for the given column.getTableName
(String tableName) Returns the table name.protected Table
getTemporaryTableFor
(Table targetTable) Creates a temporary table object that corresponds to the given table.protected Table
getTemporaryTableFor
(Table targetTable, String suffix) getUpdateSql
(Table table, Map<String, Object> 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.protected boolean
void
boolean
isAlterDatabase
(Database currentModel, Database desiredModel, IAlterDatabaseInterceptor... alterDatabaseInterceptors) boolean
boolean
protected boolean
isFullTextIndex
(IIndex index) boolean
protected static boolean
Should the identity gap maximum size be specified in table create statements.boolean
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.mapDefaultValue
(Object defaultValue, Column column) protected void
mergeOrRemovePlatformTypes
(Database currentModel, Database desiredModel) protected void
printComment
(String text, StringBuilder ddl) Prints an SQL comment to the current stream.protected void
printDefaultValue
(String defaultValue, Column column, StringBuilder ddl) 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, StringBuilder ddl) Prints the given identifier.protected void
printIndent
(StringBuilder ddl) Prints the characters used to indent SQL.protected void
println
(StringBuilder ddl) Prints a newline.protected void
println
(String text, StringBuilder ddl) Prints some text followed by a newline.protected void
printlnIdentifier
(String identifier, StringBuilder ddl) Prints the given identifier followed by a newline.protected void
Prints the start of an embedded statement.protected void
processChange
(Database currentModel, Database desiredModel, AddForeignKeyChange change, StringBuilder ddl) Processes the change representing the addition of a foreign key.protected void
processChange
(Database currentModel, Database desiredModel, AddFunctionChange change, StringBuilder ddl) protected void
processChange
(Database currentModel, Database desiredModel, AddIndexChange change, StringBuilder ddl) Processes the change representing the addition of an index.protected void
processChange
(Database currentModel, Database desiredModel, AddPrimaryKeyChange change, StringBuilder ddl) Processes the addition of a primary key to a table.protected void
processChange
(Database currentModel, Database desiredModel, AddTableChange change, StringBuilder ddl) Processes the change representing the addition of a table.protected void
processChange
(Database currentModel, Database desiredModel, AddTableLoggingChange change, StringBuilder ddl) Punt change representing the removal of a foreign key to a database-specific override.protected void
processChange
(Database currentModel, Database desiredModel, AddTriggerChange change, StringBuilder ddl) protected void
processChange
(Database currentModel, Database desiredModel, CopyColumnValueChange change, StringBuilder ddl) protected void
processChange
(Database currentModel, Database desiredModel, IModelChange change, StringBuilder ddl) 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, RemoveForeignKeyChange change, StringBuilder ddl) Processes the change representing the removal of a foreign key.protected void
processChange
(Database currentModel, Database desiredModel, RemoveFunctionChange change, StringBuilder ddl) protected void
processChange
(Database currentModel, Database desiredModel, RemoveIndexChange change, StringBuilder ddl) Processes the change representing the removal of an index.protected void
processChange
(Database currentModel, Database desiredModel, RemoveTableChange change, StringBuilder ddl) Processes the change representing the removal of a table.protected void
processChange
(Database currentModel, Database desiredModel, RemoveTableLoggingChange change, StringBuilder ddl) Punt change representing the removal of a foreign key to a database-specific override.protected void
processChange
(Database currentModel, Database desiredModel, RemoveTriggerChange change, StringBuilder ddl) protected void
processChanges
(Database currentModel, Database desiredModel, List<IModelChange> changes, StringBuilder ddl) Processes the changes.protected void
processChanges
(Database currentModel, Database desiredModel, List<IModelChange> changes, StringBuilder ddl, Class<?>[] changeTypes) protected void
processTableStructureChanges
(Database currentModel, Database desiredModel, String tableName, List<TableChange> changes, StringBuilder ddl) Processes the changes to the structure of a single table.protected void
processTableStructureChanges
(Database currentModel, Database desiredModel, Collection<IModelChange> changes, StringBuilder ddl) Processes the changes to the structure of tables.protected void
processTableStructureChanges
(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) Allows database-specific implementations to handle changes in a database specific manner.protected void
renameTable
(Table sourceTable, Table tempTable, StringBuilder ddl) protected String
replaceDelimiterWithEscapeCharacters
(String triggerText) void
restoreTableFromBackup
(Table backupTable, Table targetTable, LinkedHashMap<Column, Column> columnMap, StringBuilder ddl) void
setCaseSensitive
(boolean caseSensitive) void
setDelimitedIdentifierModeOn
(boolean delimitedIdentifierModeOn) void
Sets the string used to indent the SQL.void
setScriptModeOn
(boolean scriptModeOn) void
setSqlCommentsOn
(boolean sqlCommentsOn) void
setTriggerDelimiterReplacementCharacters
(String triggerDelimiterReplacementCharacters) 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.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 boolean
shouldUseQuotes
(String defaultValue, Column column) protected boolean
protected void
protected void
protected void
protected void
writeCastExpression
(Column sourceColumn, Column targetColumn, StringBuilder ddl) 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, StringBuilder ddl) Outputs the DDL for the specified column.protected void
writeColumnAutoIncrementStmt
(Table table, Column column, StringBuilder ddl) Prints that the column is an auto increment column.protected void
writeColumnAutoUpdateStmt
(Table table, Column column, StringBuilder ddl) protected void
writeColumnDefaultValue
(Table table, Column column, StringBuilder ddl) Prints the default value of the column.protected void
writeColumnDefaultValueStmt
(Table table, Column column, StringBuilder ddl) Prints the default value stmt part for the column.protected void
writeColumnEmbeddedPrimaryKey
(Table table, Column column, StringBuilder ddl) protected void
Prints that a column is not nullable.protected void
Prints that a column is nullable.protected void
writeColumns
(Table table, StringBuilder ddl) Writes the columns of the given table.protected void
writeColumnType
(Table table, Column column, StringBuilder ddl) protected void
writeColumnTypeDefaultRequired
(Table table, Column column, StringBuilder ddl) protected void
void
writeCopyDataStatement
(Table sourceTable, Table targetTable, StringBuilder ddl) Writes a statement that copies the data from the source to the target table.void
writeCopyDataStatement
(Table sourceTable, Table targetTable, LinkedHashMap<Column, Column> columnMap, StringBuilder ddl) protected void
writeEmbeddedForeignKeysStmt
(Table table, StringBuilder ddl) Writes the foreign key constraints inside a create table () clause.protected void
writeEmbeddedIndexCreateStmt
(Table table, IIndex index, StringBuilder ddl) Writes the given embedded index of the table.protected void
writeEmbeddedIndicesStmt
(Table table, StringBuilder ddl) Writes the indexes embedded within the create table statement.protected void
writeEmbeddedPrimaryKeysStmt
(Table table, StringBuilder ddl) Writes the primary key constraints of the table inside its definition.protected void
writeExternalForeignKeyCreateStmt
(Database database, Table table, ForeignKey key, StringBuilder ddl) Writes a single foreign key constraint using a alter table statement.protected void
writeExternalForeignKeyDropStmt
(Table table, ForeignKey foreignKey, StringBuilder ddl) Generates the statement to drop a foreignkey constraint from the database using an alter table statement.protected void
writeExternalIndexCreate
(Table table, IIndex index, StringBuilder ddl) protected void
writeExternalIndexCreateStmt
(Table table, IIndex index, StringBuilder ddl) Writes the given index of the table.void
writeExternalIndexDropStmt
(Table table, IIndex index, StringBuilder ddl) Generates the statement to drop a non-embedded index from the database.protected void
writeExternalIndicesCreateStmt
(Table table, StringBuilder ddl) Writes the indexes of the given table.protected void
writeExternalPrimaryKeysCreateStmt
(Table table, Column[] primaryKeyColumns, StringBuilder ddl) Writes the primary key constraints of the table as alter table statements.void
writeFixLastIdentityValues
(Table table, StringBuilder ddl) protected void
writeForeignReferences
(ForeignKey key, StringBuilder ddl) Writes a list of foreign references for the given foreign key.protected void
writeGeneratedColumn
(Table table, Column column, StringBuilder ddl) protected void
Writes the end of table statement indicating an identity gap maximum value.protected void
writeLocalReferences
(ForeignKey key, StringBuilder ddl) Writes a list of local references for the given foreign key.protected void
writePrimaryKeyStmt
(Table table, Column[] primaryKeyColumns, StringBuilder ddl) Writes a primary key statement for the given columns.protected void
writeTableAlterStmt
(Table table, StringBuilder ddl) Generates the first part of the ALTER TABLE statement including the table name.protected void
writeTableComment
(Table table, StringBuilder ddl) Outputs a comment for the table.protected void
writeTableCreateOpeningStmt
(Table table, StringBuilder ddl) Writes the beginning of the table create statement.protected void
writeTableCreationStmt
(Table table, StringBuilder ddl) Writes the table creation statement without the statement end.protected void
writeTableCreationStmtEnding
(Table table, StringBuilder ddl) 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.slf4j.Logger logThe Log to which logging calls will be made. -
databaseInfo
-
delimitedIdentifierModeOn
protected boolean delimitedIdentifierModeOn -
caseSensitive
protected boolean caseSensitive -
sqlCommentsOn
protected boolean sqlCommentsOn -
scriptModeOn
protected boolean scriptModeOn -
databaseName
-
-
Constructor Details
-
AbstractDdlBuilder
Creates a new sql builder.
-
-
Method Details
-
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
-
createTables
- Specified by:
createTables
in interfaceIDdlBuilder
-
createTables
Outputs the DDL required to drop (if requested) and (re)create all tables in the database model. -
alterDatabase
public String alterDatabase(Database currentModel, Database desiredModel, IAlterDatabaseInterceptor... alterDatabaseInterceptors) - Specified by:
alterDatabase
in interfaceIDdlBuilder
-
alterTable
public String alterTable(Table currentTable, Table desiredTable, IAlterDatabaseInterceptor... alterDatabaseInterceptors) - Specified by:
alterTable
in interfaceIDdlBuilder
-
mergeOrRemovePlatformTypes
-
alterDatabase
public void alterDatabase(Database currentModel, Database desiredModel, StringBuilder ddl, IAlterDatabaseInterceptor... alterDatabaseInterceptors) 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. -
isAlterDatabase
public boolean isAlterDatabase(Database currentModel, Database desiredModel, IAlterDatabaseInterceptor... alterDatabaseInterceptors) - Specified by:
isAlterDatabase
in interfaceIDdlBuilder
-
getDetectedChanges
public List<IModelChange> getDetectedChanges(Database currentModel, Database desiredModel, IAlterDatabaseInterceptor... alterDatabaseInterceptors) - Specified by:
getDetectedChanges
in interfaceIDdlBuilder
-
processChanges
protected void processChanges(Database currentModel, Database desiredModel, List<IModelChange> changes, StringBuilder ddl) Processes the changes. The default argument performs several passes:RemoveForeignKeyChange
andRemoveIndexChange
come first to allow for e.g. subsequent primary key changes or column removal.RemoveTableChange
comes after the removal of foreign keys and indices.- These are all handled together:
RemovePrimaryKeyChange
AddPrimaryKeyChange
PrimaryKeyChange
RemoveColumnChange
AddColumnChange
ColumnAutoIncrementChange
ColumnDefaultValueChange
ColumnRequiredChange
ColumnDataTypeChange
ColumnSizeChange
The reason for this is that the default algorithm rebuilds the table for these changes and thus their order is irrelevant. 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.AddForeignKeyChange
andAddIndexChange
come last after table/column/primary key additions or changes.RemoveTriggerChange
come after the tables are completely built.AddTriggerChange
is done last.
-
processChanges
protected void processChanges(Database currentModel, Database desiredModel, List<IModelChange> changes, StringBuilder ddl, Class<?>[] changeTypes) -
processChange
protected void processChange(Database currentModel, Database desiredModel, IModelChange change, StringBuilder ddl) This is a fall-through callback which generates a warning because a specific change type wasn't handled. -
processChange
protected void processChange(Database currentModel, Database desiredModel, AddTableLoggingChange change, StringBuilder ddl) Punt change representing the removal of a foreign key to a database-specific override. -
processChange
protected void processChange(Database currentModel, Database desiredModel, RemoveTableLoggingChange change, StringBuilder ddl) Punt change representing the removal of a foreign key to a database-specific override. -
processChange
protected void processChange(Database currentModel, Database desiredModel, RemoveForeignKeyChange change, StringBuilder ddl) Processes the change representing the removal of a foreign key. -
processChange
protected void processChange(Database currentModel, Database desiredModel, RemoveIndexChange change, StringBuilder ddl) Processes the change representing the removal of an index.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemachange
- The change object
-
processChange
protected void processChange(Database currentModel, Database desiredModel, RemoveTableChange change, StringBuilder ddl) Processes the change representing the removal of a table.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemachange
- The change object
-
processChange
protected void processChange(Database currentModel, Database desiredModel, AddTableChange change, StringBuilder ddl) Processes the change representing the addition of a table.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemachange
- The change object
-
processChange
protected void processChange(Database currentModel, Database desiredModel, AddForeignKeyChange change, StringBuilder ddl) Processes the change representing the addition of a foreign key.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemachange
- The change object
-
processChange
protected void processChange(Database currentModel, Database desiredModel, AddIndexChange change, StringBuilder ddl) Processes the change representing the addition of an index.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemachange
- The change object
-
filterChanges
-
processTableStructureChanges
protected void processTableStructureChanges(Database currentModel, Database desiredModel, Collection<IModelChange> changes, StringBuilder ddl) Processes the changes to the structure of tables.- Parameters:
currentModel
- The current database schemadesiredModel
- The desired database schemachanges
- The change objects
-
findTable
-
findForeignKey
-
processTableStructureChanges
protected void processTableStructureChanges(Database currentModel, Database desiredModel, String tableName, List<TableChange> changes, StringBuilder ddl) Processes the changes to the structure of a single table. Database-specific implementations might redefine this method, but it is usually sufficient to redefine the#processTableStructureChanges(Database, Database, Table, Table, Map, List)
method instead. -
renameTable
-
copy
-
processTableStructureChanges
protected void processTableStructureChanges(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) 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 tablechanges
- The change objects for the target table
-
processChange
protected void processChange(Database currentModel, Database desiredModel, CopyColumnValueChange change, StringBuilder ddl) -
processChange
protected void processChange(Database currentModel, Database desiredModel, RemoveTriggerChange change, StringBuilder ddl) -
processChange
protected void processChange(Database currentModel, Database desiredModel, AddTriggerChange change, StringBuilder ddl) -
processChange
protected void processChange(Database currentModel, Database desiredModel, RemoveFunctionChange change, StringBuilder ddl) -
processChange
protected void processChange(Database currentModel, Database desiredModel, AddFunctionChange change, StringBuilder ddl) -
replaceDelimiterWithEscapeCharacters
-
writeAlterColumnDataTypeToBigInt
-
getFullyQualifiedTableNameShorten
The fully qualified table name shorten -
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:
targetTable
- The target table- Returns:
- The temporary table
-
getBackupTableFor
-
createBackupTableFor
-
restoreTableFromBackup
public void restoreTableFromBackup(Table backupTable, Table targetTable, LinkedHashMap<Column, Column> columnMap, StringBuilder ddl) -
getTemporaryTableFor
-
createTemporaryTable
Outputs the DDL to create the given temporary table. Per default this is simply a call to#createTable(Table, Map)
.- Parameters:
table
- The table
-
dropTemporaryTable
Outputs the DDL to drop the given temporary table. Per default this is simply a call to#dropTable(Table)
.- Parameters:
table
- The table
-
getRealTargetTableFor
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
-
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 the#writeCastExpression(Column, Column)
method.- Parameters:
sourceTable
- The source tabletargetTable
- The target table
-
writeCopyDataStatement
public void writeCopyDataStatement(Table sourceTable, Table targetTable, LinkedHashMap<Column, Column> columnMap, StringBuilder ddl) -
getCopyDataColumnMapping
-
getCopyDataColumnOrderedMapping
public LinkedHashMap<Column,Column> getCopyDataColumnOrderedMapping(Table sourceTable, Table targetTable) -
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. -
processChange
protected void processChange(Database currentModel, Database desiredModel, AddPrimaryKeyChange change, StringBuilder ddl) Processes the addition of a primary key to a table. -
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).- Specified by:
createTable
in interfaceIDdlBuilder
-
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:
recreate
- TODO
-
createExternalForeignKeys
Creates the external foreignkey creation statements for all tables in the database.- Parameters:
database
- The database
-
createExternalForeignKeys
Creates external foreign key creation statements if necessary. -
dropTables
- Specified by:
dropTables
in interfaceIDdlBuilder
-
dropTables
Outputs the DDL required to drop the database. -
dropTable
Outputs the DDL required to drop the given table. This method also drops foreign keys to the table. -
dropTable
Outputs the DDL to drop the table. Note that this method does not drop foreign keys to this table. Use#dropTable(Database, Table)
if you want that.- Parameters:
recreate
- TODO
-
dropExternalForeignKeys
Creates external foreign key drop statements.- Parameters:
table
- The table
-
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
-
fixLastIdentityValues
Generates the SQL to execute that sets the current sequence number.- Parameters:
table
-id
-- Returns:
-
writeFixLastIdentityValues
-
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.- Specified by:
getTableName
in interfaceIDdlBuilder
- Parameters:
table
- The table- Returns:
- The table name
-
writeTableComment
Outputs a comment for the table.- Parameters:
table
- The table
-
writeTableAlterStmt
Generates the first part of the ALTER TABLE statement including the table name.- Parameters:
table
- The table being altered
-
writeTableCreationStmt
Writes the table creation statement without the statement end. -
writeTableCreateOpeningStmt
Writes the beginning of the table create statement. -
writeIdentityGapLimit
Writes the end of table statement indicating an identity gap maximum value. -
isSpecifyIdentityGapLimit
protected static boolean isSpecifyIdentityGapLimit()Should the identity gap maximum size be specified in table create statements. -
getGapLimitSize
protected static int getGapLimitSize()Get the maximum identity gap size from the property. -
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 table
-
writeColumns
Writes the columns of the given table. -
getColumnName
Returns the column name. This method takes care of length limitations imposed by some databases.- Parameters:
column
- The column- Returns:
- The column name
-
writeColumnTypeDefaultRequired
-
writeGeneratedColumn
-
getDefinitionForGeneratedColumn
-
getColumnTypeDdl
- Specified by:
getColumnTypeDdl
in interfaceIDdlBuilder
-
writeColumnType
-
writeColumn
Outputs the DDL for the specified column. -
writeColumnEmbeddedPrimaryKey
-
getSqlType
Returns the full SQL type specification (including size and precision/scale) for the given column.- Specified by:
getSqlType
in interfaceIDdlBuilder
- Parameters:
column
- The column- Returns:
- The full SQL type string including the size
-
hasSize
-
getSize
-
filterColumnSqlType
-
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. -
writeColumnDefaultValue
Prints the default value of the column. -
printDefaultValue
Prints the default value of the column. -
shouldUseQuotes
-
mapDefaultValue
- Specified by:
mapDefaultValue
in interfaceIDdlBuilder
-
writeColumnAutoIncrementStmt
Prints that the column is an auto increment column. -
writeColumnAutoUpdateStmt
-
writeColumnNullableStmt
Prints that a column is nullable. -
writeColumnNotNullableStmt
Prints that a column is not nullable. -
writeColumnUniqueStmt
-
areColumnSizesTheSame
- Specified by:
areColumnSizesTheSame
in interfaceIDdlBuilder
-
areMappedTypesTheSame
- Specified by:
areMappedTypesTheSame
in interfaceIDdlBuilder
-
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.- Specified by:
getForeignKeyName
in interfaceIDdlBuilder
- 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.- Specified by:
getConstraintName
in interfaceIDdlBuilder
- 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
-
writeExternalPrimaryKeysCreateStmt
protected void writeExternalPrimaryKeysCreateStmt(Table table, Column[] primaryKeyColumns, StringBuilder ddl) Writes the primary key constraints of the table as alter table statements.- Parameters:
table
- The tableprimaryKeyColumns
- The primary key columns
-
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
-
getIndexName
Returns the index name. This method takes care of length limitations imposed by some databases.- Specified by:
getIndexName
in interfaceIDdlBuilder
- Parameters:
index
- The index- Returns:
- The index name
-
writeExternalIndicesCreateStmt
Writes the indexes of the given table.- Parameters:
table
- The table
-
writeEmbeddedIndicesStmt
Writes the indexes embedded within the create table statement. -
writeExternalIndexCreateStmt
Writes the given index of the table. -
writeExternalIndexCreate
-
getFullyQualifiedIndexNameShorten
-
isFullTextIndex
-
writeEmbeddedIndexCreateStmt
Writes the given embedded index of the table. -
writeExternalIndexDropStmt
Generates the statement to drop a non-embedded index from the database. -
writeEmbeddedForeignKeysStmt
Writes the foreign key constraints inside a create table () clause. -
writeExternalForeignKeyCreateStmt
protected void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key, StringBuilder ddl) Writes a single foreign key constraint using a alter table statement.- Parameters:
database
- The database modeltable
- The tablekey
- The foreign key
-
writeCascadeAttributesForForeignKey
-
writeCascadeAttributesForForeignKeyDelete
-
writeCascadeAttributesForForeignKeyUpdate
-
writeLocalReferences
Writes a list of local references for the given foreign key.- Parameters:
key
- The foreign key
-
writeForeignReferences
Writes a list of foreign references for the given foreign key.- Parameters:
key
- The foreign key
-
writeExternalForeignKeyDropStmt
protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey, StringBuilder ddl) Generates the statement to drop a foreignkey constraint from the database using an alter table statement.- Parameters:
table
- The tableforeignKey
- The foreign key
-
printComment
Prints an SQL comment to the current stream.- Parameters:
text
- The comment text
-
printStartOfEmbeddedStatement
Prints the start of an embedded statement. -
printEndOfStatement
Prints the end of statement text, which is typically a semi colon followed by a carriage return. -
println
Prints a newline. -
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
-
printlnIdentifier
Prints the given identifier followed by a newline. For most databases, this will be a delimited identifier.- Parameters:
identifier
- The identifier
-
println
Prints some text followed by a newline.- Parameters:
text
- The text to print
-
printIndent
Prints the characters used to indent SQL. -
isScriptModeOn
public boolean isScriptModeOn() -
setScriptModeOn
public void setScriptModeOn(boolean scriptModeOn) -
isSqlCommentsOn
public boolean isSqlCommentsOn() -
setSqlCommentsOn
public void setSqlCommentsOn(boolean sqlCommentsOn) -
isDelimitedIdentifierModeOn
public boolean isDelimitedIdentifierModeOn()- Specified by:
isDelimitedIdentifierModeOn
in interfaceIDdlBuilder
-
setDelimitedIdentifierModeOn
public void setDelimitedIdentifierModeOn(boolean delimitedIdentifierModeOn) - Specified by:
setDelimitedIdentifierModeOn
in interfaceIDdlBuilder
-
getDatabaseInfo
- Specified by:
getDatabaseInfo
in interfaceIDdlBuilder
-
setCaseSensitive
public void setCaseSensitive(boolean caseSensitive) - Specified by:
setCaseSensitive
in interfaceIDdlBuilder
-
isCaseSensitive
public boolean isCaseSensitive() -
initCteExpression
public void initCteExpression()- Specified by:
initCteExpression
in interfaceIDdlBuilder
-
getTriggerDelimiterReplacementCharacters
- Specified by:
getTriggerDelimiterReplacementCharacters
in interfaceIDdlBuilder
-
setTriggerDelimiterReplacementCharacters
- Specified by:
setTriggerDelimiterReplacementCharacters
in interfaceIDdlBuilder
-