public abstract class SqlBuilder extends Object
| Modifier and Type | Field and Description |
|---|---|
protected org.apache.commons.logging.Log |
_log
The Log to which logging calls will be made.
|
static String |
LINE_SEPARATOR
The line separator for in between sql commands.
|
protected static String |
SIZE_PLACEHOLDER
The placeholder for the size value in the native type spec.
|
| Constructor and Description |
|---|
SqlBuilder(Platform platform)
Creates a new sql builder.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addColumn(Database model,
Table table,
Column newColumn)
Prints the SQL for adding a column to a table.
|
protected void |
addEscapedCharSequence(String charSequence,
String escapedVersion)
Adds a char sequence that needs escaping, and its escaped version.
|
protected boolean |
areEqual(String string1,
String string2,
boolean caseMatters)
Compares the two strings.
|
protected boolean |
columnsDiffer(Column currentColumn,
Column desiredColumn)
Compares the current column in the database with the desired one.
|
protected void |
copyData(Table sourceTable,
Table targetTable)
Writes a statement that copies the data from the source to the target table.
|
void |
createForeignKey(Database database,
Table table,
ForeignKey foreignKey)
Writes a single foreign key constraint using a alter table statement.
|
void |
createForeignKeys(Database database)
Creates the external foreignkey creation statements for all tables in the database.
|
void |
createForeignKeys(Database database,
Table table)
Creates external foreignkey creation statements if necessary.
|
void |
createIndex(Table table,
Index index)
Writes the given index for the table using an external index creation statement.
|
void |
createIndexes(Table table)
Writes the indexes for the given table using external index creation statements.
|
void |
createPrimaryKey(Table table,
Column[] primaryKeyColumns)
Writes the primary key constraints of the table as alter table statements.
|
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.
|
protected void |
createTemporaryTable(Database database,
Table table,
Map parameters)
Outputs the DDL to create the given temporary table.
|
protected String |
createUniqueIdentifier()
Creates a reasonably unique identifier only consisting of hexadecimal characters and underscores.
|
void |
dropForeignKey(Table table,
ForeignKey foreignKey)
Generates the statement to drop a foreignkey constraint from the database using an
alter table statement.
|
void |
dropForeignKeys(Table table)
Creates external foreignkey drop statements.
|
void |
dropIndex(Table table,
Index index)
Generates the statement to drop a non-embedded index from the database.
|
void |
dropTable(Database database,
Table table)
Outputs the DDL required to drop the given table.
|
void |
dropTable(Table table)
Outputs the DDL to drop the table.
|
void |
dropTables(Database database)
Outputs the DDL required to drop the database.
|
protected void |
dropTemporaryTable(Database database,
Table table)
Outputs the DDL to drop the given temporary table.
|
protected String |
escapeStringValue(String value)
Escapes the necessary characters in given string value.
|
protected String |
getBareNativeType(Column column)
Returns the bare database-native type for the given column without any size specifies.
|
protected String |
getColumnName(Column column)
Returns the column name.
|
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.
|
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.
|
int |
getMaxColumnNameLength()
Returns the maximum number of characters that a column name can have.
|
int |
getMaxConstraintNameLength()
Returns the maximum number of characters that a constraint name can have.
|
int |
getMaxForeignKeyNameLength()
Returns the maximum number of characters that a foreign key name can have.
|
int |
getMaxTableNameLength()
Returns the maximum number of characters that a table name can have.
|
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.
|
Platform |
getPlatform()
Returns the platform object.
|
PlatformInfo |
getPlatformInfo()
Returns the platform info object.
|
String |
getSelectLastIdentityValues(Table table)
Generates the SQL for querying the id that was created in the last insertion
operation.
|
protected String |
getSizeSpec(Column column)
Returns the size specification for the given column.
|
protected String |
getSqlType(Column column)
Returns the full SQL type specification (including size and precision/scale) for the
given column.
|
protected String |
getSqlType(Column column,
String nativeType)
Returns the full SQL type specification (including size and precision/scale) for the
given column.
|
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.
|
String |
getUpdateSql(Table table,
Map oldColumnValues,
Map newColumnValues,
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.
|
protected DateFormat |
getValueDateFormat()
Returns the format object for formatting dates in the specified locale.
|
String |
getValueLocale()
Returns the locale that is used for number and date formatting
(when printing default values and in generates insert/update/delete
statements).
|
protected NumberFormat |
getValueNumberFormat()
Returns the format object for formatting numbers in the specified locale.
|
protected DateFormat |
getValueTimeFormat()
Returns the format object for formatting times in the specified locale.
|
Writer |
getWriter()
Returns the writer that the DDL is printed to.
|
protected boolean |
isValidDefaultValue(String defaultSpec,
int typeCode)
Determines whether the given default spec is a non-empty spec that shall be used in a DEFAULT
expression.
|
protected void |
print(String text)
Prints some text.
|
protected void |
printComment(String text)
Prints an SQL comment to the current stream.
|
protected void |
printDefaultValue(Object defaultValue,
int typeCode)
Prints the default value of the column.
|
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.
|
protected void |
setValueDateFormat(DateFormat format)
Sets the format object for formatting dates in the specified locale.
|
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).
|
protected void |
setValueNumberFormat(NumberFormat format)
Returns a new date format object for formatting numbers in the specified locale.
|
protected void |
setValueTimeFormat(DateFormat format)
Sets the date format object for formatting times in the specified locale.
|
void |
setWriter(Writer writer)
Sets the writer for printing the DDL to.
|
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.
|
void |
writeAddIdentityColumnUsingAlterTable(Table table,
Column column)
Write the ALTER TABLE statement to set a given column as a generated
identity column.
|
protected void |
writeCastExpression(Column sourceColumn,
Column targetColumn)
Writes a cast expression that converts the value of the source column to the data type
of the target column.
|
protected void |
writeColumn(Table table,
Column column)
Outputs the DDL for the specified column.
|
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.
|
protected void |
writeColumnDefaultValueStmt(Table table,
Column column)
Prints the default value stmt part for the column.
|
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 |
writeForeignKeyOnDeleteAction(Table table,
ForeignKey foreignKey)
Writes the onDelete action for the given foreign key.
|
protected void |
writeForeignKeyOnUpdateAction(Table table,
ForeignKey foreignKey)
Writes the onDelete action for the given foreign key.
|
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 |
writeTableCreationStmt(Database database,
Table table,
Map parameters)
Writes the table creation statement without the statement end.
|
protected void |
writeTableCreationStmtEnding(Table table,
Map parameters)
Writes the end of the table creation statement.
|
public static final String LINE_SEPARATOR
protected static final String SIZE_PLACEHOLDER
protected final org.apache.commons.logging.Log _log
public SqlBuilder(Platform platform)
platform - The plaftform this builder belongs topublic Platform getPlatform()
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 DateFormat getValueDateFormat()
protected void setValueDateFormat(DateFormat format)
format - The date format objectprotected DateFormat getValueTimeFormat()
protected void setValueTimeFormat(DateFormat format)
format - The time format objectprotected NumberFormat getValueNumberFormat()
protected void setValueNumberFormat(NumberFormat format)
format - The number format objectprotected void addEscapedCharSequence(String charSequence, String escapedVersion)
charSequence - The char sequenceescapedVersion - The escaped versionpublic int getMaxTableNameLength()
public int getMaxColumnNameLength()
public int getMaxConstraintNameLength()
public int getMaxForeignKeyNameLength()
public void createTables(Database database) throws IOException
database - The database modelIOExceptionpublic void createTables(Database database, boolean dropTables) throws IOException
database - The databasedropTables - Whether to drop tables before creating themIOExceptionpublic 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 themIOExceptionprotected void createTemporaryTable(Database database, Table table, Map parameters) throws IOException
createTable(Database, Table, Map).database - The database modeltable - The tableparameters - Additional platform-specific parameters for the table creationIOExceptionprotected void dropTemporaryTable(Database database, Table table) throws IOException
dropTable(Table).database - The database modeltable - The tableIOExceptionprotected void copyData(Table sourceTable, Table targetTable) throws IOException
writeCastExpression(Column, Column) method.sourceTable - The source tabletargetTable - The target tableIOExceptionprotected void writeCastExpression(Column sourceColumn, Column targetColumn) throws IOException
sourceColumn - The source columntargetColumn - The target columnIOExceptionprotected boolean areEqual(String string1, String string2, boolean caseMatters)
string1 - The first stringstring2 - The second stringcaseMatters - Whether case matters in the comparisontrue if the string are equalpublic void createTable(Database database, Table table) throws IOException
database - The database modeltable - The tableIOExceptionpublic void createTable(Database database, Table table, Map parameters) throws IOException
database - The database modeltable - The tableparameters - Additional platform-specific parameters for the table creationIOExceptionpublic void createPrimaryKey(Table table, Column[] primaryKeyColumns) throws IOException
table - The tableprimaryKeyColumns - The primary key columnsIOExceptionpublic void createIndexes(Table table) throws IOException
table - The tableIOExceptionpublic void createIndex(Table table, Index index) throws IOException
table - The tableindex - The indexIOExceptionpublic void createForeignKeys(Database database) throws IOException
database - The databaseIOExceptionpublic void createForeignKeys(Database database, Table table) throws IOException
database - The database modeltable - The tableIOExceptionpublic void createForeignKey(Database database, Table table, ForeignKey foreignKey) throws IOException
database - The database modeltable - The tableforeignKey - The foreign keyIOExceptionpublic void addColumn(Database model, Table table, Column newColumn) throws IOException
model - The database modeltable - The tablenewColumn - The new columnIOExceptionpublic void dropTables(Database database) throws IOException
database - The databaseIOExceptionpublic void dropTable(Database database, Table table) throws IOException
database - The databasetable - The tableIOExceptionpublic void dropTable(Table table) throws IOException
dropTable(Database, Table)
if you want that.table - The table to dropIOExceptionpublic void dropForeignKeys(Table table) throws IOException
table - The tableIOExceptionpublic void dropForeignKey(Table table, ForeignKey foreignKey) throws IOException
table - The tableforeignKey - The foreign keyIOExceptionpublic 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 statementpublic 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 falsegenPlaceholders - Whether to generate value placeholders for a
prepared statement (both for the pk values and the object values)public String getUpdateSql(Table table, Map oldColumnValues, Map newColumnValues, boolean genPlaceholders)
table - The tableoldColumnValues - Contains the column values to identify the row to updatenewColumnValues - Contains the values for the columns to updategenPlaceholders - 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)
genPlaceholders, the generated SQL will contain
prepared statement place holders or concrete values. Only those primary key
columns wil be used that are present in the given map. If the map is null or
completely empty, then the SQL will not have a WHERE clause. The SQL will contain
the columns in the order defined in the table.table - The tablepkValues - The primary key columns to use, and optionally their valuesgenPlaceholders - Whether to generate value placeholders for a
prepared statementprotected String getValueAsString(Column column, Object value)
column - The columnvalue - The valuepublic String getSelectLastIdentityValues(Table table)
null.table - The tablenull if the database does not support thispublic String shortenName(String name, int desiredLength)
name - The original namedesiredLength - The desired maximum lengthpublic String getTableName(Table table)
table - The tableprotected void writeTableComment(Table table) throws IOException
table - The tableIOExceptionprotected void writeTableAlterStmt(Table table) throws IOException
table - The table being alteredIOExceptionprotected void writeTableCreationStmt(Database database, Table table, Map parameters) throws IOException
database - The modeltable - The tableparameters - Additional platform-specific parameters for the table creationIOExceptionprotected void writeTableCreationStmtEnding(Table table, Map parameters) throws IOException
table - The tableparameters - Additional platform-specific parameters for the table creationIOExceptionprotected void writeColumns(Table table) throws IOException
table - The tableIOExceptionprotected String getColumnName(Column column) throws IOException
column - The columnIOExceptionprotected void writeColumn(Table table, Column column) throws IOException
table - The table containing the columncolumn - The columnIOExceptionprotected String getSqlType(Column column)
column - The columnprotected String getSqlType(Column column, String nativeType)
column - The columnnativeType - Overrides the native type of the column; can include the size placeholderprotected String getNativeType(Column column)
column - The columnprotected String getBareNativeType(Column column)
column - The columnprotected String getSizeSpec(Column column)
column - The columnprotected String getNativeDefaultValue(Column column)
column - The columnprotected String escapeStringValue(String value)
value - The valueprotected boolean isValidDefaultValue(String defaultSpec, int typeCode)
defaultSpec - The default value spectypeCode - The JDBC type codetrue if the default value spec is validprotected void writeColumnDefaultValueStmt(Table table, Column column) throws IOException
table - The tablecolumn - The columnIOExceptionprotected void writeColumnDefaultValue(Table table, Column column) throws IOException
table - The tablecolumn - The columnIOExceptionprotected void printDefaultValue(Object defaultValue, int typeCode) throws IOException
defaultValue - The default valuetypeCode - The type code to write the default value forIOExceptionprotected void writeColumnAutoIncrementStmt(Table table, Column column) throws IOException
table - The tablecolumn - The columnIOExceptionprotected void writeColumnNullableStmt()
throws IOException
IOExceptionprotected void writeColumnNotNullableStmt()
throws IOException
IOExceptionprotected boolean columnsDiffer(Column currentColumn, Column desiredColumn)
currentColumn - The current column as it is in the databasedesiredColumn - The desired columntrue if the column specifications differpublic String getForeignKeyName(Table table, ForeignKey fk)
table - The table for whith the foreign key is definedfk - The foreign keypublic String getConstraintName(String prefix, Table table, String secondPart, String suffix)
prefix - The constraint prefix, can be nulltable - 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 tableIOExceptionprotected boolean shouldGeneratePrimaryKeys(Column[] primaryKeyColumns)
primaryKeyColumns - The pk columnstrue if a pk statement should be generated for the columnsprotected void writePrimaryKeyStmt(Table table, Column[] primaryKeyColumns) throws IOException
table - The tableprimaryKeyColumns - The primary columnsIOExceptionpublic String getIndexName(Index index)
index - The indexprotected void writeEmbeddedIndicesStmt(Table table) throws IOException
table - The tableIOExceptionprotected void writeEmbeddedIndexCreateStmt(Table table, Index index) throws IOException
table - The tableindex - The indexIOExceptionpublic void dropIndex(Table table, Index index) throws IOException
table - The table the index is onindex - The index to dropIOExceptionprotected void writeEmbeddedForeignKeysStmt(Database database, Table table) throws IOException
database - The database modeltable - The tableIOExceptionprotected void writeLocalReferences(ForeignKey key) throws IOException
key - The foreign keyIOExceptionprotected void writeForeignReferences(ForeignKey key) throws IOException
key - The foreign keyIOExceptionprotected void writeForeignKeyOnDeleteAction(Table table, ForeignKey foreignKey) throws IOException
table - The tableforeignKey - The foreignkeyIOExceptionprotected void writeForeignKeyOnUpdateAction(Table table, ForeignKey foreignKey) throws IOException
table - The tableforeignKey - The foreignkeyIOExceptionprotected void printComment(String text) throws IOException
text - The comment textIOExceptionprotected void printStartOfEmbeddedStatement()
throws IOException
IOExceptionprotected void printEndOfStatement()
throws IOException
IOExceptionprotected void println()
throws IOException
IOExceptionprotected void print(String text) throws IOException
text - The text to printIOExceptionprotected String getDelimitedIdentifier(String identifier)
identifier - The identifierprotected void printIdentifier(String identifier) throws IOException
identifier - The identifierIOExceptionprotected void printlnIdentifier(String identifier) throws IOException
identifier - The identifierIOExceptionprotected void println(String text) throws IOException
text - The text to printIOExceptionprotected void printIndent()
throws IOException
IOExceptionprotected String createUniqueIdentifier()
d578271282b42fce__2955b56e_107df3fbc96__8000 and is 48 characters long.public void writeAddIdentityColumnUsingAlterTable(Table table, Column column) throws IOException
IOExceptionCopyright © 2010-2015 Pivotal Software, Inc. All rights reserved.