Search in sources :

Example 1 with AlterExpression

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);
}
Also used : AlterOperation(net.sf.jsqlparser.statement.alter.AlterOperation) Table(herddb.model.Table) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) AbstractIndexManager(herddb.core.AbstractIndexManager) AlterTableStatement(herddb.model.commands.AlterTableStatement) ArrayList(java.util.ArrayList) StatementExecutionException(herddb.model.StatementExecutionException) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) Column(herddb.model.Column) AbstractTableManager(herddb.core.AbstractTableManager) TableSpaceManager(herddb.core.TableSpaceManager) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) ColumnsList(herddb.model.ColumnsList) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) List(java.util.List) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 2 with AlterExpression

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());
}
Also used : Alter(net.sf.jsqlparser.statement.alter.Alter) Statement(net.sf.jsqlparser.statement.Statement) ColumnDataType(net.sf.jsqlparser.statement.alter.AlterExpression.ColumnDataType) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression)

Example 3 with AlterExpression

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");
}
Also used : Statement(net.sf.jsqlparser.statement.Statement) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression)

Example 4 with AlterExpression

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());
}
Also used : Alter(net.sf.jsqlparser.statement.alter.Alter) Statement(net.sf.jsqlparser.statement.Statement) ColumnDataType(net.sf.jsqlparser.statement.alter.AlterExpression.ColumnDataType) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression)

Example 5 with AlterExpression

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());
}
Also used : Alter(net.sf.jsqlparser.statement.alter.Alter) Statement(net.sf.jsqlparser.statement.Statement) ColumnDataType(net.sf.jsqlparser.statement.alter.AlterExpression.ColumnDataType) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression)

Aggregations

AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)11 Statement (net.sf.jsqlparser.statement.Statement)10 Alter (net.sf.jsqlparser.statement.alter.Alter)7 ColumnDataType (net.sf.jsqlparser.statement.alter.AlterExpression.ColumnDataType)5 AbstractIndexManager (herddb.core.AbstractIndexManager)1 AbstractTableManager (herddb.core.AbstractTableManager)1 TableSpaceManager (herddb.core.TableSpaceManager)1 Column (herddb.model.Column)1 ColumnsList (herddb.model.ColumnsList)1 StatementExecutionException (herddb.model.StatementExecutionException)1 Table (herddb.model.Table)1 AlterTableStatement (herddb.model.commands.AlterTableStatement)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)1 ItemsList (net.sf.jsqlparser.expression.operators.relational.ItemsList)1 MultiExpressionList (net.sf.jsqlparser.expression.operators.relational.MultiExpressionList)1