Search in sources :

Example 1 with MySqlAlterTableModifyColumn

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn in project druid by alibaba.

the class MySqlStatementParser method parseAlterTable.

protected SQLStatement parseAlterTable(boolean ignore) {
    lexer.nextToken();
    SQLAlterTableStatement stmt = new SQLAlterTableStatement(getDbType());
    stmt.setIgnore(ignore);
    stmt.setName(this.exprParser.name());
    for (; ; ) {
        if (lexer.token() == Token.DROP) {
            parseAlterDrop(stmt);
        } else if (lexer.token() == Token.TRUNCATE) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableTruncatePartition item = new SQLAlterTableTruncatePartition();
            if (lexer.token() == Token.ALL) {
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
                lexer.nextToken();
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("ADD")) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
                parseAlterTableAddColumn(stmt);
            } else if (lexer.token() == Token.INDEX) {
                SQLAlterTableAddIndex item = parseAlterTableAddIndex();
                item.setParent(stmt);
                stmt.addItem(item);
            } else if (lexer.token() == Token.UNIQUE) {
                SQLAlterTableAddIndex item = parseAlterTableAddIndex();
                item.setParent(stmt);
                stmt.addItem(item);
            } else if (lexer.token() == Token.PRIMARY) {
                SQLPrimaryKey primaryKey = this.exprParser.parsePrimaryKey();
                SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(primaryKey);
                stmt.addItem(item);
            } else if (lexer.token() == Token.KEY) {
                // throw new ParserException("TODO " + lexer.token() +
                // " " + lexer.stringVal());
                SQLAlterTableAddIndex item = parseAlterTableAddIndex();
                item.setParent(stmt);
                stmt.addItem(item);
            } else if (lexer.token() == Token.CONSTRAINT) {
                lexer.nextToken();
                SQLName constraintName = this.exprParser.name();
                if (lexer.token() == Token.PRIMARY) {
                    SQLPrimaryKey primaryKey = ((MySqlExprParser) this.exprParser).parsePrimaryKey();
                    primaryKey.setName(constraintName);
                    SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(primaryKey);
                    item.setParent(stmt);
                    stmt.addItem(item);
                } else if (lexer.token() == Token.FOREIGN) {
                    MysqlForeignKey fk = this.getExprParser().parseForeignKey();
                    fk.setName(constraintName);
                    fk.setHasConstraint(true);
                    SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(fk);
                    stmt.addItem(item);
                } else if (lexer.token() == Token.UNIQUE) {
                    SQLUnique unique = this.exprParser.parseUnique();
                    SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(unique);
                    stmt.addItem(item);
                } else {
                    throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
                }
            } else if (lexer.token() == Token.PARTITION) {
                lexer.nextToken();
                SQLAlterTableAddPartition item = new SQLAlterTableAddPartition();
                if (identifierEquals("PARTITIONS")) {
                    lexer.nextToken();
                    item.setPartitionCount(this.exprParser.integerExpr());
                }
                if (lexer.token() == Token.LPAREN) {
                    lexer.nextToken();
                    SQLPartition partition = this.getExprParser().parsePartition();
                    accept(Token.RPAREN);
                    item.addPartition(partition);
                }
                stmt.addItem(item);
            } else if (identifierEquals(FULLTEXT)) {
                throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
            } else if (identifierEquals(SPATIAL)) {
                throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
            } else {
                parseAlterTableAddColumn(stmt);
            }
        } else if (lexer.token() == Token.ALTER) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
            }
            MySqlAlterTableAlterColumn alterColumn = new MySqlAlterTableAlterColumn();
            alterColumn.setColumn(this.exprParser.name());
            if (lexer.token() == Token.SET) {
                lexer.nextToken();
                accept(Token.DEFAULT);
                alterColumn.setDefaultExpr(this.exprParser.expr());
            } else {
                accept(Token.DROP);
                accept(Token.DEFAULT);
                alterColumn.setDropDefault(true);
            }
            stmt.addItem(alterColumn);
        } else if (identifierEquals("CHANGE")) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
            }
            MySqlAlterTableChangeColumn item = new MySqlAlterTableChangeColumn();
            item.setColumnName(this.exprParser.name());
            item.setNewColumnDefinition(this.exprParser.parseColumn());
            if (identifierEquals("AFTER")) {
                lexer.nextToken();
                item.setAfterColumn(this.exprParser.name());
            } else if (identifierEquals("FIRST")) {
                lexer.nextToken();
                if (lexer.token() == Token.IDENTIFIER) {
                    item.setFirstColumn(this.exprParser.name());
                } else {
                    item.setFirst(true);
                }
            }
            stmt.addItem(item);
        } else if (identifierEquals("MODIFY")) {
            lexer.nextToken();
            if (lexer.token() == Token.COLUMN) {
                lexer.nextToken();
            }
            boolean paren = false;
            if (lexer.token() == Token.LPAREN) {
                paren = true;
                lexer.nextToken();
            }
            for (; ; ) {
                MySqlAlterTableModifyColumn item = new MySqlAlterTableModifyColumn();
                item.setNewColumnDefinition(this.exprParser.parseColumn());
                if (identifierEquals("AFTER")) {
                    lexer.nextToken();
                    item.setAfterColumn(this.exprParser.name());
                } else if (identifierEquals("FIRST")) {
                    lexer.nextToken();
                    if (lexer.token() == Token.IDENTIFIER) {
                        item.setFirstColumn(this.exprParser.name());
                    } else {
                        item.setFirst(true);
                    }
                }
                stmt.addItem(item);
                if (paren && lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                }
                break;
            }
            if (paren) {
                accept(Token.RPAREN);
            }
        } else if (lexer.token() == Token.DISABLE) {
            lexer.nextToken();
            if (lexer.token() == Token.CONSTRAINT) {
                lexer.nextToken();
                SQLAlterTableDisableConstraint item = new SQLAlterTableDisableConstraint();
                item.setConstraintName(this.exprParser.name());
                stmt.addItem(item);
            } else {
                acceptIdentifier("KEYS");
                SQLAlterTableDisableKeys item = new SQLAlterTableDisableKeys();
                stmt.addItem(item);
            }
        } else if (lexer.token() == Token.ENABLE) {
            lexer.nextToken();
            if (lexer.token() == Token.CONSTRAINT) {
                lexer.nextToken();
                SQLAlterTableEnableConstraint item = new SQLAlterTableEnableConstraint();
                item.setConstraintName(this.exprParser.name());
                stmt.addItem(item);
            } else {
                acceptIdentifier("KEYS");
                SQLAlterTableEnableKeys item = new SQLAlterTableEnableKeys();
                stmt.addItem(item);
            }
        } else if (identifierEquals("RENAME")) {
            lexer.nextToken();
            if (lexer.token() == Token.TO || lexer.token() == Token.AS) {
                lexer.nextToken();
            }
            MySqlRenameTableStatement renameStmt = new MySqlRenameTableStatement();
            MySqlRenameTableStatement.Item item = new MySqlRenameTableStatement.Item();
            item.setName(stmt.getTableSource().getExpr());
            item.setTo(this.exprParser.name());
            renameStmt.addItem(item);
            return renameStmt;
        } else if (lexer.token() == Token.ORDER) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (identifierEquals("CONVERT")) {
            lexer.nextToken();
            accept(Token.TO);
            acceptIdentifier("CHARACTER");
            accept(Token.SET);
            SQLAlterTableConvertCharSet item = new SQLAlterTableConvertCharSet();
            SQLExpr charset = this.exprParser.primary();
            item.setCharset(charset);
            if (identifierEquals("COLLATE")) {
                lexer.nextToken();
                SQLExpr collate = this.exprParser.primary();
                item.setCollate(collate);
            }
            stmt.addItem(item);
        } else if (lexer.token() == Token.DEFAULT) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (identifierEquals("DISCARD")) {
            lexer.nextToken();
            if (lexer.token() == Token.PARTITION) {
                lexer.nextToken();
                SQLAlterTableDiscardPartition item = new SQLAlterTableDiscardPartition();
                if (lexer.token() == Token.ALL) {
                    lexer.nextToken();
                    item.getPartitions().add(new SQLIdentifierExpr("ALL"));
                } else {
                    this.exprParser.names(item.getPartitions(), item);
                }
                stmt.addItem(item);
            } else {
                accept(Token.TABLESPACE);
                MySqlAlterTableDiscardTablespace item = new MySqlAlterTableDiscardTablespace();
                stmt.addItem(item);
            }
        } else if (lexer.token() == Token.CHECK) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableCheckPartition item = new SQLAlterTableCheckPartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("IMPORT")) {
            lexer.nextToken();
            if (lexer.token() == Token.PARTITION) {
                lexer.nextToken();
                SQLAlterTableImportPartition item = new SQLAlterTableImportPartition();
                if (lexer.token() == Token.ALL) {
                    lexer.nextToken();
                    item.getPartitions().add(new SQLIdentifierExpr("ALL"));
                } else {
                    this.exprParser.names(item.getPartitions(), item);
                }
                stmt.addItem(item);
            } else {
                accept(Token.TABLESPACE);
                MySqlAlterTableImportTablespace item = new MySqlAlterTableImportTablespace();
                stmt.addItem(item);
            }
        } else if (lexer.token() == Token.ANALYZE) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableAnalyzePartition item = new SQLAlterTableAnalyzePartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("FORCE")) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (identifierEquals("COALESCE")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableCoalescePartition item = new SQLAlterTableCoalescePartition();
            SQLIntegerExpr countExpr = this.exprParser.integerExpr();
            item.setCount(countExpr);
            stmt.addItem(item);
        } else if (identifierEquals("REORGANIZE")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableReOrganizePartition item = new SQLAlterTableReOrganizePartition();
            this.exprParser.names(item.getNames(), item);
            accept(Token.INTO);
            accept(Token.LPAREN);
            for (; ; ) {
                SQLPartition partition = this.getExprParser().parsePartition();
                item.addPartition(partition);
                if (lexer.token() == Token.COMMA) {
                    lexer.nextToken();
                    continue;
                } else {
                    break;
                }
            }
            accept(Token.RPAREN);
            stmt.addItem(item);
        } else if (identifierEquals("EXCHANGE")) {
            throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
        } else if (lexer.token() == Token.OPTIMIZE) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableOptimizePartition item = new SQLAlterTableOptimizePartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("REBUILD")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableRebuildPartition item = new SQLAlterTableRebuildPartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("REPAIR")) {
            lexer.nextToken();
            accept(Token.PARTITION);
            SQLAlterTableRepairPartition item = new SQLAlterTableRepairPartition();
            if (lexer.token() == Token.ALL) {
                lexer.nextToken();
                item.getPartitions().add(new SQLIdentifierExpr("ALL"));
            } else {
                this.exprParser.names(item.getPartitions(), item);
            }
            stmt.addItem(item);
        } else if (identifierEquals("REMOVE")) {
            lexer.nextToken();
            acceptIdentifier("PARTITIONING");
            stmt.setRemovePatiting(true);
        } else if (identifierEquals("UPGRADE")) {
            lexer.nextToken();
            acceptIdentifier("PARTITIONING");
            stmt.setUpgradePatiting(true);
        } else if (identifierEquals("ALGORITHM")) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption("ALGORITHM", lexer.stringVal()));
            lexer.nextToken();
        } else if (identifierEquals(ENGINE)) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption(ENGINE, lexer.stringVal()));
            lexer.nextToken();
        } else if (identifierEquals(AUTO_INCREMENT)) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption(AUTO_INCREMENT, lexer.integerValue()));
            lexer.nextToken();
        } else if (identifierEquals(COLLATE2)) {
            lexer.nextToken();
            accept(Token.EQ);
            stmt.addItem(new MySqlAlterTableOption(COLLATE2, lexer.stringVal()));
            lexer.nextToken();
        } else if (identifierEquals("PACK_KEYS")) {
            lexer.nextToken();
            accept(Token.EQ);
            if (identifierEquals("PACK")) {
                lexer.nextToken();
                accept(Token.ALL);
                stmt.addItem(new MySqlAlterTableOption("PACK_KEYS", "PACK ALL"));
            } else {
                stmt.addItem(new MySqlAlterTableOption("PACK_KEYS", lexer.stringVal()));
                lexer.nextToken();
            }
        } else if (identifierEquals(CHARACTER)) {
            lexer.nextToken();
            accept(Token.SET);
            accept(Token.EQ);
            MySqlAlterTableCharacter item = new MySqlAlterTableCharacter();
            item.setCharacterSet(this.exprParser.primary());
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                acceptIdentifier(COLLATE2);
                accept(Token.EQ);
                item.setCollate(this.exprParser.primary());
            }
            stmt.addItem(item);
        } else if (lexer.token() == Token.COMMENT) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                accept(Token.EQ);
            }
            stmt.addItem(new MySqlAlterTableOption("COMMENT", '\'' + lexer.stringVal() + '\''));
            lexer.nextToken();
        } else if (lexer.token() == Token.UNION) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            accept(Token.LPAREN);
            SQLTableSource tableSrc = this.createSQLSelectParser().parseTableSource();
            stmt.getTableOptions().put("UNION", tableSrc);
            accept(Token.RPAREN);
        } else if (identifierEquals("ROW_FORMAT")) {
            lexer.nextToken();
            if (lexer.token() == Token.EQ) {
                lexer.nextToken();
            }
            if (lexer.token() == Token.DEFAULT || lexer.token() == Token.IDENTIFIER) {
                SQLIdentifierExpr rowFormat = new SQLIdentifierExpr(lexer.stringVal());
                lexer.nextToken();
                stmt.getTableOptions().put("ROW_FORMAT", rowFormat);
            } else {
                throw new ParserException("illegal syntax.");
            }
        } else {
            break;
        }
        if (lexer.token() == Token.COMMA) {
            lexer.nextToken();
            continue;
        } else {
            break;
        }
    }
    return stmt;
}
Also used : MySqlAlterTableModifyColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn) MySqlAlterTableCharacter(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter) MySqlAlterTableAlterColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAlterColumn) SQLDeclareItem(com.alibaba.druid.sql.ast.SQLDeclareItem) MySqlAlterTableDiscardTablespace(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableDiscardTablespace) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) MySqlRenameTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) MySqlAlterTableOption(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) MysqlForeignKey(com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey) SQLPartition(com.alibaba.druid.sql.ast.SQLPartition) MySqlAlterTableImportTablespace(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTablespace) MySqlAlterTableChangeColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn)

