use of net.sf.jsqlparser.statement.alter.AlterOperation in project herddb by diennea.
the class SQLPlanner method buildAlterStatement.
private Statement buildAlterStatement(String defaultTableSpace, Alter alter) throws StatementExecutionException {
if (alter.getTable() == null) {
throw new StatementExecutionException("missing table name");
}
String tableSpace = alter.getTable().getSchemaName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
List<Column> addColumns = new ArrayList<>();
List<Column> modifyColumns = new ArrayList<>();
List<String> dropColumns = new ArrayList<>();
String tableName = alter.getTable().getName();
if (alter.getAlterExpressions() == null || alter.getAlterExpressions().size() != 1) {
throw new StatementExecutionException("supported multi-alter operation '" + alter + "'");
}
AlterExpression alterExpression = alter.getAlterExpressions().get(0);
AlterOperation operation = alterExpression.getOperation();
Boolean changeAutoIncrement = null;
switch(operation) {
case ADD:
{
List<AlterExpression.ColumnDataType> cols = alterExpression.getColDataTypeList();
for (AlterExpression.ColumnDataType cl : cols) {
Column newColumn = Column.column(cl.getColumnName(), sqlDataTypeToColumnType(cl.getColDataType().getDataType(), cl.getColDataType().getArgumentsStringList()));
addColumns.add(newColumn);
}
}
break;
case DROP:
dropColumns.add(alterExpression.getColumnName());
break;
case MODIFY:
{
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
throw new StatementExecutionException("bad tablespace '" + tableSpace + "'");
}
AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
if (tableManager == null) {
throw new StatementExecutionException("bad table " + tableName + " in tablespace '" + tableSpace + "'");
}
Table table = tableManager.getTable();
List<AlterExpression.ColumnDataType> cols = alterExpression.getColDataTypeList();
for (AlterExpression.ColumnDataType cl : cols) {
String columnName = cl.getColumnName().toLowerCase();
Column oldColumn = table.getColumn(columnName);
if (oldColumn == null) {
throw new StatementExecutionException("bad column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
}
Map<String, AbstractIndexManager> indexes = tableSpaceManager.getIndexesOnTable(tableName);
if (indexes != null) {
for (AbstractIndexManager am : indexes.values()) {
for (String indexedColumn : am.getColumnNames()) {
if (indexedColumn.equalsIgnoreCase(oldColumn.name)) {
throw new StatementExecutionException("cannot alter indexed " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'," + "index name is " + am.getIndexName());
}
}
}
}
int newType = sqlDataTypeToColumnType(cl.getColDataType().getDataType(), cl.getColDataType().getArgumentsStringList());
if (oldColumn.type != newType) {
throw new StatementExecutionException("cannot change datatype to " + cl.getColDataType().getDataType() + " for column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
}
List<String> columnSpecs = decodeColumnSpecs(cl.getColumnSpecs());
if (table.isPrimaryKeyColumn(columnName)) {
boolean new_auto_increment = decodeAutoIncrement(columnSpecs);
if (new_auto_increment && table.primaryKey.length > 1) {
throw new StatementExecutionException("cannot add auto_increment flag to " + cl.getColDataType().getDataType() + " for column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
}
if (table.auto_increment != new_auto_increment) {
changeAutoIncrement = new_auto_increment;
}
}
String renameTo = decodeRenameTo(columnSpecs);
if (renameTo != null) {
columnName = renameTo;
}
Column newColumnDef = Column.column(columnName, newType, oldColumn.serialPosition);
modifyColumns.add(newColumnDef);
}
}
break;
default:
throw new StatementExecutionException("supported alter operation '" + alter + "'");
}
return new AlterTableStatement(addColumns, modifyColumns, dropColumns, changeAutoIncrement, tableName, tableSpace, null);
}
use of net.sf.jsqlparser.statement.alter.AlterOperation in project herddb by diennea.
the class JSQLParserPlanner method buildAlterStatement.
private Statement buildAlterStatement(String defaultTableSpace, Alter alter) throws StatementExecutionException {
if (alter.getTable() == null) {
throw new StatementExecutionException("missing table name");
}
String tableSpace = alter.getTable().getSchemaName();
if (tableSpace == null) {
tableSpace = defaultTableSpace;
}
tableSpace = fixMySqlBackTicks(tableSpace);
List<Column> addColumns = new ArrayList<>();
List<Column> modifyColumns = new ArrayList<>();
List<String> dropColumns = new ArrayList<>();
List<String> dropForeignKeys = new ArrayList<>();
List<ForeignKeyDef> addForeignKeys = new ArrayList<>();
String tableName = fixMySqlBackTicks(alter.getTable().getName().toLowerCase());
if (alter.getAlterExpressions() == null || alter.getAlterExpressions().size() != 1) {
throw new StatementExecutionException("supported multi-alter operation '" + alter + "'");
}
AlterExpression alterExpression = alter.getAlterExpressions().get(0);
AlterOperation operation = alterExpression.getOperation();
Boolean changeAutoIncrement = null;
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
if (tableSpaceManager == null) {
throw new StatementExecutionException("bad tablespace '" + tableSpace + "'");
}
Table table = getTable(defaultTableSpace, alter.getTable());
switch(operation) {
case ADD:
{
if (alterExpression.getColDataTypeList() != null) {
List<AlterExpression.ColumnDataType> cols = alterExpression.getColDataTypeList();
for (AlterExpression.ColumnDataType cl : cols) {
List<String> columnSpecs = decodeColumnSpecs(cl.getColumnSpecs());
int type = sqlDataTypeToColumnType(cl.getColDataType().getDataType(), cl.getColDataType().getArgumentsStringList(), columnSpecs);
Column newColumn = Column.column(fixMySqlBackTicks(cl.getColumnName()), type, decodeDefaultValue(cl, type));
addColumns.add(newColumn);
}
} else if (alterExpression.getIndex() != null && alterExpression.getIndex() instanceof ForeignKeyIndex) {
ForeignKeyDef fkIndex = parseForeignKeyIndex((ForeignKeyIndex) alterExpression.getIndex(), table, tableName, tableSpace);
addForeignKeys.add(fkIndex);
} else {
throw new StatementExecutionException("Unrecognized ALTER TABLE ADD ... statement");
}
}
break;
case DROP:
if (alterExpression.getColumnName() != null) {
dropColumns.add(fixMySqlBackTicks(alterExpression.getColumnName()));
} else if (alterExpression.getConstraintName() != null) {
dropForeignKeys.add(fixMySqlBackTicks(alterExpression.getConstraintName()));
} else {
throw new StatementExecutionException("Unrecognized ALTER TABLE DROP ... statement");
}
break;
case MODIFY:
{
List<AlterExpression.ColumnDataType> cols = alterExpression.getColDataTypeList();
for (AlterExpression.ColumnDataType cl : cols) {
String columnName = fixMySqlBackTicks(cl.getColumnName().toLowerCase());
Column oldColumn = table.getColumn(columnName);
if (oldColumn == null) {
throw new StatementExecutionException("bad column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
}
Map<String, AbstractIndexManager> indexes = tableSpaceManager.getIndexesOnTable(tableName);
if (indexes != null) {
for (AbstractIndexManager am : indexes.values()) {
for (String indexedColumn : am.getColumnNames()) {
indexedColumn = fixMySqlBackTicks(indexedColumn);
if (indexedColumn.equalsIgnoreCase(oldColumn.name)) {
throw new StatementExecutionException("cannot alter indexed " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'," + "index name is " + am.getIndexName());
}
}
}
}
List<String> columnSpecs = decodeColumnSpecs(cl.getColumnSpecs());
int newType = sqlDataTypeToColumnType(cl.getColDataType().getDataType(), cl.getColDataType().getArgumentsStringList(), columnSpecs);
if (oldColumn.type != newType) {
if (ColumnTypes.isNotNullToNullConversion(oldColumn.type, newType)) {
// allow change from "STRING NOT NULL" to "STRING NULL"
} else if (ColumnTypes.isNullToNotNullConversion(oldColumn.type, newType)) {
// allow change from "STRING NULL" to "STRING NOT NULL"
// it will require a check on table at execution time
} else {
throw new StatementExecutionException("cannot change datatype to " + ColumnTypes.typeToString(newType) + " for column " + columnName + " (" + ColumnTypes.typeToString(oldColumn.type) + ") in table " + tableName + " in tablespace '" + tableSpace + "'");
}
}
if (table.isPrimaryKeyColumn(columnName)) {
boolean new_auto_increment = decodeAutoIncrement(columnSpecs);
if (new_auto_increment && table.primaryKey.length > 1) {
throw new StatementExecutionException("cannot add auto_increment flag to " + cl.getColDataType().getDataType() + " for column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
}
if (table.auto_increment != new_auto_increment) {
changeAutoIncrement = new_auto_increment;
}
}
Bytes newDefault = oldColumn.defaultValue;
if (containsDefaultClause(cl)) {
newDefault = decodeDefaultValue(cl, newType);
}
Column newColumnDef = Column.column(columnName, newType, oldColumn.serialPosition, newDefault);
modifyColumns.add(newColumnDef);
}
}
break;
case CHANGE:
{
String columnName = alterExpression.getColOldName();
List<AlterExpression.ColumnDataType> cols = alterExpression.getColDataTypeList();
if (cols.size() != 1) {
throw new StatementExecutionException("bad CHANGE column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
}
AlterExpression.ColumnDataType cl = cols.get(0);
Column oldColumn = table.getColumn(columnName);
if (oldColumn == null) {
throw new StatementExecutionException("bad column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
}
Map<String, AbstractIndexManager> indexes = tableSpaceManager.getIndexesOnTable(tableName);
if (indexes != null) {
for (AbstractIndexManager am : indexes.values()) {
for (String indexedColumn : am.getColumnNames()) {
indexedColumn = fixMySqlBackTicks(indexedColumn);
if (indexedColumn.equalsIgnoreCase(oldColumn.name)) {
throw new StatementExecutionException("cannot alter indexed " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'," + "index name is " + am.getIndexName());
}
}
}
}
List<String> columnSpecs = decodeColumnSpecs(cl.getColumnSpecs());
int newType = sqlDataTypeToColumnType(cl.getColDataType().getDataType(), cl.getColDataType().getArgumentsStringList(), columnSpecs);
if (oldColumn.type != newType) {
throw new StatementExecutionException("cannot change datatype to " + ColumnTypes.typeToString(newType) + " for column " + columnName + " (" + ColumnTypes.typeToString(oldColumn.type) + ") in table " + tableName + " in tablespace '" + tableSpace + "'");
}
if (table.isPrimaryKeyColumn(columnName)) {
boolean new_auto_increment = decodeAutoIncrement(columnSpecs);
if (new_auto_increment && table.primaryKey.length > 1) {
throw new StatementExecutionException("cannot add auto_increment flag to " + cl.getColDataType().getDataType() + " for column " + columnName + " in table " + tableName + " in tablespace '" + tableSpace + "'");
}
if (table.auto_increment != new_auto_increment) {
changeAutoIncrement = new_auto_increment;
}
}
String renameTo = fixMySqlBackTicks(cl.getColumnName().toLowerCase());
if (renameTo != null) {
columnName = renameTo;
}
Column newColumnDef = Column.column(columnName, newType, oldColumn.serialPosition, oldColumn.defaultValue);
modifyColumns.add(newColumnDef);
}
break;
default:
throw new StatementExecutionException("supported alter operation '" + alter + "'");
}
return new AlterTableStatement(addColumns, modifyColumns, dropColumns, changeAutoIncrement, tableName.toLowerCase(), tableSpace, null, dropForeignKeys, addForeignKeys);
}
Aggregations