Search in sources :

Example 66 with SchemaStatVisitor

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

the class SchemaStatTest12 method test_schemaStat.

public void test_schemaStat() throws Exception {
    String sql = "select work_no , nick_name , name , ifnull(sum(investment_percentage ) / 100 , 0 ) as invest_percent " + "from ( " + "   select t1.id , t1.work_no , t2.nick_name , t2.name , t2.super_work_no , t2.super_nick_name , t2.super_name " + "       , t3.project_name , t1.investment_percentage , t4.dept_name " + "   from t_pm_aur t2 " + "       left outer join ( " + "           select * from t_pm_rs_acl " + "           where is_deleted = 0 and prj_biz_id is not null and ivt_pt > 0 and iwk = '2016W17' " + "       ) t1 on t2.work_no = t1.work_no " + "       left outer join ( " + "           select * from t_pm_prj_i " + "           where( status not in( 'DEPRECATED' , 'FINISHED' ) or status is null ) and( task_type not in( 'WEEKLY_REPORT' ) or task_type is null ) and is_deleted = 0 " + "       ) t3 on t1.prj_biz_id = t3.biz_id " + "       left outer join t_pm_dpt t4 on t2.dept_no = t4.biz_id " + "   where t4.dept_name like 'MY-XDSYB-JSB%' and t2.emp_type in( 'R' , 'V' ) and t2.is_deleted = 'N' and t2.work_status = 'A' " + ") t5 group by work_no , nick_name , name having invest_percent < 1.0 ";
    String dbType = JdbcConstants.ORACLE;
    SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType);
    SQLStatement stmt = parser.parseStatementList().get(0);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(statVisitor);
    Set<TableStat.Relationship> relationships = statVisitor.getRelationships();
    for (TableStat.Relationship relationship : relationships) {
        // table1.id = table2.id
        System.out.println(relationship);
    }
    //        System.out.println(statVisitor.getColumns());
    //        System.out.println(statVisitor.getGroupByColumns()); // group by
    // group by
    System.out.println("relationships : " + statVisitor.getRelationships());
    System.out.println(statVisitor.getConditions());
    assertEquals(3, relationships.size());
    Assert.assertEquals(21, statVisitor.getColumns().size());
    Assert.assertEquals(20, statVisitor.getConditions().size());
    assertEquals(1, statVisitor.getFunctions().size());
}
Also used : SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) TableStat(com.alibaba.druid.stat.TableStat) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor)

Example 67 with SchemaStatVisitor

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

the class SchemaStatTest4 method test_schemaStat.

public void test_schemaStat() throws Exception {
    String sql = "select name, age from t_user where id = 1";
    String dbType = JdbcConstants.MYSQL;
    List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
    SQLStatement stmt = stmtList.get(0);
    SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(dbType);
    stmt.accept(statVisitor);
    System.out.println(statVisitor.getColumns());
    System.out.println(statVisitor.getTables());
    System.out.println(statVisitor.getConditions());
    Assert.assertEquals(3, statVisitor.getColumns().size());
}
Also used : 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