use of org.jumpmind.db.util.MultiInstanceofPredicate in project symmetric-ds by JumpMind.
the class AbstractDdlBuilder method applyForSelectedChanges.
/**
* 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<IModelChange> 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;
}
}
});
}
use of org.jumpmind.db.util.MultiInstanceofPredicate in project symmetric-ds by JumpMind.
the class AbstractDdlBuilder method processChanges.
/**
* Processes the changes. The default argument performs several passes:
* <ol>
* <li>
* {@link org.jumpmind.db.alter.RemoveForeignKeyChange} and
* {@link org.jumpmind.db.alter.RemoveIndexChange} come first to allow for
* e.g. subsequent primary key changes or column removal.</li>
* <li>{@link org.jumpmind.db.alter.RemoveTableChange} comes after the
* removal of foreign keys and indices.</li>
* <li>These are all handled together:<br/>
* {@link org.jumpmind.db.alter.RemovePrimaryKeyChange}<br/>
* {@link org.jumpmind.db.alter.AddPrimaryKeyChange}<br/>
* {@link org.jumpmind.db.alter.PrimaryKeyChange}<br/>
* {@link org.jumpmind.db.alter.RemoveColumnChange}<br/>
* {@link org.jumpmind.db.alter.AddColumnChange}<br/>
* {@link org.jumpmind.db.alter.ColumnAutoIncrementChange} <br/>
* {@link org.jumpmind.db.alter.ColumnDefaultValueChange} <br/>
* {@link org.jumpmind.db.alter.ColumnRequiredChange}<br/>
* {@link org.jumpmind.db.alter.ColumnDataTypeChange}<br/>
* {@link org.jumpmind.db.alter.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.jumpmind.db.alter.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.jumpmind.db.alter.AddForeignKeyChange} and
* {@link org.jumpmind.db.alter.AddIndexChange} come last after
* table/column/primary key additions or changes.</li>
* </ol>
*/
@SuppressWarnings("unchecked")
protected void processChanges(Database currentModel, Database desiredModel, List<IModelChange> changes, StringBuilder ddl) {
CallbackClosure callbackClosure = new CallbackClosure(this, "processChange", new Class[] { Database.class, Database.class, null, StringBuilder.class }, new Object[] { currentModel, desiredModel, null, ddl });
// 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, ColumnAutoIncrementChange.class, ColumnDefaultValueChange.class, ColumnRequiredChange.class, ColumnDataTypeChange.class, ColumnSizeChange.class, CopyColumnValueChange.class });
processTableStructureChanges(currentModel, desiredModel, CollectionUtils.select(changes, predicate), ddl);
// 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);
}
Aggregations