Search in sources :

Example 96 with OracleSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor in project druid by alibaba.

the class OracleSelectTest2 method test_0.

public void test_0() throws Exception {
    String sql = //
    "WITH " + //
    "   dept_costs AS (" + //
    "      SELECT d.department_name, SUM(d.salary) dept_total" + //
    "         FROM employees e, departments d" + //
    "         WHERE e.department_id = d.department_id" + //
    "      GROUP BY d.department_name)," + //
    "   avg_cost AS (" + //
    "      SELECT SUM(dept_total)/COUNT(*) avg" + //
    "      FROM dept_costs)" + //
    "SELECT * FROM dept_costs" + //
    "   WHERE dept_total >" + //
    "      (SELECT avg FROM avg_cost)" + "      ORDER BY department_name;";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    String result = SQLUtils.toOracleString(stmt);
    String result_lcase = SQLUtils.toOracleString(stmt, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION);
    Assert.assertEquals(result.toLowerCase(), result_lcase);
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    stmt.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(2, visitor.getTables().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("employees")));
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("departments")));
    Assert.assertEquals(4, visitor.getColumns().size());
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("films", "producer_id")));
}
Also used : 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 97 with OracleSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor in project druid by alibaba.

the class OracleSelectTest22 method test_0.

public void test_0() throws Exception {
    //
    String sql = //
    "select /*+ no_parallel(t) no_parallel_index(t) dbms_stats cursor_sharing_exact use_weak_name_resl dynamic_sampling(0) no_monitoring */ count(*),sum(sys_op_opnsize(\"ID\")),substrb(dump(min(\"ID\"),16,0,32),1,120),substrb(dump(max(\"ID\"),16,0,32),1,120),count(distinct \"GMT_MODIFIED\"),substrb(dump(min(\"GMT_MODIFIED\"),16,0,32),1,120),substrb(dump(max(\"GMT_MODIFIED\"),16,0,32),1,120),count(distinct \"GMT_CREATE\"),substrb(dump(min(\"GMT_CREATE\"),16,0,32),1,120),substrb(dump(max(\"GMT_CREATE\"),16,0,32),1,120),count(\"TRADE_ID\"),count(distinct \"TRADE_ID\"),sum(sys_op_opnsize(\"TRADE_ID\")),substrb(dump(min(\"TRADE_ID\"),16,0,32),1,120),substrb(dump(max(\"TRADE_ID\"),16,0,32),1,120),count(\"STATUS\"),count(distinct \"STATUS\"),sum(sys_op_opnsize(\"STATUS\")),substrb(dump(min(substrb(\"STATUS\",1,32)),16,0,32),1,120),substrb(dump(max(substrb(\"STATUS\",1,32)),16,0,32),1,120),count(\"OWNER\"),count(distinct \"OWNER\"),sum(sys_op_opnsize(\"OWNER\")),substrb(dump(min(substrb(\"OWNER\",1,32)),16,0,32),1,120),substrb(dump(max(substrb(\"OWNER\",1,32)),16,0,32),1,120),count(\"GMT_FETCH_TASK\"),count(distinct \"GMT_FETCH_TASK\"),substrb(dump(min(\"GMT_FETCH_TASK\"),16,0,32),1,120),substrb(dump(max(\"GMT_FETCH_TASK\"),16,0,32),1,120),count(\"GMT_FINISH_TASK\"),count(distinct \"GMT_FINISH_TASK\"),substrb(dump(min(\"GMT_FINISH_TASK\"),16,0,32),1,120),substrb(dump(max(\"GMT_FINISH_TASK\"),16,0,32),1,120),count(\"VERSION\"),count(distinct \"VERSION\"),sum(sys_op_opnsize(\"VERSION\")),substrb(dump(min(\"VERSION\"),16,0,32),1,120),substrb(dump(max(\"VERSION\"),16,0,32),1,120),count(\"RECORD_TYPE\"),count(distinct \"RECORD_TYPE\"),sum(sys_op_opnsize(\"RECORD_TYPE\")),substrb(dump(min(substrb(\"RECORD_TYPE\",1,32)),16,0,32),1,120),substrb(dump(max(substrb(\"RECORD_TYPE\",1,32)),16,0,32),1,120),count(\"TASK_FLOW_LEVEL\"),count(distinct \"TASK_FLOW_LEVEL\"),sum(sys_op_opnsize(\"TASK_FLOW_LEVEL\")),substrb(dump(min(\"TASK_FLOW_LEVEL\"),16,0,32),1,120),substrb(dump(max(\"TASK_FLOW_LEVEL\"),16,0,32),1,120),count(\"DEAL_TYPE\"),count(distinct \"DEAL_TYPE\"),sum(sys_op_opnsize(\"DEAL_TYPE\")),substrb(dump(min(\"DEAL_TYPE\"),16,0,32),1,120),substrb(dump(max(\"DEAL_TYPE\"),16,0,32),1,120),count(\"END_REASON\"),count(distinct \"END_REASON\"),sum(sys_op_opnsize(\"END_REASON\")),substrb(dump(min(\"END_REASON\"),16,0,32),1,120),substrb(dump(max(\"END_REASON\"),16,0,32),1,120),count(\"TRANSIT_TIME\"),count(distinct \"TRANSIT_TIME\"),sum(sys_op_opnsize(\"TRANSIT_TIME\")),substrb(dump(min(\"TRANSIT_TIME\"),16,0,32),1,120),substrb(dump(max(\"TRANSIT_TIME\"),16,0,32),1,120) from \"ESCROW\".\"HT_TASK_TRADE_HISTORY\" sample (   .5000000000) t ";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    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());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("ESCROW.HT_TASK_TRADE_HISTORY")));
    Assert.assertEquals(14, visitor.getColumns().size());
