Search in sources :

Example 56 with SchemaStatVisitor

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

the class OdpsSelectTest18 method test_select.

public void test_select() throws Exception {
    //
    String sql = "select a, b, c from tableA where a in ( select a from tableB limit 10 )";
    Assert.assertEquals("SELECT a\n" + "\t, b\n" + "\t, c\n" + "FROM tableA\n" + "WHERE a IN (SELECT a\n" + "\tFROM tableB\n" + "\tLIMIT 10)", SQLUtils.formatOdps(sql));
    Assert.assertEquals("select a\n" + "\t, b\n" + "\t, c\n" + "from tableA\n" + "where a in (select a\n" + "\tfrom tableB\n" + "\tlimit 10)", 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(4, visitor.getColumns().size());
    Assert.assertEquals(1, visitor.getConditions().size());
//        Assert.assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 57 with SchemaStatVisitor

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

the class OdpsSelectTest20 method test_select.

public void test_select() throws Exception {
    // 1095288847322
    //
    String sql = "select * from a DISTRIBUTE BY b,c";
    Assert.assertEquals("SELECT *\n" + "FROM a\n" + "DISTRIBUTE BY b, c", SQLUtils.formatOdps(sql));
    Assert.assertEquals("select *\n" + "from a\n" + "distribute by b, c", 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(3, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
//        Assert.assertTrue(visitor.getColumns().contains(new Column("abc", "name")));
}
Also used : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 58 with SchemaStatVisitor

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

the class OdpsSelectTest8 method test_select.

public void test_select() throws Exception {
    //
    String sql = "select * from (select * from abc limit 1) a;";
    Assert.assertEquals("SELECT *" + "\nFROM (" + "\n\tSELECT *" + "\n\tFROM abc" + "\n\tLIMIT 1" + "\n) a;", SQLUtils.formatOdps(sql));
    Assert.assertEquals("select *" + "\nfrom (" + "\n\tselect *" + "\n\tfrom abc" + "\n\tlimit 1" + "\n) a;", 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", "*")));
}
Also used : Column(com.alibaba.druid.stat.TableStat.Column) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 59 with SchemaStatVisitor

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

the class PhoenixUpsertTest_1 method test_0.

public void test_0() throws Exception {
    String sql = "upsert into t_1 (a BIGINT,b,c) values (?,?,?)";
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.PHOENIX);
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = new PhoenixSchemaStatVisitor();
    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(3, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_1")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "a")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "b")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "c")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.PHOENIX);
    Assert.assertEquals("UPSERT INTO t_1 (a BIGINT, b, c)\n" + //
    "VALUES (?, ?, ?)", output);
}
Also used : PhoenixSchemaStatVisitor(com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) PhoenixSchemaStatVisitor(com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixSchemaStatVisitor)

Example 60 with SchemaStatVisitor

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

the class PhoenixUpsertTest_2 method test_0.

public void test_0() throws Exception {
    String sql = "upsert into t_1 (a CHAR(15),b,c) values (?,?,?)";
    List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.PHOENIX);
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = new PhoenixSchemaStatVisitor();
    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(3, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("t_1")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "a")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "b")));
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("t_1", "c")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "first_name")));
    // Assert.assertTrue(visitor.getColumns().contains(new Column("mytable", "full_name")));
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.PHOENIX);
    Assert.assertEquals("UPSERT INTO t_1 (a CHAR(15), b, c)\n" + //
    "VALUES (?, ?, ?)", output);
}
Also used : PhoenixSchemaStatVisitor(com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixSchemaStatVisitor) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) PhoenixSchemaStatVisitor(com.alibaba.druid.sql.dialect.phoenix.visitor.PhoenixSchemaStatVisitor)

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