Search in sources :

Example 6 with SQLUtils

use of com.alibaba.druid.sql.SQLUtils in project druid by alibaba.

the class MySqlAlterTableAddUniqueTest method test_alter_first.

public void test_alter_first() throws Exception {
    String sql = "ALTER TABLE icp.wx_msg ADD CONSTRAINT idx_msgId_msgType_event_eventKey UNIQUE (msgId, msgType, event, eventKey)";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    Assert.assertEquals("ALTER TABLE icp.wx_msg" + "\n\tADD UNIQUE (msgId, msgType, event, eventKey)", SQLUtils.toMySqlString(stmt));
    Assert.assertEquals("alter table icp.wx_msg" + "\n\tadd unique (msgId, msgType, event, eventKey)", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
    stmt.accept(visitor);
    TableStat tableStat = visitor.getTableStat("icp.wx_msg");
    assertNotNull(tableStat);
    assertEquals(1, tableStat.getAlterCount());
    assertEquals(1, tableStat.getCreateIndexCount());
}
Also used : TableStat(com.alibaba.druid.stat.TableStat) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SQLUtils(com.alibaba.druid.sql.SQLUtils) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Aggregations

SQLUtils (com.alibaba.druid.sql.SQLUtils)6 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)6 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)6 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)6 TableStat (com.alibaba.druid.stat.TableStat)6 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)1