Ejemplo n.º 1
0
  public void token(String token, QueryTranslatorImpl q) throws QueryException {
    String lcToken = token.toLowerCase(Locale.ROOT);

    // Cope with [,]
    if (token.equals("[") && !expectingPathContinuation) {
      expectingPathContinuation = false;
      if (expectingIndex == 0) {
        throw new QueryException("unexpected [");
      }
      return;
    } else if (token.equals("]")) {
      expectingIndex--;
      expectingPathContinuation = true;
      return;
    }

    // Cope with a continued path expression (ie. ].baz)
    if (expectingPathContinuation) {
      boolean pathExpressionContinuesFurther = continuePathExpression(token, q);
      if (pathExpressionContinuesFurther) {
        return; // NOTE: early return
      }
    }

    // Cope with a subselect
    if (!inSubselect && (lcToken.equals("select") || lcToken.equals("from"))) {
      inSubselect = true;
      subselect = new StringBuilder(20);
    }
    if (inSubselect && token.equals(")")) {
      bracketsSinceSelect--;

      if (bracketsSinceSelect == -1) {
        QueryTranslatorImpl subq =
            new QueryTranslatorImpl(subselect.toString(), q.getEnabledFilters(), q.getFactory());
        try {
          subq.compile(q);
        } catch (MappingException me) {
          throw new QueryException("MappingException occurred compiling subquery", me);
        }
        appendToken(q, subq.getSQLString());
        inSubselect = false;
        bracketsSinceSelect = 0;
      }
    }
    if (inSubselect) {
      if (token.equals("(")) {
        bracketsSinceSelect++;
      }
      subselect.append(token).append(' ');
      return;
    }

    // Cope with special cases of AND, NOT, ()
    specialCasesBefore(lcToken);

    // Close extra brackets we opened
    if (!betweenSpecialCase && EXPRESSION_TERMINATORS.contains(lcToken)) {
      closeExpression(q, lcToken);
    }

    // take note when this is a boolean expression
    if (BOOLEAN_OPERATORS.contains(lcToken)) {
      booleanTests.removeLast();
      booleanTests.addLast(Boolean.TRUE);
    }

    if (lcToken.equals("not")) {
      nots.addLast(!(nots.removeLast()));
      negated = !negated;
      return; // NOTE: early return
    }

    // process a token, mapping OO path expressions to SQL expressions
    doToken(token, q);

    // Open any extra brackets we might need.
    if (!betweenSpecialCase && EXPRESSION_OPENERS.contains(lcToken)) {
      openExpression(q, lcToken);
    }

    // Cope with special cases of AND, NOT, )
    specialCasesAfter(lcToken);
  }