use of org.jumpmind.db.alter.AddPrimaryKeyChange in project symmetric-ds by JumpMind.
the class InterbaseDdlBuilder method processTableStructureChanges.
@Override
protected void processTableStructureChanges(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) {
// TODO: Dropping of primary keys is currently not supported because we
// cannot
// determine the pk constraint names and drop them in one go
// (We could used a stored procedure if Interbase would allow them to
// use DDL)
// This will be easier once named primary keys are supported
boolean pkColumnAdded = false;
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof AddColumnChange) {
AddColumnChange addColumnChange = (AddColumnChange) change;
// add a new one afterwards which is not supported yet
if (addColumnChange.getNewColumn().isPrimaryKey()) {
pkColumnAdded = true;
} else {
processChange(currentModel, desiredModel, addColumnChange, ddl);
changeIt.remove();
}
} else if (change instanceof RemoveColumnChange) {
RemoveColumnChange removeColumnChange = (RemoveColumnChange) change;
// add a new one afterwards which is not supported yet
if (!removeColumnChange.getColumn().isPrimaryKey()) {
processChange(currentModel, desiredModel, removeColumnChange, ddl);
changeIt.remove();
}
} else if (change instanceof CopyColumnValueChange) {
CopyColumnValueChange copyColumnChange = (CopyColumnValueChange) change;
processChange(currentModel, desiredModel, copyColumnChange, ddl);
changeIt.remove();
}
}
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
// i.e. none was added during this alteration
if ((change instanceof AddPrimaryKeyChange) && !pkColumnAdded) {
processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
changeIt.remove();
}
}
}
use of org.jumpmind.db.alter.AddPrimaryKeyChange in project symmetric-ds by JumpMind.
the class SqlAnywhereDdlBuilder method processTableStructureChanges.
@Override
protected void processTableStructureChanges(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) {
// First we drop primary keys as necessary
for (Iterator changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = (TableChange) changeIt.next();
if (change instanceof RemovePrimaryKeyChange) {
processChange(currentModel, desiredModel, (RemovePrimaryKeyChange) change, ddl);
changeIt.remove();
} else if (change instanceof PrimaryKeyChange) {
PrimaryKeyChange pkChange = (PrimaryKeyChange) change;
RemovePrimaryKeyChange removePkChange = new RemovePrimaryKeyChange(pkChange.getChangedTable(), pkChange.getOldPrimaryKeyColumns());
processChange(currentModel, desiredModel, removePkChange, ddl);
}
}
HashMap columnChanges = new HashMap();
// Next we add/remove columns
for (Iterator changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = (TableChange) changeIt.next();
if (change instanceof AddColumnChange) {
AddColumnChange addColumnChange = (AddColumnChange) change;
// Sybase can only add not insert columns
if (addColumnChange.isAtEnd()) {
processChange(currentModel, desiredModel, addColumnChange, ddl);
changeIt.remove();
}
} else if (change instanceof RemoveColumnChange) {
processChange(currentModel, desiredModel, (RemoveColumnChange) change, ddl);
changeIt.remove();
} else if (change instanceof CopyColumnValueChange) {
CopyColumnValueChange copyColumnChange = (CopyColumnValueChange) change;
processChange(currentModel, desiredModel, copyColumnChange, ddl);
changeIt.remove();
} else if (change instanceof ColumnAutoIncrementChange) {
// Sybase has no way of adding or removing an IDENTITY
// constraint
// Thus we have to rebuild the table anyway and can ignore all
// the other
// column changes
columnChanges = null;
} else if ((change instanceof ColumnChange) && (columnChanges != null)) {
// we gather all changed columns because we can use the ALTER
// TABLE ALTER COLUMN
// statement for them
ColumnChange columnChange = (ColumnChange) change;
ArrayList changesPerColumn = (ArrayList) columnChanges.get(columnChange.getChangedColumn());
if (changesPerColumn == null) {
changesPerColumn = new ArrayList();
columnChanges.put(columnChange.getChangedColumn(), changesPerColumn);
}
changesPerColumn.add(change);
}
}
if (columnChanges != null) {
for (Iterator changesPerColumnIt = columnChanges.entrySet().iterator(); changesPerColumnIt.hasNext(); ) {
Map.Entry entry = (Map.Entry) changesPerColumnIt.next();
Column sourceColumn = (Column) entry.getKey();
ArrayList changesPerColumn = (ArrayList) entry.getValue();
// different handler
if ((changesPerColumn.size() == 1) && (changesPerColumn.get(0) instanceof ColumnDefaultValueChange)) {
processChange(currentModel, desiredModel, (ColumnDefaultValueChange) changesPerColumn.get(0), ddl);
} else {
Column targetColumn = targetTable.findColumn(sourceColumn.getName(), delimitedIdentifierModeOn);
processColumnChange(sourceTable, targetTable, sourceColumn, targetColumn, ddl);
}
for (Iterator changeIt = changesPerColumn.iterator(); changeIt.hasNext(); ) {
((ColumnChange) changeIt.next()).apply(currentModel, delimitedIdentifierModeOn);
}
}
}
// Finally we add primary keys
for (Iterator changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = (TableChange) changeIt.next();
if (change instanceof AddPrimaryKeyChange) {
processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
changeIt.remove();
} else if (change instanceof PrimaryKeyChange) {
PrimaryKeyChange pkChange = (PrimaryKeyChange) change;
AddPrimaryKeyChange addPkChange = new AddPrimaryKeyChange(pkChange.getChangedTable(), pkChange.getNewPrimaryKeyColumns());
processChange(currentModel, desiredModel, addPkChange, ddl);
changeIt.remove();
}
}
}
use of org.jumpmind.db.alter.AddPrimaryKeyChange in project symmetric-ds by JumpMind.
the class MsSql2000DdlBuilder method processTableStructureChanges.
@Override
protected void processTableStructureChanges(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) {
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof ColumnAutoIncrementChange) {
/*
* Sql Server has no way of adding or removing an IDENTITY
* constraint thus we have to rebuild the table anyway and can
* ignore all the other column changes
*/
return;
}
}
// First we drop primary keys as necessary
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof RemovePrimaryKeyChange) {
processChange(currentModel, desiredModel, (RemovePrimaryKeyChange) change, ddl);
changeIt.remove();
} else if (change instanceof PrimaryKeyChange) {
PrimaryKeyChange pkChange = (PrimaryKeyChange) change;
RemovePrimaryKeyChange removePkChange = new RemovePrimaryKeyChange(pkChange.getChangedTable(), pkChange.getOldPrimaryKeyColumns());
processChange(currentModel, desiredModel, removePkChange, ddl);
}
}
ArrayList<ColumnChange> columnChanges = new ArrayList<ColumnChange>();
// Next we add/remove columns
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof AddColumnChange) {
AddColumnChange addColumnChange = (AddColumnChange) change;
processChange(currentModel, desiredModel, addColumnChange, ddl);
changeIt.remove();
} else if (change instanceof RemoveColumnChange) {
processChange(currentModel, desiredModel, (RemoveColumnChange) change, ddl);
changeIt.remove();
} else if (change instanceof CopyColumnValueChange) {
CopyColumnValueChange copyColumnChange = (CopyColumnValueChange) change;
processChange(currentModel, desiredModel, copyColumnChange, ddl);
changeIt.remove();
} else if ((change instanceof ColumnChange) && (columnChanges != null)) {
/*
* We gather all changed columns because we can use the ALTER
* TABLE ALTER COLUMN statement for them
*/
columnChanges.add((ColumnChange) change);
}
}
if (columnChanges != null) {
HashSet<Column> processedColumns = new HashSet<Column>();
for (Iterator<ColumnChange> changeIt = columnChanges.iterator(); changeIt.hasNext(); ) {
ColumnChange change = changeIt.next();
Column sourceColumn = change.getChangedColumn();
if (!sourceColumn.isPrimaryKey()) {
Column targetColumn = targetTable.findColumn(sourceColumn.getName(), delimitedIdentifierModeOn);
if (!processedColumns.contains(targetColumn)) {
processColumnChange(sourceTable, targetTable, sourceColumn, targetColumn, (change instanceof ColumnDataTypeChange) || (change instanceof ColumnSizeChange), ddl);
processedColumns.add(targetColumn);
}
changes.remove(change);
change.apply(currentModel, delimitedIdentifierModeOn);
} else {
log.debug("Cannot alter a primay key column on sql server (azure). Just let the table rebuild.");
}
}
}
// Finally we add primary keys
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof AddPrimaryKeyChange) {
processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
changeIt.remove();
} else if (change instanceof PrimaryKeyChange) {
PrimaryKeyChange pkChange = (PrimaryKeyChange) change;
AddPrimaryKeyChange addPkChange = new AddPrimaryKeyChange(pkChange.getChangedTable(), pkChange.getNewPrimaryKeyColumns());
processChange(currentModel, desiredModel, addPkChange, ddl);
changeIt.remove();
}
}
}
use of org.jumpmind.db.alter.AddPrimaryKeyChange in project symmetric-ds by JumpMind.
the class MsSql2000DdlBuilder method processChanges.
@Override
protected void processChanges(Database currentModel, Database desiredModel, List<IModelChange> changes, StringBuilder ddl) {
if (!changes.isEmpty()) {
writeQuotationOnStatement(ddl);
}
/*
* For column data type and size changes, we need to drop and then
* re-create indexes and foreign keys using the column, as well as any
* primary keys containg these columns However, if the index/foreign
* key/primary key is already slated for removal or change, then we
* don't want to generate change duplication
*/
HashSet<IIndex> removedIndexes = new HashSet<IIndex>();
HashSet<ForeignKey> removedForeignKeys = new HashSet<ForeignKey>();
HashSet<Table> removedPKs = new HashSet<Table>();
for (Iterator<IModelChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
IModelChange change = changeIt.next();
if (change instanceof RemoveIndexChange) {
removedIndexes.add(((RemoveIndexChange) change).getIndex());
} else if (change instanceof RemoveForeignKeyChange) {
removedForeignKeys.add(((RemoveForeignKeyChange) change).getForeignKey());
} else if (change instanceof RemovePrimaryKeyChange) {
removedPKs.add(((RemovePrimaryKeyChange) change).getChangedTable());
}
}
ArrayList<TableChange> additionalChanges = new ArrayList<TableChange>();
for (Iterator<IModelChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
IModelChange change = changeIt.next();
if ((change instanceof ColumnDataTypeChange) || (change instanceof ColumnSizeChange)) {
Column column = ((ColumnChange) change).getChangedColumn();
Table table = ((ColumnChange) change).getChangedTable();
if (column.isPrimaryKey() && !removedPKs.contains(table)) {
Column[] pk = table.getPrimaryKeyColumns();
additionalChanges.add(new RemovePrimaryKeyChange(table, pk));
additionalChanges.add(new AddPrimaryKeyChange(table, pk));
removedPKs.add(table);
}
for (int idx = 0; idx < table.getIndexCount(); idx++) {
IIndex index = table.getIndex(idx);
if (index.hasColumn(column) && !removedIndexes.contains(index)) {
additionalChanges.add(new RemoveIndexChange(table, index));
additionalChanges.add(new AddIndexChange(table, index));
removedIndexes.add(index);
}
}
for (int tableIdx = 0; tableIdx < currentModel.getTableCount(); tableIdx++) {
Table curTable = currentModel.getTable(tableIdx);
for (int fkIdx = 0; fkIdx < curTable.getForeignKeyCount(); fkIdx++) {
ForeignKey curFk = curTable.getForeignKey(fkIdx);
if ((curFk.hasLocalColumn(column) || curFk.hasForeignColumn(column)) && !removedForeignKeys.contains(curFk)) {
additionalChanges.add(new RemoveForeignKeyChange(curTable, curFk));
additionalChanges.add(new AddForeignKeyChange(curTable, curFk));
removedForeignKeys.add(curFk);
}
}
}
}
}
changes.addAll(additionalChanges);
super.processChanges(currentModel, desiredModel, changes, ddl);
}
use of org.jumpmind.db.alter.AddPrimaryKeyChange in project symmetric-ds by JumpMind.
the class MySqlDdlBuilder method processTableStructureChanges.
@Override
protected void processTableStructureChanges(Database currentModel, Database desiredModel, Table sourceTable, Table targetTable, List<TableChange> changes, StringBuilder ddl) {
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof AddColumnChange) {
processChange(currentModel, desiredModel, (AddColumnChange) change, ddl);
changeIt.remove();
} else if (change instanceof ColumnAutoIncrementChange) {
/**
* This has to happen before any primary key changes because if
* a column is bring dropped as auto increment and being dropped
* from the primary key, an auto increment column can't be a non
* primary key column on mysql.
*/
try {
Column sourceColumn = ((ColumnAutoIncrementChange) change).getColumn();
Column targetColumn = (Column) sourceColumn.clone();
targetColumn.setAutoIncrement(!sourceColumn.isAutoIncrement());
processColumnChange(sourceTable, targetTable, sourceColumn, targetColumn, ddl);
changeIt.remove();
} catch (CloneNotSupportedException e) {
log.error("", e);
}
}
}
List<Column> changedColumns = new ArrayList<Column>();
// columns are present
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof RemoveColumnChange) {
processChange(currentModel, desiredModel, (RemoveColumnChange) change, ddl);
changeIt.remove();
} else if (change instanceof CopyColumnValueChange) {
CopyColumnValueChange copyColumnChange = (CopyColumnValueChange) change;
processChange(currentModel, desiredModel, copyColumnChange, ddl);
changeIt.remove();
} else if (change instanceof AddPrimaryKeyChange) {
processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
changeIt.remove();
} else if (change instanceof PrimaryKeyChange) {
processChange(currentModel, desiredModel, (PrimaryKeyChange) change, ddl);
changeIt.remove();
} else if (change instanceof RemovePrimaryKeyChange) {
processChange(currentModel, desiredModel, (RemovePrimaryKeyChange) change, ddl);
changeIt.remove();
} else if (change instanceof ColumnChange) {
/*
* we gather all changed columns because we can use the ALTER
* TABLE MODIFY COLUMN statement for them
*/
Column column = ((ColumnChange) change).getChangedColumn();
if (!changedColumns.contains(column)) {
changedColumns.add(column);
}
changeIt.remove();
}
}
for (Iterator<Column> columnIt = changedColumns.iterator(); columnIt.hasNext(); ) {
Column sourceColumn = columnIt.next();
Column targetColumn = targetTable.findColumn(sourceColumn.getName(), delimitedIdentifierModeOn);
processColumnChange(sourceTable, targetTable, sourceColumn, targetColumn, ddl);
}
}
Aggregations