示例#1
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;
  }
示例#2
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;
    }
  }