Example 2 with MySqlAlterTableModifyColumn

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn in project dble by actiontech.

the class ProxyMetaManager method alterTable.

private void alterTable(String schema, String sql, SQLAlterTableStatement alterStatement, boolean isSuccess, boolean needNotifyOther) {
    SchemaInfo schemaInfo = getSchemaInfo(schema, alterStatement.getTableSource());
    try {
        if (!isSuccess) {
            return;
        }
        StructureMeta.TableMeta orgTbMeta = getTableMeta(schemaInfo.getSchema(), schemaInfo.getTable());
        if (orgTbMeta == null)
            return;
        StructureMeta.TableMeta.Builder tmBuilder = orgTbMeta.toBuilder();
        List<StructureMeta.ColumnMeta> cols = new ArrayList<>();
        cols.addAll(orgTbMeta.getColumnsList());
        int autoColumnIndex = -1;
        Set<String> indexNames = new HashSet<>();
        for (StructureMeta.IndexMeta index : tmBuilder.getIndexList()) {
            indexNames.add(index.getName());
        }
        for (SQLAlterTableItem alterItem : alterStatement.getItems()) {
            if (alterItem instanceof SQLAlterTableAddColumn) {
                autoColumnIndex = addColumn(tmBuilder, cols, (SQLAlterTableAddColumn) alterItem, indexNames);
            } else if (alterItem instanceof SQLAlterTableAddIndex) {
                addIndex(tmBuilder, (SQLAlterTableAddIndex) alterItem, indexNames);
            } else if (alterItem instanceof SQLAlterTableAddConstraint) {
                SQLAlterTableAddConstraint addConstraint = (SQLAlterTableAddConstraint) alterItem;
                SQLConstraint constraint = addConstraint.getConstraint();
                if (constraint instanceof MySqlPrimaryKey) {
                    MySqlPrimaryKey primaryKey = (MySqlPrimaryKey) constraint;
                    StructureMeta.IndexMeta indexMeta = MetaHelper.makeIndexMeta(MetaHelper.PRIMARY, IndexType.PRI, primaryKey.getColumns());
                    tmBuilder.setPrimary(indexMeta);
                } else {
                // NOT SUPPORT
                }
            } else if (alterItem instanceof SQLAlterTableDropIndex) {
                SQLAlterTableDropIndex dropIndex = (SQLAlterTableDropIndex) alterItem;
                String dropName = StringUtil.removeBackQuote(dropIndex.getIndexName().getSimpleName());
                dropIndex(tmBuilder, dropName);
            } else if (alterItem instanceof SQLAlterTableDropKey) {
                SQLAlterTableDropKey dropIndex = (SQLAlterTableDropKey) alterItem;
                String dropName = StringUtil.removeBackQuote(dropIndex.getKeyName().getSimpleName());
                dropIndex(tmBuilder, dropName);
            } else if (alterItem instanceof MySqlAlterTableChangeColumn) {
                autoColumnIndex = changeColumn(tmBuilder, cols, (MySqlAlterTableChangeColumn) alterItem, indexNames);
            } else if (alterItem instanceof MySqlAlterTableModifyColumn) {
                autoColumnIndex = modifyColumn(tmBuilder, cols, (MySqlAlterTableModifyColumn) alterItem, indexNames);
            } else if (alterItem instanceof SQLAlterTableDropColumnItem) {
                dropColumn(cols, (SQLAlterTableDropColumnItem) alterItem);
            } else if (alterItem instanceof SQLAlterTableDropPrimaryKey) {
                tmBuilder.clearPrimary();
            } else {
            // TODO: further
            }
        }
        tmBuilder.clearColumns().addAllColumns(cols);
        if (autoColumnIndex != -1) {
            tmBuilder.setAiColPos(autoColumnIndex);
        }
        tmBuilder.setVersion(System.currentTimeMillis());
        StructureMeta.TableMeta newTblMeta = tmBuilder.build();
        addTable(schemaInfo.getSchema(), newTblMeta);
    } catch (Exception e) {
        LOGGER.info("updateMetaData alterTable failed,sql is" + alterStatement.toString(), e);
    } finally {
        try {
            notifyResponseClusterDDL(schemaInfo.getSchema(), schemaInfo.getTable(), sql, isSuccess ? DDLInfo.DDLStatus.SUCCESS : DDLInfo.DDLStatus.FAILED, needNotifyOther);
        } catch (Exception e) {
            LOGGER.warn(AlarmCode.CORE_CLUSTER_WARN + "notifyResponseZKDdl error", e);
        }
        removeMetaLock(schemaInfo.getSchema(), schemaInfo.getTable());
    }
}
Also used : MySqlAlterTableModifyColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo) StructureMeta(com.actiontech.dble.meta.protocol.StructureMeta) SQLException(java.sql.SQLException) MySqlPrimaryKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey) MySqlAlterTableChangeColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn)

Example 3 with MySqlAlterTableModifyColumn

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn in project dble by actiontech.

the class DruidAlterTableParser method modifyColumn.

private void modifyColumn(List<String> cols, MySqlAlterTableModifyColumn modifyColumn, List<SQLAlterTableItem> newAlterItems) {
    String modifyColName = StringUtil.removeBackQuote(modifyColumn.getNewColumnDefinition().getName().getSimpleName());
    MySqlAlterTableModifyColumn newModifyColumn = modifyColumn;
    if (modifyColName.equalsIgnoreCase(GlobalTableUtil.GLOBAL_TABLE_CHECK_COLUMN)) {
        removeOldCol(cols, modifyColName);
        newModifyColumn.setNewColumnDefinition(GlobalTableUtil.createCheckColumn());
        newModifyColumn.setAfterColumn(new SQLIdentifierExpr(cols.get(cols.size() - 1)));
        cols.add(modifyColName);
    } else {
        SQLExpr afterColumn = modifyColumn.getAfterColumn();
        if (afterColumn != null) {
            removeOldCol(cols, modifyColName);
            int lastIndex = cols.size() - 1;
            String afterColName = StringUtil.removeBackQuote(((SQLIdentifierExpr) afterColumn).getName());
            // last column is GLOBAL_TABLE_CHECK_COLUMN,so new add will move to the pos before it
            if (afterColName.equalsIgnoreCase(GlobalTableUtil.GLOBAL_TABLE_CHECK_COLUMN) && cols.get(lastIndex).equalsIgnoreCase(GlobalTableUtil.GLOBAL_TABLE_CHECK_COLUMN)) {
                newModifyColumn.setAfterColumn(new SQLIdentifierExpr(cols.get(lastIndex - 1)));
                cols.add(lastIndex, modifyColName);
            } else {
                addAfterCol(cols, afterColName, modifyColName);
            }
        } else if (modifyColumn.isFirst()) {
            removeOldCol(cols, modifyColName);
            cols.add(0, modifyColName);
        }
    }
    newAlterItems.add(newModifyColumn);
}
Also used : MySqlAlterTableModifyColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 4 with MySqlAlterTableModifyColumn

use of com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn in project dble by actiontech.

the class DruidAlterTableParser method visitorParse.

@Override
public SchemaConfig visitorParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt, ServerSchemaStatVisitor visitor, ServerConnection sc) throws SQLException {
    SQLAlterTableStatement alterTable = (SQLAlterTableStatement) stmt;
    String schemaName = schema == null ? null : schema.getName();
    SchemaInfo schemaInfo = SchemaUtil.getSchemaInfo(sc.getUser(), schemaName, alterTable.getTableSource());
    boolean support = false;
    String msg = "The DDL is not supported, sql:";
    for (SQLAlterTableItem alterItem : alterTable.getItems()) {
        if (alterItem instanceof SQLAlterTableAddColumn || alterItem instanceof SQLAlterTableAddIndex || alterItem instanceof SQLAlterTableDropIndex || alterItem instanceof SQLAlterTableDropKey || alterItem instanceof SQLAlterTableDropPrimaryKey) {
            support = true;
        } else if (alterItem instanceof SQLAlterTableAddConstraint) {
            SQLConstraint constraint = ((SQLAlterTableAddConstraint) alterItem).getConstraint();
            if (constraint instanceof MySqlPrimaryKey) {
                support = true;
            }
        } else if (alterItem instanceof MySqlAlterTableChangeColumn || alterItem instanceof MySqlAlterTableModifyColumn || alterItem instanceof SQLAlterTableDropColumnItem) {
            List<SQLName> columnList = new ArrayList<>();
            if (alterItem instanceof MySqlAlterTableChangeColumn) {
                columnList.add(((MySqlAlterTableChangeColumn) alterItem).getColumnName());
            } else if (alterItem instanceof MySqlAlterTableModifyColumn) {
                columnList.add(((MySqlAlterTableModifyColumn) alterItem).getNewColumnDefinition().getName());
            } else if (alterItem instanceof SQLAlterTableDropColumnItem) {
                columnList = ((SQLAlterTableDropColumnItem) alterItem).getColumns();
            }
            support = !this.columnInfluenceCheck(columnList, schemaInfo.getSchemaConfig(), schemaInfo.getTable());
            if (!support) {
                msg = "The columns may be sharding keys or ER keys, are not allowed to alter sql:";
            }
        }
    }
    if (!support) {
        msg = msg + stmt;
        throw new SQLNonTransientException(msg);
    }
    String statement = RouterUtil.removeSchema(rrs.getStatement(), schemaInfo.getSchema());
    rrs.setStatement(statement);
    if (RouterUtil.isNoSharding(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
        RouterUtil.routeToSingleDDLNode(schemaInfo, rrs);
        return schemaInfo.getSchemaConfig();
    }
    if (GlobalTableUtil.useGlobalTableCheck() && GlobalTableUtil.isGlobalTable(schemaInfo.getSchemaConfig(), schemaInfo.getTable())) {
        String sql = modifyColumnIfAlter(schemaInfo, rrs.getStatement(), alterTable);
        rrs.setSrcStatement(sql);
        sql = RouterUtil.removeSchema(sql, schemaInfo.getSchema());
        rrs.setStatement(sql);
    }
    RouterUtil.routeToDDLNode(schemaInfo, rrs);
    return schemaInfo.getSchemaConfig();
}
Also used : MySqlAlterTableModifyColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn) SQLName(com.alibaba.druid.sql.ast.SQLName) ArrayList(java.util.ArrayList) SQLNonTransientException(java.sql.SQLNonTransientException) MySqlPrimaryKey(com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey) MySqlAlterTableChangeColumn(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn) SchemaInfo(com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)

Aggregations

MySqlAlterTableModifyColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableModifyColumn)4 MySqlAlterTableChangeColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn)3 SchemaInfo (com.actiontech.dble.server.util.SchemaUtil.SchemaInfo)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLName (com.alibaba.druid.sql.ast.SQLName)2 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)2 MySqlPrimaryKey (com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey)2 StructureMeta (com.actiontech.dble.meta.protocol.StructureMeta)1 SQLDeclareItem (com.alibaba.druid.sql.ast.SQLDeclareItem)1 SQLPartition (com.alibaba.druid.sql.ast.SQLPartition)1 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)1 MysqlForeignKey (com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey)1 MySqlAlterTableAlterColumn (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAlterColumn)1 MySqlAlterTableCharacter (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableCharacter)1 MySqlAlterTableDiscardTablespace (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableDiscardTablespace)1 MySqlAlterTableImportTablespace (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableImportTablespace)1 MySqlAlterTableOption (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableOption)1 MySqlRenameTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement)1 ParserException (com.alibaba.druid.sql.parser.ParserException)1 SQLException (java.sql.SQLException)1