예제 #1
0
  public void endVisit(SQLSelectQueryBlock x) {
    String originalTable = (String) x.getAttribute("_old_local_");
    x.putAttribute("table", getCurrentTable());
    setCurrentTable(originalTable);

    setModeOrigin(x);
  }
예제 #2
0
  protected void parseInto(SQLSelectQueryBlock queryBlock) {
    if (lexer.token() == (Token.INTO)) {
      lexer.nextToken();

      if (identifierEquals("OUTFILE")) {
        lexer.nextToken();

        MySqlOutFileExpr outFile = new MySqlOutFileExpr();
        outFile.setFile(expr());

        queryBlock.setInto(outFile);

        if (identifierEquals("FIELDS") || identifierEquals("COLUMNS")) {
          lexer.nextToken();

          if (identifierEquals("TERMINATED")) {
            lexer.nextToken();
            accept(Token.BY);
          }
          outFile.setColumnsTerminatedBy((SQLLiteralExpr) expr());

          if (identifierEquals("OPTIONALLY")) {
            lexer.nextToken();
            outFile.setColumnsEnclosedOptionally(true);
          }

          if (identifierEquals("ENCLOSED")) {
            lexer.nextToken();
            accept(Token.BY);
            outFile.setColumnsEnclosedBy((SQLLiteralExpr) expr());
          }

          if (identifierEquals("ESCAPED")) {
            lexer.nextToken();
            accept(Token.BY);
            outFile.setColumnsEscaped((SQLLiteralExpr) expr());
          }
        }

        if (identifierEquals("LINES")) {
          lexer.nextToken();

          if (identifierEquals("STARTING")) {
            lexer.nextToken();
            accept(Token.BY);
            outFile.setLinesStartingBy((SQLLiteralExpr) expr());
          } else {
            identifierEquals("TERMINATED");
            lexer.nextToken();
            accept(Token.BY);
            outFile.setLinesTerminatedBy((SQLLiteralExpr) expr());
          }
        }
      } else {
        queryBlock.setInto(this.exprParser.name());
      }
    }
  }
예제 #3
0
  public void test_1() throws Exception {
    String sql = "(select * from a) union select * from b";
    PGSQLStatementParser parser = new PGSQLStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement statemen = statementList.get(0);
    //         print(statementList);

    Assert.assertEquals(1, statementList.size());
    assertTrue(statemen instanceof PGSelectStatement);
    PGSelectStatement select = (PGSelectStatement) statemen;
    assertTrue(select.getSelect().getQuery() instanceof SQLUnionQuery);
    SQLUnionQuery unionQuery = (SQLUnionQuery) select.getSelect().getQuery();
    assertTrue(unionQuery.getLeft() instanceof SQLSelectQueryBlock);
    assertTrue(unionQuery.getRight() instanceof SQLSelectQueryBlock);
    SQLSelectQueryBlock leftQueryBlock = (SQLSelectQueryBlock) unionQuery.getLeft();
    assertTrue(leftQueryBlock.isParenthesized());
  }
예제 #4
0
  public static boolean visit(MappingVisitor visitor, SQLSelectQueryBlock x) {
    if (x.getSelectList().size() == 0) {
      fillSelectList(visitor, x);
    }

    if (x.getSelectList().size() == 1) {
      if (x.getSelectList().get(0).getExpr() instanceof SQLAllColumnExpr) {
        x.getSelectList().clear();
        fillSelectList(visitor, x);
      }
    }

    if (x.getFrom() == null) {
      Entity firstEntity = visitor.getFirstEntity();
      SQLExprTableSource from =
          new SQLExprTableSource(new SQLIdentifierExpr(firstEntity.getName()));
      x.setFrom(from);
    }

    for (SQLSelectItem item : x.getSelectList()) {
      item.setParent(x);
    }

    return true;
  }
예제 #5
0
  public static void fillSelectList(MappingVisitor visitor, SQLSelectQueryBlock x) {
    Entity entity = visitor.getFirstEntity();

    for (Property item : entity.getProperties().values()) {
      x.getSelectList()
          .add(
              new SQLSelectItem(new SQLIdentifierExpr(item.getName()), '"' + item.getName() + '"'));
    }
  }
