Example #1
0
  private void doToken(String token, QueryTranslatorImpl q) throws QueryException {
    if (q.isName(StringHelper.root(token))) { // path expression
      doPathExpression(q.unalias(token), q);
    } else if (token.startsWith(ParserHelper.HQL_VARIABLE_PREFIX)) { // named query parameter
      q.addNamedParameter(token.substring(1));
      appendToken(q, "?");
    } else {
      Queryable persister = q.getEntityPersisterUsingImports(token);
      if (persister != null) { // the name of a class
        final String discrim = persister.getDiscriminatorSQLValue();
        if (InFragment.NULL.equals(discrim) || InFragment.NOT_NULL.equals(discrim)) {
          throw new QueryException("subclass test not allowed for null or not null discriminator");
        } else {
          appendToken(q, discrim);
        }
      } else {
        Object constant;
        if (token.indexOf('.') > -1
            && (constant =
                    ReflectHelper.getConstantValue(
                        token,
                        q.getFactory().getServiceRegistry().getService(ClassLoaderService.class)))
                != null) {
          Type type;
          try {
            type = q.getFactory().getTypeResolver().heuristicType(constant.getClass().getName());
          } catch (MappingException me) {
            throw new QueryException(me);
          }
          if (type == null) {
            throw new QueryException(QueryTranslator.ERROR_CANNOT_DETERMINE_TYPE + token);
          }
          try {
            //noinspection unchecked
            appendToken(
                q, ((LiteralType) type).objectToSQLString(constant, q.getFactory().getDialect()));
          } catch (Exception e) {
            throw new QueryException(QueryTranslator.ERROR_CANNOT_FORMAT_LITERAL + token, e);
          }
        } else { // anything else

          String negatedToken = negated ? NEGATIONS.get(token.toLowerCase(Locale.ROOT)) : null;
          if (negatedToken != null && (!betweenSpecialCase || !"or".equals(negatedToken))) {
            appendToken(q, negatedToken);
          } else {
            appendToken(q, token);
          }
        }
      }
    }
  }
 boolean isName(String name) {
   return aliasNames.containsKey(name)
       || typeMap.containsKey(name)
       || collections.containsKey(name)
       || (superQuery != null && superQuery.isName(name));
 }
  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);
        }
      }
    }
  }