Search in sources :

Example 46 with Column

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

the class OdpsDoubleQuoteTest 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)

Example 47 with Column

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

the class OdpsSelectTest7 method test_select.

public void test_select() throws Exception {
    //
    String sql = "select name as close from abc";
    Assert.assertEquals("SELECT name AS close" + "\nFROM abc", SQLUtils.formatOdps(sql));
    Assert.assertEquals("select name as close" + "\nfrom abc", 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(1, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertEquals(0, 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)

Example 48 with Column

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

the class OracleSchemaStatVisitorTest method test_0.

public void test_0() throws Exception {
    String sql = //
    "SELECT id, name name from department d" + "   WHERE d.id = ? order by name desc";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    List<Object> parameters = new ArrayList<Object>();
    parameters.add(23456);
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    visitor.setParameters(parameters);
    statemen.accept(visitor);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    System.out.println("coditions : " + visitor.getConditions());
    System.out.println("relationships : " + visitor.getRelationships());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("department")));
    Assert.assertEquals(2, visitor.getColumns().size());
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("department", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("department", "name")));
    Assert.assertEquals(1, visitor.getConditions().size());
    Condition condition = visitor.getConditions().get(0);
    Assert.assertSame(parameters.get(0), condition.getValues().get(0));
    Column orderByColumn = visitor.getOrderByColumns().iterator().next();
    Assert.assertEquals(SQLOrderingSpecification.DESC, orderByColumn.getAttributes().get("orderBy.type"));
}
Also used : Condition(com.alibaba.druid.stat.TableStat.Condition) Column(com.alibaba.druid.stat.TableStat.Column) OracleSchemaStatVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor) ArrayList(java.util.ArrayList) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Example 49 with Column

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

the class OracleOutputVisitorTest_dblink method test_0.

public void test_0() throws Exception {
    String sql = "SELECT salary from master@emp";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(true, visitor.containsTable("master@emp"));
    Assert.assertEquals(1, visitor.getColumns().size());
    Assert.assertEquals(true, visitor.getColumns().contains(new Column("master@emp", "salary")));
    StringBuilder buf = new StringBuilder();
    OracleOutputVisitor outputVisitor = new OracleOutputVisitor(buf);
    stmt.accept(outputVisitor);
    Assert.assertEquals("SELECT salary\nFROM master@emp;\n", buf.toString());
}
Also used : OracleOutputVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor) Column(com.alibaba.druid.stat.TableStat.Column) OracleSchemaStatVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

Example 50 with Column

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

the class OracleSchemaStatVisitorTest_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 WHERE ROWNUM < 10) a inner join groups b on a.groupId = b.id";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    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 : Column(com.alibaba.druid.stat.TableStat.Column) OracleSchemaStatVisitor(com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleStatementParser(com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)

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