Ejemplo n.º 1
0
 /**
  * Compile a subquery.
  *
  * @param superquery The containing query of the query to be compiled.
  * @throws org.hibernate.MappingException Indicates problems resolving things referenced in the
  *     query.
  * @throws org.hibernate.QueryException Generally some form of syntatic failure.
  */
 void compile(QueryTranslatorImpl superquery) throws QueryException, MappingException {
   this.tokenReplacements = superquery.tokenReplacements;
   this.superQuery = superquery;
   this.shallowQuery = true;
   this.enabledFilters = superquery.getEnabledFilters();
   compile();
 }
Ejemplo n.º 2
0
  private void doPathExpression(String token, QueryTranslatorImpl q) throws QueryException {

    preprocess(token, q);

    StringTokenizer tokens = new StringTokenizer(token, ".", true);
    pathExpressionParser.start(q);
    while (tokens.hasMoreTokens()) {
      pathExpressionParser.token(tokens.nextToken(), q);
    }
    pathExpressionParser.end(q);
    if (pathExpressionParser.isCollectionValued()) {
      openExpression(q, "");
      appendToken(q, pathExpressionParser.getCollectionSubquery(q.getEnabledFilters()));
      closeExpression(q, "");
      // this is ugly here, but needed because its a subquery
      q.addQuerySpaces(
          q.getCollectionPersister(pathExpressionParser.getCollectionRole()).getCollectionSpaces());
    } else {
      if (pathExpressionParser.isExpectingCollectionIndex()) {
        expectingIndex++;
      } else {
        addJoin(pathExpressionParser.getWhereJoin(), q);
        appendToken(q, pathExpressionParser.getWhereColumn());
      }
    }
  }
Ejemplo n.º 3
0
 private void addJoin(JoinSequence joinSequence, QueryTranslatorImpl q) throws QueryException {
   // JoinFragment fromClause = q.createJoinFragment(true);
   // fromClause.addJoins( join.toJoinFragment().toFromFragmentString(), StringHelper.EMPTY_STRING
   // );
   q.addFromJoinOnly(pathExpressionParser.getName(), joinSequence);
   try {
     addToCurrentJoin(
         joinSequence.toJoinFragment(q.getEnabledFilters(), true).toWhereFragmentString());
   } catch (MappingException me) {
     throw new QueryException(me);
   }
 }
Ejemplo n.º 4
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);
  }
  public void token(String token, QueryTranslatorImpl q) throws QueryException {

    if (token != null) path.append(token);

    String alias = q.getPathAlias(path.toString());
    if (alias != null) {
      reset(q); // reset the dotcount (but not the path)
      currentName = alias; // after reset!
      currentPropertyMapping = q.getPropertyMapping(currentName);
      if (!ignoreInitialJoin) {
        JoinSequence ojf = q.getPathJoin(path.toString());
        try {
          joinSequence.addCondition(
              ojf.toJoinFragment(q.getEnabledFilters(), true)
                  .toWhereFragmentString()); // after reset!
        } catch (MappingException me) {
          throw new QueryException(me);
        }
        // we don't need to worry about any condition in the ON clause
        // here (toFromFragmentString), since anything in the ON condition
        // is already applied to the whole query
      }
    } else if (".".equals(token)) {
      dotcount++;
    } else {
      if (dotcount == 0) {
        if (!continuation) {
          if (!q.isName(token)) throw new QueryException("undefined alias: " + token);
          currentName = token;
          currentPropertyMapping = q.getPropertyMapping(currentName);
        }
      } else if (dotcount == 1) {
        if (currentName != null) {
          currentProperty = token;
        } else if (collectionName != null) {
          // processCollectionProperty(token, q.getCollectionPersister(collectionRole),
          // collectionName);
          continuation = false;
        } else {
          throw new QueryException("unexpected");
        }
      } else { // dotcount>=2

        // Do the corresponding RHS
        Type propertyType = getPropertyType();

        if (propertyType == null) {
          throw new QueryException("unresolved property: " + path);
        }

        if (propertyType.isComponentType()) {
          dereferenceComponent(token);
        } else if (propertyType.isEntityType()) {
          if (!isCollectionValued()) dereferenceEntity(token, (EntityType) propertyType, q);
        } else if (propertyType.isCollectionType()) {
          dereferenceCollection(token, ((CollectionType) propertyType).getRole(), q);

        } else {
          if (token != null) throw new QueryException("dereferenced: " + path);
        }
      }
    }
  }