Search in sources :

Example 36 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class MySqlAlterTableDropKey method test_alter_first.

public void test_alter_first() throws Exception {
    String sql = "alter table t6 drop key v";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    Assert.assertEquals(//
    "ALTER TABLE t6" + "\n\tDROP KEY v", SQLUtils.toMySqlString(stmt));
    Assert.assertEquals(//
    "alter table t6" + "\n\tdrop key v", SQLUtils.toMySqlString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    SchemaStatVisitor visitor = new SQLUtils().createSchemaStatVisitor(JdbcConstants.MYSQL);
    stmt.accept(visitor);
    TableStat tableStat = visitor.getTableStat("t6");
    assertNotNull(tableStat);
    assertEquals(1, tableStat.getAlterCount());
    assertEquals(1, tableStat.getDropIndexCount());
}
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)

Example 37 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor 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)

Example 38 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class OdpsMultiInsertTest method exec_test.

public void exec_test(String resource) throws Exception {
    //        System.out.println(resource);
    InputStream is = null;
    is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
    Reader reader = new InputStreamReader(is, "UTF-8");
    String input = Utils.read(reader);
    JdbcUtils.close(reader);
    String[] items = input.split("---------------------------");
    String sql = items[0].trim();
    String expect = items[1].trim();
    OdpsStatementParser parser = new OdpsStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = new OdpsSchemaStatVisitor();
    stmt.accept(visitor);
//        System.out.println(sql);
//        System.out.println("Tables : " + visitor.getTables());
//        System.out.println("fields : " + visitor.getColumns());
//
//        System.out.println();
//        System.out.println("---------------------------");
//        System.out.println(SQLUtils.toOdpsString(stmt));
}
Also used : InputStreamReader(java.io.InputStreamReader) OdpsStatementParser(com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser) InputStream(java.io.InputStream) OdpsSchemaStatVisitor(com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) OdpsSchemaStatVisitor(com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor)

Example 39 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class OdpsResourceTest method exec_test.

public void exec_test(String resource) throws Exception {
    //        System.out.println(resource);
    InputStream is = null;
    is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
    Reader reader = new InputStreamReader(is, "UTF-8");
    String input = Utils.read(reader);
    JdbcUtils.close(reader);
    String[] items = input.split("---------------------------");
    String sql = items[0].trim();
    String expect = null;
    if (items.length > 1) {
        expect = items[1].trim();
    }
    OdpsStatementParser parser = new OdpsStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = new OdpsSchemaStatVisitor();
    stmt.accept(visitor);
//        System.out.println(sql);
//        System.out.println("Tables : " + visitor.getTables());
//        System.out.println("fields : " + visitor.getColumns());
//
//        System.out.println();
//        System.out.println("---------------------------");
//        System.out.println(SQLUtils.toOdpsString(stmt));
}
Also used : InputStreamReader(java.io.InputStreamReader) OdpsStatementParser(com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser) InputStream(java.io.InputStream) OdpsSchemaStatVisitor(com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) OdpsSchemaStatVisitor(com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor)

Example 40 with SchemaStatVisitor

use of com.alibaba.druid.sql.visitor.SchemaStatVisitor in project druid by alibaba.

the class OdpsSelectTest10 method test_select.

public void test_select() throws Exception {
    String sql = "select /* + mapjoin(a) */ " + " a.name, b.fals" + //
    " from abc a join abc1 b  on a.name = b.fals;";
    Assert.assertEquals("SELECT /*+ mapjoin(a) */ a.name" + "\n\t, b.fals" + "\nFROM abc a" + "\nJOIN abc1 b" + "\nON a.name = b.fals;", SQLUtils.formatOdps(sql));
    Assert.assertEquals("select /*+ mapjoin(a) */ a.name" + "\n\t, b.fals" + "\nfrom abc a" + "\njoin abc1 b" + "\non a.name = b.fals;", SQLUtils.formatOdps(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.ODPS);
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.ODPS);
    stmt.accept(visitor);
    //        System.out.println("Tables : " + visitor.getTables());
    //      System.out.println("fields : " + visitor.getColumns());
    //      System.out.println("coditions : " + visitor.getConditions());
    //      System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(2, visitor.getTables().size());
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertEquals(2, visitor.getConditions().size());
    Assert.assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
}
Also used : Column(com.alibaba.druid.stat.TableStat.Column) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)67 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)67 TableStat (com.alibaba.druid.stat.TableStat)18 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)14 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)9 Column (com.alibaba.druid.stat.TableStat.Column)8 SQLUtils (com.alibaba.druid.sql.SQLUtils)6 OdpsStatementParser (com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser)6 OdpsSchemaStatVisitor (com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor)6 InputStream (java.io.InputStream)6 InputStreamReader (java.io.InputStreamReader)6 Reader (java.io.Reader)6 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)4 PhoenixSchemaStatVisitor (com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixSchemaStatVisitor)4 SQLDeleteStatement (com.alibaba.druid.sql.ast.statement.SQLDeleteStatement)1 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)1 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)1 SQLUpdateStatement (com.alibaba.druid.sql.ast.statement.SQLUpdateStatement)1 DB2SchemaStatVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor)1 MySqlReplaceStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement)1