Search in sources :

Example 1 with SQLUnique

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

the class SQLExprParser method parseUnique.

public SQLUnique parseUnique() {
    accept(Token.UNIQUE);
    SQLUnique unique = new SQLUnique();
    accept(Token.LPAREN);
    exprList(unique.getColumns(), unique);
    accept(Token.RPAREN);
    return unique;
}
Also used : SQLUnique(com.alibaba.druid.sql.ast.statement.SQLUnique)

Example 2 with SQLUnique

use of com.alibaba.druid.sql.ast.statement.SQLUnique 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 3 with SQLUnique

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

the class MySqlSchemaStatVisitorTest_pk_0 method test_0.

public void test_0() throws Exception {
    String sql = "CREATE TABLE `m_dt` (\n" + "  `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',\n" + "  `gmt_create` datetime NOT NULL COMMENT '创建时间',\n" + "  `gmt_modified` datetime NOT NULL COMMENT '修改时间',\n" + "  `instance_id` int(11) NOT NULL COMMENT '实例id',\n" + "  `schema_name` varchar(128) NOT NULL COMMENT '数据库schema名称',\n" + "  `state` tinyint(4) NOT NULL DEFAULT '0' COMMENT ' x dd ',\n" + "  `busi_user` varchar(64) DEFAULT NULL COMMENT 'JDBC业务用户',\n" + "  `bzp` varchar(128) DEFAULT NULL COMMENT 'bcp',\n" + "  `ecd` varchar(32) DEFAULT NULL COMMENT 'xxx',\n" + "  `last_sync_time` datetime DEFAULT NULL COMMENT '最后同步时间',\n" + "  `catalog_name` varchar(128) NOT NULL COMMENT '物理库名称',\n" + "  `search_name` varchar(256) NOT NULL COMMENT '用于搜索,区分不同数据库的不同字段信息',\n" + "  `db_type` tinyint(4) NOT NULL COMMENT '数据库类型,和meta_instance表一致',\n" + "  `et` varchar(32) NOT NULL DEFAULT '' COMMENT 'et',\n" + "  `ae` varchar(32) DEFAULT NULL COMMENT 'ae',\n" + "  PRIMARY KEY (`id`),\n" + "  UNIQUE KEY `uk_instanceid_schemaname` (`instance_id`,`catalog_name`,`schema_name`),\n" + "  KEY `idx_schema_name` (`schema_name`),\n" + "  KEY `idx_instance_id_state` (`instance_id`,`id`,`state`),\n" + "  KEY `idx_search_name` (`search_name`(255))\n" + ") ENGINE=InnoDB AUTO_INCREMENT=408695 DEFAULT CHARSET=utf8 COMMENT='数据库表信息'";
    // sql = "select columnName from table1 where id in (select id from table3 where name = ?)";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    stmt.accept(visitor);
    System.out.println(sql);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(true, visitor.containsTable("m_dt"));
    Assert.assertEquals(15, visitor.getColumns().size());
    {
        TableStat.Column column = visitor.getColumn("m_dt", "id");
        assertNotNull(column);
        assertTrue(column.isPrimaryKey());
    }
    {
        TableStat.Column column = visitor.getColumn("m_dt", "schema_name");
        assertNotNull(column);
        assertTrue(column.isUnique());
    }
    {
        TableStat.Column column = visitor.getColumn("m_dt", "instance_id");
        assertNotNull(column);
        assertTrue(column.isUnique());
    }
    for (SQLTableElement element : stmt.getTableElementList()) {
        if (element instanceof SQLUnique) {
            SQLName name = ((SQLUnique) element).getName();
            if (name != null) {
                String uniqueName = name.toString();
            }
        }
    }
// Assert.assertEquals(true, visitor.getFields().contains(new
// Column("users", "id")));
// Assert.assertEquals(true, visitor.getFields().contains(new
// Column("users", "name")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLUnique(com.alibaba.druid.sql.ast.statement.SQLUnique) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLUnique (com.alibaba.druid.sql.ast.statement.SQLUnique)3 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)2 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLAlterTableAddConstraint (com.alibaba.druid.sql.ast.statement.SQLAlterTableAddConstraint)1 SQLAlterTableAddIndex (com.alibaba.druid.sql.ast.statement.SQLAlterTableAddIndex)1 SQLAlterTableDropConstraint (com.alibaba.druid.sql.ast.statement.SQLAlterTableDropConstraint)1 SQLAlterTableDropIndex (com.alibaba.druid.sql.ast.statement.SQLAlterTableDropIndex)1 SQLAlterTableDropKey (com.alibaba.druid.sql.ast.statement.SQLAlterTableDropKey)1 SQLAlterTableItem (com.alibaba.druid.sql.ast.statement.SQLAlterTableItem)1 SQLAlterTableRename (com.alibaba.druid.sql.ast.statement.SQLAlterTableRename)1 SQLAlterTableStatement (com.alibaba.druid.sql.ast.statement.SQLAlterTableStatement)1 SQLConstraint (com.alibaba.druid.sql.ast.statement.SQLConstraint)1 SQLCreateDatabaseStatement (com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement)1 SQLCreateIndexStatement (com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement)1 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)1 SQLDropDatabaseStatement (com.alibaba.druid.sql.ast.statement.SQLDropDatabaseStatement)1 SQLDropIndexStatement (com.alibaba.druid.sql.ast.statement.SQLDropIndexStatement)1 SQLDropTableStatement (com.alibaba.druid.sql.ast.statement.SQLDropTableStatement)1 SQLExprTableSource (com.alibaba.druid.sql.ast.statement.SQLExprTableSource)1