org.apache.ddlutils.platform.mssql
Class MSSqlBuilder

java.lang.Object
  extended by org.apache.ddlutils.platform.SqlBuilder
      extended by org.apache.ddlutils.platform.mssql.MSSqlBuilder

public class MSSqlBuilder
extends SqlBuilder

The SQL Builder for the Microsoft SQL Server.

Version:
$Revision: 504014 $

Field Summary
 
Fields inherited from class org.apache.ddlutils.platform.SqlBuilder
_log, SIZE_PLACEHOLDER
 
Constructor Summary
MSSqlBuilder(Platform platform)
          Creates a new builder instance.
 
Method Summary
 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 dropExternalForeignKeys(Table table)
          Creates external foreignkey drop statements.
 void dropTable(Table table)
          Outputs the DDL to drop the table.
 String getDeleteSql(Table table, Map pkValues, boolean genPlaceholders)
          Creates the SQL for deleting an object from the specified table.
protected  String getDisableIdentityOverrideSql(Table table)
          Returns the SQL to disable identity override mode.
protected  String getEnableIdentityOverrideSql(Table table)
          Returns the SQL to enable identity override mode.
 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.
 String getSelectLastIdentityValues(Table table)
          Generates the SQL for querying the id that was created in the last insertion operation.
 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.
protected  DateFormat getValueDateFormat()
          Returns the format object for formatting dates in the specified locale.
protected  DateFormat getValueTimeFormat()
          Returns the format object for formatting times in the specified locale.
protected  void processChange(Database currentModel, Database desiredModel, AddColumnChange change)
          Processes the addition of a column to a table.
protected  void processChange(Database currentModel, Database desiredModel, RemoveColumnChange change)
          Processes the removal of a column from a table.
protected  void processChange(Database currentModel, Database desiredModel, RemovePrimaryKeyChange change)
          Processes the removal of a primary key from a table.
protected  void processChanges(Database currentModel, Database desiredModel, List changes, CreationParameters params)
          Processes the changes.
protected  void processColumnChange(Table sourceTable, Table targetTable, Column sourceColumn, Column targetColumn, boolean typeChange)
          Processes a change to a column.
protected  void processTableStructureChanges(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, Map parameters, List changes)
          Allows database-specific implementations to handle changes in a database specific manner.
protected  void writeColumnAutoIncrementStmt(Table table, Column column)
          Prints that the column is an auto increment column.
protected  void writeCopyDataStatement(Table sourceTable, Table targetTable)
          Writes a statement that copies the data from the source to the target table.
protected  void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
          Generates the statement to drop a foreignkey constraint from the database using an alter table statement.
 void writeExternalIndexDropStmt(Table table, Index index)
          Generates the statement to drop a non-embedded index from the database.
 
Methods inherited from class org.apache.ddlutils.platform.SqlBuilder
addEscapedCharSequence, alterDatabase, applyForSelectedChanges, areEqual, columnsDiffer, createExternalForeignKeys, createExternalForeignKeys, createTable, createTables, createTables, createTables, createTemporaryTable, createUniqueIdentifier, dropTable, dropTables, dropTemporaryTable, escapeStringValue, findCorrespondingForeignKey, getBareNativeType, getColumnName, getConstraintName, getDefaultValueHelper, getDelimitedIdentifier, getForeignKeyName, getIndent, getIndexName, getMaxColumnNameLength, getMaxConstraintNameLength, getMaxForeignKeyNameLength, getMaxTableNameLength, getNativeType, getPlatform, getPlatformInfo, getRealTargetTableFor, getSqlType, getTableName, getTemporaryTableFor, getValueLocale, getValueNumberFormat, getWriter, isValidDefaultValue, print, printComment, printDefaultValue, printEndOfStatement, printIdentifier, printIndent, println, println, printlnIdentifier, printStartOfEmbeddedStatement, processChange, processChange, processChange, processChange, processChange, processChange, processChange, processChange, processTableStructureChanges, processTableStructureChanges, setIndent, setValueDateFormat, setValueLocale, setValueNumberFormat, setValueTimeFormat, setWriter, shortenName, shouldGeneratePrimaryKeys, writeCastExpression, writeColumn, writeColumnDefaultValue, writeColumnDefaultValueStmt, writeColumnNotNullableStmt, writeColumnNullableStmt, writeColumns, writeEmbeddedForeignKeysStmt, writeEmbeddedIndexCreateStmt, writeEmbeddedIndicesStmt, writeEmbeddedPrimaryKeysStmt, writeExternalForeignKeyCreateStmt, writeExternalIndexCreateStmt, writeExternalIndicesCreateStmt, writeExternalPrimaryKeysCreateStmt, writeForeignReferences, writeLocalReferences, writePrimaryKeyStmt, writeTableAlterStmt, writeTableComment, writeTableCreationStmt, writeTableCreationStmtEnding
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MSSqlBuilder

public MSSqlBuilder(Platform platform)
Creates a new builder instance.

Parameters:
platform - The plaftform this builder belongs to
Method Detail

createTable

public void createTable(Database database,
                        Table table,
                        Map parameters)
                 throws IOException
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).

Overrides:
createTable in class SqlBuilder
Parameters:
database - The database model
table - The table
parameters - Additional platform-specific parameters for the table creation
Throws:
IOException

dropTable

public void dropTable(Table table)
               throws IOException
Outputs the DDL to drop the table. Note that this method does not drop foreign keys to this table. Use SqlBuilder.dropTable(Database, Table) if you want that.

Overrides:
dropTable in class SqlBuilder
Parameters:
table - The table to drop
Throws:
IOException

dropExternalForeignKeys

public void dropExternalForeignKeys(Table table)
                             throws IOException
Creates external foreignkey drop statements.

Overrides:
dropExternalForeignKeys in class SqlBuilder
Parameters:
table - The table
Throws:
IOException

getValueDateFormat

protected DateFormat getValueDateFormat()
Returns the format object for formatting dates in the specified locale.

Overrides:
getValueDateFormat in class SqlBuilder
Returns:
The date format object or null if no locale is set

getValueTimeFormat

protected DateFormat getValueTimeFormat()
Returns the format object for formatting times in the specified locale.

Overrides:
getValueTimeFormat in class SqlBuilder
Returns:
The time format object or null if no locale is set

getValueAsString

protected String getValueAsString(Column column,
                                  Object value)
Generates the string representation of the given value.

Overrides:
getValueAsString in class SqlBuilder
Parameters:
column - The column
value - The value
Returns:
The string representation

getNativeDefaultValue

protected String getNativeDefaultValue(Column column)
Returns the native default value for the column.

Overrides:
getNativeDefaultValue in class SqlBuilder
Parameters:
column - The column
Returns:
The native default value

writeColumnAutoIncrementStmt

protected void writeColumnAutoIncrementStmt(Table table,
                                            Column column)
                                     throws IOException
Prints that the column is an auto increment column.

Overrides:
writeColumnAutoIncrementStmt in class SqlBuilder
Parameters:
table - The table
column - The column
Throws:
IOException

writeExternalIndexDropStmt

public void writeExternalIndexDropStmt(Table table,
                                       Index index)
                                throws IOException
Generates the statement to drop a non-embedded index from the database.

Overrides:
writeExternalIndexDropStmt in class SqlBuilder
Parameters:
table - The table the index is on
index - The index to drop
Throws:
IOException

writeExternalForeignKeyDropStmt

protected void writeExternalForeignKeyDropStmt(Table table,
                                               ForeignKey foreignKey)
                                        throws IOException
Generates the statement to drop a foreignkey constraint from the database using an alter table statement.

Overrides:
writeExternalForeignKeyDropStmt in class SqlBuilder
Parameters:
table - The table
foreignKey - The foreign key
Throws:
IOException

getSelectLastIdentityValues

public String getSelectLastIdentityValues(Table table)
Generates the SQL for querying the id that was created in the last insertion operation. This is obviously only useful for pk fields that are auto-incrementing. A database that does not support this, will return null.

Overrides:
getSelectLastIdentityValues in class SqlBuilder
Parameters:
table - The table
Returns:
The sql, or null if the database does not support this

getEnableIdentityOverrideSql

protected String getEnableIdentityOverrideSql(Table table)
Returns the SQL to enable identity override mode.

Parameters:
table - The table to enable the mode for
Returns:
The SQL

getDisableIdentityOverrideSql

protected String getDisableIdentityOverrideSql(Table table)
Returns the SQL to disable identity override mode.

Parameters:
table - The table to disable the mode for
Returns:
The SQL

getDeleteSql

public String getDeleteSql(Table table,
                           Map pkValues,
                           boolean genPlaceholders)
Creates the SQL for deleting an object from the specified table. If values are given then a concrete delete statement is created, otherwise an delete statement usable in a prepared statement is build.

Overrides:
getDeleteSql in class SqlBuilder
Parameters:
table - The table
pkValues - The primary key values indexed by the column names, can be empty
genPlaceholders - Whether to generate value placeholders for a prepared statement
Returns:
The delete sql

getInsertSql

public String getInsertSql(Table table,
                           Map columnValues,
                           boolean genPlaceholders)
