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);
}
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);
}
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);
}
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);
}
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();
}
Aggregations