use of net.sf.jsqlparser.statement.alter.AlterExpression 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.AlterExpression in project JSqlParser by JSQLParser.
the class AlterTest method testAlterTableAddColumn4.
public void testAlterTableAddColumn4() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 varchar (255), ADD COLUMN col2 integer");
Statement stmt = CCJSqlParserUtil.parse("ALTER TABLE mytable ADD COLUMN col1 varchar (255), ADD COLUMN col2 integer");
Alter alter = (Alter) stmt;
List<AlterExpression> alterExps = alter.getAlterExpressions();
AlterExpression col1Exp = alterExps.get(0);
AlterExpression col2Exp = alterExps.get(1);
List<ColumnDataType> col1DataTypes = col1Exp.getColDataTypeList();
List<ColumnDataType> col2DataTypes = col2Exp.getColDataTypeList();
assertEquals("col1", col1DataTypes.get(0).getColumnName());
assertEquals("col2", col2DataTypes.get(0).getColumnName());
assertEquals("varchar (255)", col1DataTypes.get(0).getColDataType().toString());
assertEquals("integer", col2DataTypes.get(0).getColDataType().toString());
}
use of net.sf.jsqlparser.statement.alter.AlterExpression in project JSqlParser by JSQLParser.
the class AlterTest method testAlterTableDropConstraint.
public void testAlterTableDropConstraint() throws JSQLParserException {
final String sql = "ALTER TABLE test DROP CONSTRAINT YYY";
Statement stmt = CCJSqlParserUtil.parse(sql);
assertStatementCanBeDeparsedAs(stmt, sql);
AlterExpression alterExpression = ((Alter) stmt).getAlterExpressions().get(0);
assertEquals(alterExpression.getConstraintName(), "YYY");
}
use of net.sf.jsqlparser.statement.alter.AlterExpression in project JSqlParser by JSQLParser.
the class AlterTest method testAlterTableAddColumnWithZone.
public void testAlterTableAddColumnWithZone() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 timestamp with time zone");
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 timestamp without time zone");
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 date with time zone");
assertSqlCanBeParsedAndDeparsed("ALTER TABLE mytable ADD COLUMN col1 date without time zone");
Statement stmt = CCJSqlParserUtil.parse("ALTER TABLE mytable ADD COLUMN col1 timestamp with time zone");
Alter alter = (Alter) stmt;
List<AlterExpression> alterExps = alter.getAlterExpressions();
AlterExpression col1Exp = alterExps.get(0);
List<ColumnDataType> col1DataTypes = col1Exp.getColDataTypeList();
assertEquals("timestamp with time zone", col1DataTypes.get(0).getColDataType().toString());
}
use of net.sf.jsqlparser.statement.alter.AlterExpression in project JSqlParser by JSQLParser.
the class AlterTest method testAlterTableAddColumn.
public void testAlterTableAddColumn() throws JSQLParserException {
Statement stmt = CCJSqlParserUtil.parse("ALTER TABLE mytable ADD COLUMN mycolumn varchar (255)");
assertTrue(stmt instanceof Alter);
Alter alter = (Alter) stmt;
assertEquals("mytable", alter.getTable().getFullyQualifiedName());
AlterExpression alterExp = alter.getAlterExpressions().get(0);
assertNotNull(alterExp);
List<ColumnDataType> colDataTypes = alterExp.getColDataTypeList();
assertEquals("mycolumn", colDataTypes.get(0).getColumnName());
assertEquals("varchar (255)", colDataTypes.get(0).getColDataType().toString());
}
Aggregations