Search in sources :

Example 6 with Call

use of org.immutables.criteria.expression.Call in project immutables by immutables.

the class ElasticsearchQueryVisitor method visit.

@Override
public QueryBuilders.QueryBuilder visit(Call call) {
    final Operator op = call.operator();
    final List<Expression> args = call.arguments();
    if (op == OptionalOperators.IS_PRESENT || op == OptionalOperators.IS_ABSENT) {
        final String field = pathNaming.name(Visitors.toPath(args.get(0)));
        QueryBuilders.QueryBuilder builder = QueryBuilders.existsQuery(field);
        if (op == OptionalOperators.IS_ABSENT) {
            builder = QueryBuilders.boolQuery().mustNot(builder);
        }
        return builder;
    }
    if (op == Operators.AND || op == Operators.OR) {
        Preconditions.checkArgument(!args.isEmpty(), "Size should be >=1 for %s but was %s", op, args.size());
        final QueryBuilders.BoolQueryBuilder builder = args.stream().map(a -> a.accept(this)).reduce(QueryBuilders.boolQuery(), (a, b) -> op == Operators.AND ? a.must(b) : a.should(b), (a, b) -> b);
        return builder;
    }
    if (op == Operators.NOT) {
        Preconditions.checkArgument(args.size() == 1, "Size should be 1 for %s but was %s", op, args.size());
        final QueryBuilders.QueryBuilder builder = args.get(0).accept(this);
        return QueryBuilders.boolQuery().mustNot(builder);
    }
    if (op.arity() == Operator.Arity.BINARY) {
        return binaryCall(call);
    }
    throw new UnsupportedOperationException("Don't know how to handle " + call);
}
Also used : Operator(org.immutables.criteria.expression.Operator) Operators(org.immutables.criteria.expression.Operators) ImmutableSet(com.google.common.collect.ImmutableSet) Path(org.immutables.criteria.expression.Path) OptionalOperators(org.immutables.criteria.expression.OptionalOperators) PathNaming(org.immutables.criteria.backend.PathNaming) Predicate(java.util.function.Predicate) AbstractExpressionVisitor(org.immutables.criteria.expression.AbstractExpressionVisitor) Expression(org.immutables.criteria.expression.Expression) Collection(java.util.Collection) Call(org.immutables.criteria.expression.Call) StringOperators(org.immutables.criteria.expression.StringOperators) Objects(java.util.Objects) Visitors(org.immutables.criteria.expression.Visitors) List(java.util.List) Preconditions(com.google.common.base.Preconditions) Pattern(java.util.regex.Pattern) Operator(org.immutables.criteria.expression.Operator) Collections(java.util.Collections) ComparableOperators(org.immutables.criteria.expression.ComparableOperators) Expression(org.immutables.criteria.expression.Expression)

Example 7 with Call

use of org.immutables.criteria.expression.Call in project immutables by immutables.

the class Combiner method combineAndSimplify.

static Call combineAndSimplify(Operator operator, Expression leftExpr, Expression rightExpr) {
    Objects.requireNonNull(operator, "operator");
    if (!(leftExpr instanceof Call && rightExpr instanceof Call)) {
        // regular call which can't be simplified
        return Expressions.binaryCall(operator, leftExpr, rightExpr);
    }
    Call left = (Call) leftExpr;
    Call right = (Call) rightExpr;
    if (!(left.operator() == operator && right.operator() == operator)) {
        if (left.operator() == operator) {
            List<Expression> args = new ArrayList<>(left.arguments());
            args.add(right);
            return Expressions.call(operator, args);
        }
        return Expressions.binaryCall(operator, left, right);
    }
    // combine expressions with same operator (AND / OR) into single expression
    // with multiple arguments
    List<Expression> args = new ArrayList<>();
    args.addAll(left.arguments());
    args.addAll(right.arguments());
    return Expressions.call(operator, args);
}
Also used : Call(org.immutables.criteria.expression.Call) Expression(org.immutables.criteria.expression.Expression) ArrayList(java.util.ArrayList)

Example 8 with Call

use of org.immutables.criteria.expression.Call in project immutables by immutables.

the class Combiner method dnfAnd.

/**
 * Combines expression using <a href="https://en.wikipedia.org/wiki/Disjunctive_normal_form">DNF logic</a>
 */
static Combiner dnfAnd() {
    Combiner combiner = new Combiner() {

        @Override
        public Expression combine(Expression left, Expression right) {
            if (!(left instanceof Call)) {
                // regular and
                return and().combine(left, right);
            }
            Call root = (Call) left;
            if (root.operator() == Operators.OR) {
                // change right-most child
                // a.is(1)
                // .or()
                // .b.is(2).c.is(3)
                List<Expression> args = root.arguments().stream().limit(// skip last
                root.arguments().size() - 1).collect(Collectors.toList());
                Expression last = root.arguments().get(root.arguments().size() - 1);
                Expression newLast = combine(last, right);
                args.add(newLast);
                return Expressions.or(args);
            }
            // simple 2 arg AND call
            return and().combine(left, right);
        }
    };
    return nullSafe(combiner);
}
Also used : Call(org.immutables.criteria.expression.Call) Expression(org.immutables.criteria.expression.Expression)

Example 9 with Call

use of org.immutables.criteria.expression.Call in project immutables by immutables.

the class AggregationQuery method extractPath.

private static Path extractPath(Expression expression) {
    if (expression instanceof Path) {
        return (Path) expression;
    }
    if (expression instanceof Call) {
        Call call = (Call) expression;
        if (call.operator().arity() != Operator.Arity.UNARY) {
            throw new IllegalArgumentException("Expected unary operator but got " + call);
        }
        Expression arg = call.arguments().get(0);
        Preconditions.checkArgument(arg instanceof Path, "expected path got %s", arg);
        return (Path) arg;
    }
    throw new IllegalArgumentException("Can't extract path from " + expression);
}
Also used : Path(org.immutables.criteria.expression.Path) Call(org.immutables.criteria.expression.Call) Expression(org.immutables.criteria.expression.Expression)

Aggregations

Call (org.immutables.criteria.expression.Call)9 Expression (org.immutables.criteria.expression.Expression)8 Path (org.immutables.criteria.expression.Path)6 Operator (org.immutables.criteria.expression.Operator)5 Pattern (java.util.regex.Pattern)4 BsonString (org.bson.BsonString)4 ComparableOperators (org.immutables.criteria.expression.ComparableOperators)4 Operators (org.immutables.criteria.expression.Operators)4 OptionalOperators (org.immutables.criteria.expression.OptionalOperators)4 StringOperators (org.immutables.criteria.expression.StringOperators)4 Preconditions (com.google.common.base.Preconditions)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 List (java.util.List)3 Objects (java.util.Objects)3 Bson (org.bson.conversions.Bson)3 PathNaming (org.immutables.criteria.backend.PathNaming)3 AbstractExpressionVisitor (org.immutables.criteria.expression.AbstractExpressionVisitor)3 Constant (org.immutables.criteria.expression.Constant)3