this.addAfter = after;
}
public int update() {
session.commit(true);
Database db = session.getDatabase();
session.getUser().checkRight(table, Right.ALL);
table.checkSupportAlter();
table.lock(session, true, true);
Sequence sequence = oldColumn == null ? null : oldColumn.getSequence();
if (newColumn != null) {
checkDefaultReferencesTable(newColumn.getDefaultExpression());
}
if (columnsToAdd != null) {
for (Column column : columnsToAdd) {
checkDefaultReferencesTable(column.getDefaultExpression());
}
}
switch (type) {
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NOT_NULL: {
if (!oldColumn.isNullable()) {
// no change
break;
}
checkNoNullValues();
oldColumn.setNullable(false);
db.update(session, table);
break;
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_NULL: {
if (oldColumn.isNullable()) {
// no change
break;
}
checkNullable();
oldColumn.setNullable(true);
db.update(session, table);
break;
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_DEFAULT: {
checkDefaultReferencesTable(defaultExpression);
oldColumn.setSequence(null);
oldColumn.setDefaultExpression(session, defaultExpression);
removeSequence(sequence);
db.update(session, table);
break;
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_CHANGE_TYPE: {
// if the change is only increasing the precision, then we don't
// need to copy the table because the length is only a constraint,
// and does not affect the storage structure.
if (oldColumn.isWideningConversion(newColumn)) {
convertAutoIncrementColumn(newColumn);
oldColumn.copy(newColumn);
db.update(session, table);
} else {
if (table.supportsAlterColumnWithCopyData()) { //TODO
oldColumn.setSequence(null);
oldColumn.setDefaultExpression(session, null);
oldColumn.setConvertNullToDefault(false);
if (oldColumn.isNullable() && !newColumn.isNullable()) {
checkNoNullValues();
} else if (!oldColumn.isNullable() && newColumn.isNullable()) {
checkNullable();
}
convertAutoIncrementColumn(newColumn);
copyData();
}
}
break;
}
case CommandInterface.ALTER_TABLE_ADD_COLUMN: {
// ifNotExists only supported for single column add
if (ifNotExists && columnsToAdd.size() == 1 && table.doesColumnExist(columnsToAdd.get(0).getFullName())) {
break;
}
for (Column column : columnsToAdd) {
convertAutoIncrementColumn(column);
}
if (table.supportsAlterColumnWithCopyData()) { //TODO
copyData();
} else {
for (Column c : columnsToAdd)
table.addColumn(c);
db.update(session, table);
}
break;
}
case CommandInterface.ALTER_TABLE_DROP_COLUMN: {
if (table.getColumns().length == 1) {
throw DbException.get(ErrorCode.CANNOT_DROP_LAST_COLUMN, oldColumn.getSQL());
}
table.dropSingleColumnConstraintsAndIndexes(session, oldColumn);
if (table.supportsAlterColumnWithCopyData()) { //TODO
copyData();
} else {
table.dropColumn(oldColumn);
db.update(session, table);
}
break;
}
case CommandInterface.ALTER_TABLE_ALTER_COLUMN_SELECTIVITY: {
int value = newSelectivity.optimize(session).getValue(session).getInt();
oldColumn.setSelectivity(value);
db.update(session, table);
break;
}
default:
DbException.throwInternalError("type=" + type);
}