예제 #6
0
  private void setColumn(SQLExpr x, Column column) {
    SQLObject current = x;
    for (; ; ) {
      SQLObject parent = current.getParent();

      if (parent == null) {
        break;
      }

      if (parent instanceof SQLSelectQueryBlock) {
        SQLSelectQueryBlock query = (SQLSelectQueryBlock) parent;
        if (query.getWhere() == current) {
          column.setWhere(true);
        }
        break;
      }

      if (parent instanceof SQLSelectGroupByClause) {
        SQLSelectGroupByClause groupBy = (SQLSelectGroupByClause) parent;
        if (current == groupBy.getHaving()) {
          column.setHaving(true);
        } else if (groupBy.getItems().contains(current)) {
          column.setGroupBy(true);
        }
        break;
      }

      if (parent instanceof SQLSelectItem) {
        column.setSelec(true);
        break;
      }

      if (parent instanceof SQLJoinTableSource) {
        SQLJoinTableSource join = (SQLJoinTableSource) parent;
        if (join.getCondition() == current) {
          column.setJoin(true);
        }
        break;
      }

      current = parent;
    }
  }
예제 #7
0
 public boolean visit(SQLOrderBy x) {
   OrderByStatVisitor orderByVisitor = new OrderByStatVisitor(x);
   SQLSelectQueryBlock query = null;
   if (x.getParent() instanceof SQLSelectQueryBlock) {
     query = (SQLSelectQueryBlock) x.getParent();
   }
   if (query != null) {
     for (SQLSelectOrderByItem item : x.getItems()) {
       SQLExpr expr = item.getExpr();
       if (expr instanceof SQLIntegerExpr) {
         int intValue = ((SQLIntegerExpr) expr).getNumber().intValue() - 1;
         if (intValue < query.getSelectList().size()) {
           SQLSelectItem selectItem = query.getSelectList().get(intValue);
           selectItem.getExpr().accept(orderByVisitor);
         }
       }
     }
   }
   x.accept(orderByVisitor);
   return true;
 }
예제 #8
0
  public static boolean visit(SQLEvalVisitor visitor, SQLQueryExpr x) {
    if (x.getSubQuery().getQuery() instanceof SQLSelectQueryBlock) {
      SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) x.getSubQuery().getQuery();

      boolean nullFrom = false;
      if (queryBlock.getFrom() == null) {
        nullFrom = true;
      } else if (queryBlock.getFrom() instanceof SQLExprTableSource) {
        SQLExpr expr = ((SQLExprTableSource) queryBlock.getFrom()).getExpr();
        if (expr instanceof SQLIdentifierExpr) {
          if ("dual".equalsIgnoreCase(((SQLIdentifierExpr) expr).getName())) {
            nullFrom = true;
          }
        }
      }

      if (nullFrom) {
        List<Object> row = new ArrayList<Object>(queryBlock.getSelectList().size());
        for (int i = 0; i < queryBlock.getSelectList().size(); ++i) {
          SQLSelectItem item = queryBlock.getSelectList().get(i);
          item.getExpr().accept(visitor);
          Object cell = item.getExpr().getAttribute(EVAL_VALUE);
          row.add(cell);
        }
        List<List<Object>> rows = new ArrayList<List<Object>>(1);
        rows.add(row);

        Object result = rows;
        queryBlock.putAttribute(EVAL_VALUE, result);
        x.getSubQuery().putAttribute(EVAL_VALUE, result);
        x.putAttribute(EVAL_VALUE, result);

        return false;
      }
    }

    return false;
  }
예제 #9
0
  public boolean visit(SQLSelectQueryBlock x) {
    if (x.getFrom() == null) {
      return false;
    }

    setMode(x, Mode.Select);

    if (x.getFrom() instanceof SQLSubqueryTableSource) {
      x.getFrom().accept(this);
      return false;
    }

    if (x.getInto() != null && x.getInto().getExpr() instanceof SQLName) {
      SQLName into = (SQLName) x.getInto().getExpr();
      String ident = into.toString();
      TableStat stat = getTableStat(ident);
      if (stat != null) {
        stat.incrementInsertCount();
      }
    }

    String originalTable = getCurrentTable();

    if (x.getFrom() instanceof SQLExprTableSource) {
      SQLExprTableSource tableSource = (SQLExprTableSource) x.getFrom();
      if (tableSource.getExpr() instanceof SQLName) {
        String ident = tableSource.getExpr().toString();

        setCurrentTable(x, ident);
        x.putAttribute(ATTR_TABLE, ident);
        if (x.getParent() instanceof SQLSelect) {
          x.getParent().putAttribute(ATTR_TABLE, ident);
        }
        x.putAttribute("_old_local_", originalTable);
      }
    }

    if (x.getFrom() != null) {
      x.getFrom().accept(this); // 提前执行,获得aliasMap
      String table = (String) x.getFrom().getAttribute(ATTR_TABLE);
      if (table != null) {
        x.putAttribute(ATTR_TABLE, table);
      }
    }

    // String ident = x.getTable().toString();
    //
    // TableStat stat = getTableStat(ident);
    // stat.incrementInsertCount();
    // return false;

    if (x.getWhere() != null) {
      x.getWhere().setParent(x);
    }

    return true;
  }