Search in sources :

Example 16 with Expression

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

the class SyncSelect method toTuple.

private static ProjectedTuple toTuple(Query query, Object value) {
    if (!(value instanceof Struct)) {
        // most likely single projection
        Preconditions.checkArgument(query.projections().size() == 1, "Expected single projection got %s", query.projections().size());
        Expression projection = query.projections().get(0);
        return ProjectedTuple.ofSingle(projection, UNDEFINED_TO_NULL.apply(value));
    }
    Struct struct = (Struct) value;
    List<Object> values = Arrays.stream(struct.getFieldValues()).map(UNDEFINED_TO_NULL).collect(Collectors.toList());
    return ProjectedTuple.of(query.projections(), values);
}
Also used : Expression(org.immutables.criteria.expression.Expression) Struct(org.apache.geode.cache.query.Struct)

Example 17 with Expression

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

the class ReactorReader method select.

public ReactorMapperTuple.DistinctLimitOffset select(Iterable<Projection<?>> projections) {
    Objects.requireNonNull(projections, "projections");
    Preconditions.checkArgument(!Iterables.isEmpty(projections), "empty projections");
    List<Expression> expressions = StreamSupport.stream(projections.spliterator(), false).map(Matchers::toExpression).collect(Collectors.toList());
    Query newQuery = this.query.addProjections(expressions);
    return new ReactorMappers.MapperTuple(newQuery, session);
}
Also used : Query(org.immutables.criteria.expression.Query) ImmutableQuery(org.immutables.criteria.expression.ImmutableQuery) Expression(org.immutables.criteria.expression.Expression)

Example 18 with Expression

use of org.immutables.criteria.expression.Expression 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 19 with Expression

use of org.immutables.criteria.expression.Expression 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 20 with Expression

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

the class CriteriaContext method applyAndCreateRoot.

<R> R applyAndCreateRoot(UnaryOperator<Expression> fn, Combiner nextCombiner) {
    Expression newPartial = fn.apply(state.partial());
    Expression newExpression = state.combiner().combine(state.current(), newPartial);
    // use initial creator
    CriteriaCreator<?> creator = first().state.creator();
    ImmutableState newState = state.withCombiner(nextCombiner).withCreator(creator).withCurrent(newExpression).withPartial(state.defaultPartial());
    return new CriteriaContext(null, newState).create();
}
Also used : Expression(org.immutables.criteria.expression.Expression)

Aggregations

Expression (org.immutables.criteria.expression.Expression)32 Path (org.immutables.criteria.expression.Path)11 Call (org.immutables.criteria.expression.Call)8 Operator (org.immutables.criteria.expression.Operator)8 Pattern (java.util.regex.Pattern)5 PathNaming (org.immutables.criteria.backend.PathNaming)5 OptionalOperators (org.immutables.criteria.expression.OptionalOperators)5 Query (org.immutables.criteria.expression.Query)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Collection (java.util.Collection)4 Objects (java.util.Objects)4 ComparableOperators (org.immutables.criteria.expression.ComparableOperators)4 Constant (org.immutables.criteria.expression.Constant)4 ImmutableQuery (org.immutables.criteria.expression.ImmutableQuery)4 Operators (org.immutables.criteria.expression.Operators)4 StringOperators (org.immutables.criteria.expression.StringOperators)4 Visitors (org.immutables.criteria.expression.Visitors)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Preconditions (com.google.common.base.Preconditions)3 ArrayList (java.util.ArrayList)3