Beispiel #1
0
  public void test_0() throws Exception {
    String sql =
        "INSERT INTO films VALUES ('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes');";

    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("alias : " + visitor.getAliasMap());
    System.out.println("conditions : " + visitor.getConditions());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    System.out.println("groupBy : " + visitor.getGroupByColumns());
    System.out.println("variant : " + visitor.getVariants());
    System.out.println("relationShip : " + visitor.getRelationships());
    System.out.println("--------------------------------");

    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("films")));

    Assert.assertEquals(0, visitor.getColumns().size());

    //        Assert.assertTrue(visitor.getFields().contains(new TableStat.Column("films",
    // "producer_id")));
  }
  @Test
  public void test_0() throws Exception {
    String sql = "select id, name from users where id>? and name=? and ROWNUM < 10";

    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);

    Assert.assertEquals(1, statementList.size());

    ZdalOracleSchemaStatVisitor visitor = new ZdalOracleSchemaStatVisitor();
    stmt.accept(visitor);

    System.out.println(sql);
    System.out.println("Tables : " + visitor.getTables());
    System.out.println("fields : " + visitor.getColumns());
    System.out.println("alias : " + visitor.getAliasMap());
    System.out.println("conditions : " + visitor.getConditions());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    System.out.println("groupBy : " + visitor.getGroupByColumns());
    System.out.println("variant : " + visitor.getVariants());
    System.out.println("relationShip : " + visitor.getRelationships());
    System.out.println("bindColumns : " + visitor.getBindVarConditions());
    System.out.println("rownums : " + visitor.getRownums());
    System.out.println("--------------------------------");

    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(true, visitor.containsTable("users"));

    Assert.assertEquals(2, visitor.getColumns().size());

    StringBuilder buf = new StringBuilder();
    OracleOutputVisitor outputVisitor = new OracleOutputVisitor(buf);
    stmt.accept(outputVisitor);
  }
  public void test_0() throws Exception {
    String sql = //
        "ALTER TABLE wl_service_record add ( service_type VARCHAR2(32) )";

    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    print(statementList);

    Assert.assertEquals(1, statementList.size());

    ZdalOracleSchemaStatVisitor visitor = new ZdalOracleSchemaStatVisitor();
    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("wl_service_record")));

    Assert.assertEquals(1, visitor.getColumns().size());

    Assert.assertTrue(
        visitor.getColumns().contains(new TableStat.Column("wl_service_record", "service_type")));
    // Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table",
    // "YEAR")));
    // Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table",
    // "order_mode")));
  }
  public void test_0() throws Exception {
    String sql = "alter session set current_schema=retl";

    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());

    //        Assert.assertTrue(visitor.getTables().containsKey(new
    // TableStat.Name("employees@remote")));

    Assert.assertEquals(0, visitor.getTables().size());
    Assert.assertEquals(0, visitor.getColumns().size());

    //        Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("bonuses",
    // "employee_id")));
  }
  @Test
  public void test_0() throws Exception {
    String sql = //
        "alter table WRH$_SERVICE_WAIT_CLASS drop partition WRH$_SERVIC_1870432296_13478";

    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);

    Assert.assertEquals(1, statementList.size());

    ZdalOracleSchemaStatVisitor visitor = new ZdalOracleSchemaStatVisitor();
    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("WRH$_SERVICE_WAIT_CLASS")));

    Assert.assertEquals(0, visitor.getColumns().size());

    // Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "*")));
    // Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table",
    // "YEAR")));
    // Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table",
    // "order_mode")));
  }
Beispiel #6
0
  private String output(List<SQLStatement> stmtList) {
    StringBuilder out = new StringBuilder();

    for (SQLStatement stmt : stmtList) {
      stmt.accept(new MySqlOutputVisitor(out));
      out.append(";");
    }

    return out.toString();
  }
  void validateOracle(String sql, String expect) {

    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);

    Assert.assertEquals(1, statementList.size());

    StringBuilder out = new StringBuilder();
    OracleParameterizedOutputVisitor visitor = new OracleParameterizedOutputVisitor(out, false);
    statemen.accept(visitor);

    Assert.assertEquals(expect, out.toString());
  }
