protected void processTableStructureChanges(Database currentModel,
Database desiredModel,
CreationParameters params,
Collection changes) throws IOException
{
ListOrderedMap changesPerTable = new ListOrderedMap();
ListOrderedMap unchangedFKs = new ListOrderedMap();
boolean caseSensitive = getPlatform().isDelimitedIdentifierModeOn();
// we first sort the changes for the tables
// however since the changes might contain source or target tables
// we use the names rather than the table objects
for (Iterator changeIt = changes.iterator(); changeIt.hasNext();)
{
TableChange change = (TableChange)changeIt.next();
String name = change.getChangedTable().getName();
if (!caseSensitive)
{
name = name.toUpperCase();
}
List changesForTable = (ArrayList)changesPerTable.get(name);
if (changesForTable == null)
{
changesForTable = new ArrayList();
changesPerTable.put(name, changesForTable);
unchangedFKs.put(name, getUnchangedForeignKeys(currentModel, desiredModel, name));
}
changesForTable.add(change);
}
// we also need to drop the foreign keys of the unchanged tables referencing the changed tables
addRelevantFKsFromUnchangedTables(currentModel, desiredModel, changesPerTable.keySet(), unchangedFKs);
// we're dropping the unchanged foreign keys
for (Iterator tableFKIt = unchangedFKs.entrySet().iterator(); tableFKIt.hasNext();)
{
Map.Entry entry = (Map.Entry)tableFKIt.next();
Table targetTable = desiredModel.findTable((String)entry.getKey(), caseSensitive);
for (Iterator fkIt = ((List)entry.getValue()).iterator(); fkIt.hasNext();)
{
writeExternalForeignKeyDropStmt(targetTable, (ForeignKey)fkIt.next());
}
}
// We're using a copy of the current model so that the table structure changes can
// modify it
Database copyOfCurrentModel = null;
try
{
copyOfCurrentModel = (Database)currentModel.clone();
}
catch (CloneNotSupportedException ex)
{
throw new DdlUtilsException(ex);
}
for (Iterator tableChangeIt = changesPerTable.entrySet().iterator(); tableChangeIt.hasNext();)
{
Map.Entry entry = (Map.Entry)tableChangeIt.next();
Table targetTable = desiredModel.findTable((String)entry.getKey(), caseSensitive);
processTableStructureChanges(copyOfCurrentModel,
desiredModel,
(String)entry.getKey(),
params == null ? null : params.getParametersFor(targetTable),
(List)entry.getValue());
}
// and finally we're re-creating the unchanged foreign keys
for (Iterator tableFKIt = unchangedFKs.entrySet().iterator(); tableFKIt.hasNext();)
{
Map.Entry entry = (Map.Entry)tableFKIt.next();
Table targetTable = desiredModel.findTable((String)entry.getKey(), caseSensitive);
for (Iterator fkIt = ((List)entry.getValue()).iterator(); fkIt.hasNext();)