Creates the SQL for inserting an object into the specified table. If values are given then a concrete insert statement is created, otherwise an insert statement usable in a prepared statement is build.

Overrides:
getInsertSql in class SqlBuilder
Parameters:
table - The table
columnValues - The columns values indexed by the column names
genPlaceholders - Whether to generate value placeholders for a prepared statement
Returns:
The insertion sql

getUpdateSql

public String getUpdateSql(Table table,
                           Map columnValues,
                           boolean genPlaceholders)
Creates the SQL for updating an object in the specified table. If values are given then a concrete update statement is created, otherwise an update statement usable in a prepared statement is build.

Overrides:
getUpdateSql in class SqlBuilder
Parameters:
table - The table
columnValues - 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)
Returns:
The update sql

writeCopyDataStatement

protected void writeCopyDataStatement(Table sourceTable,
                                      Table targetTable)
                               throws IOException
Writes a statement that copies the data from the source to the target table. Note that this copies only those columns that are in both tables. Database-specific implementations might redefine this method though they usually it suffices to redefine the SqlBuilder.writeCastExpression(Column, Column) method.

Overrides:
writeCopyDataStatement in class SqlBuilder
Parameters:
sourceTable - The source table
targetTable - The target table
Throws:
IOException

processChanges

protected void processChanges(Database currentModel,
                              Database desiredModel,
                              List changes,
                              CreationParameters params)
                       throws IOException
Processes the changes. The default argument performs several passes:
  1. RemoveForeignKeyChange and RemoveIndexChange come first to allow for e.g. subsequent primary key changes or column removal.
  2. RemoveTableChange comes after the removal of foreign keys and indices.
  3. These are all handled together:
    RemovePrimaryKeyChange
    AddPrimaryKeyChange
    PrimaryKeyChange
    RemoveColumnChange
    AddColumnChange
    ColumnAutoIncrementChange
    ColumnDefaultValueChange
    ColumnRequiredChange
    ColumnDataTypeChange
    ColumnSizeChange
    The reason for this is that the default algorithm rebuilds the table for these changes and thus their order is irrelevant.
  4. AddTableChange
    needs to come after the table removal (so that tables of the same name are removed) and before the addition of foreign keys etc.
  5. AddForeignKeyChange and AddIndexChange come last after table/column/primary key additions or changes.

Overrides:
processChanges in class SqlBuilder
Parameters:
currentModel - The current database schema
desiredModel - The desired database schema
changes - The changes
params - The parameters used in the creation of new tables. Note that for existing tables, the parameters won't be applied
Throws:
IOException

processTableStructureChanges

protected void processTableStructureChanges(Database currentModel,
                                            Database desiredModel,
                                            Table sourceTable,
                                            Table targetTable,
                                            Map parameters,
                                            List changes)
                                     throws IOException
Allows database-specific implementations to handle changes in a database specific manner. Any handled change should be applied to the given current model (which is a copy of the real original model) and be removed from the list of changes.
In the default implementation, all AddPrimaryKeyChange changes are applied via an ALTER TABLE ADD CONSTRAINT statement.

Overrides:
processTableStructureChanges in class SqlBuilder
Parameters:
currentModel - The current database schema
desiredModel - The desired database schema
sourceTable - The original table
targetTable - The desired table
parameters - The creation parameters for the table
changes - The change objects for the target table
Throws:
IOException

processChange

protected void processChange(Database currentModel,
                             Database desiredModel,
                             AddColumnChange change)
                      throws IOException
Processes the addition of a column to a table.

Parameters:
currentModel - The current database schema
desiredModel - The desired database schema
change - The change object
Throws:
IOException

processChange

protected void processChange(Database currentModel,
                             Database desiredModel,
                             RemoveColumnChange change)
                      throws IOException
Processes the removal of a column from a table.

Parameters:
currentModel - The current database schema
desiredModel - The desired database schema
change - The change object
Throws:
IOException

processChange

protected void processChange(Database currentModel,
                             Database desiredModel,
                             RemovePrimaryKeyChange change)
                      throws IOException
Processes the removal of a primary key from a table.

Parameters:
currentModel - The current database schema
desiredModel - The desired database schema
change - The change object
Throws:
IOException

processColumnChange

protected void processColumnChange(Table sourceTable,
                                   Table targetTable,
                                   Column sourceColumn,
                                   Column targetColumn,
                                   boolean typeChange)
                            throws IOException
Processes a change to a column.

Parameters:
sourceTable - The current table
targetTable - The desired table
sourceColumn - The current column
targetColumn - The desired column
typeChange - Whether this is a type change
Throws:
IOException


Copyright © 2005-2007 Apache Software Foundation. All Rights Reserved.