use of org.immutables.criteria.expression.Expression in project immutables by immutables.
the class MatchersTest method projection.
@Test
void projection() {
Expression expr1 = Matchers.toExpression(PersonCriteria.person.fullName);
check(expr1 instanceof Path);
check(((Path) expr1).toStringPath()).is("fullName");
Expression expr2 = Matchers.toExpression(PersonCriteria.person.bestFriend.value().hobby);
check(expr2 instanceof Path);
check(((Path) expr2).toStringPath()).is("bestFriend.hobby");
}
use of org.immutables.criteria.expression.Expression 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);
}
use of org.immutables.criteria.expression.Expression in project immutables by immutables.
the class KeyLookupAnalyzerTest method disabled.
@Test
void disabled() {
KeyLookupAnalyzer analyzer = KeyLookupAnalyzer.disabled();
Expression expr = Criterias.toQuery(string.id.is("foo")).filter().get();
KeyLookupAnalyzer.Result result = analyzer.analyze(expr);
check(!result.isOptimizable());
Assertions.assertThrows(UnsupportedOperationException.class, result::values);
}
use of org.immutables.criteria.expression.Expression in project immutables by immutables.
the class GeodeQueryVisitor method binaryOperator.
/**
* Used for operators with two arguments like {@code =}, {@code IN} etc.
*/
private Oql binaryOperator(Call call) {
final Operator op = call.operator();
final List<Expression> args = call.arguments();
Preconditions.checkArgument(args.size() == 2, "Size should be 2 for %s but was %s on call %s", op, args.size(), call);
// left node
Expression left = args.get(0);
// right node
Expression right = args.get(1);
if (op == IterableOperators.CONTAINS || op == StringOperators.MATCHES || op == StringOperators.CONTAINS || op == StringOperators.STARTS_WITH || op == StringOperators.ENDS_WITH) {
return oql(String.format("%s.%s(%s)", left.accept(this).oql(), toMethodName(op), right.accept(this).oql()));
}
if (op == StringOperators.HAS_LENGTH || op == IterableOperators.HAS_SIZE) {
return oql(String.format("%s.%s = %s", left.accept(this).oql(), toMethodName(op), right.accept(this).oql()));
}
final String operator;
if (op == Operators.EQUAL || op == Operators.NOT_EQUAL) {
operator = op == Operators.EQUAL ? "=" : "!=";
} else if (op == Operators.IN || op == Operators.NOT_IN) {
if (right instanceof Constant) {
// optimization for IN / NOT IN operators
// make constant value(s) distinct using Set
Set<Object> newValues = ImmutableSet.copyOf(Visitors.toConstant(right).values());
right = Expressions.constant(newValues);
}
operator = op == Operators.IN ? "IN" : "NOT IN";
} else if (op == ComparableOperators.GREATER_THAN) {
operator = ">";
} else if (op == ComparableOperators.GREATER_THAN_OR_EQUAL) {
operator = ">=";
} else if (op == ComparableOperators.LESS_THAN) {
operator = "<";
} else if (op == ComparableOperators.LESS_THAN_OR_EQUAL) {
operator = "<=";
} else {
throw new IllegalArgumentException("Unknown binary operator " + call);
}
return oql(String.format("%s %s %s", left.accept(this).oql(), operator, right.accept(this).oql()));
}
use of org.immutables.criteria.expression.Expression in project immutables by immutables.
the class SyncDelete method call.
@Override
public WriteResult call() throws Exception {
if (!operation.query().filter().isPresent()) {
// no filter means delete all (ie clear whole region)
region.clear();
return WriteResult.unknown();
}
Expression filter = operation.query().filter().orElseThrow(() -> new IllegalStateException("For " + operation));
// special case when expression may contain only ID / key attribute
// assume idProperty is defined (see IdResolver)
KeyLookupAnalyzer.Result result = session.keyLookupAnalyzer.analyze(filter);
if (result.isOptimizable()) {
return deleteByKeys(result.values());
}
GeodeQueryVisitor visitor = new GeodeQueryVisitor(true, path -> String.format("e.value.%s", session.pathNaming.name(path)));
Oql oql = filter.accept(visitor);
// delete by query. Perform separate query to get list of IDs
String query = String.format("select distinct e.key from %s.entries e where %s", region.getFullPath(), oql.oql());
Collection<?> keys = (Collection<?>) session.queryService.newQuery(query).execute(oql.variables().toArray(new Object[0]));
deleteByKeys(keys);
return WriteResult.empty().withDeletedCount(keys.size());
}
Aggregations