Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next phase criteria refactoring to allow more complex scenarios #3100

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
import java.util.Map;
import java.util.StringJoiner;

import static io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils.requireProperty;

/**
* The Azure Cosmos DB sql query builder.
*
Expand Down Expand Up @@ -141,7 +143,8 @@ protected SqlPredicateVisitor createPredicateVisitor(AnnotationMetadata annotati
private static final String ARRAY_CONTAINS = "ARRAY_CONTAINS";

@Override
public void visitIsNull(PersistentPropertyPath propertyPath) {
public void visitIsNull(Expression<?> expression) {
PersistentPropertyPath propertyPath = requireProperty(expression).getPropertyPath();
query.append(NOT).append(SPACE).append(IS_DEFINED).append(OPEN_BRACKET);
appendPropertyRef(propertyPath);
query.append(CLOSE_BRACKET).append(SPACE).append(OR).append(SPACE);
Expand All @@ -151,7 +154,8 @@ public void visitIsNull(PersistentPropertyPath propertyPath) {
}

@Override
public void visitIsNotNull(PersistentPropertyPath propertyPath) {
public void visitIsNotNull(Expression<?> expression) {
PersistentPropertyPath propertyPath = requireProperty(expression).getPropertyPath();
query.append(IS_DEFINED).append(OPEN_BRACKET);
appendPropertyRef(propertyPath);
query.append(CLOSE_BRACKET).append(SPACE).append(AND).append(SPACE);
Expand All @@ -161,7 +165,8 @@ public void visitIsNotNull(PersistentPropertyPath propertyPath) {
}

@Override
public void visitIsEmpty(PersistentPropertyPath propertyPath) {
public void visitIsEmpty(Expression<?> expression) {
PersistentPropertyPath propertyPath = requireProperty(expression).getPropertyPath();
query.append(NOT).append(SPACE).append(IS_DEFINED).append(OPEN_BRACKET);
appendPropertyRef(propertyPath);
query.append(CLOSE_BRACKET).append(SPACE).append(OR).append(SPACE);
Expand All @@ -173,7 +178,8 @@ public void visitIsEmpty(PersistentPropertyPath propertyPath) {
}

@Override
public void visitIsNotEmpty(PersistentPropertyPath propertyPath) {
public void visitIsNotEmpty(Expression<?> expression) {
PersistentPropertyPath propertyPath = requireProperty(expression).getPropertyPath();
query.append(IS_DEFINED).append(OPEN_BRACKET);
appendPropertyRef(propertyPath);
query.append(CLOSE_BRACKET).append(SPACE).append(AND).append(SPACE);
Expand All @@ -185,11 +191,12 @@ public void visitIsNotEmpty(PersistentPropertyPath propertyPath) {
}

@Override
public void visitArrayContains(PersistentPropertyPath leftProperty, Expression<?> expression) {
public void visitArrayContains(Expression<?> leftExpression, Expression<?> rightExpression) {
PersistentPropertyPath leftProperty = requireProperty(leftExpression).getPropertyPath();
query.append(ARRAY_CONTAINS).append(OPEN_BRACKET);
appendPropertyRef(leftProperty);
query.append(COMMA);
appendExpression(expression, leftProperty);
appendExpression(rightExpression, leftExpression);
query.append(COMMA);
query.append("true").append(CLOSE_BRACKET);
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ class H2RepositorySpec extends AbstractRepositorySpec implements H2TestPropertyP
return true
}

@PendingFeature
void "test criteria lower select" () {
when:
savePersons(["Jeff", "James"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public PersistentPropertyPath(@NonNull PersistentProperty property) {
* @param associations The associations
* @param property The property
*/
public PersistentPropertyPath(List<Association> associations, @NonNull PersistentProperty property) {
public PersistentPropertyPath(@NonNull List<Association> associations, @NonNull PersistentProperty property) {
this(associations, property, null);
}

Expand All @@ -65,7 +65,9 @@ public PersistentPropertyPath(List<Association> associations, @NonNull Persisten
* @param property The property
* @param path The path
*/
public PersistentPropertyPath(List<Association> associations, @NonNull PersistentProperty property, @Nullable String path) {
public PersistentPropertyPath(@NonNull List<Association> associations, @NonNull PersistentProperty property, @Nullable String path) {
Objects.requireNonNull(associations);
Objects.requireNonNull(property);
this.associations = associations;
this.property = property;
this.path = path;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2017-2024 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.model.jpa.criteria;

import io.micronaut.core.annotation.Experimental;
import io.micronaut.data.model.jpa.criteria.impl.expression.ClassExpressionType;

/**
* The expression type.
*
* @param <E> The type
* @author Denis Stepanov
* @since 4.10
*/
@Experimental
public interface ExpressionType<E> {

/**
* The boolean type.
*/
ExpressionType<Boolean> BOOLEAN = new ClassExpressionType<>(Boolean.class);

/**
* The object type.
*/
ExpressionType<Object> OBJECT = new ClassExpressionType<>(Object.class);

/**
* @return The type name
*/
String getName();

/**
* @return true if the expression is of boolean type
*/
boolean isBoolean();

/**
* @return true if the expression is of numeric type
*/
boolean isNumeric();

/**
* @return true if the expression is of comparable type
*/
boolean isComparable();

/**
* @return true if the expression is of string type
*/
boolean isTextual();

/**
* @return The Java type
*/
Class<E> getJavaType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,15 @@
public interface IExpression<T> extends Expression<T>, ISelection<T> {

/**
* @return true if the expression is of boolean type
* @return The expression type
*/
boolean isBoolean();

/**
* @return true if the expression is of numeric type
*/
boolean isNumeric();
@NonNull
ExpressionType<T> getExpressionType();

/**
* @return true if the expression is of comparable type
*/
boolean isComparable();
@Override
default Class<? extends T> getJavaType() {
return getExpressionType().getJavaType();
}

@Override
@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,8 @@
public interface IPredicate extends Predicate, IExpression<Boolean> {

@Override
default boolean isBoolean() {
return true;
}

@Override
default boolean isNumeric() {
return false;
}

@Override
default boolean isComparable() {
return true;
default ExpressionType<Boolean> getExpressionType() {
return ExpressionType.BOOLEAN;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import io.micronaut.core.annotation.NonNull;
import io.micronaut.data.model.Association;
import io.micronaut.data.model.PersistentProperty;
import io.micronaut.data.model.jpa.criteria.impl.CriteriaUtils;
import io.micronaut.data.model.jpa.criteria.impl.predicate.PersistentPropertyUnaryPredicate;
import io.micronaut.data.model.jpa.criteria.impl.expression.ClassExpressionType;
import io.micronaut.data.model.jpa.criteria.impl.predicate.UnaryPredicate;
import io.micronaut.data.model.jpa.criteria.impl.predicate.PredicateUnaryOp;
import jakarta.persistence.criteria.Path;
import jakarta.persistence.criteria.Predicate;
Expand Down Expand Up @@ -59,28 +59,18 @@ default String getPathAsString() {
}

@Override
default boolean isBoolean() {
return CriteriaUtils.isBoolean(getJavaType());
}

@Override
default boolean isNumeric() {
return CriteriaUtils.isNumeric(getJavaType());
}

@Override
default boolean isComparable() {
return CriteriaUtils.isComparable(getJavaType());
default ExpressionType<T> getExpressionType() {
return (ExpressionType<T>) new ClassExpressionType<>(getJavaType());
}

@Override
default Predicate isNull() {
return new PersistentPropertyUnaryPredicate<>(this, PredicateUnaryOp.IS_NULL);
return new UnaryPredicate(this, PredicateUnaryOp.IS_NULL);
}

@Override
default Predicate isNotNull() {
return new PersistentPropertyUnaryPredicate<>(this, PredicateUnaryOp.IS_NON_NULL);
return new UnaryPredicate(this, PredicateUnaryOp.IS_NON_NULL);
}

}
Loading
Loading