Search in sources :

Example 6 with Expression

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

the class GeodeQueryVisitorTest method oqlChecker.

private static StringChecker oqlChecker(Criterion<?> criteria, boolean useBindVariables) {
    final PathNaming pathNaming = ReservedWordNaming.of(PathNaming.defaultNaming());
    final GeodeQueryVisitor queryVisitor = new GeodeQueryVisitor(useBindVariables, pathNaming);
    Expression filter = Criterias.toQuery(criteria).filter().orElseThrow(() -> new IllegalStateException("no filter"));
    return check(filter.accept(queryVisitor).oql());
}
Also used : Expression(org.immutables.criteria.expression.Expression) PathNaming(org.immutables.criteria.backend.PathNaming)

Example 7 with Expression

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

the class KeyLookupAnalyzerTest method ids.

private static Iterable<String> ids(StringHolderCriteria crit) {
    Expression filter = Criterias.toQuery(crit).filter().orElseThrow(() -> new AssertionError("No filter present"));
    KeyExtractor extractor = KeyExtractor.defaultFactory().create(TypeHolder.StringHolder.class);
    KeyLookupAnalyzer analyzer = KeyLookupAnalyzer.fromExtractor(extractor);
    KeyLookupAnalyzer.Result result = analyzer.analyze(filter);
    if (!result.isOptimizable()) {
        return Collections.emptyList();
    }
    return (Iterable<String>) result.values();
}
Also used : TypeHolder(org.immutables.criteria.typemodel.TypeHolder) Expression(org.immutables.criteria.expression.Expression) KeyExtractor(org.immutables.criteria.backend.KeyExtractor)

Example 8 with Expression

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

the class InMemory method toPredicate.

/**
 * Converts criteria to a predicate. Note that filter must be set on criteria.
 * For instance {@code person.name.is("John")} is a valid filter but {@code person.name} is not.
 *
 * @throws NullPointerException if argument is null
 * @throws IllegalArgumentException if filter is not set on criteria
 * @return predicate which reflects criteria filter expression
 */
public static <T> Predicate<T> toPredicate(Criterion<T> criterion) {
    Objects.requireNonNull(criterion, "criterion");
    Expression expression = Criterias.toQuery(criterion).filter().orElseThrow(() -> new IllegalArgumentException("Filter not set"));
    @SuppressWarnings("unchecked") Predicate<T> predicate = (Predicate<T>) ExpressionInterpreter.of(expression).asPredicate();
    return predicate;
}
Also used : Expression(org.immutables.criteria.expression.Expression) Predicate(java.util.function.Predicate)

Example 9 with Expression

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

the class TupleExtractor method extract.

ProjectedTuple extract(Object instance) {
    List<Object> values = new ArrayList<>();
    for (Expression expr : query.projections()) {
        Path path = (Path) expr;
        Object value = pathExtractor.extract(path, instance);
        value = maybeWrapOptional(value, path);
        values.add(value);
    }
    return ProjectedTuple.of(query.projections(), values);
}
Also used : Path(org.immutables.criteria.expression.Path) Expression(org.immutables.criteria.expression.Expression) ArrayList(java.util.ArrayList)

Example 10 with Expression

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

the class AggregateQueryBuilder method processResult.

List<ProjectedTuple> processResult(Json.Result result) {
    final List<ProjectedTuple> tuples = new ArrayList<>();
    if (result.aggregations() != null) {
        Converter<String, Expression> converter = naming.asConverter().reverse();
        // collect values
        Json.visitValueNodes(result.aggregations(), m -> {
            Map<Expression, Object> values = Maps.newHashMapWithExpectedSize(query.projections().size());
            for (String field : m.keySet()) {
                Expression expression = converter.convert(field);
                Object value = m.get(field);
                if (value == null) {
                    // otherwise jackson returns null even for optionals
                    value = NullNode.getInstance();
                } else if (value instanceof Number && (expression.returnType() == LocalDate.class || expression.returnType() == LocalDateTime.class)) {
                    // hack/work-around because JavaTimeModule doesn't handle epoch millis for LocalDate and LocalDateTime
                    // and elastic always returns epoch millis
                    // this ideally should be handled directly by Deserializer
                    Instant instant = Instant.ofEpochMilli(((Number) value).longValue());
                    value = nodeFactory.textNode(instant.toString());
                }
                values.put(expression, mapper.convertValue(value, mapper.getTypeFactory().constructType(expression.returnType())));
            }
            List<Object> projections = query.projections().stream().map(values::get).collect(Collectors.toList());
            tuples.add(ProjectedTuple.of(query.projections(), projections));
        });
    }
    // elastic exposes total number of documents matching a query in "/hits/total" path
    // this can be used for simple "select count(*) from table"
    final long total = result.searchHits().total().value();
    return tuples;
}
Also used : Expression(org.immutables.criteria.expression.Expression) Instant(java.time.Instant) ArrayList(java.util.ArrayList) ProjectedTuple(org.immutables.criteria.backend.ProjectedTuple)

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