Search in sources :

Example 1 with PersistentPropertyPath

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

Example 2 with PersistentPropertyPath

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;
}
Also used : IdExpression(io.micronaut.data.model.jpa.criteria.impl.IdExpression) Predicate(jakarta.persistence.criteria.Predicate) LiteralExpression(io.micronaut.data.model.jpa.criteria.impl.LiteralExpression) SelectionVisitor(io.micronaut.data.model.jpa.criteria.impl.SelectionVisitor) SourcePersistentProperty(io.micronaut.data.processor.model.SourcePersistentProperty) ClassElement(io.micronaut.inject.ast.ClassElement) Internal(io.micronaut.core.annotation.Internal) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) SourcePersistentEntityCriteriaQuery(io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaQuery) Function(java.util.function.Function) ISelection(io.micronaut.data.model.jpa.criteria.ISelection) PersistentEntityRoot(io.micronaut.data.model.jpa.criteria.PersistentEntityRoot) AbstractPersistentEntityCriteriaQuery(io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaQuery) AggregateExpression(io.micronaut.data.model.jpa.criteria.impl.selection.AggregateExpression) PersistentEntity(io.micronaut.data.model.PersistentEntity) CriteriaUtils.requireProperty(io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.requireProperty) PersistentPropertyPath(io.micronaut.data.model.jpa.criteria.PersistentPropertyPath) SelectionVisitable(io.micronaut.data.model.jpa.criteria.impl.SelectionVisitable) TypeUtils(io.micronaut.data.processor.visitors.finders.TypeUtils) CompoundSelection(io.micronaut.data.model.jpa.criteria.impl.selection.CompoundSelection) AliasedSelection(io.micronaut.data.model.jpa.criteria.impl.selection.AliasedSelection) SourcePersistentEntity(io.micronaut.data.processor.model.SourcePersistentEntity) SelectionVisitor(io.micronaut.data.model.jpa.criteria.impl.SelectionVisitor) ClassElement(io.micronaut.inject.ast.ClassElement) Predicate(jakarta.persistence.criteria.Predicate) ISelection(io.micronaut.data.model.jpa.criteria.ISelection) SelectionVisitable(io.micronaut.data.model.jpa.criteria.impl.SelectionVisitable)

Example 3 with PersistentPropertyPath

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);
    }
}
Also used : PredicateBinaryOp(io.micronaut.data.model.jpa.criteria.impl.predicate.PredicateBinaryOp) IdExpression(io.micronaut.data.model.jpa.criteria.impl.IdExpression) PersistentPropertyPath(io.micronaut.data.model.jpa.criteria.PersistentPropertyPath)

Aggregations

PersistentPropertyPath (io.micronaut.data.model.jpa.criteria.PersistentPropertyPath)3 PersistentEntity (io.micronaut.data.model.PersistentEntity)2 PersistentEntityRoot (io.micronaut.data.model.jpa.criteria.PersistentEntityRoot)2 CriteriaUtils.requireProperty (io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.requireProperty)2 IdExpression (io.micronaut.data.model.jpa.criteria.impl.IdExpression)2 CompoundSelection (io.micronaut.data.model.jpa.criteria.impl.selection.CompoundSelection)2 Predicate (jakarta.persistence.criteria.Predicate)2 Internal (io.micronaut.core.annotation.Internal)1 Join (io.micronaut.data.annotation.Join)1 Sort (io.micronaut.data.model.Sort)1 IExpression (io.micronaut.data.model.jpa.criteria.IExpression)1 ISelection (io.micronaut.data.model.jpa.criteria.ISelection)1 PersistentEntityCriteriaQuery (io.micronaut.data.model.jpa.criteria.PersistentEntityCriteriaQuery)1 AbstractPersistentEntityCriteriaQuery (io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaQuery)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 LiteralExpression (io.micronaut.data.model.jpa.criteria.impl.LiteralExpression)1 SelectionVisitable (io.micronaut.data.model.jpa.criteria.impl.SelectionVisitable)1 SelectionVisitor (io.micronaut.data.model.jpa.criteria.impl.SelectionVisitor)1 ConjunctionPredicate (io.micronaut.data.model.jpa.criteria.impl.predicate.ConjunctionPredicate)1