Search in sources :

Example 1 with Evaluator

use of com.querydsl.codegen.utils.Evaluator in project querydsl by querydsl.

the class DefaultQueryEngine method project.

private List<?> project(QueryMetadata metadata, List<Expression<?>> sources, List<?> list) {
    Expression<?> projection = metadata.getProjection();
    Operator aggregator = null;
    if (projection instanceof Operation && Ops.aggOps.contains(((Operation) projection).getOperator())) {
        Operation<?> aggregation = (Operation<?>) projection;
        aggregator = aggregation.getOperator();
        projection = aggregation.getArg(0);
    }
    Evaluator projectionEvaluator = evaluatorFactory.create(metadata, sources, projection);
    EvaluatorFunction<Object, Object> transformer = new EvaluatorFunction(projectionEvaluator);
    List target = list.stream().map(transformer).collect(Collectors.toList());
    if (aggregator != null) {
        return Collections.singletonList(CollQueryFunctions.aggregate(target, projection, aggregator));
    } else {
        return target;
    }
}
Also used : Operator(com.querydsl.core.types.Operator) ArrayList(java.util.ArrayList) List(java.util.List) Operation(com.querydsl.core.types.Operation) Evaluator(com.querydsl.codegen.utils.Evaluator)

Example 2 with Evaluator

use of com.querydsl.codegen.utils.Evaluator in project querydsl by querydsl.

the class DefaultQueryEngine method evaluateSingleSource.

private List evaluateSingleSource(QueryMetadata metadata, Map<Expression<?>, Iterable<?>> iterables, boolean count) {
    final Expression<?> source = metadata.getJoins().get(0).getTarget();
    final List<Expression<?>> sources = Collections.<Expression<?>>singletonList(source);
    final Iterable<?> iterable = iterables.values().iterator().next();
    List<?> list;
    if (iterable instanceof List) {
        list = (List) iterable;
    } else {
        list = IteratorAdapter.asList(iterable.iterator());
    }
    // from & where
    if (metadata.getWhere() != null) {
        Evaluator<List<?>> evaluator = (Evaluator) evaluatorFactory.createEvaluator(metadata, source, metadata.getWhere());
        list = evaluator.evaluate(list);
    }
    if (!count && !list.isEmpty()) {
        // ordered
        if (!metadata.getOrderBy().isEmpty()) {
            // clone list
            if (list == iterable) {
                list = new ArrayList(list);
            }
            order(metadata, sources, list);
        }
        // projection
        if (metadata.getProjection() != null && !metadata.getProjection().equals(source)) {
            list = project(metadata, sources, list);
        }
        // limit + offset
        if (metadata.getModifiers().isRestricting()) {
            list = metadata.getModifiers().subList(list);
        }
        if (list.isEmpty()) {
            return list;
        }
    }
    // distinct
    if (metadata.isDistinct()) {
        list = distinct(list);
    }
    return list;
}
Also used : Expression(com.querydsl.core.types.Expression) ArrayConstructorExpression(com.querydsl.core.types.ArrayConstructorExpression) JoinExpression(com.querydsl.core.JoinExpression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Evaluator(com.querydsl.codegen.utils.Evaluator)

Example 3 with Evaluator

use of com.querydsl.codegen.utils.Evaluator in project querydsl by querydsl.

the class DefaultQueryEngine method order.

private void order(QueryMetadata metadata, List<Expression<?>> sources, List<?> list) {
    // create a projection for the order
    List<OrderSpecifier<?>> orderBy = metadata.getOrderBy();
    Expression<Object>[] orderByExpr = new Expression[orderBy.size()];
    boolean[] directions = new boolean[orderBy.size()];
    boolean[] nullsLast = new boolean[orderBy.size()];
    for (int i = 0; i < orderBy.size(); i++) {
        orderByExpr[i] = (Expression) orderBy.get(i).getTarget();
        directions[i] = orderBy.get(i).getOrder() == Order.ASC;
        nullsLast[i] = orderBy.get(i).getNullHandling() == OrderSpecifier.NullHandling.NullsLast;
    }
    Expression<?> expr = new ArrayConstructorExpression<Object>(Object[].class, orderByExpr);
    Evaluator orderEvaluator = evaluatorFactory.create(metadata, sources, expr);
    list.sort(new MultiComparator(orderEvaluator, directions, nullsLast));
}
Also used : ArrayConstructorExpression(com.querydsl.core.types.ArrayConstructorExpression) Expression(com.querydsl.core.types.Expression) ArrayConstructorExpression(com.querydsl.core.types.ArrayConstructorExpression) JoinExpression(com.querydsl.core.JoinExpression) Evaluator(com.querydsl.codegen.utils.Evaluator) OrderSpecifier(com.querydsl.core.types.OrderSpecifier)

Example 4 with Evaluator

use of com.querydsl.codegen.utils.Evaluator in project querydsl by querydsl.

the class EvaluatorTransformerTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() {
    DefaultEvaluatorFactory evaluatorFactory = new DefaultEvaluatorFactory(CollQueryTemplates.DEFAULT);
    QCat cat = QCat.cat;
    Evaluator projectionEvaluator = evaluatorFactory.create(metadata, Collections.singletonList(cat), cat.name);
    EvaluatorFunction transformer = new EvaluatorFunction(projectionEvaluator);
    Cat c = new Cat("Kitty");
    assertEquals("Kitty", transformer.apply(c));
}
Also used : Evaluator(com.querydsl.codegen.utils.Evaluator) Test(org.junit.Test)

Aggregations

Evaluator (com.querydsl.codegen.utils.Evaluator)4 JoinExpression (com.querydsl.core.JoinExpression)2 ArrayConstructorExpression (com.querydsl.core.types.ArrayConstructorExpression)2 Expression (com.querydsl.core.types.Expression)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Operation (com.querydsl.core.types.Operation)1 Operator (com.querydsl.core.types.Operator)1 OrderSpecifier (com.querydsl.core.types.OrderSpecifier)1 Test (org.junit.Test)1