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.
alterColumn
public void alterColumn(Table table,
Column column,
boolean add)
throws IOException
Generates the alter statement to add or modify a single column on a table.
table
- The table the index is oncolumn
- The column to dropadd
- true if the column is new, false if it is to be changed
columnsDiffer
protected boolean columnsDiffer(Column desired,
Column current)
Helper method to determine if two column specifications represent
different types. Type, nullability, size, scale, default value,
and precision radix are the attributes checked. Currently default
values are compared where null and empty string are considered equal.
See comments in the method body for explanation.
desired
- First column to comparecurrent
- Second column to compare
- true if the columns differ
createColumn
public void createColumn(Table table,
Column column)
throws IOException
Outputs the DDL to add a column to a table.
createDatabase
public void createDatabase(Database database)
throws IOException
Outputs the DDL required to drop and recreate the database
createDatabase
public void createDatabase(Database database,
boolean dropTable)
throws IOException
Outputs the DDL required to drop and recreate the database
createTable
public void createTable(Table table)
throws IOException
Outputs the DDL to create the table along with any constraints
dropColumn
public void dropColumn(Table table,
Column column)
throws IOException
Generates the statement to drop an column from a table.
table
- The table the index is oncolumn
- The column to drop
dropDatabase
public void dropDatabase(Database database)
throws IOException
Outputs the DDL required to drop the database
dropIndex
public void dropIndex(Table table,
Index index)
throws IOException
Generates the statement to drop an index from the database. The
alterTableForDrop
property is checked to determine what
style of drop is generated.
table
- The table the index is onindex
- The index to drop
dropTable
public void dropTable(Table table)
throws IOException
Outputs the DDL to drop the table
getIndent
public String getIndent()
- the indentation used to indent the SQL
getNativeType
protected String getNativeType(Column column)
getSqlType
protected String getSqlType(Column column)
- the full SQL type string including the size
getWriter
public Writer getWriter()
- the Writer used to print the DDL to
isForeignKeyConstraintsNamed
public boolean isForeignKeyConstraintsNamed()
Returns whether foreign key constraints should be named when they are embedded inside
a create table clause.
isForeignKeysEmbedded
public boolean isForeignKeysEmbedded()
- whether the foreign key constraints are embedded in the create
table clause or as a seperate alter table statements.
The default is false.
isIndexesEmbedded
public boolean isIndexesEmbedded()
- whether the indexes are embedded in the create
table clause or as seperate statements.
The default is false.
isPrimaryKeyEmbedded
public boolean isPrimaryKeyEmbedded()
- whether the primary key constraint is embedded in the create
table clause or as a seperate alter table.
The default is true.
print
protected void print(String text)
throws IOException
Prints some text
printAutoIncrementColumn
protected void printAutoIncrementColumn(Table table,
Column column)
throws IOException
Outputs the fact that this column is an auto increment column.
printComment
protected void printComment(String text)
throws IOException
Prints an SQL comment to the current stream
printEndOfStatement
protected void printEndOfStatement()
throws IOException
Prints the end of statement text, which is typically a semi colon followed by
a carriage return
printIndent
protected void printIndent()
throws IOException
Prints the indentation used to indent SQL
printNotNullable
protected void printNotNullable()
throws IOException
Prints that a column is not nullable
printNullable
protected void printNullable()
throws IOException
Prints that a column is nullable
println
protected void println()
throws IOException
Prints a new line
println
protected void println(String text)
throws IOException
Prints some text then a newline
setAlterTableForDrop
public void setAlterTableForDrop(boolean alterTableForDrop)
Whether an ALTER TABLE statement is necessary when dropping indexes
or constraints. The default is false.
alterTableForDrop
- The new value
setForeignKeyConstraintsNamed
public void setForeignKeyConstraintsNamed(boolean foreignKeyConstraintsNamed)
Sets whether foreign key constraints should be named when they are embedded inside
a create table clause.
foreignKeyConstraintsNamed
- The foreignKeyConstraintsNamed to set
setForeignKeysEmbedded
public void setForeignKeysEmbedded(boolean foreignKeysEmbedded)
Sets whether the foreign key constraints are embedded in the create
table clause or as a seperate alter table statements.
The default is false.
setIndent
public void setIndent(String indent)
Sets the indentation used to indent the SQL
setIndexesEmbedded
public void setIndexesEmbedded(boolean indexesEmbedded)
Sets whether the indexes are embedded in the create
table clause or as seperate statements.
The default is false.
setPrimaryKeyEmbedded
public void setPrimaryKeyEmbedded(boolean primaryKeyEmbedded)
Sets whether the primary key constraint is embedded in the create
table clause or as a seperate alter table.
The default is true.
setWriter
public void setWriter(Writer writer)
Sets the writer used to print the DDL to
shouldGeneratePrimaryKeys
protected boolean shouldGeneratePrimaryKeys(List primaryKeyColumns)
- true if we should generate a primary key constraint for the given
primary key columns. By default if there are no primary keys or the column(s) are
all auto increment (identity) columns then there is no need to generate a primary key
constraint.
tableComment
public void tableComment(Table table)
throws IOException
Outputs a comment for the table
useAlterTableForDrop
public boolean useAlterTableForDrop()
Whether an ALTER TABLE statement is necessary when dropping indexes
or constraints. The default is false.
- true if ALTER TABLE is required
writeAlterHeader
protected void writeAlterHeader(Table table)
throws IOException
Generates the first part of the ALTER TABLE statement including the
table name.
table
- The table being altered
writeColumnTypes
protected void writeColumnTypes(Table table)
throws IOException
Writes the column types for a table
writeEmbeddedIndexes
protected void writeEmbeddedIndexes(Table table)
throws IOException
Writes the indexes embedded within the create table statement. not
yet implemented
writeForeignKeyAlterTable
protected void writeForeignKeyAlterTable(Table table,
ForeignKey key)
throws IOException
writeForeignKeys
protected void writeForeignKeys(Table table)
throws IOException
Writes the foreign key constraints inside a create table () clause.
writeForeignKeysAlterTable
protected void writeForeignKeysAlterTable(Table table)
throws IOException
Writes the foreign key constraints as an AlterTable clause.
writeForeignReferences
protected void writeForeignReferences(ForeignKey key)
throws IOException
Writes a list of foreign references for the given key
writeIndex
protected void writeIndex(Table table,
Index index)
throws IOException
Writes one index for a table
writeIndexes
protected void writeIndexes(Table table)
throws IOException
Writes the indexes.
writeLocalReferences
protected void writeLocalReferences(ForeignKey key)
throws IOException
Writes a list of local references for the givek key
writePrimaryKeyStatement
protected void writePrimaryKeyStatement(List primaryKeyColumns)
throws IOException
Writes the 'PRIMARY KEY(A,B,...,N)' statement
writePrimaryKeys
protected void writePrimaryKeys(Table table)
throws IOException
Writes the primary key constraints inside a create table () clause.
writePrimaryKeysAlterTable
protected void writePrimaryKeysAlterTable(Table table)
throws IOException
Writes the primary key constraints as an AlterTable clause.