use of org.jumpmind.db.alter.RemoveColumnChange in project symmetric-ds by JumpMind.
the class HsqlDb2DdlBuilder 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();
// HsqlDb can only drop columns that are not part of a primary key
if ((change instanceof RemoveColumnChange) && ((RemoveColumnChange) change).getColumn().isPrimaryKey()) {
changeIt.remove();
}
// LONGVARCHAR columns always report changes
if (change instanceof ColumnSizeChange) {
ColumnSizeChange sizeChange = (ColumnSizeChange) change;
if (sizeChange.getChangedColumn().getMappedTypeCode() == Types.VARCHAR && sizeChange.getNewSize() == 0) {
changeIt.remove();
}
}
if (change instanceof ColumnDataTypeChange) {
ColumnDataTypeChange dataTypeChange = (ColumnDataTypeChange) change;
// LONGVARCHAR columns always report changes
if (dataTypeChange.getChangedColumn().getMappedTypeCode() == Types.VARCHAR && dataTypeChange.getNewTypeCode() == Types.LONGVARCHAR) {
changeIt.remove();
} else if (dataTypeChange.getNewTypeCode() == Types.BIGINT) {
if (writeAlterColumnDataTypeToBigInt(dataTypeChange, ddl)) {
changeIt.remove();
}
}
}
}
// in order to utilize the ALTER TABLE ADD COLUMN BEFORE statement
// we have to apply the add column changes in the correct order
// thus we first gather all add column changes and then execute them
// Since we get them in target table column order, we can simply
// iterate backwards
ArrayList<AddColumnChange> addColumnChanges = new ArrayList<AddColumnChange>();
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof AddColumnChange) {
addColumnChanges.add((AddColumnChange) change);
changeIt.remove();
}
}
for (ListIterator<AddColumnChange> changeIt = addColumnChanges.listIterator(addColumnChanges.size()); changeIt.hasPrevious(); ) {
AddColumnChange addColumnChange = changeIt.previous();
processChange(currentModel, desiredModel, addColumnChange, ddl);
changeIt.remove();
}
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof RemoveColumnChange) {
RemoveColumnChange removeColumnChange = (RemoveColumnChange) change;
processChange(currentModel, desiredModel, removeColumnChange, ddl);
changeIt.remove();
} else if (change instanceof CopyColumnValueChange) {
CopyColumnValueChange copyColumnChange = (CopyColumnValueChange) change;
processChange(currentModel, desiredModel, copyColumnChange, ddl);
changeIt.remove();
}
}
}
use of org.jumpmind.db.alter.RemoveColumnChange in project symmetric-ds by JumpMind.
the class AseDdlBuilder 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 CopyColumnValueChange) {
CopyColumnValueChange copyColumnChange = (CopyColumnValueChange) change;
processChange(currentModel, desiredModel, copyColumnChange, ddl);
changeIt.remove();
} else if (change instanceof RemoveColumnChange) {
processChange(currentModel, desiredModel, (RemoveColumnChange) change, 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.RemoveColumnChange in project symmetric-ds by JumpMind.
the class Db2DdlBuilder 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) {
AddColumnChange addColumnChange = (AddColumnChange) change;
/*
* DB2 does not allow the GENERATED BY DEFAULT AS IDENTITY
* clause in the ALTER TABLE ADD COLUMN statement, so we have to
* rebuild the table instead
*/
if (!addColumnChange.getNewColumn().isAutoIncrement()) {
processChange(currentModel, desiredModel, addColumnChange, ddl);
changeIt.remove();
} else {
return;
}
}
}
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
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();
}
}
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();
}
}
for (Iterator<TableChange> changeIt = changes.iterator(); changeIt.hasNext(); ) {
TableChange change = changeIt.next();
if (change instanceof AddPrimaryKeyChange) {
processChange(currentModel, desiredModel, (AddPrimaryKeyChange) change, ddl);
changeIt.remove();
}
}
super.processTableStructureChanges(currentModel, desiredModel, sourceTable, targetTable, changes, ddl);
}
use of org.jumpmind.db.alter.RemoveColumnChange in project symmetric-ds by JumpMind.
the class OracleDdlBuilder 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) {
AddColumnChange addColumnChange = (AddColumnChange) change;
if (addColumnChange.getNewColumn().isRequired() && (addColumnChange.getNewColumn().getDefaultValue() == null)) {
// we need to rebuild the full table
return;
}
} else if (change instanceof ColumnSizeChange) {
processChange(currentModel, desiredModel, (ColumnSizeChange) change, ddl);
changeIt.remove();
} else if (change instanceof ColumnDefaultValueChange) {
processChange(currentModel, desiredModel, (ColumnDefaultValueChange) change, ddl);
changeIt.remove();
} else if (change instanceof ColumnRequiredChange) {
processChange(currentModel, desiredModel, (ColumnRequiredChange) change, ddl);
changeIt.remove();
} else if (change instanceof ColumnAutoIncrementChange) {
if (processChange(currentModel, desiredModel, (ColumnAutoIncrementChange) change, ddl)) {
changeIt.remove();
}
}
}
// 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);
}
}
/*
* Next we add/remove columns While Oracle has an ALTER TABLE MODIFY
* statement, it is somewhat limited esp. if there is data in the table,
* so we don't use it
*/
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 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();
}
}
// 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();
}
}
super.processTableStructureChanges(currentModel, desiredModel, sourceTable, targetTable, changes, ddl);
}
use of org.jumpmind.db.alter.RemoveColumnChange in project symmetric-ds by JumpMind.
the class FirebirdDdlBuilder 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 Firebird 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();
} else if (change instanceof ColumnRequiredChange) {
processChange(currentModel, desiredModel, (ColumnRequiredChange) change, 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();
}
}
super.processTableStructureChanges(currentModel, desiredModel, sourceTable, targetTable, changes, ddl);
}
Aggregations