// ColumnFamily specify do not supported.
if (newColumnDefinition instanceof WaspSqlColumnDefinition) {
WaspSqlColumnDefinition waspSqlColumnDefinition =
(WaspSqlColumnDefinition) newColumnDefinition;
if (waspSqlColumnDefinition.getColumnFamily() != null) {
throw new UnsupportedException("Alter Table, columnFamily specify do not supported.");
}
}
if (newColumnDefinition.getDataType() != null) {
field.setType(parse(newColumnDefinition.getDataType()));
}
String newColumnName = parseName(newColumnDefinition.getName());
if (!oldColumnName.equals(newColumnName)) { // Change column name
for (Field f : ftableColumns.values()) {
if (f.getName().equalsIgnoreCase(newColumnName)) {
throw new UnsupportedException(
"Unsupported. Rename one column to a column that already column "
+ newColumnName);
}
}
field.setName(newColumnName);
}
} else if (item instanceof MySqlAlterTableAddColumn) {
// Alter Table Add Column
MySqlAlterTableAddColumn addColumn = (MySqlAlterTableAddColumn) item;
List<SQLColumnDefinition> columns = addColumn.getColumns();
boolean first = addColumn.isFirst();
SQLName afterColumn = addColumn.getAfterColumn();
LinkedHashMap<String, Field> ftableColumns = newTable.getColumns();
List<Field> addFields = convertColumnDefForAlterTable(columns);
// check Duplicate column name
metaEventOperation.areLegalTableColumns(ftableColumns.values(),
addFields);
// Do not support add ColumnFamily dynamic right now.
metaEventOperation.checkColumnFamilyName(ftableColumns.values(), addFields);
if (first) {
this.addFieldByPosition(-1, addFields, ftableColumns, newTable);
} else if (afterColumn != null) {
int index = getIndex(parseName(afterColumn), ftableColumns);
this.addFieldByPosition(index, addFields, ftableColumns, newTable);
} else {
int index = ftableColumns.size() - 1;
this.addFieldByPosition(index, addFields, ftableColumns, newTable);
}
} else if (item instanceof SQLAlterTableDropColumnItem) {
// Alter Table Drop Column
SQLAlterTableDropColumnItem dropColumn = (SQLAlterTableDropColumnItem) item;
SQLName columnName = dropColumn.getColumnName();
String cname = parseName(columnName);
// This column is not primary key
metaEventOperation.checkFieldNotInPrimaryKeys(oldTable, cname);
// Check column not in a index, if you want to drop the column you
// should drop the index first
metaEventOperation.checkColumnNotInIndex(oldTable, cname);
LinkedHashMap<String, Field> ftableColumns = newTable.getColumns();
Field field = ftableColumns.remove(cname);
if (field == null) {
throw new UnsupportedException("Unsupported Do not find this column "
+ SQLUtils.toSQLString(((SQLAlterTableDropColumnItem) item).getColumnName()));
}
newTable.setColumns(ftableColumns);
} else {
throw new UnsupportedException(SQLUtils.toSQLString(item) + " SQLAlterTableItem Unsupported");
}
}
AlterTablePlan alterTable = new AlterTablePlan(oldTable, newTable);
context.setPlan(alterTable);