Search in sources :

Example 41 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class MySqlSchemaStatVisitorTest_Alter method test_0.

public void test_0() throws Exception {
    String sql = "alter table sql_perf add index `idx_instance_8` (`host`,`port`,`hashcode`,`item`,`time`,`value`);";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    statemen.accept(visitor);
    //        System.out.println(sql);
    //        System.out.println("Tables : " + visitor.getTables());
    //        System.out.println("fields : " + visitor.getColumns());
    assertEquals(1, visitor.getTables().size());
    assertTrue(visitor.containsTable("sql_perf"));
    assertEquals(6, visitor.getColumns().size());
    assertTrue(visitor.getColumns().contains(new Column("sql_perf", "host")));
    assertTrue(visitor.getColumns().contains(new Column("sql_perf", "port")));
    assertTrue(visitor.getColumns().contains(new Column("sql_perf", "hashcode")));
    assertTrue(visitor.getColumns().contains(new Column("sql_perf", "item")));
    assertTrue(visitor.getColumns().contains(new Column("sql_perf", "time")));
    assertTrue(visitor.getColumns().contains(new Column("sql_perf", "value")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) Column(com.alibaba.druid.stat.TableStat.Column) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 42 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class MySqlSchemaStatVisitorTest_Subquery method test_0.

public void test_0() throws Exception {
    String sql = "SELECT id2, name2 FROM (select id, name from users u where id = 22 limit 10) a";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    statemen.accept(visitor);
    System.out.println(sql);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    System.out.println("alias : " + visitor.getAliasMap());
    System.out.println("conditions : " + visitor.getConditions());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(true, visitor.containsTable("users"));
    Assert.assertEquals(22, visitor.getConditions().get(0).getValues().get(0));
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertEquals(true, visitor.getColumns().contains(new Column("users", "id")));
    Assert.assertEquals(true, visitor.getColumns().contains(new Column("users", "name")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) Column(com.alibaba.druid.stat.TableStat.Column) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 43 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class MySqlSchemaStatVisitorTest_Subquery2 method test_0.

public void test_0() throws Exception {
    String sql = "SELECT a.id, a.name, b.name groupName FROM (select id, name, groupId from users limit 10) a inner join groups b on a.groupId = b.id";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    statemen.accept(visitor);
    System.out.println(sql);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    Assert.assertEquals(2, visitor.getTables().size());
    Assert.assertEquals(true, visitor.containsTable("users"));
    Assert.assertEquals(true, visitor.containsTable("groups"));
    Assert.assertEquals(5, visitor.getColumns().size());
    Assert.assertEquals(true, visitor.getColumns().contains(new Column("users", "id")));
    Assert.assertEquals(true, visitor.getColumns().contains(new Column("users", "groupId")));
    Assert.assertEquals(true, visitor.getColumns().contains(new Column("users", "name")));
    Assert.assertEquals(true, visitor.getColumns().contains(new Column("groups", "id")));
    Assert.assertEquals(true, visitor.getColumns().contains(new Column("groups", "name")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) Column(com.alibaba.druid.stat.TableStat.Column) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 44 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class MySqlUpdateTest_0 method test_0.

public void test_0() throws Exception {
    String sql = "UPDATE `z_code` SET `key`='INTRANT_NOTALLOWED_CATEGORY_C'";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    statemen.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(1, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("z_code")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("z_code", "key")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) Column(com.alibaba.druid.stat.TableStat.Column) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 45 with Column

use of com.alibaba.druid.stat.TableStat.Column in project druid by alibaba.

the class OdpsUDTFTest method test_0.

public void test_0() throws Exception {
    String sql = "SELECT my_udtf(1,2,3) as (a, b, c) from employee t1 WHERE t1.name = 'aaa';";
    OdpsStatementParser parser = new OdpsStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    OdpsSchemaStatVisitor visitor = new OdpsSchemaStatVisitor();
    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(1, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("employee")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("employee", "name")));
}
Also used : OdpsStatementParser(com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser) Column(com.alibaba.druid.stat.TableStat.Column) OdpsSchemaStatVisitor(com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

Column (com.alibaba.druid.stat.TableStat.Column)134 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)122 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)74 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)73 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)19 OracleSchemaStatVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor)19 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)12 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)12 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)10 DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)8 DB2SchemaStatVisitor (com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor)8 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)8 OracleOutputVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor)6 MysqlTest (com.alibaba.druid.sql.MysqlTest)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)5 SQLObject (com.alibaba.druid.sql.ast.SQLObject)5 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)5 Test (org.junit.Test)5 SQLCreateViewStatement (com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement)4 MySqlCreateTableStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement)4