use of io.micronaut.data.model.jpa.criteria.PersistentPropertyPath in project micronaut-data by micronaut-projects.
the class AbstractPersistentEntityCriteriaQuery method getQueryModel.
@NotNull
@Override
public QueryModel getQueryModel() {
if (entityRoot == null) {
throw new IllegalStateException("The root entity must be specified!");
}
QueryModel qm = QueryModel.from(entityRoot.getPersistentEntity());
Joiner joiner = new Joiner();
if (predicate instanceof PredicateVisitable) {
PredicateVisitable predicate = (PredicateVisitable) this.predicate;
predicate.accept(new QueryModelPredicateVisitor(qm));
predicate.accept(joiner);
}
if (selection instanceof SelectionVisitable) {
SelectionVisitable selection = (SelectionVisitable) this.selection;
selection.accept(new QueryModelSelectionVisitor(qm, distinct));
selection.accept(joiner);
SelectionVisitable entityRoot = (SelectionVisitable) this.entityRoot;
entityRoot.accept(joiner);
} else {
SelectionVisitable entityRoot = (SelectionVisitable) this.entityRoot;
entityRoot.accept(new QueryModelSelectionVisitor(qm, distinct));
entityRoot.accept(joiner);
}
if (orders != null && !orders.isEmpty()) {
List<Sort.Order> sortOrders = orders.stream().map(o -> {
PersistentPropertyPath<?> propertyPath = requireProperty(o.getExpression());
joiner.joinIfNeeded(propertyPath);
String name = propertyPath.getProperty().getName();
// todo path
if (o.isAscending()) {
return Sort.Order.asc(name);
}
return Sort.Order.desc(name);
}).collect(Collectors.toList());
qm.sort(Sort.of(sortOrders));
}
for (Map.Entry<String, Joiner.Joined> e : joiner.getJoins().entrySet()) {
qm.join(e.getKey(), Optional.ofNullable(e.getValue().getType()).orElse(Join.Type.DEFAULT), e.getValue().getAlias());
}
qm.max(max);
qm.offset(offset);
if (forUpdate) {
qm.forUpdate();
}
return qm;
}
use of io.micronaut.data.model.jpa.criteria.PersistentPropertyPath in project micronaut-data by micronaut-projects.
the class SourcePersistentEntityCriteriaQueryImpl method getQueryResultTypeName.
@Override
public String getQueryResultTypeName() {
if (selection instanceof ISelection) {
String[] result = new String[1];
((SelectionVisitable) selection).accept(new SelectionVisitor() {
@Override
public void visit(Predicate predicate) {
}
@Override
public void visit(PersistentPropertyPath<?> persistentPropertyPath) {
result[0] = ((SourcePersistentPropertyPath) persistentPropertyPath).getProperty().getType().getName();
}
@Override
public void visit(AliasedSelection<?> aliasedSelection) {
((SelectionVisitable) aliasedSelection.getSelection()).accept(this);
}
@Override
public void visit(PersistentEntityRoot<?> entityRoot) {
result[0] = ((SourcePersistentEntity) entityRoot.getPersistentEntity()).getType().getName();
}
@Override
public void visit(AggregateExpression<?, ?> aggregateExpression) {
switch(aggregateExpression.getType()) {
case COUNT:
case COUNT_DISTINCT:
result[0] = Long.class.getName();
break;
case MAX:
case MIN:
result[0] = requireProperty(aggregateExpression.getExpression()).getProperty().getTypeName();
break;
case SUM:
case AVG:
ClassElement type = ((SourcePersistentProperty) requireProperty(aggregateExpression.getExpression()).getProperty()).getType();
if (aggregateExpression.getExpressionType() != null) {
result[0] = aggregateExpression.getExpressionType().getName();
}
if (TypeUtils.isNumber(type)) {
result[0] = Number.class.getName();
} else {
result[0] = type.getName();
}
break;
default:
return;
}
}
@Override
public void visit(CompoundSelection<?> compoundSelection) {
if (compoundSelection.getCompoundSelectionItems().size() == 1) {
// Multiple selection shouldn't result in one type
compoundSelection.getCompoundSelectionItems().forEach(s -> ((SelectionVisitable) s).accept(this));
}
}
@Override
public void visit(LiteralExpression<?> literalExpression) {
result[0] = literalExpression.getValue().getClass().getName();
}
@Override
public void visit(IdExpression<?, ?> idExpression) {
SourcePersistentEntity persistentEntity = (SourcePersistentEntity) idExpression.getRoot().getPersistentEntity();
if (persistentEntity.hasCompositeIdentity()) {
throw new IllegalStateException("IdClass is unknown!");
}
result[0] = persistentEntity.getIdentity().getType().getName();
}
});
return result[0];
}
return null;
}
use of io.micronaut.data.model.jpa.criteria.PersistentPropertyPath in project micronaut-data by micronaut-projects.
the class QueryModelPredicateVisitor method visit.
@Override
public void visit(ExpressionBinaryPredicate expressionBinaryPredicate) {
Expression<?> left = expressionBinaryPredicate.getLeft();
PredicateBinaryOp op = expressionBinaryPredicate.getOp();
if (left instanceof PersistentPropertyPath) {
visitPropertyPathPredicate((PersistentPropertyPath<?>) left, expressionBinaryPredicate.getRight(), op);
} else if (left instanceof IdExpression) {
if (op == PredicateBinaryOp.EQUALS) {
add(Restrictions.idEq(asValue(expressionBinaryPredicate.getRight())));
} else {
throw new IllegalStateException("Unsupported ID expression OP: " + op);
}
} else {
throw new IllegalStateException("Unsupported expression: " + left);
}
}
Aggregations