org.codehaus.modello.generator.database.sql
Class SqlBuilder

java.lang.Object
  extended byorg.codehaus.modello.generator.database.sql.SqlBuilder
Direct Known 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 $
Author:
James Strachan, John Marshall/Connectria

Field Summary
private  boolean alterTableForDrop
          Is an ALTER TABLE needed to drop indexes?
private  int counter
          A counter used to count the constraints
private  boolean foreignKeyConstraintsNamed
          Should foreign key constraints be explicitly named
private  boolean foreignKeysEmbedded
          Whether or not foreign key constraints are embedded inside the create table statement
private  java.lang.String indent
          The indentation used to indent commands
private  boolean indexesEmbedded
          Whether or not indexes are embedded inside the create table statement
private static java.lang.String LINE_SEP
           
private  boolean primaryKeyEmbedded
          Whether or not primary key constraints are embedded inside the create table statement
private  java.io.Writer writer
          The current Writer used to output the SQL to
 
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
 java.lang.String getIndent()
           
protected  java.lang.String getNativeType(Column column)
           
protected  java.lang.String getSqlType(Column column)
           
 java.io.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(java.lang.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(java.lang.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 println()
          Prints a new line
protected  void println(java.lang.String text)
          Prints some text then a newline
protected  void printNotNullable()
          Prints that a column is not nullable
protected  void printNullable()
          Prints that a column is nullable
 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(java.lang.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(java.io.Writer writer)
          Sets the writer used to print the DDL to
protected  boolean shouldGeneratePrimaryKeys(java.util.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 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.
protected  void writePrimaryKeyStatement(java.util.List primaryKeyColumns)
          Writes the 'PRIMARY KEY(A,B,...,N)' statement
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_SEP

private static final java.lang.String LINE_SEP

writer

private java.io.Writer writer
The current Writer used to output the SQL to


counter

private int counter
A counter used to count the constraints


indent

private java.lang.String indent
The indentation used to indent commands


primaryKeyEmbedded

private boolean primaryKeyEmbedded
Whether or not primary key constraints are embedded inside the create table statement


foreignKeysEmbedded

private boolean foreignKeysEmbedded
Whether or not foreign key constraints are embedded inside the create table statement


indexesEmbedded

private boolean indexesEmbedded
Whether or not indexes are embedded inside the create table statement


foreignKeyConstraintsNamed

private boolean foreignKeyConstraintsNamed
Should foreign key constraints be explicitly named


alterTableForDrop

private boolean alterTableForDrop
Is an ALTER TABLE needed to drop indexes?

Constructor Detail

SqlBuilder

public SqlBuilder()
Method Detail

createDatabase

public void createDatabase(Database database)
                    throws java.io.IOException
Outputs the DDL required to drop and recreate the database

Throws:
java.io.IOException

createDatabase

public void createDatabase(Database database,
                           boolean dropTable)
                    throws java.io.IOException
Outputs the DDL required to drop and recreate the database

Throws:
java.io.IOException

dropDatabase

public void dropDatabase(Database database)
                  throws java.io.IOException
Outputs the DDL required to drop the database

Throws:
java.io.IOException

tableComment

public void tableComment(Table table)
                  throws java.io.IOException
Outputs a comment for the table

Throws:
java.io.IOException

dropTable

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

Throws:
java.io.IOException

createTable

public void createTable(Table table)
                 throws java.io.IOException
Outputs the DDL to create the table along with any constraints

Throws:
java.io.IOException

createColumn

public void createColumn(Table table,
                         Column column)
                  throws java.io.IOException
Outputs the DDL to add a column to a table.

Throws:
java.io.IOException

getWriter

public java.io.Writer getWriter()
Returns:
the Writer used to print the DDL to

setWriter

public void setWriter(java.io.Writer writer)
Sets the writer used to print the DDL to


getIndent

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

setIndent

public void setIndent(java.lang.String indent)
Sets the indentation used to indent the SQL


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.

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.


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.

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.


isIndexesEmbedded

public boolean isIndexesEmbedded()
Returns:
whether the indexes are embedded in the create table clause or as seperate statements. The default is false.

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.


isForeignKeyConstraintsNamed

public boolean isForeignKeyConstraintsNamed()
Returns whether foreign key constraints should be named when they are embedded inside a create table clause.

Returns:
boolean

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

shouldGeneratePrimaryKeys

protected boolean shouldGeneratePrimaryKeys(java.util.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.

getSqlType

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

writeColumnTypes

protected void writeColumnTypes(Table table)
                         throws java.io.IOException
Writes the column types for a table

Throws:
java.io.IOException

writePrimaryKeys

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

Throws:
java.io.IOException

writePrimaryKeysAlterTable

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

Throws:
java.io.IOException

writePrimaryKeyStatement

protected void writePrimaryKeyStatement(java.util.List primaryKeyColumns)
                                 throws java.io.IOException
Writes the 'PRIMARY KEY(A,B,...,N)' statement

Throws:
java.io.IOException

writeForeignKeys

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

Throws:
java.io.IOException

writeForeignKeysAlterTable

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

Throws:
java.io.IOException

writeForeignKeyAlterTable

protected void writeForeignKeyAlterTable(Table table,
                                         ForeignKey key)
                                  throws java.io.IOException
Throws:
java.io.IOException

writeIndexes

protected void writeIndexes(Table table)
                     throws java.io.IOException
Writes the indexes.

Throws:
java.io.IOException

writeIndex

protected void writeIndex(Table table,
                          Index index)
                   throws java.io.IOException
Writes one index for a table

Throws:
java.io.IOException

writeEmbeddedIndexes

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

Throws:
java.io.IOException

writeLocalReferences

protected void writeLocalReferences(ForeignKey key)
                             throws java.io.IOException
Writes a list of local references for the givek key

Throws:
java.io.IOException

writeForeignReferences

protected void writeForeignReferences(ForeignKey key)
                               throws java.io.IOException
Writes a list of foreign references for the given key

Throws:
java.io.IOException

printComment

protected void printComment(java.lang.String text)
                     throws java.io.IOException
Prints an SQL comment to the current stream

Throws:
java.io.IOException

printNullable

protected void printNullable()
                      throws java.io.IOException
Prints that a column is nullable

Throws:
java.io.IOException

printNotNullable

protected void printNotNullable()
                         throws java.io.IOException
Prints that a column is not nullable

Throws:
java.io.IOException

printEndOfStatement

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

Throws:
java.io.IOException

println

protected void println()
                throws java.io.IOException
Prints a new line

Throws:
java.io.IOException

print

protected void print(java.lang.String text)
              throws java.io.IOException
Prints some text

Throws:
java.io.IOException

println

protected void println(java.lang.String text)
                throws java.io.IOException
Prints some text then a newline

Throws:
java.io.IOException

printIndent

protected void printIndent()
                    throws java.io.IOException
Prints the indentation used to indent SQL

Throws:
java.io.IOException

printAutoIncrementColumn

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

Throws:
java.io.IOException

getNativeType

protected java.lang.String getNativeType(Column column)

alterColumn

public void alterColumn(Table table,
                        Column column,
                        boolean add)
                 throws java.io.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
Throws:
java.io.IOException - if the statement cannot be written

dropColumn

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

Parameters:
table - The table the index is on
column - The column to drop
Throws:
java.io.IOException - if the statement cannot be written

writeAlterHeader

protected void writeAlterHeader(Table table)
                         throws java.io.IOException
Generates the first part of the ALTER TABLE statement including the table name.

Parameters:
table - The table being altered
Throws:
java.io.IOException - if the statement cannot be written

dropIndex

public void dropIndex(Table table,
                      Index index)
               throws java.io.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
Throws:
java.io.IOException - if the statement cannot be written
See Also:
useAlterTableForDrop()

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

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

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