org.codehaus.modello.generator.database.sql

Class SqlBuilder

public class SqlBuilder extends 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 $

Author: James Strachan John Marshall/Connectria

Constructor Summary
SqlBuilder()
Method Summary
voidalterColumn(Table table, Column column, boolean add)
Generates the alter statement to add or modify a single column on a table.
protected booleancolumnsDiffer(Column desired, Column current)
Helper method to determine if two column specifications represent different types.
voidcreateColumn(Table table, Column column)
Outputs the DDL to add a column to a table.
voidcreateDatabase(Database database)
Outputs the DDL required to drop and recreate the database
voidcreateDatabase(Database database, boolean dropTable)
Outputs the DDL required to drop and recreate the database
voidcreateTable(Table table)
Outputs the DDL to create the table along with any constraints
voiddropColumn(Table table, Column column)
Generates the statement to drop an column from a table.
voiddropDatabase(Database database)
Outputs the DDL required to drop the database
voiddropIndex(Table table, Index index)
Generates the statement to drop an index from the database.
voiddropTable(Table table)
Outputs the DDL to drop the table
StringgetIndent()
protected StringgetNativeType(Column column)
protected StringgetSqlType(Column column)
WritergetWriter()
booleanisForeignKeyConstraintsNamed()
Returns whether foreign key constraints should be named when they are embedded inside a create table clause.
booleanisForeignKeysEmbedded()
booleanisIndexesEmbedded()
booleanisPrimaryKeyEmbedded()
protected voidprint(String text)
Prints some text
protected voidprintAutoIncrementColumn(Table table, Column column)
Outputs the fact that this column is an auto increment column.
protected voidprintComment(String text)
Prints an SQL comment to the current stream
protected voidprintEndOfStatement()
Prints the end of statement text, which is typically a semi colon followed by a carriage return
protected voidprintIndent()
Prints the indentation used to indent SQL
protected voidprintln()
Prints a new line
protected voidprintln(String text)
Prints some text then a newline
protected voidprintNotNullable()
Prints that a column is not nullable
protected voidprintNullable()
Prints that a column is nullable
voidsetAlterTableForDrop(boolean alterTableForDrop)
Whether an ALTER TABLE statement is necessary when dropping indexes or constraints.
voidsetForeignKeyConstraintsNamed(boolean foreignKeyConstraintsNamed)
Sets whether foreign key constraints should be named when they are embedded inside a create table clause.
voidsetForeignKeysEmbedded(boolean foreignKeysEmbedded)
Sets whether the foreign key constraints are embedded in the create table clause or as a seperate alter table statements.
voidsetIndent(String indent)
Sets the indentation used to indent the SQL
voidsetIndexesEmbedded(boolean indexesEmbedded)
Sets whether the indexes are embedded in the create table clause or as seperate statements.
voidsetPrimaryKeyEmbedded(boolean primaryKeyEmbedded)
Sets whether the primary key constraint is embedded in the create table clause or as a seperate alter table.
voidsetWriter(Writer writer)
Sets the writer used to print the DDL to
protected booleanshouldGeneratePrimaryKeys(List primaryKeyColumns)
voidtableComment(Table table)
Outputs a comment for the table
booleanuseAlterTableForDrop()
Whether an ALTER TABLE statement is necessary when dropping indexes or constraints.
protected voidwriteAlterHeader(Table table)
Generates the first part of the ALTER TABLE statement including the table name.
protected voidwriteColumnTypes(Table table)
Writes the column types for a table
protected voidwriteEmbeddedIndexes(Table table)
Writes the indexes embedded within the create table statement. not yet implemented
protected voidwriteForeignKeyAlterTable(Table table, ForeignKey key)
protected voidwriteForeignKeys(Table table)
Writes the foreign key constraints inside a create table () clause.
protected voidwriteForeignKeysAlterTable(Table table)
Writes the foreign key constraints as an AlterTable clause.
protected voidwriteForeignReferences(ForeignKey key)
Writes a list of foreign references for the given key
protected voidwriteIndex(Table table, Index index)
Writes one index for a table
protected voidwriteIndexes(Table table)
Writes the indexes.
protected voidwriteLocalReferences(ForeignKey key)
Writes a list of local references for the givek key
protected voidwritePrimaryKeys(Table table)
Writes the primary key constraints inside a create table () clause.
protected voidwritePrimaryKeysAlterTable(Table table)
Writes the primary key constraints as an AlterTable clause.
protected voidwritePrimaryKeyStatement(List primaryKeyColumns)
Writes the 'PRIMARY KEY(A,B,...,N)' statement

