MySqlAlterTableStatement sqlAlterTableStatement,
MetaEventOperation metaEventOperation) throws IOException {
SQLExprTableSource tableSource = sqlAlterTableStatement.getTableSource();
String tableName = parseFromClause(tableSource);
// check if table exists and get Table info
FTable oldTable = metaEventOperation.checkAndGetTable(tableName, true);
FTable newTable = FTable.clone(oldTable);
List<SQLAlterTableItem> items = sqlAlterTableStatement.getItems();
for (SQLAlterTableItem item : items) {
if (item instanceof WaspSqlAlterTableChangeColumn) {
// Alter Table Change Column
WaspSqlAlterTableChangeColumn changeColumn = (WaspSqlAlterTableChangeColumn) item;
SQLName columnName = changeColumn.getColumnName();
LinkedHashMap<String, Field> ftableColumns = newTable.getColumns();
String oldColumnName = parseName(columnName);
// Table have this column and this column is not primary key
metaEventOperation.checkFieldExists(oldTable, oldColumnName);
metaEventOperation.checkFieldNotInPrimaryKeys(oldTable, oldColumnName);
// Check column not in a index
metaEventOperation.checkColumnNotInIndex(oldTable, oldColumnName);
// Which column(index) to change
Field field = ftableColumns.get(oldColumnName); // Change this Field
SQLColumnDefinition newColumnDefinition = changeColumn
.getNewColumnDefinition();
// 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");
}
}