org.codehaus.modello.generator.database.sql

Class SqlBuilder

Known Direct Subclasses:
AxionBuilder, Db2Builder, HsqlDbBuilder, MckoiSqlBuilder, MSSqlBuilder, MySqlBuilder, OracleBuilder, PostgreSqlBuilder, SapDbBuilder, SybaseBuilder

public class SqlBuilder
extends java.lang.Object

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: 149 $
Authors:
James Strachan
John Marshall/Connectria

Constructor Summary

SqlBuilder()

Method Summary

void
alterColumn(Table table, Column column, boolean add)
Generates the alter statement to add or modify a single column on a table.
protected boolean
columnsDiffer(Column desired, Column current)
Helper method to determine if two column specifications represent different types.
void
createColumn(Table table, Column column)
Outputs the DDL to add a column to a table.
void
createDatabase(Database database)
Outputs the DDL required to drop and recreate the database
void
createDatabase(Database database, boolean dropTable)
Outputs the DDL required to drop and recreate the database
void
createTable(Table table)
Outputs the DDL to create the table along with any constraints
void
dropColumn(Table table, Column column)
Generates the statement to drop an column from a table.
void
dropDatabase(Database database)
Outputs the DDL required to drop the database
void
dropIndex(Table table, Index index)
Generates the statement to drop an index from the database.
void
dropTable(Table table)
Outputs the DDL to drop the table
String
getIndent()
protected String
getNativeType(Column column)
protected String
getSqlType(Column column)
Writer
getWriter()
boolean
isForeignKeyConstraintsNamed()
Returns whether foreign key constraints should be named when they are embedded inside a create table clause.
boolean
isForeignKeysEmbedded()
boolean
isIndexesEmbedded()
boolean
isPrimaryKeyEmbedded()
protected void
print(String text)
Prints some text
protected void
printAutoIncrementColumn(Table table, Column column)
Outputs the fact that this column is an auto increment column.
protected void
printComment(String text)
Prints an SQL comment to the current stream
protected void
printEndOfStatement()
Prints the end of statement text, which is typically a semi colon followed by a carriage return
protected void
printIndent()
Prints the indentation used to indent SQL
protected void
printNotNullable()
Prints that a column is not nullable
protected void
printNullable()
Prints that a column is nullable
protected void
println()
Prints a new line
protected void
println(String text)
Prints some text then a newline
void
setAlterTableForDrop(boolean alterTableForDrop)
Whether an ALTER TABLE statement is necessary when dropping indexes or constraints.
void
setForeignKeyConstraintsNamed(boolean foreignKeyConstraintsNamed)
Sets whether foreign key constraints should be named when they are embedded inside a create table clause.
void
setForeignKeysEmbedded(boolean foreignKeysEmbedded)
Sets whether the foreign key constraints are embedded in the create table clause or as a seperate alter table statements.
void
setIndent(String indent)
Sets the indentation used to indent the SQL
void
setIndexesEmbedded(boolean indexesEmbedded)
Sets whether the indexes are embedded in the create table clause or as seperate statements.
void
setPrimaryKeyEmbedded(boolean primaryKeyEmbedded)
Sets whether the primary key constraint is embedded in the create table clause or as a seperate alter table.
void
setWriter(Writer writer)
Sets the writer used to print the DDL to
protected boolean
shouldGeneratePrimaryKeys(List primaryKeyColumns)
void
tableComment(Table table)
Outputs a comment for the table
boolean
useAlterTableForDrop()
Whether an ALTER TABLE statement is necessary when dropping indexes or constraints.
protected void
writeAlterHeader(Table table)
Generates the first part of the ALTER TABLE statement including the table name.
protected void
writeColumnTypes(Table table)
Writes the column types for a table
protected void
writeEmbeddedIndexes(Table table)
Writes the indexes embedded within the create table statement.
protected void
writeForeignKeyAlterTable(Table table, ForeignKey key)
protected void
writeForeignKeys(Table table)
Writes the foreign key constraints inside a create table () clause.
protected void
writeForeignKeysAlterTable(Table table)
Writes the foreign key constraints as an AlterTable clause.
protected void
writeForeignReferences(ForeignKey key)
Writes a list of foreign references for the given key
protected void
writeIndex(Table table, Index index)
Writes one index for a table
protected void
writeIndexes(Table table)
Writes the indexes.
protected void
writeLocalReferences(ForeignKey key)
Writes a list of local references for the givek key
protected void
writePrimaryKeyStatement(List primaryKeyColumns)
Writes the 'PRIMARY KEY(A,B,...,N)' statement
protected void
writePrimaryKeys(Table table)
Writes the primary key constraints inside a create table () clause.
protected void
writePrimaryKeysAlterTable(Table table)
Writes the primary key constraints as an AlterTable clause.

Constructor Details

SqlBuilder

public SqlBuilder()

Method Details

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.
Parameters:
table - The table the index is on
column - The column to drop
add - 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.
Parameters:
desired - First column to compare
current - Second column to compare
Returns:
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.
Parameters:
table - The table the index is on
column - 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.
Parameters:
table - The table the index is on
index - The index to drop

dropTable

public void dropTable(Table table)
            throws IOException
Outputs the DDL to drop the table

getIndent

public String getIndent()
Returns:
the indentation used to indent the SQL

getNativeType

protected String getNativeType(Column column)

getSqlType

protected String getSqlType(Column column)
Returns:
the full SQL type string including the size

getWriter

public Writer getWriter()
Returns:
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.
Returns:
boolean

isForeignKeysEmbedded

public boolean isForeignKeysEmbedded()
Returns:
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()
Returns:
whether the indexes are embedded in the create table clause or as seperate statements. The default is false.

isPrimaryKeyEmbedded

public boolean isPrimaryKeyEmbedded()
Returns:
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.
Parameters:
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.
Parameters:
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)
Returns:
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.
Returns:
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.
Parameters:
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.