|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codehaus.modello.generator.database.sql.SqlBuilder
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 | |
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 |
private static final java.lang.String LINE_SEP
private java.io.Writer writer
private int counter
private java.lang.String indent
private boolean primaryKeyEmbedded
private boolean foreignKeysEmbedded
private boolean indexesEmbedded
private boolean foreignKeyConstraintsNamed
private boolean alterTableForDrop
Constructor Detail |
public SqlBuilder()
Method Detail |
public void createDatabase(Database database) throws java.io.IOException
java.io.IOException
public void createDatabase(Database database, boolean dropTable) throws java.io.IOException
java.io.IOException
public void dropDatabase(Database database) throws java.io.IOException
java.io.IOException
public void tableComment(Table table) throws java.io.IOException
java.io.IOException
public void dropTable(Table table) throws java.io.IOException
java.io.IOException
public void createTable(Table table) throws java.io.IOException
java.io.IOException
public void createColumn(Table table, Column column) throws java.io.IOException
java.io.IOException
public java.io.Writer getWriter()
public void setWriter(java.io.Writer writer)
public java.lang.String getIndent()
public void setIndent(java.lang.String indent)
public boolean isPrimaryKeyEmbedded()
public void setPrimaryKeyEmbedded(boolean primaryKeyEmbedded)
public boolean isForeignKeysEmbedded()
public void setForeignKeysEmbedded(boolean foreignKeysEmbedded)
public boolean isIndexesEmbedded()
public void setIndexesEmbedded(boolean indexesEmbedded)
public boolean isForeignKeyConstraintsNamed()
public void setForeignKeyConstraintsNamed(boolean foreignKeyConstraintsNamed)
foreignKeyConstraintsNamed
- The foreignKeyConstraintsNamed to setprotected boolean shouldGeneratePrimaryKeys(java.util.List primaryKeyColumns)
protected java.lang.String getSqlType(Column column)
protected void writeColumnTypes(Table table) throws java.io.IOException
java.io.IOException
protected void writePrimaryKeys(Table table) throws java.io.IOException
java.io.IOException
protected void writePrimaryKeysAlterTable(Table table) throws java.io.IOException
java.io.IOException
protected void writePrimaryKeyStatement(java.util.List primaryKeyColumns) throws java.io.IOException
java.io.IOException
protected void writeForeignKeys(Table table) throws java.io.IOException
java.io.IOException
protected void writeForeignKeysAlterTable(Table table) throws java.io.IOException
java.io.IOException
protected void writeForeignKeyAlterTable(Table table, ForeignKey key) throws java.io.IOException
java.io.IOException
protected void writeIndexes(Table table) throws java.io.IOException
java.io.IOException
protected void writeIndex(Table table, Index index) throws java.io.IOException
java.io.IOException
protected void writeEmbeddedIndexes(Table table) throws java.io.IOException
java.io.IOException
protected void writeLocalReferences(ForeignKey key) throws java.io.IOException
java.io.IOException
protected void writeForeignReferences(ForeignKey key) throws java.io.IOException
java.io.IOException
protected void printComment(java.lang.String text) throws java.io.IOException
java.io.IOException
protected void printNullable() throws java.io.IOException
java.io.IOException
protected void printNotNullable() throws java.io.IOException
java.io.IOException
protected void printEndOfStatement() throws java.io.IOException
java.io.IOException
protected void println() throws java.io.IOException
java.io.IOException
protected void print(java.lang.String text) throws java.io.IOException
java.io.IOException
protected void println(java.lang.String text) throws java.io.IOException
java.io.IOException
protected void printIndent() throws java.io.IOException
java.io.IOException
protected void printAutoIncrementColumn(Table table, Column column) throws java.io.IOException
java.io.IOException
protected java.lang.String getNativeType(Column column)
public void alterColumn(Table table, Column column, boolean add) throws java.io.IOException
table
- The table the index is oncolumn
- The column to dropadd
- true if the column is new, false if it is to be changed
java.io.IOException
- if the statement cannot be writtenpublic void dropColumn(Table table, Column column) throws java.io.IOException
table
- The table the index is oncolumn
- The column to drop
java.io.IOException
- if the statement cannot be writtenprotected void writeAlterHeader(Table table) throws java.io.IOException
table
- The table being altered
java.io.IOException
- if the statement cannot be writtenpublic void dropIndex(Table table, Index index) throws java.io.IOException
alterTableForDrop
property is checked to determine what
style of drop is generated.
table
- The table the index is onindex
- The index to drop
java.io.IOException
- if the statement cannot be writtenuseAlterTableForDrop()
protected boolean columnsDiffer(Column desired, Column current)
desired
- First column to comparecurrent
- Second column to compare
public boolean useAlterTableForDrop()
public void setAlterTableForDrop(boolean alterTableForDrop)
alterTableForDrop
- The new value
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |