Search in sources :

Example 1 with SQLAlterTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement in project druid by alibaba.

the class MySqlAlterTableTest54 method test_6.

public void test_6() throws Exception {
    String sql = "alter table event_log storage_policy = HOT";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    try {
        SQLAlterTableStatement stmt = (SQLAlterTableStatement) parser.parseStatementList().get(0);
        fail();
    } catch (Exception e) {
    }
}
Also used : SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) ParserException(com.alibaba.druid.sql.parser.ParserException)

Example 2 with SQLAlterTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement in project Mycat_plus by coderczp.

the class DruidAlterTableParser method statementParse.

@Override
public void statementParse(SchemaConfig schema, RouteResultset rrs, SQLStatement stmt) throws SQLNonTransientException {
    SQLAlterTableStatement alterTable = (SQLAlterTableStatement) stmt;
    String tableName = StringUtil.removeBackquote(alterTable.getTableSource().toString().toUpperCase());
    // 
    ctx.addTable(tableName);
}
Also used : SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement)

Example 3 with SQLAlterTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement in project canal by alibaba.

the class DruidDdlParser method parse.

public static List<DdlResult> parse(String queryString, String schmeaName) {
    List<SQLStatement> stmtList = null;
    try {
        stmtList = SQLUtils.parseStatements(queryString, JdbcConstants.MYSQL, false);
    } catch (ParserException e) {
        // 可能存在一些SQL是不支持的,比如存储过程
        DdlResult ddlResult = new DdlResult();
        ddlResult.setType(EventType.QUERY);
        return Arrays.asList(ddlResult);
    }
    List<DdlResult> ddlResults = new ArrayList<>();
    for (SQLStatement statement : stmtList) {
        if (statement instanceof SQLCreateTableStatement) {
            DdlResult ddlResult = new DdlResult();
            SQLCreateTableStatement createTable = (SQLCreateTableStatement) statement;
            processName(ddlResult, schmeaName, createTable.getName(), false);
            ddlResult.setType(EventType.CREATE);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLAlterTableStatement) {
            SQLAlterTableStatement alterTable = (SQLAlterTableStatement) statement;
            if (alterTable.getTableOptions().size() > 0) {
                DdlResult ddlResult = new DdlResult();
                processName(ddlResult, schmeaName, alterTable.getName(), false);
                ddlResult.setType(EventType.ALTER);
                ddlResults.add(ddlResult);
            }
            for (SQLAlterTableItem item : alterTable.getItems()) {
                if (item instanceof SQLAlterTableRename) {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), true);
                    processName(ddlResult, schmeaName, ((SQLAlterTableRename) item).getToName(), false);
                    ddlResult.setType(EventType.RENAME);
                    ddlResults.add(ddlResult);
                } else if (item instanceof SQLAlterTableAddIndex) {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), false);
                    ddlResult.setType(EventType.CINDEX);
                    ddlResults.add(ddlResult);
                } else if (item instanceof SQLAlterTableDropIndex || item instanceof SQLAlterTableDropKey) {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), false);
                    ddlResult.setType(EventType.DINDEX);
                    ddlResults.add(ddlResult);
                } else if (item instanceof SQLAlterTableAddConstraint) {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), false);
                    SQLConstraint constraint = ((SQLAlterTableAddConstraint) item).getConstraint();
                    if (constraint instanceof SQLUnique) {
                        ddlResult.setType(EventType.CINDEX);
                        ddlResults.add(ddlResult);
                    }
                } else if (item instanceof SQLAlterTableDropConstraint) {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), false);
                    ddlResult.setType(EventType.DINDEX);
                    ddlResults.add(ddlResult);
                } else {
                    DdlResult ddlResult = new DdlResult();
                    processName(ddlResult, schmeaName, alterTable.getName(), false);
                    ddlResult.setType(EventType.ALTER);
                    ddlResults.add(ddlResult);
                }
            }
        } else if (statement instanceof SQLDropTableStatement) {
            SQLDropTableStatement dropTable = (SQLDropTableStatement) statement;
            for (SQLExprTableSource tableSource : dropTable.getTableSources()) {
                DdlResult ddlResult = new DdlResult();
                processName(ddlResult, schmeaName, tableSource.getExpr(), false);
                ddlResult.setType(EventType.ERASE);
                ddlResults.add(ddlResult);
            }
        } else if (statement instanceof SQLCreateIndexStatement) {
            SQLCreateIndexStatement createIndex = (SQLCreateIndexStatement) statement;
            SQLTableSource tableSource = createIndex.getTable();
            DdlResult ddlResult = new DdlResult();
            processName(ddlResult, schmeaName, ((SQLExprTableSource) tableSource).getExpr(), false);
            ddlResult.setType(EventType.CINDEX);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLDropIndexStatement) {
            SQLDropIndexStatement dropIndex = (SQLDropIndexStatement) statement;
            SQLExprTableSource tableSource = dropIndex.getTableName();
            DdlResult ddlResult = new DdlResult();
            processName(ddlResult, schmeaName, tableSource.getExpr(), false);
            ddlResult.setType(EventType.DINDEX);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLTruncateStatement) {
            SQLTruncateStatement truncate = (SQLTruncateStatement) statement;
            for (SQLExprTableSource tableSource : truncate.getTableSources()) {
                DdlResult ddlResult = new DdlResult();
                processName(ddlResult, schmeaName, tableSource.getExpr(), false);
                ddlResult.setType(EventType.TRUNCATE);
                ddlResults.add(ddlResult);
            }
        } else if (statement instanceof MySqlRenameTableStatement) {
            MySqlRenameTableStatement rename = (MySqlRenameTableStatement) statement;
            for (Item item : rename.getItems()) {
                DdlResult ddlResult = new DdlResult();
                processName(ddlResult, schmeaName, item.getName(), true);
                processName(ddlResult, schmeaName, item.getTo(), false);
                ddlResult.setType(EventType.RENAME);
                ddlResults.add(ddlResult);
            }
        } else if (statement instanceof SQLInsertStatement) {
            DdlResult ddlResult = new DdlResult();
            SQLInsertStatement insert = (SQLInsertStatement) statement;
            processName(ddlResult, schmeaName, insert.getTableName(), false);
            ddlResult.setType(EventType.INSERT);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLUpdateStatement) {
            DdlResult ddlResult = new DdlResult();
            SQLUpdateStatement update = (SQLUpdateStatement) statement;
            // 拿到的表名可能为null,比如update a,b set a.id=x
            processName(ddlResult, schmeaName, update.getTableName(), false);
            ddlResult.setType(EventType.UPDATE);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLDeleteStatement) {
            DdlResult ddlResult = new DdlResult();
            SQLDeleteStatement delete = (SQLDeleteStatement) statement;
            // 拿到的表名可能为null,比如delete a,b from a where a.id = b.id
            processName(ddlResult, schmeaName, delete.getTableName(), false);
            ddlResult.setType(EventType.DELETE);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLCreateDatabaseStatement) {
            SQLCreateDatabaseStatement create = (SQLCreateDatabaseStatement) statement;
            DdlResult ddlResult = new DdlResult();
            ddlResult.setType(EventType.QUERY);
            processName(ddlResult, create.getDatabaseName(), null, false);
            ddlResults.add(ddlResult);
        } else if (statement instanceof SQLDropDatabaseStatement) {
            SQLDropDatabaseStatement drop = (SQLDropDatabaseStatement) statement;
            DdlResult ddlResult = new DdlResult();
            ddlResult.setType(EventType.QUERY);
            processName(ddlResult, drop.getDatabaseName(), null, false);
            ddlResults.add(ddlResult);
        }
    }
    return ddlResults;
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLAlterTableAddConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableAddConstraint) ArrayList(java.util.ArrayList) SQLAlterTableDropIndex(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex) SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement) SQLDropDatabaseStatement(com.alibaba.druid.sql.ast.statement.SQLDropDatabaseStatement) SQLUnique(com.alibaba.druid.sql.ast.statement.SQLUnique) SQLAlterTableDropKey(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropKey) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLTableSource(com.alibaba.druid.sql.ast.statement.SQLTableSource) SQLAlterTableItem(com.alibaba.druid.sql.ast.statement.SQLAlterTableItem) SQLDropTableStatement(com.alibaba.druid.sql.ast.statement.SQLDropTableStatement) Item(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement.Item) SQLAlterTableItem(com.alibaba.druid.sql.ast.statement.SQLAlterTableItem) SQLCreateDatabaseStatement(com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement) SQLAlterTableRename(com.alibaba.druid.sql.ast.statement.SQLAlterTableRename) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) MySqlRenameTableStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement) SQLDropIndexStatement(com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLConstraint(com.alibaba.druid.sql.ast.statement.SQLConstraint) SQLAlterTableDropConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) SQLAlterTableAddIndex(com.alibaba.druid.sql.ast.statement.SQLAlterTableAddIndex) SQLTruncateStatement(com.alibaba.druid.sql.ast.statement.SQLTruncateStatement) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLCreateIndexStatement(com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement)

Example 4 with SQLAlterTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement in project druid by alibaba.

the class SQLASTVisitorAdapterTest method test_adapter.

public void test_adapter() throws Exception {
    SQLASTVisitorAdapter adapter = new SQLASTVisitorAdapter();
    new SQLBinaryOpExpr().accept(adapter);
    new SQLInListExpr().accept(adapter);
    new SQLSelectQueryBlock().accept(adapter);
    new SQLDropTableStatement().accept(adapter);
    new SQLCreateTableStatement().accept(adapter);
    new SQLDeleteStatement().accept(adapter);
    new SQLCurrentOfCursorExpr().accept(adapter);
    new SQLInsertStatement().accept(adapter);
    new SQLUpdateStatement().accept(adapter);
    new SQLNotNullConstraint().accept(adapter);
    new SQLMethodInvokeExpr().accept(adapter);
    new SQLCallStatement().accept(adapter);
    new SQLSomeExpr().accept(adapter);
    new SQLAnyExpr().accept(adapter);
    new SQLAllExpr().accept(adapter);
    new SQLDefaultExpr().accept(adapter);
    new SQLCommentStatement().accept(adapter);
    new SQLDropViewStatement().accept(adapter);
    new SQLSavePointStatement().accept(adapter);
    new SQLReleaseSavePointStatement().accept(adapter);
    new SQLCreateDatabaseStatement().accept(adapter);
    new SQLAlterTableDropIndex().accept(adapter);
    new SQLOver().accept(adapter);
    new SQLWithSubqueryClause().accept(adapter);
    new SQLAlterTableAlterColumn().accept(adapter);
    new SQLAlterTableStatement().accept(adapter);
    new SQLAlterTableDisableConstraint().accept(adapter);
    new SQLAlterTableEnableConstraint().accept(adapter);
    new SQLColumnCheck().accept(adapter);
    new SQLExprHint().accept(adapter);
    new SQLAlterTableDropConstraint().accept(adapter);
}
Also used : SQLASTVisitorAdapter(com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLAlterTableDropIndex(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex) SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement) SQLAllExpr(com.alibaba.druid.sql.ast.expr.SQLAllExpr) SQLReleaseSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLReleaseSavePointStatement) SQLNotNullConstraint(com.alibaba.druid.sql.ast.statement.SQLNotNullConstraint) SQLOver(com.alibaba.druid.sql.ast.SQLOver) SQLDropTableStatement(com.alibaba.druid.sql.ast.statement.SQLDropTableStatement) SQLCreateDatabaseStatement(com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement) SQLDropViewStatement(com.alibaba.druid.sql.ast.statement.SQLDropViewStatement) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) SQLAlterTableDisableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDisableConstraint) SQLSavePointStatement(com.alibaba.druid.sql.ast.statement.SQLSavePointStatement) SQLAlterTableDropConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint) SQLUpdateStatement(com.alibaba.druid.sql.ast.statement.SQLUpdateStatement) SQLCallStatement(com.alibaba.druid.sql.ast.statement.SQLCallStatement) SQLSomeExpr(com.alibaba.druid.sql.ast.expr.SQLSomeExpr) SQLInListExpr(com.alibaba.druid.sql.ast.expr.SQLInListExpr) SQLExprHint(com.alibaba.druid.sql.ast.statement.SQLExprHint) SQLWithSubqueryClause(com.alibaba.druid.sql.ast.statement.SQLWithSubqueryClause) SQLDeleteStatement(com.alibaba.druid.sql.ast.statement.SQLDeleteStatement) SQLCurrentOfCursorExpr(com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr) SQLCommentStatement(com.alibaba.druid.sql.ast.statement.SQLCommentStatement) SQLColumnCheck(com.alibaba.druid.sql.ast.statement.SQLColumnCheck) SQLAnyExpr(com.alibaba.druid.sql.ast.expr.SQLAnyExpr) SQLAlterTableAlterColumn(com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn) SQLSelectQueryBlock(com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock) SQLDefaultExpr(com.alibaba.druid.sql.ast.expr.SQLDefaultExpr) SQLAlterTableEnableConstraint(com.alibaba.druid.sql.ast.statement.SQLAlterTableEnableConstraint)

Example 5 with SQLAlterTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement in project druid by alibaba.

the class CreateCompareTest_cycle method test_0.

public void test_0() throws Exception {
    String sql = "CREATE TABLE t0 (\n" + "\tint bigint\n" + ");\n" + "\n" + "CREATE TABLE t1 (\n" + "\tint bigint\n" + ");\n" + "CREATE TABLE t2 (\n" + "\tint bigint,\n" + "\tFOREIGN KEY (id)\n" + "\t\tREFERENCES t1 (id)\n" + ");\n" + "\n" + "CREATE TABLE t3 (\n" + "\tint bigint,\n" + "\tFOREIGN KEY (id)\n" + "\t\tREFERENCES t2 (id)\n" + ");\n" + "\n" + "CREATE TABLE t4 (\n" + "\tint bigint,\n" + "\tFOREIGN KEY (id)\n" + "\t\tREFERENCES t3 (id),\n" + "\tFOREIGN KEY (id)\n" + "\t\tREFERENCES t4 (id)\n" + ");\n" + "\n" + "CREATE TABLE t5 (\n" + "\tint bigint,\n" + "\tFOREIGN KEY (id)\n" + "\t\tREFERENCES t4 (id)\n" + ");\n" + "\n" + "CREATE TABLE t6 (\n" + "\tint bigint,\n" + "\tFOREIGN KEY (id)\n" + "\t\tREFERENCES t5 (id)\n" + ");\n" + "\n" + "CREATE TABLE t7 (\n" + "\tint bigint,\n" + "\tFOREIGN KEY (id)\n" + "\t\tREFERENCES t6 (id)\n" + ");\n" + "\n" + "\n" + "CREATE TABLE t8 (\n" + "\tint bigint,\n" + "\tFOREIGN KEY (id)\n" + "\t\tREFERENCES t7 (id)\n" + ");\n" + "\n" + "CREATE TABLE t9 (\n" + "\tint bigint,\n" + "\tFOREIGN KEY (id)\n" + "\t\tREFERENCES t8 (id)\n" + ");";
    List stmtList = SQLUtils.parseStatements(sql, JdbcConstants.ORACLE);
    SQLCreateTableStatement.sort(stmtList);
    String sortedSql = SQLUtils.toSQLString(stmtList, JdbcConstants.ORACLE);
    System.out.println(sortedSql);
    assertEquals("t0", ((SQLCreateTableStatement) stmtList.get(9)).getName().getSimpleName());
    assertEquals("t9", ((SQLCreateTableStatement) stmtList.get(8)).getName().getSimpleName());
    assertEquals("t8", ((SQLCreateTableStatement) stmtList.get(7)).getName().getSimpleName());
    assertEquals("t7", ((SQLCreateTableStatement) stmtList.get(6)).getName().getSimpleName());
    assertEquals("t6", ((SQLCreateTableStatement) stmtList.get(5)).getName().getSimpleName());
    assertEquals("t5", ((SQLCreateTableStatement) stmtList.get(4)).getName().getSimpleName());
    assertEquals("t4", ((SQLCreateTableStatement) stmtList.get(3)).getName().getSimpleName());
    assertEquals("t3", ((SQLCreateTableStatement) stmtList.get(2)).getName().getSimpleName());
    assertEquals("t2", ((SQLCreateTableStatement) stmtList.get(1)).getName().getSimpleName());
    assertEquals("t1", ((SQLCreateTableStatement) stmtList.get(0)).getName().getSimpleName());
    assertEquals("t4", ((SQLAlterTableStatement) stmtList.get(10)).getName().getSimpleName());
    assertEquals(11, stmtList.size());
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLAlterTableStatement(com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement) List(java.util.List)

Aggregations

SQLAlterTableStatement (com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement)11 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)6 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)3 ParserException (com.alibaba.druid.sql.parser.ParserException)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLAlterTableDropConstraint (com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint)2 SQLAlterTableDropIndex (com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex)2 SQLCreateDatabaseStatement (com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement)2 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)2 SQLDropTableStatement (com.alibaba.druid.sql.ast.statement.SQLDropTableStatement)2 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)2 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)2 SQLOver (com.alibaba.druid.sql.ast.SQLOver)1 SQLAllExpr (com.alibaba.druid.sql.ast.expr.SQLAllExpr)1 SQLAnyExpr (com.alibaba.druid.sql.ast.expr.SQLAnyExpr)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLCurrentOfCursorExpr (com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr)1 SQLDefaultExpr (com.alibaba.druid.sql.ast.expr.SQLDefaultExpr)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1