Search in sources :

Example 41 with SchemaStatVisitor

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

the class OdpsSelectTest11 method test_select.

public void test_select() throws Exception {
    //
    String sql = "select 'a\\'b' from abc;";
    Assert.assertEquals("SELECT 'a\\'b'" + "\nFROM abc;", SQLUtils.formatOdps(sql));
    Assert.assertEquals("select 'a\\'b'" + "\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(0, 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 42 with SchemaStatVisitor

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

the class OdpsSelectTest12 method test_select.

public void test_select() throws Exception {
    //
    String sql = "select name, sp from abc lateral view explode(split(concat(',','1','2','3'),',')) t as sp;";
    Assert.assertEquals("SELECT name" + "\n\t, sp" + "\nFROM abc" + "\n\tLATERAL VIEW EXPLODE(SPLIT(concat(',', '1', '2', '3'), ',')) t AS sp;", SQLUtils.formatOdps(sql));
    Assert.assertEquals("select name" + "\n\t, sp" + "\nfrom abc" + "\n\tlateral view explode(split(concat(',', '1', '2', '3'), ',')) t as sp;", 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(2, 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 43 with SchemaStatVisitor

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

the class OdpsSelectTest13 method test_select.

public void test_select() throws Exception {
    //
    String sql = "select `name` from `abc`;";
    Assert.assertEquals("SELECT `name`" + "\nFROM `abc`;", SQLUtils.formatOdps(sql));
    Assert.assertEquals("select `name`" + "\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 44 with SchemaStatVisitor

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

the class OdpsSelectTest14 method test_select.

public void test_select() throws Exception {
    //
    String sql = "SELECT split_part(content, '\\001')[1] FROM dual;";
    Assert.assertEquals("SELECT split_part(content, '\\001')[1]\n" + "FROM dual;", SQLUtils.formatOdps(sql));
    Assert.assertEquals("select split_part(content, '\\001')[1]\n" + "from dual;", 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 : SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 45 with SchemaStatVisitor

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

the class OdpsSelectTest9 method test_select.

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

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