Exemple #1
0
  @Test
  public void Various() {
    StringPath str = user.lastName;
    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(str.between("a", "b"));
    predicates.add(str.contains("a"));
    predicates.add(str.containsIgnoreCase("a"));
    predicates.add(str.endsWith("a"));
    predicates.add(str.endsWithIgnoreCase("a"));
    predicates.add(str.eq("a"));
    predicates.add(str.equalsIgnoreCase("a"));
    predicates.add(str.goe("a"));
    predicates.add(str.gt("a"));
    predicates.add(str.in("a", "b", "c"));
    predicates.add(str.isEmpty());
    predicates.add(str.isNotNull());
    predicates.add(str.isNull());
    //        predicates.add(str.like("a"));
    predicates.add(str.loe("a"));
    predicates.add(str.lt("a"));
    predicates.add(str.matches("a"));
    predicates.add(str.ne("a"));
    predicates.add(str.notBetween("a", "b"));
    predicates.add(str.notIn("a", "b", "c"));
    predicates.add(str.startsWith("a"));
    predicates.add(str.startsWithIgnoreCase("a"));

    for (Predicate predicate : predicates) {
      where(predicate).count();
      where(predicate.not()).count();
    }
  }
    @Override
    public Return where(Predicate predicate) {
      // Parse predicate
      query.whereExpression =
          predicate.accept(
              new Visitor<WhereExpression.BooleanExpression, WhereExpression.BooleanExpression>() {
                @Override
                public WhereExpression.BooleanExpression visit(
                    Constant<?> constant,
                    @Nullable WhereExpression.BooleanExpression booleanExpression) {
                  return null;
                }

                @Override
                public WhereExpression.BooleanExpression visit(
                    FactoryExpression<?> factoryExpression,
                    @Nullable WhereExpression.BooleanExpression booleanExpression) {
                  return null;
                }

                @Override
                public WhereExpression.BooleanExpression visit(
                    Operation<?> operation,
                    @Nullable WhereExpression.BooleanExpression booleanExpression) {
                  String id = operation.getOperator().getId();
                  if (id.equals(Ops.AND.getId())) {
                    return WhereExpression.and(
                        operation.getArg(0).accept(this, null),
                        operation.getArg(1).accept(this, null));
                  } else if (id.equals(Ops.OR.getId())) {
                    return WhereExpression.or(
                        operation.getArg(0).accept(this, null),
                        operation.getArg(1).accept(this, null));
                  } else if (id.equals(Ops.NOT.getId())) {
                    return WhereExpression.not(
                        (WhereExpression.PredicateExpression)
                            operation.getArg(0).accept(this, null));
                  } else if (id.equals(Ops.EQ_PRIMITIVE.getId())
                      || id.equals(Ops.EQ_OBJECT.getId())) {
                    return WhereExpression.eq(
                        operation.getArg(0).toString(), arg(operation.getArg(1)));
                  } else if (id.equals(Ops.NE_PRIMITIVE.getId())
                      || id.equals(Ops.NE_OBJECT.getId())) {
                    return WhereExpression.ne(
                        operation.getArg(0).toString(), arg(operation.getArg(1)));
                  } else if (id.equals(Ops.GT.getId())) {
                    return WhereExpression.gt(
                        operation.getArg(0).toString(), arg(operation.getArg(1)));
                  } else if (id.equals(Ops.LT.getId())) {
                    return WhereExpression.lt(
                        operation.getArg(0).toString(),
                        ((Constant) operation.getArg(1)).getConstant());
                  } else if (id.equals(Ops.GOE.getId())) {
                    return WhereExpression.gte(
                        operation.getArg(0).toString(), arg(operation.getArg(1)));
                  } else if (id.equals(Ops.LOE.getId())) {
                    return WhereExpression.lte(
                        operation.getArg(0).toString(), arg(operation.getArg(1)));
                  } else if (id.equals(Ops.EXISTS.getId())) {
                    return WhereExpression.exists(operation.getArg(0).toString());
                  } else if (id.equals(Ops.IS_NULL.getId())) {
                    return WhereExpression.isNull(operation.getArg(0).toString());
                  } else if (id.equals(Ops.IS_NOT_NULL.getId())) {
                    return WhereExpression.isNotNull(operation.getArg(0).toString());
                  } else
                    throw new IllegalArgumentException(
                        "Unknown operator:" + id + " in expression " + operation);
                }

                @Override
                public WhereExpression.BooleanExpression visit(
                    ParamExpression<?> paramExpression,
                    @Nullable WhereExpression.BooleanExpression booleanExpression) {
                  return null;
                }

                @Override
                public WhereExpression.BooleanExpression visit(
                    Path<?> path, @Nullable WhereExpression.BooleanExpression booleanExpression) {
                  return null;
                }

                @Override
                public WhereExpression.BooleanExpression visit(
                    SubQueryExpression<?> subQueryExpression,
                    @Nullable WhereExpression.BooleanExpression booleanExpression) {
                  return null;
                }

                @Override
                public WhereExpression.BooleanExpression visit(
                    TemplateExpression<?> templateExpression,
                    @Nullable WhereExpression.BooleanExpression booleanExpression) {
                  return null;
                }

                public Object arg(com.mysema.query.types.Expression expression) {
                  if (expression instanceof Constant) return ((Constant) expression).getConstant();
                  else if (expression instanceof ParamExpression)
                    return Expression.param(((ParamExpression) expression).getName());
                  else throw new IllegalArgumentException("Unknown argument type:" + expression);
                }
              },
              null);

      return this;
    }