//         Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("ESCROW", "HT_TASK_TRADE_HISTORY.ID")));
//         Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("ESCROW", "HT_TASK_TRADE_HISTORY.GMT_MODIFIED")));
//         Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("ESCROW", "HT_TASK_TRADE_HISTORY.GMT_CREATE")));
//         Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("ESCROW", "ESCROW.HT_TASK_TRADE_HISTORY.TRADE_ID")));
}
Also used : 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 98 with OracleSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor in project druid by alibaba.

the class OracleSelectTest23 method test_0.

public void test_0() throws Exception {
    //
    String sql = //
    "select (select decode(count(*),1,'YES','NO') FROM sys.obj$ o, sys.user$ u WHERE u.name = 'PERFSTAT' AND o.owner# = u.user# AND o.name = 'STATSPACK' AND o.type# = 11 AND o.status = 1) is_installed , (select  nvl(INTERVAL,'') from dba_jobs where what like 'statspack.snap%' and SCHEMA_USER='PERFSTAT' and rownum =1) freq from dual";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    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());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(3, visitor.getTables().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("sys.obj$")));
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("sys.user$")));
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("dba_jobs")));
    Assert.assertEquals(9, visitor.getColumns().size());
    Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("sys.user$", "name")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "YEAR")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "order_mode")));
}
Also used : 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 99 with OracleSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor in project druid by alibaba.

the class OracleSelectTest43 method test_0.

public void test_0() throws Exception {
    //
    String sql = //
    "SELECT * FROM table(t_department)  " + //
    "WHERE name IN ('0000','4444') " + //
    "ORDER BY name ASC";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    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());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("acduser.vw_acd_info", "xzqh")));
// Assert.assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
}
Also used : 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 100 with OracleSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor in project druid by alibaba.

the class OracleSelectTest46 method test_0.

public void test_0() throws Exception {
    //
    String sql = //
    "select *" + //
    " FROM MT_PRODUCT_ORDER T1,MT_ORDER T2 WHERE " + //
    " T1.MT_ORDER_ID = T2.MT_ORDER_ID" + //
    " and SELLER_SSOID = 1" + //
    " AND T1.MT_ORDER_ID = '1'" + //
    " AND T1.MT_BATCH_ORDER_ID IN '1'";
    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    print(statementList);
    Assert.assertEquals(1, statementList.size());
    OracleSchemaStatVisitor visitor = new OracleSchemaStatVisitor();
    stmt.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());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(2, visitor.getTables().size());
    Assert.assertEquals(4, visitor.getColumns().size());
    String text = TestUtils.outputOracle(stmt);
    Assert.assertEquals(//
    "SELECT *" + //
    "\nFROM MT_PRODUCT_ORDER T1, MT_ORDER T2" + //
    "\nWHERE T1.MT_ORDER_ID = T2.MT_ORDER_ID" + //
    "\n\tAND SELLER_SSOID = 1" + //
    "\n\tAND T1.MT_ORDER_ID = '1'" + //
    "\n\tAND T1.MT_BATCH_ORDER_ID IN ('1');" + "\n", text);
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("acduser.vw_acd_info", "xzqh")));
// Assert.assertTrue(visitor.getOrderByColumns().contains(new TableStat.Column("employees", "last_name")));
}
Also used : 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

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)254 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)254 OracleSchemaStatVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor)254 Column (com.alibaba.druid.stat.TableStat.Column)19 OracleOutputVisitor (com.alibaba.druid.sql.dialect.oracle.visitor.OracleOutputVisitor)6 Condition (com.alibaba.druid.stat.TableStat.Condition)2 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)1 SQLSelectQueryBlock (com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1