Search in sources :

Example 1 with SqlCodeStat

use of com.alibaba.druid.support.opds.udf.SqlCodeStat in project druid by alibaba.

the class OdpsListResourcesTest 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();
        if (expect != null) {
            expect = expect.replaceAll("\\r\\n", "\n");
        }
    }
    new SqlCodeStat().evaluate(sql, "odps");
    OdpsStatementParser parser = new OdpsStatementParser(sql, SQLParserFeature.EnableSQLBinaryOpExprGroup, SQLParserFeature.EnableMultiUnion, SQLParserFeature.KeepComments);
    List<SQLStatement> statementList = parser.parseStatementList();
    if (statementList.size() == 0) {
        throw new Exception("empty");
    }
    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));
    if (expect != null && !expect.isEmpty()) {
        assertEquals(expect, stmt.toString());
    }
    SchemaStatVisitor schemaStatVisitor = SQLUtils.createSchemaStatVisitor(DbType.odps);
    for (SQLStatement item : statementList) {
        item.accept(schemaStatVisitor);
    }
}
Also used : SqlCodeStat(com.alibaba.druid.support.opds.udf.SqlCodeStat) 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)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 OdpsStatementParser (com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser)1 OdpsSchemaStatVisitor (com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor)1 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)1 SqlCodeStat (com.alibaba.druid.support.opds.udf.SqlCodeStat)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1