📄 sqlbuilder.java
字号:
/**
* Returns a new date format object for formatting numbers in the specified locale.
* Platforms can override this if necessary.
*
* @param format The number format object
*/
protected void setValueNumberFormat(NumberFormat format)
{
_valueNumberFormat = format;
}
/**
* Adds a char sequence that needs escaping, and its escaped version.
*
* @param charSequence The char sequence
* @param escapedVersion The escaped version
*/
protected void addEscapedCharSequence(String charSequence, String escapedVersion)
{
_charSequencesToEscape.put(charSequence, escapedVersion);
}
/**
* Returns the maximum number of characters that a table name can have.
* This method is intended to give platform specific builder implementations
* more control over the maximum length.
*
* @return The number of characters, or -1 if not limited
*/
public int getMaxTableNameLength()
{
return getPlatformInfo().getMaxTableNameLength();
}
/**
* Returns the maximum number of characters that a column name can have.
* This method is intended to give platform specific builder implementations
* more control over the maximum length.
*
* @return The number of characters, or -1 if not limited
*/
public int getMaxColumnNameLength()
{
return getPlatformInfo().getMaxColumnNameLength();
}
/**
* Returns the maximum number of characters that a constraint name can have.
* This method is intended to give platform specific builder implementations
* more control over the maximum length.
*
* @return The number of characters, or -1 if not limited
*/
public int getMaxConstraintNameLength()
{
return getPlatformInfo().getMaxConstraintNameLength();
}
/**
* Returns the maximum number of characters that a foreign key name can have.
* This method is intended to give platform specific builder implementations
* more control over the maximum length.
*
* @return The number of characters, or -1 if not limited
*/
public int getMaxForeignKeyNameLength()
{
return getPlatformInfo().getMaxForeignKeyNameLength();
}
//
// public interface
//
/**
* Outputs the DDL required to drop and (re)create all tables in the database model.
*
* @param database The database model
*/
public void createTables(Database database) throws IOException
{
createTables(database, null, true);
}
/**
* Outputs the DDL required to drop (if requested) and (re)create all tables in the database model.
*
* @param database The database
* @param dropTables Whether to drop tables before creating them
*/
public void createTables(Database database, boolean dropTables) throws IOException
{
createTables(database, null, dropTables);
}
/**
* Outputs the DDL required to drop (if requested) and (re)create all tables in the database model.
*
* @param database The database
* @param params The parameters used in the creation
* @param dropTables Whether to drop tables before creating them
*/
public void createTables(Database database, CreationParameters params, boolean dropTables) throws IOException
{
if (dropTables)
{
dropTables(database);
}
for (int idx = 0; idx < database.getTableCount(); idx++)
{
Table table = database.getTable(idx);
writeTableComment(table);
createTable(database,
table,
params == null ? null : params.getParametersFor(table));
}
// we're writing the external foreignkeys last to ensure that all referenced tables are already defined
createExternalForeignKeys(database);
}
/**
* Generates the DDL to modify an existing database so the schema matches
* the specified database schema by using drops, modifications and additions.
* Database-specific implementations can change aspect of this algorithm by
* redefining the individual methods that compromise it.
*
* @param currentModel The current database schema
* @param desiredModel The desired database schema
* @param params The parameters used in the creation of new tables. Note that for existing
* tables, the parameters won't be applied
*/
public void alterDatabase(Database currentModel, Database desiredModel, CreationParameters params) throws IOException
{
ModelComparator comparator = new ModelComparator(getPlatformInfo(),
getPlatform().isDelimitedIdentifierModeOn());
List changes = comparator.compare(currentModel, desiredModel);
processChanges(currentModel, desiredModel, changes, params);
}
/**
* Calls the given closure for all changes that are of one of the given types, and
* then removes them from the changes collection.
*
* @param changes The changes
* @param changeTypes The types to search for
* @param closure The closure to invoke
*/
protected void applyForSelectedChanges(Collection changes, Class[] changeTypes, final Closure closure)
{
final Predicate predicate = new MultiInstanceofPredicate(changeTypes);
// basically we filter the changes for all objects where the above predicate
// returns true, and for these filtered objects we invoke the given closure
CollectionUtils.filter(changes,
new Predicate()
{
public boolean evaluate(Object obj)
{
if (predicate.evaluate(obj))
{
closure.execute(obj);
return false;
}
else
{
return true;
}
}
});
}
/**
* Processes the changes. The default argument performs several passes:
* <ol>
* <li>{@link org.apache.ddlutils.alteration.RemoveForeignKeyChange} and
* {@link org.apache.ddlutils.alteration.RemoveIndexChange} come first
* to allow for e.g. subsequent primary key changes or column removal.</li>
* <li>{@link org.apache.ddlutils.alteration.RemoveTableChange}
* comes after the removal of foreign keys and indices.</li>
* <li>These are all handled together:<br/>
* {@link org.apache.ddlutils.alteration.RemovePrimaryKeyChange}<br/>
* {@link org.apache.ddlutils.alteration.AddPrimaryKeyChange}<br/>
* {@link org.apache.ddlutils.alteration.PrimaryKeyChange}<br/>
* {@link org.apache.ddlutils.alteration.RemoveColumnChange}<br/>
* {@link org.apache.ddlutils.alteration.AddColumnChange}<br/>
* {@link org.apache.ddlutils.alteration.ColumnAutoIncrementChange}<br/>
* {@link org.apache.ddlutils.alteration.ColumnDefaultValueChange}<br/>
* {@link org.apache.ddlutils.alteration.ColumnRequiredChange}<br/>
* {@link org.apache.ddlutils.alteration.ColumnDataTypeChange}<br/>
* {@link org.apache.ddlutils.alteration.ColumnSizeChange}<br/>
* The reason for this is that the default algorithm rebuilds the table for these
* changes and thus their order is irrelevant.</li>
* <li>{@link org.apache.ddlutils.alteration.AddTableChange}<br/>
* needs to come after the table removal (so that tables of the same name are
* removed) and before the addition of foreign keys etc.</li>
* <li>{@link org.apache.ddlutils.alteration.AddForeignKeyChange} and
* {@link org.apache.ddlutils.alteration.AddIndexChange} come last
* after table/column/primary key additions or changes.</li>
* </ol>
*
* @param currentModel The current database schema
* @param desiredModel The desired database schema
* @param changes The changes
* @param params The parameters used in the creation of new tables. Note that for existing
* tables, the parameters won't be applied
*/
protected void processChanges(Database currentModel,
Database desiredModel,
List changes,
CreationParameters params) throws IOException
{
CallbackClosure callbackClosure = new CallbackClosure(this,
"processChange",
new Class[] { Database.class, Database.class, CreationParameters.class, null },
new Object[] { currentModel, desiredModel, params, null });
// 1st pass: removing external constraints and indices
applyForSelectedChanges(changes,
new Class[] { RemoveForeignKeyChange.class,
RemoveIndexChange.class },
callbackClosure);
// 2nd pass: removing tables
applyForSelectedChanges(changes,
new Class[] { RemoveTableChange.class },
callbackClosure);
// 3rd pass: changing the structure of tables
Predicate predicate = new MultiInstanceofPredicate(new Class[] { RemovePrimaryKeyChange.class,
AddPrimaryKeyChange.class,
PrimaryKeyChange.class,
RemoveColumnChange.class,
AddColumnChange.class,
ColumnOrderChange.class,
ColumnAutoIncrementChange.class,
ColumnDefaultValueChange.class,
ColumnRequiredChange.class,
ColumnDataTypeChange.class,
ColumnSizeChange.class });
processTableStructureChanges(currentModel,
desiredModel,
params,
CollectionUtils.select(changes, predicate));
// 4th pass: adding tables
applyForSelectedChanges(changes,
new Class[] { AddTableChange.class },
callbackClosure);
// 5th pass: adding external constraints and indices
applyForSelectedChanges(changes,
new Class[] { AddForeignKeyChange.class,
AddIndexChange.class },
callbackClosure);
}
/**
* This is a fall-through callback which generates a warning because a specific
* change type wasn't handled.
*
* @param currentModel The current database schema
* @param desiredModel The desired database schema
* @param params The parameters used in the creation of new tables. Note that for existing
* tables, the parameters won't be applied
* @param change The change object
*/
protected void processChange(Database currentModel,
Database desiredModel,
CreationParameters params,
ModelChange change) throws IOException
{
_log.warn("Change of type " + change.getClass() + " was not handled");
}
/**
* Processes the change representing the removal of a foreign key.
*
* @param currentModel The current database schema
* @param desiredModel The desired database schema
* @param params The parameters used in the creation of new tables. Note that for existing
* tables, the parameters won't be applied
* @param change The change object
*/
protected void processChange(Database currentModel,
Database desiredModel,
CreationParameters params,
RemoveForeignKeyChange change) throws IOException
{
writeExternalForeignKeyDropStmt(change.getChangedTable(), change.getForeignKey());
change.apply(currentModel, getPlatform().isDelimitedIdentifierModeOn());
}
/**
* Processes the change representing the removal of an index.
*
* @param currentModel The current database schema
* @param desiredModel The desired database schema
* @param params The parameters used in the creation of new tables. Note that for existing
* tables, the parameters won't be applied
* @param change The change object
*/
protected void processChange(Database currentModel,
Database desiredModel,
CreationParameters params,
RemoveIndexChange change) throws IOException
{
writeExternalIndexDropStmt(change.getChangedTable(), change.getIndex());
change.apply(currentModel, getPlatform().isDelimitedIdentifierModeOn());
}
/**
* Processes the change representing the removal of a table.
*
* @param currentModel The current database schema
* @param desiredModel The desired database schema
* @param params The parameters used in the creation of new tables. Note that for existing
* tables, the parameters won't be applied
* @param change The change object
*/
protected void processChange(Database currentModel,
Database desiredModel,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -