|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.ddlutils.platform.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. TODO: It might be useful to add foreignkey analysis for creation/dropping of tables
Field Summary | |
protected org.apache.commons.logging.Log |
_log
The Log to which logging calls will be made. |
protected static String |
SIZE_PLACEHOLDER
The placeholder for the size value in the native type spec. |
Constructor Summary | |
SqlBuilder(PlatformInfo info)
Creates a new sql builder. |
Method Summary | |
protected void |
addEscapedCharSequence(String charSequence,
String escapedVersion)
Adds a char sequence that needs escaping, and its escaped version. |
void |
alterDatabase(Database currentModel,
Database desiredModel)
Generates the DDL to modify an existing database so the schema matches the current specified database schema. |
void |
alterDatabase(Database currentModel,
Database desiredModel,
boolean doDrops,
boolean modifyColumns)
Generates the DDL to modify an existing database so the schema matches the current specified database schema. |
void |
alterDatabase(Database currentModel,
Database desiredModel,
CreationParameters params,
boolean doDrops,
boolean modifyColumns)
Generates the DDL to modify an existing database so the schema matches the current specified database schema. |
protected void |
alterTable(Database currentModel,
Table currentTable,
Database desiredModel,
Table desiredTable,
boolean doDrops,
boolean modifyColumns)
Alters the given currently existing table object to match the given desired table object. |
protected boolean |
columnsDiffer(Column currentColumn,
Column desiredColumn)
Compares the current column in the database with the desired one. |
void |
createExternalForeignKeys(Database database)
Creates the external foreignkey creation statements for all tables in the database. |
void |
createExternalForeignKeys(Database database,
Table table)
Creates external foreignkey creation statements if necessary. |
void |
createTable(Database database,
Table table)
Outputs the DDL to create the table along with any non-external constraints as well as with external primary keys and indices (but not foreign keys). |
void |
createTable(Database database,
Table table,
Map parameters)
Outputs the DDL to create the table along with any non-external constraints as well as with external primary keys and indices (but not foreign keys). |
void |
createTables(Database database)
Outputs the DDL required to drop and (re)create all tables in the database model. |
void |
createTables(Database database,
boolean dropTables)
Outputs the DDL required to drop (if requested) and (re)create all tables in the database model. |
void |
createTables(Database database,
CreationParameters params,
boolean dropTables)
Outputs the DDL required to drop (if requested) and (re)create all tables in the database model. |
void |
dropExternalForeignKeys(Table table)
Creates external foreignkey drop statements. |
void |
dropTable(Table table)
Outputs the DDL to drop the table. |
void |
dropTables(Database database)
Outputs the DDL required to drop the database. |
protected String |
escapeStringValue(String value)
Escapes the necessary characters in given string value. |
protected String |
getColumnName(Column column)
Returns the column name. |
protected String |
getConstraintName(String prefix,
Table table,
String secondPart,
String suffix)
Returns the constraint name. |
DefaultValueHelper |
getDefaultValueHelper()
Returns the default value helper. |
String |
getDeleteSql(Table table,
Map pkValues,
boolean genPlaceholders)
Creates the SQL for deleting an object from the specified table. |
protected String |
getDelimitedIdentifier(String identifier)
Returns the delimited version of the identifier (if configured). |
String |
getForeignKeyName(Table table,
ForeignKey fk)
Returns the name to be used for the given foreign key. |
String |
getIndent()
Returns the string used to indent the SQL. |
protected String |
getIndexName(Index index)
Returns the index name. |
String |
getInsertSql(Table table,
Map columnValues,
boolean genPlaceholders)
Creates the SQL for inserting an object into the specified table. |
protected String |
getNativeDefaultValue(Column column)
Returns the native default value for the column. |
protected String |
getNativeType(Column column)
Returns the database-native type for the given column. |
PlatformInfo |
getPlatformInfo()
Returns the platform info object. |
String |
getSelectLastInsertId(Table table)
Generates the SQL for querying the id that was created in the last insertion operation. |
protected String |
getSqlType(Column column)
Returns the full SQL type specification (including size and precision/scale) for the given column. |
protected String |
getTableName(Table table)
Returns the table name. |
String |
getUpdateSql(Table table,
Map columnValues,
boolean genPlaceholders)
Creates the SQL for updating an object in the specified table. |
protected String |
getValueAsString(Column column,
Object value)
Generates the string representation of the given value. |
String |
getValueLocale()
Returns the locale that is used for number and date formatting (when printing default values and in generates insert/update/delete statements). |
Writer |
getWriter()
Returns the writer that the DDL is printed to. |
protected void |
print(String text)
Prints some text. |
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 |
printIdentifier(String identifier)
Prints the given identifier. |
protected void |
printIndent()
Prints the characters used to indent SQL. |
protected void |
println()
Prints a newline. |
protected void |
println(String text)
Prints some text followed by a newline. |
protected void |
printlnIdentifier(String identifier)
Prints the given identifier followed by a newline. |
protected void |
printStartOfEmbeddedStatement()
Prints the start of an embedded statement. |
void |
setIndent(String indent)
Sets the string used to indent the SQL. |
void |
setValueLocale(String localeStr)
Sets the locale that is used for number and date formatting (when printing default values and in generates insert/update/delete statements). |
void |
setWriter(Writer writer)
Sets the writer for printing the DDL to. |
protected String |
shortenName(String name,
int desiredLength)
Generates a version of the name that has at most the specified length. |
protected boolean |
shouldGeneratePrimaryKeys(Column[] primaryKeyColumns)
Determines whether we should generate a primary key constraint for the given primary key columns. |
protected void |
writeColumn(Table table,
Column column)
Outputs the DDL for the specified column. |
void |
writeColumnAlterStmt(Table table,
Column column,
boolean isNewColumn)
Generates the alter statement to add or modify a single column on a table. |
protected void |
writeColumnAutoIncrementStmt(Table table,
Column column)
Prints that the column is an auto increment column. |
protected void |
writeColumnDefaultValue(Table table,
Column column)
Prints the default value of the column. |
void |
writeColumnDropStmt(Table table,
Column column)
Generates the statement to drop an column from a table. |
protected void |
writeColumnNotNullableStmt()
Prints that a column is not nullable. |
protected void |
writeColumnNullableStmt()
Prints that a column is nullable. |
protected void |
writeColumns(Table table)
Writes the columns of the given table. |
protected void |
writeEmbeddedForeignKeysStmt(Database database,
Table table)
Writes the foreign key constraints inside a create table () clause. |
protected void |
writeEmbeddedIndexCreateStmt(Table table,
Index index)
Writes the given embedded index of the table. |
protected void |
writeEmbeddedIndicesStmt(Table table)
Writes the indexes embedded within the create table statement. |
protected void |
writeEmbeddedPrimaryKeysStmt(Table table)
Writes the primary key constraints of the table inside its definition. |
protected void |
writeExternalForeignKeyCreateStmt(Database database,
Table table,
ForeignKey key)
Writes a single foreign key constraint using a alter table statement. |
protected void |
writeExternalForeignKeyDropStmt(Table table,
ForeignKey foreignKey)
Generates the statement to drop a foreignkey constraint from the database using an alter table statement. |
protected void |
writeExternalIndexCreateStmt(Table table,
Index index)
Writes the given index of the table. |
void |
writeExternalIndexDropStmt(Table table,
Index index)
Generates the statement to drop a non-embedded index from the database. |
protected void |
writeExternalIndicesCreateStmt(Table table)
Writes the indexes of the given table. |
protected void |
writeExternalPrimaryKeysCreateStmt(Table table)
Writes the primary key constraints of the table as alter table statements. |
protected void |
writeForeignReferences(ForeignKey key)
Writes a list of foreign references for the given foreign key. |
protected void |
writeLocalReferences(ForeignKey key)
Writes a list of local references for the given foreign key. |
protected void |
writePrimaryKeyStmt(Table table,
Column[] primaryKeyColumns)
Writes a primary key statement for the given columns. |
protected void |
writeTableAlterStmt(Table table)
Generates the first part of the ALTER TABLE statement including the table name. |
protected void |
writeTableComment(Table table)
Outputs a comment for the table. |
protected void |
writeTableCreationStmtEnding(Table table,
Map parameters)
Writes the end of the table creation statement. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected static final String SIZE_PLACEHOLDER
protected final org.apache.commons.logging.Log _log
Constructor Detail |
public SqlBuilder(PlatformInfo info)
info
- The plaftform informationMethod Detail |
public PlatformInfo getPlatformInfo()
public Writer getWriter()
public void setWriter(Writer writer)
writer
- The writerpublic DefaultValueHelper getDefaultValueHelper()
public String getIndent()
public void setIndent(String indent)
indent
- The indentation stringpublic String getValueLocale()
null
if default formatting is usedpublic void setValueLocale(String localeStr)
localeStr
- The new locale or null
if default formatting
should be used; Format is "language[_country[_variant]]"protected void addEscapedCharSequence(String charSequence, String escapedVersion)
charSequence
- The char sequenceescapedVersion
- The escaped versionpublic void createTables(Database database) throws IOException
database
- The database model
IOException
public void createTables(Database database, boolean dropTables) throws IOException
database
- The databasedropTables
- Whether to drop tables before creating them
IOException
public void createTables(Database database, CreationParameters params, boolean dropTables) throws IOException
database
- The databaseparams
- The parameters used in the creationdropTables
- Whether to drop tables before creating them
IOException
public void alterDatabase(Database currentModel, Database desiredModel) throws IOException
currentModel
- The current database schemadesiredModel
- The desired database schema
IOException
public void alterDatabase(Database currentModel, Database desiredModel, boolean doDrops, boolean modifyColumns) throws IOException
currentModel
- The current database schemadesiredModel
- The desired database schemadoDrops
- Whether columns and indexes should be dropped if not in the
new schemamodifyColumns
- Whether columns should be altered for datatype, size as required
IOException
public void alterDatabase(Database currentModel, Database desiredModel, CreationParameters params, boolean doDrops, boolean modifyColumns) throws IOException
currentModel
- The current database schemadesiredModel
- The desired database schemaparams
- The parameters used in the creation of new tables. Note that for existing
tables, parameters won't be applieddoDrops
- Whether columns and indexes should be dropped if not in the
new schemamodifyColumns
- Whether columns should be altered for datatype, size as required
IOException
protected void alterTable(Database currentModel, Table currentTable, Database desiredModel, Table desiredTable, boolean doDrops, boolean modifyColumns) throws IOException
currentModel
- The current modelcurrentTable
- The current table definitiondesiredModel
- The desired modeldesiredTable
- The desired table definitiondoDrops
- Whether columns and indexes should be dropped if not in the
new schemamodifyColumns
- Whether columns should be altered for datatype, size as required
IOException
public void createTable(Database database, Table table) throws IOException
database
- The database modeltable
- The table
IOException
public void createTable(Database database, Table table, Map parameters) throws IOException
database
- The database modeltable
- The tableparameters
- Additional platform-specific parameters for the table creation
IOException
public void createExternalForeignKeys(Database database) throws IOException
database
- The database
IOException
public void createExternalForeignKeys(Database database, Table table) throws IOException
database
- The database modeltable
- The table
IOException
public void dropTables(Database database) throws IOException
database
- The database
IOException
public void dropTable(Table table) throws IOException
table
- The table to drop
IOException
public void dropExternalForeignKeys(Table table) throws IOException
table
- The table
IOException
public String getInsertSql(Table table, Map columnValues, boolean genPlaceholders)
table
- The tablecolumnValues
- The columns values indexed by the column namesgenPlaceholders
- Whether to generate value placeholders for a
prepared statement
public String getUpdateSql(Table table, Map columnValues, boolean genPlaceholders)
table
- The tablecolumnValues
- Contains the values for the columns to update, and should also
contain the primary key values to identify the object to update
in case genPlaceholders
is false
genPlaceholders
- Whether to generate value placeholders for a
prepared statement (both for the pk values and the object values)
public String getDeleteSql(Table table, Map pkValues, boolean genPlaceholders)
table
- The tablepkValues
- The primary key values indexed by the column names, can be emptygenPlaceholders
- Whether to generate value placeholders for a
prepared statement
protected String getValueAsString(Column column, Object value)
column
- The columnvalue
- The value
public String getSelectLastInsertId(Table table)
null
.
table
- The table
null
if the database does not support thisprotected String shortenName(String name, int desiredLength)
name
- The original namedesiredLength
- The desired maximum length
protected String getTableName(Table table)
table
- The table
protected void writeTableComment(Table table) throws IOException
table
- The table
IOException
protected void writeTableAlterStmt(Table table) throws IOException
table
- The table being altered
IOException
protected void writeTableCreationStmtEnding(Table table, Map parameters) throws IOException
table
- The tableparameters
- Additional platform-specific parameters for the table creation
IOException
protected void writeColumns(Table table) throws IOException
table
- The table
IOException
protected String getColumnName(Column column) throws IOException
column
- The column
IOException
protected void writeColumn(Table table, Column column) throws IOException
table
- The table containing the columncolumn
- The column
IOException
public void writeColumnAlterStmt(Table table, Column column, boolean isNewColumn) throws IOException
table
- The table the index is oncolumn
- The column to dropisNewColumn
- Whether the column should be added
IOException
public void writeColumnDropStmt(Table table, Column column) throws IOException
table
- The table the index is oncolumn
- The column to drop
IOException
protected String getSqlType(Column column)
column
- The column
protected String getNativeType(Column column)
column
- The column
protected String getNativeDefaultValue(Column column)
column
- The column
protected String escapeStringValue(String value)
value
- The value
protected void writeColumnDefaultValue(Table table, Column column) throws IOException
table
- The tablecolumn
- The column
IOException
protected void writeColumnAutoIncrementStmt(Table table, Column column) throws IOException
table
- The tablecolumn
- The column
IOException
protected void writeColumnNullableStmt() throws IOException
IOException
protected void writeColumnNotNullableStmt() throws IOException
IOException
protected boolean columnsDiffer(Column currentColumn, Column desiredColumn)
currentColumn
- The current column as it is in the databasedesiredColumn
- The desired column
true
if the column specifications differpublic String getForeignKeyName(Table table, ForeignKey fk)
table
- The table for whith the foreign key is definedfk
- The foreign key
protected String getConstraintName(String prefix, Table table, String secondPart, String suffix)
prefix
- The constraint prefix, can be null
table
- The table that the constraint belongs tosecondPart
- The second name part, e.g. the name of the constraint columnsuffix
- The constraint suffix, e.g. a counter (can be null
)
protected void writeEmbeddedPrimaryKeysStmt(Table table) throws IOException
table
- The table
IOException
protected void writeExternalPrimaryKeysCreateStmt(Table table) throws IOException
table
- The table
IOException
protected boolean shouldGeneratePrimaryKeys(Column[] primaryKeyColumns)
primaryKeyColumns
- The pk columns
true
if a pk statement should be generated for the columnsprotected void writePrimaryKeyStmt(Table table, Column[] primaryKeyColumns) throws IOException
table
- The tableprimaryKeyColumns
- The primary columns
IOException
protected String getIndexName(Index index) throws IOException
index
- The index
IOException
protected void writeExternalIndicesCreateStmt(Table table) throws IOException
table
- The table
IOException
protected void writeEmbeddedIndicesStmt(Table table) throws IOException
table
- The table
IOException
protected void writeExternalIndexCreateStmt(Table table, Index index) throws IOException
table
- The tableindex
- The index
IOException
protected void writeEmbeddedIndexCreateStmt(Table table, Index index) throws IOException
table
- The tableindex
- The index
IOException
public void writeExternalIndexDropStmt(Table table, Index index) throws IOException
table
- The table the index is onindex
- The index to drop
IOException
protected void writeEmbeddedForeignKeysStmt(Database database, Table table) throws IOException
database
- The database modeltable
- The table
IOException
protected void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key) throws IOException
database
- The database modeltable
- The tablekey
- The foreign key
IOException
protected void writeLocalReferences(ForeignKey key) throws IOException
key
- The foreign key
IOException
protected void writeForeignReferences(ForeignKey key) throws IOException
key
- The foreign key
IOException
protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey) throws IOException
table
- The tableforeignKey
- The foreign key
IOException
protected void printComment(String text) throws IOException
text
- The comment text
IOException
protected void printStartOfEmbeddedStatement() throws IOException
IOException
protected void printEndOfStatement() throws IOException
IOException
protected void println() throws IOException
IOException
protected void print(String text) throws IOException
text
- The text to print
IOException
protected String getDelimitedIdentifier(String identifier)
identifier
- The identifier
protected void printIdentifier(String identifier) throws IOException
identifier
- The identifier
IOException
protected void printlnIdentifier(String identifier) throws IOException
identifier
- The identifier
IOException
protected void println(String text) throws IOException
text
- The text to print
IOException
protected void printIndent() throws IOException
IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |