Search in sources :

Example 1 with QueryModelSelectionVisitor

use of io.micronaut.data.model.jpa.criteria.impl.query.QueryModelSelectionVisitor 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;
}
Also used : Order(jakarta.persistence.criteria.Order) Arrays(java.util.Arrays) Join(io.micronaut.data.annotation.Join) EntityType(jakarta.persistence.metamodel.EntityType) QueryModel(io.micronaut.data.model.query.QueryModel) ParameterExpression(jakarta.persistence.criteria.ParameterExpression) PersistentEntityCriteriaQuery(io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaQuery) CriteriaUtils.hasVersionPredicate(io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.hasVersionPredicate) IExpression(io.micronaut.data.model.jpa.criteria.IExpression) Expression(jakarta.persistence.criteria.Expression) Map(java.util.Map) CriteriaUtils.requireProperty(io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.requireProperty) QueryModelPredicateVisitor(io.micronaut.data.model.jpa.criteria.impl.query.QueryModelPredicateVisitor) QueryResult(io.micronaut.data.model.query.builder.QueryResult) Order(jakarta.persistence.criteria.Order) Sort(io.micronaut.data.model.Sort) PersistentPropertyBinaryPredicate(io.micronaut.data.model.jpa.criteria.impl.predicate.PersistentPropertyBinaryPredicate) Predicate(jakarta.persistence.criteria.Predicate) Set(java.util.Set) ConjunctionPredicate(io.micronaut.data.model.jpa.criteria.impl.predicate.ConjunctionPredicate) CriteriaUtils.notSupportedOperation(io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.notSupportedOperation) Collectors(java.util.stream.Collectors) Selection(jakarta.persistence.criteria.Selection) Subquery(jakarta.persistence.criteria.Subquery) QueryModelSelectionVisitor(io.micronaut.data.model.jpa.criteria.impl.query.QueryModelSelectionVisitor) Objects(java.util.Objects) PersistentEntityRoot(io.micronaut.data.model.jpa.criteria.PersistentEntityRoot) DisjunctionPredicate(io.micronaut.data.model.jpa.criteria.impl.predicate.DisjunctionPredicate) List(java.util.List) Root(jakarta.persistence.criteria.Root) PersistentEntity(io.micronaut.data.model.PersistentEntity) Joiner(io.micronaut.data.model.jpa.criteria.impl.util.Joiner) QueryBuilder(io.micronaut.data.model.query.builder.QueryBuilder) Optional(java.util.Optional) PersistentPropertyPath(io.micronaut.data.model.jpa.criteria.PersistentPropertyPath) CompoundSelection(io.micronaut.data.model.jpa.criteria.impl.selection.CompoundSelection) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Joiner(io.micronaut.data.model.jpa.criteria.impl.util.Joiner) QueryModelPredicateVisitor(io.micronaut.data.model.jpa.criteria.impl.query.QueryModelPredicateVisitor) PersistentPropertyPath(io.micronaut.data.model.jpa.criteria.PersistentPropertyPath) QueryModel(io.micronaut.data.model.query.QueryModel) Map(java.util.Map) QueryModelSelectionVisitor(io.micronaut.data.model.jpa.criteria.impl.query.QueryModelSelectionVisitor) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Join (io.micronaut.data.annotation.Join)1 PersistentEntity (io.micronaut.data.model.PersistentEntity)1 Sort (io.micronaut.data.model.Sort)1 IExpression (io.micronaut.data.model.jpa.criteria.IExpression)1 PersistentEntityCriteriaQuery (io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaQuery)1 PersistentEntityRoot (io.micronaut.data.model.jpa.criteria.PersistentEntityRoot)1 PersistentPropertyPath (io.micronaut.data.model.jpa.criteria.PersistentPropertyPath)1 CriteriaUtils.hasVersionPredicate (io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.hasVersionPredicate)1 CriteriaUtils.notSupportedOperation (io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.notSupportedOperation)1 CriteriaUtils.requireProperty (io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.requireProperty)1 ConjunctionPredicate (io.micronaut.data.model.jpa.criteria.impl.predicate.ConjunctionPredicate)1 DisjunctionPredicate (io.micronaut.data.model.jpa.criteria.impl.predicate.DisjunctionPredicate)1 PersistentPropertyBinaryPredicate (io.micronaut.data.model.jpa.criteria.impl.predicate.PersistentPropertyBinaryPredicate)1 QueryModelPredicateVisitor (io.micronaut.data.model.jpa.criteria.impl.query.QueryModelPredicateVisitor)1 QueryModelSelectionVisitor (io.micronaut.data.model.jpa.criteria.impl.query.QueryModelSelectionVisitor)1 CompoundSelection (io.micronaut.data.model.jpa.criteria.impl.selection.CompoundSelection)1 Joiner (io.micronaut.data.model.jpa.criteria.impl.util.Joiner)1 QueryModel (io.micronaut.data.model.query.QueryModel)1 QueryBuilder (io.micronaut.data.model.query.builder.QueryBuilder)1 QueryResult (io.micronaut.data.model.query.builder.QueryResult)1