Search in sources :

Example 26 with Expression

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");
}
Also used : Path(org.immutables.criteria.expression.Path) Expression(org.immutables.criteria.expression.Expression) Test(org.junit.jupiter.api.Test)

Example 27 with Expression

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);
}
Also used : Path(org.immutables.criteria.expression.Path) Call(org.immutables.criteria.expression.Call) Expression(org.immutables.criteria.expression.Expression)

Example 28 with 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);
}
Also used : Expression(org.immutables.criteria.expression.Expression) Test(org.junit.jupiter.api.Test)

Example 29 with Expression

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()));
}
Also used : Operator(org.immutables.criteria.expression.Operator) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Expression(org.immutables.criteria.expression.Expression) Constant(org.immutables.criteria.expression.Constant)

Example 30 with Expression

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());
}
Also used : Expression(org.immutables.criteria.expression.Expression) Collection(java.util.Collection)

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