Пример #1
0
  @Override
  public Object evaluate(Object... args) {
    final Class sourceType = ((EntityPathBase) sources.get(0)).getType();

    if (projection instanceof EntityPathBase) return getArgByType(projection.getType(), args);

    if (projection instanceof SimpleExpression || projection instanceof PathImpl)
      return getValue(
          (Path) projection, getArgByType(((Path) projection).getRoot().getType(), args));

    if (projection.getType().equals(Object[].class)) return complexProjection(args);

    return getArgByType(projection.getType(), args);
  }
Пример #2
0
 /**
  * SUM returns Long when applied to state-fields of integral types (other than BigInteger); Double
  * when applied to state-fields of floating point types; BigInteger when applied to state-fields
  * of type BigInteger; and BigDecimal when applied to state-fields of type BigDecimal.
  */
 public static <D extends Number & Comparable<? super D>> NumberExpression<?> sum(
     Expression<D> left) {
   Class<?> type = left.getType();
   if (type.equals(Byte.class) || type.equals(Integer.class) || type.equals(Short.class)) {
     type = Long.class;
   } else if (type.equals(Float.class)) {
     type = Double.class;
   }
   return NumberOperation.create((Class<D>) type, Ops.AggOps.SUM_AGG, left);
 }
Пример #3
0
 public static String toPropertyPath(
     Expression<?> expr, char propertySeparator, char collectionSeparator) {
   Stack<String> results = new Stack<>();
   expr.accept(
       new PropertyPathExpressionVisitor(
           String.valueOf(propertySeparator), String.valueOf(collectionSeparator)),
       results);
   StringBuilder sb = new StringBuilder();
   while (!results.isEmpty()) {
     sb.append(results.pop());
   }
   return StringUtils.stripEnd(
       sb.toString(), String.valueOf(propertySeparator) + String.valueOf(collectionSeparator));
 }
Пример #4
0
 @SuppressWarnings("unchecked")
 @Override
 public <RT> ListSubQuery<RT> list(Expression<RT> projection) {
   return new ListSubQuery<RT>((Class) projection.getType(), projection(projection));
 }
Пример #5
0
 @SuppressWarnings("unchecked")
 @Override
 public <RT> SimpleSubQuery<RT> unique(Expression<RT> projection) {
   return new SimpleSubQuery<RT>((Class) projection.getType(), uniqueProjection(projection));
 }
Пример #6
0
 public GAvg(Expression<T> expr) {
   super((Class) expr.getType(), expr);
 }
Пример #7
0
 @Override
 public Class getType() {
   return projection.getType();
 }
