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