Constructor Detail

SqlBuilder

public SqlBuilder()

Method Detail

alterColumn

public void alterColumn(Table table, Column column, boolean add)
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

Throws: IOException if the statement cannot be written

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)
Outputs the DDL to add a column to a table.

createDatabase

public void createDatabase(Database database)
Outputs the DDL required to drop and recreate the database

createDatabase

public void createDatabase(Database database, boolean dropTable)
Outputs the DDL required to drop and recreate the database

createTable

public void createTable(Table table)
Outputs the DDL to create the table along with any constraints

dropColumn

public void dropColumn(Table table, Column column)
Generates the statement to drop an column from a table.

Parameters: table The table the index is on column The column to drop

Throws: IOException if the statement cannot be written

dropDatabase

public void dropDatabase(Database database)
Outputs the DDL required to drop the database

dropIndex

public void dropIndex(Table table, Index index)
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

Throws: IOException if the statement cannot be written

See Also: SqlBuilder

dropTable

public void dropTable(Table table)
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)
Prints some text

printAutoIncrementColumn

protected void printAutoIncrementColumn(Table table, Column column)
Outputs the fact that this column is an auto increment column.

printComment

protected void printComment(String text)
Prints an SQL comment to the current stream

printEndOfStatement

protected void printEndOfStatement()
Prints the end of statement text, which is typically a semi colon followed by a carriage return

printIndent

protected void printIndent()
Prints the indentation used to indent SQL

println

protected void println()
Prints a new line

println

protected void println(String text)
Prints some text then a newline

printNotNullable

protected void printNotNullable()
Prints that a column is not nullable

printNullable

protected void printNullable()
Prints that a column is nullable

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)
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)
Generates the first part of the ALTER TABLE statement including the table name.

Parameters: table The table being altered

Throws: IOException if the statement cannot be written

writeColumnTypes

protected void writeColumnTypes(Table table)
Writes the column types for a table

writeEmbeddedIndexes

protected void writeEmbeddedIndexes(Table table)
Writes the indexes embedded within the create table statement. not yet implemented

writeForeignKeyAlterTable

protected void writeForeignKeyAlterTable(Table table, ForeignKey key)

writeForeignKeys

protected void writeForeignKeys(Table table)
Writes the foreign key constraints inside a create table () clause.

writeForeignKeysAlterTable

protected void writeForeignKeysAlterTable(Table table)
Writes the foreign key constraints as an AlterTable clause.

writeForeignReferences

protected void writeForeignReferences(ForeignKey key)
Writes a list of foreign references for the given key

writeIndex

protected void writeIndex(Table table, Index index)
Writes one index for a table

writeIndexes

protected void writeIndexes(Table table)
Writes the indexes.

writeLocalReferences

protected void writeLocalReferences(ForeignKey key)
Writes a list of local references for the givek key

writePrimaryKeys

protected void writePrimaryKeys(Table table)
Writes the primary key constraints inside a create table () clause.

writePrimaryKeysAlterTable

protected void writePrimaryKeysAlterTable(Table table)
Writes the primary key constraints as an AlterTable clause.

writePrimaryKeyStatement

protected void writePrimaryKeyStatement(List primaryKeyColumns)
Writes the 'PRIMARY KEY(A,B,...,N)' statement
Copyright © 2001-2009 Codehaus. All Rights Reserved.