Beispiel #8
0
  private String convert(String sql, List<Object> parameters) {
    SQLStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> stmtList = parser.parseStatementList(); //

    SQLStatement first = (SQLStatement) stmtList.get(0);

    MyVisitor visitor = new MyVisitor();
    first.accept(visitor);

    if (visitor.getVariantList().size() > 0) {
      SQLExpr firstVar = visitor.getVariantList().get(0);

      int userId;
      if (firstVar instanceof SQLVariantRefExpr) {
        int varIndex = (Integer) firstVar.getAttribute("varIndex");
        userId = (Integer) parameters.get(varIndex);
      } else {
        userId = ((SQLNumericLiteralExpr) firstVar).getNumber().intValue();
      }

      String tableName;
      if (userId == 1) {
        tableName = "user_1";
      } else {
        tableName = "user_x";
      }

      for (SQLExprTableSource tableSource : visitor.getTableSourceList()) {
        SQLExpr expr = tableSource.getExpr();
        if (expr instanceof SQLIdentifierExpr) {
          SQLIdentifierExpr identExpr = (SQLIdentifierExpr) expr;
          String ident = identExpr.getName();

          if (ident.equals("user")) {
            identExpr.setName(tableName);
          }
        } else if (expr instanceof SQLPropertyExpr) {
          SQLPropertyExpr proExpr = (SQLPropertyExpr) expr;
          String ident = proExpr.getName();

          if (ident.equals("user")) {
            proExpr.setName(tableName);
          }
        }
      }
    }

    String realSql = SQLUtils.toOracleString(first);
    return realSql;
  }
Beispiel #9
0
  @Test
  public void test_0() throws Exception {
    String sql = //
        "EXPLAIN PLAN "
            + //
            "    SET STATEMENT_ID = 'Raise in Tokyo' "
            + //
            "    INTO plan_table "
            + //
            "    FOR UPDATE employees "
            + //
            "        SET salary = salary * 1.10 "
            + //
            "        WHERE department_id =  "
            + //
            "           (SELECT department_id FROM departments"
            + //
            "               WHERE location_id = 1700); ";

    OracleStatementParser parser = new OracleStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);

    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(0, visitor.getTables().size());

    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("\"DUAL\"")));

    Assert.assertEquals(0, visitor.getColumns().size());

    // Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table", "*")));
    // Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table",
    // "YEAR")));
    // Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pivot_table",
    // "order_mode")));
  }