Пример #8
0
  @Override
  public Object visit(Operation<?> expr, Void context) {
    Operator<?> op = expr.getOperator();
    if (op == Ops.EQ) {
      if (expr.getArg(0) instanceof Operation) {
        Operation<?> lhs = (Operation<?>) expr.getArg(0);
        if (lhs.getOperator() == Ops.COL_SIZE || lhs.getOperator() == Ops.ARRAY_SIZE) {
          return asDBObject(asDBKey(lhs, 0), asDBObject("$size", asDBValue(expr, 1)));
        } else {
          throw new UnsupportedOperationException("Illegal operation " + expr);
        }
      } else {
        return asDBObject(asDBKey(expr, 0), asDBValue(expr, 1));
      }

    } else if (op == Ops.STRING_IS_EMPTY) {
      return asDBObject(asDBKey(expr, 0), "");

    } else if (op == Ops.AND) {
      BasicDBObject left = (BasicDBObject) handle(expr.getArg(0));
      left.putAll((BSONObject) handle(expr.getArg(1)));
      return left;

    } else if (op == Ops.NOT) {
      // Handle the not's child
      BasicDBObject arg = (BasicDBObject) handle(expr.getArg(0));

      // Only support the first key, let's see if there
      // is cases where this will get broken
      String key = arg.keySet().iterator().next();

      Operator<?> subOp = ((Operation<?>) expr.getArg(0)).getOperator();
      if (subOp != Ops.EQ && subOp != Ops.STRING_IS_EMPTY) {
        return asDBObject(key, asDBObject("$not", arg.get(key)));
      } else {
        return asDBObject(key, asDBObject("$ne", arg.get(key)));
      }

    } else if (op == Ops.OR) {
      BasicDBList list = new BasicDBList();
      list.add(handle(expr.getArg(0)));
      list.add(handle(expr.getArg(1)));
      return asDBObject("$or", list);

    } else if (op == Ops.NE) {
      return asDBObject(asDBKey(expr, 0), asDBObject("$ne", asDBValue(expr, 1)));

    } else if (op == Ops.STARTS_WITH) {
      return asDBObject(asDBKey(expr, 0), Pattern.compile("^" + regexValue(expr, 1)));

    } else if (op == Ops.STARTS_WITH_IC) {
      return asDBObject(
          asDBKey(expr, 0), Pattern.compile("^" + regexValue(expr, 1), Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.ENDS_WITH) {
      return asDBObject(asDBKey(expr, 0), Pattern.compile(regexValue(expr, 1) + "$"));

    } else if (op == Ops.ENDS_WITH_IC) {
      return asDBObject(
          asDBKey(expr, 0), Pattern.compile(regexValue(expr, 1) + "$", Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.EQ_IGNORE_CASE) {
      return asDBObject(
          asDBKey(expr, 0),
          Pattern.compile("^" + regexValue(expr, 1) + "$", Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.STRING_CONTAINS) {
      return asDBObject(asDBKey(expr, 0), Pattern.compile(".*" + regexValue(expr, 1) + ".*"));

    } else if (op == Ops.STRING_CONTAINS_IC) {
      return asDBObject(
          asDBKey(expr, 0),
          Pattern.compile(".*" + regexValue(expr, 1) + ".*", Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.MATCHES) {
      return asDBObject(asDBKey(expr, 0), Pattern.compile(asDBValue(expr, 1).toString()));

    } else if (op == Ops.MATCHES_IC) {
      return asDBObject(
          asDBKey(expr, 0),
          Pattern.compile(asDBValue(expr, 1).toString(), Pattern.CASE_INSENSITIVE));

    } else if (op == Ops.LIKE) {
      String regex = ExpressionUtils.likeToRegex((Expression) expr.getArg(1)).toString();
      return asDBObject(asDBKey(expr, 0), Pattern.compile(regex));

    } else if (op == Ops.BETWEEN) {
      BasicDBObject value = new BasicDBObject("$gte", asDBValue(expr, 1));
      value.append("$lte", asDBValue(expr, 2));
      return asDBObject(asDBKey(expr, 0), value);

    } else if (op == Ops.IN) {
      int constIndex = 0;
      int exprIndex = 1;
      if (expr.getArg(1) instanceof Constant<?>) {
        constIndex = 1;
        exprIndex = 0;
      }
      if (Collection.class.isAssignableFrom(expr.getArg(constIndex).getType())) {
        Collection<?> values =
            (Collection<?>) ((Constant<?>) expr.getArg(constIndex)).getConstant();
        return asDBObject(asDBKey(expr, exprIndex), asDBObject("$in", values.toArray()));
      } else {
        return asDBObject(asDBKey(expr, exprIndex), asDBValue(expr, constIndex));
      }

    } else if (op == Ops.LT) {
      return asDBObject(asDBKey(expr, 0), asDBObject("$lt", asDBValue(expr, 1)));

    } else if (op == Ops.GT) {
      return asDBObject(asDBKey(expr, 0), asDBObject("$gt", asDBValue(expr, 1)));

    } else if (op == Ops.LOE) {
      return asDBObject(asDBKey(expr, 0), asDBObject("$lte", asDBValue(expr, 1)));

    } else if (op == Ops.GOE) {
      return asDBObject(asDBKey(expr, 0), asDBObject("$gte", asDBValue(expr, 1)));

    } else if (op == Ops.IS_NULL) {
      return asDBObject(asDBKey(expr, 0), asDBObject("$exists", false));

    } else if (op == Ops.IS_NOT_NULL) {
      return asDBObject(asDBKey(expr, 0), asDBObject("$exists", true));

    } else if (op == Ops.CONTAINS_KEY) {
      Path<?> path = (Path<?>) expr.getArg(0);
      Expression<?> key = expr.getArg(1);
      return asDBObject(visit(path, context) + "." + key.toString(), asDBObject("$exists", true));

    } else if (op == MongodbExpressions.NEAR) {
      return asDBObject(asDBKey(expr, 0), asDBObject("$near", asDBValue(expr, 1)));

    } else if (op == MongodbExpressions.ELEM_MATCH) {
      return asDBObject(asDBKey(expr, 0), asDBObject("$elemMatch", asDBValue(expr, 1)));
    }

    throw new UnsupportedOperationException("Illegal operation " + expr);
  }
Пример #9
0
 public Object handle(Expression<?> expression) {
   return expression.accept(this, null);
 }
Пример #10
0
 public T addFlag(Position position, String prefix, Expression<?> expr) {
   Expression<?> flag = SimpleTemplate.create(expr.getType(), prefix + "{0}", expr);
   return queryMixin.addFlag(new QueryFlag(position, flag));
 }