Search in sources :

Example 1 with LiteralExpression

use of io.micronaut.data.model.jpa.criteria.impl.LiteralExpression 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)

Aggregations

Internal (io.micronaut.core.annotation.Internal)1 PersistentEntity (io.micronaut.data.model.PersistentEntity)1 ISelection (io.micronaut.data.model.jpa.criteria.ISelection)1 PersistentEntityRoot (io.micronaut.data.model.jpa.criteria.PersistentEntityRoot)1 PersistentPropertyPath (io.micronaut.data.model.jpa.criteria.PersistentPropertyPath)1 AbstractPersistentEntityCriteriaQuery (io.micronaut.data.model.jpa.criteria.impl.AbstractPersistentEntityCriteriaQuery)1 CriteriaUtils.requireProperty (io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.requireProperty)1 IdExpression (io.micronaut.data.model.jpa.criteria.impl.IdExpression)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 AggregateExpression (io.micronaut.data.model.jpa.criteria.impl.selection.AggregateExpression)1 AliasedSelection (io.micronaut.data.model.jpa.criteria.impl.selection.AliasedSelection)1 CompoundSelection (io.micronaut.data.model.jpa.criteria.impl.selection.CompoundSelection)1 SourcePersistentEntity (io.micronaut.data.processor.model.SourcePersistentEntity)1 SourcePersistentProperty (io.micronaut.data.processor.model.SourcePersistentProperty)1 SourcePersistentEntityCriteriaQuery (io.micronaut.data.processor.model.criteria.SourcePersistentEntityCriteriaQuery)1 TypeUtils (io.micronaut.data.processor.visitors.finders.TypeUtils)1 ClassElement (io.micronaut.inject.ast.ClassElement)1 Predicate (jakarta.persistence.criteria.Predicate)1