Beispiel #10
0
  public void test_0() throws Exception {
    String sql =
        "insert into \"OPS$ADMIN\".\"ORASTAT\" select /*+ rule */ :5 statid, 'C' type, :6 version, bitand(h.spare2,7) flags,       ot.name c1, null c2, null c3, c.name c4, u.name c5,       h.distcnt n1, h.density n2, h.spare1 n3, h.sample_size n4, h.null_cnt n5,       h.minimum n6, h.maximum n7, h.avgcln n8,       decode(h.cache_cnt,0,null,1) n9, hg.bucket n10, hg.endpoint n11,       null n12,       h.timestamp# d1,       h.lowval r1, h.hival r2, hg.epvalue ch1     from sys.user$ u,  sys.obj$ ot, sys.col$ c,   sys.hist_head$ h, histgrm$ hg     where       :3 is null and       u.name = :1 and ot.owner# = u.user# and       ot.name = :2 and ot.type# = 2 and        c.obj# = ot.obj# and       (:4 is null or c.name = :4) and       h.obj# = ot.obj# and h.intcol# = c.intcol# and       hg.obj#(+) = h.obj# and hg.intcol#(+) = h.intcol#     union all     select        :5 statid, 'C' type, :6 version, bitand(h.spare2,7) flags,       ot.name c1, op.subname c2, null c3, c.name c4, u.name c5,       h.distcnt n1, h.density n2, h.spare1 n3, h.sample_size n4, h.null_cnt n5,       h.minimum n6, h.maximum n7, h.avgcln n8,       decode(h.cache_cnt,0,null,1) n9, hg.bucket n10, hg.endpoint n11,       null n12,       h.timestamp# d1,       h.lowval r1, h.hival r2, hg.epvalue ch1     from sys.user$ u,  sys.obj$ ot, sys.col$ c,   sys.tabpart$ tp, sys.obj$ op,   sys.hist_head$ h, histgrm$ hg     where       u.name = :1 and ot.owner# = u.user# and       ot.name = :2 and ot.type# = 2 and        c.obj# = ot.obj# and       (:4 is null or c.name = :4) and       tp.bo# = ot.obj# and tp.obj# = op.obj# and       ((:3 is null and :vc_cascade_parts is not null)         or op.subname = :3) and       h.obj# = op.obj# and h.intcol# = c.intcol# and       hg.obj#(+) = h.obj# and hg.intcol#(+) = h.intcol#     union all     select        :5 statid, 'C' type, :6 version, bitand(h.spare2,7) flags,       op.name c1, op.subname c2, null c3, c.name c4, u.name c5,       h.distcnt n1, h.density n2, h.spare1 n3, h.sample_size n4, h.null_cnt n5,       h.minimum n6, h.maximum n7, h.avgcln n8,       decode(h.cache_cnt,0,null,1) n9, hg.bucket n10, hg.endpoint n11,       null n12,       h.timestamp# d1,       h.lowval r1, h.hival r2, hg.epvalue ch1     from sys.user$ u, sys.col$ c,   sys.tabcompart$ tp, sys.obj$ op,   sys.hist_head$ h, histgrm$ hg     where       u.name = :1 and op.owner# = u.user# and       op.name = :2 and op.type# = 19 and        ((:3 is null and :vc_cascade_parts is not null)         or op.subname = :3) and       tp.obj# = op.obj# and c.obj# = tp.bo# and       (:4 is null or c.name = :4) and       h.obj# = op.obj# and h.intcol# = c.intcol# and       hg.obj#(+) = h.obj# and hg.intcol#(+) = h.intcol#     union all     select        :5 statid, 'C' type, :6 version, bitand(h.spare2,7) flags,       op.name c1, op.subname c2, os.subname c3, c.name c4, u.name c5,       h.distcnt n1, h.density n2, h.spare1 n3, h.sample_size n4, h.null_cnt n5,       h.minimum n6, h.maximum n7, h.avgcln n8,       decode(h.cache_cnt,0,null,1) n9, hg.bucket n10, hg.endpoint n11,       null n12,       h.timestamp# d1,       h.lowval r1, h.hival r2, hg.epvalue ch1     from sys.user$ u, sys.col$ c,   sys.tabcompart$ tp, sys.obj$ op,   sys.tabsubpart$ ts, sys.obj$ os,   sys.hist_head$ h, histgrm$ hg     where       u.name = :1 and op.owner# = u.user# and       op.name = :2 and op.type# = 19 and        tp.obj# = op.obj# and c.obj# = tp.bo# and       (:4 is null or c.name = :4) and       ts.pobj# = tp.obj# and ts.obj# = os.obj# and       ((:3 is null and :vc_cascade_parts is not null)         or (op.subname = :3             and (:vc_cascade_parts is not null                    or os.subname is null))              or os.subname = :3) and                 h.obj# = os.obj# and h.intcol# = c.intcol# and       hg.obj#(+) = h.obj# and hg.intcol#(+) = h.intcol#     order by c5,c1,c2,c3,c4,n10";

    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("alias : " + visitor.getAliasMap());
    System.out.println("conditions : " + visitor.getConditions());
    System.out.println("orderBy : " + visitor.getOrderByColumns());
    System.out.println("groupBy : " + visitor.getGroupByColumns());
    System.out.println("variant : " + visitor.getVariants());
    System.out.println("relationShip : " + visitor.getRelationships());
    System.out.println("--------------------------------");

    Assert.assertEquals(9, visitor.getTables().size());
    Assert.assertEquals(42, visitor.getColumns().size());
    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("raises")));
    //        Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("employees")));
    //
    //        Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees",
    // "employee_id")));
    //        Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees",
    // "salary")));
    //        Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("employees",
    // "commission_pct")));
  }