Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Remove method overloads accepting pure strings. Use switch-expressions. Correctly navigate nested joins. Introduce PathExpression interface, refine naming.

See #3588
Original pull request: #3653
  • Loading branch information
mp911de committed Jan 14, 2025
1 parent 363b946 commit be963f6
Show file tree
Hide file tree
Showing 11 changed files with 493 additions and 486 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
*/
package org.springframework.data.jpa.repository.query;

import static org.springframework.data.repository.query.parser.Part.Type.IS_NOT_EMPTY;
import static org.springframework.data.repository.query.parser.Part.Type.NOT_CONTAINING;
import static org.springframework.data.repository.query.parser.Part.Type.NOT_LIKE;
import static org.springframework.data.repository.query.parser.Part.Type.SIMPLE_PROPERTY;
import static org.springframework.data.repository.query.parser.Part.Type.*;

import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaQuery;
Expand All @@ -39,7 +36,6 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.data.jpa.repository.query.JpqlQueryBuilder.ParameterPlaceholder;
import org.springframework.data.jpa.repository.query.JpqlQueryBuilder.PathAndOrigin;
import org.springframework.data.jpa.repository.query.ParameterBinding.PartTreeParameterBinding;
import org.springframework.data.jpa.repository.support.JpqlQueryTemplates;
import org.springframework.data.mapping.PropertyPath;
Expand Down Expand Up @@ -183,8 +179,8 @@ protected JpqlQueryBuilder.Select buildQuery(Sort sort) {
QueryUtils.checkSortExpression(order);

try {
expression = JpqlQueryBuilder.expression(JpqlUtils.toExpressionRecursively(metamodel, entity, entityType,
PropertyPath.from(order.getProperty(), entityType.getJavaType())));
expression = JpqlUtils.toExpressionRecursively(metamodel, entity, entityType,
PropertyPath.from(order.getProperty(), entityType.getJavaType()));
} catch (PropertyReferenceException e) {

if (order instanceof JpaSort.JpaOrder jpaOrder && jpaOrder.isUnsafe()) {
Expand Down Expand Up @@ -227,7 +223,7 @@ private JpqlQueryBuilder.Select doSelect(Sort sort) {
requiredSelection = getRequiredSelection(sort, returnedType);
}

List<PathAndOrigin> paths = new ArrayList<>(requiredSelection.size());
List<JpqlQueryBuilder.PathExpression> paths = new ArrayList<>(requiredSelection.size());
for (String selection : requiredSelection) {
paths.add(JpqlUtils.toExpressionRecursively(metamodel, entity, entityType,
PropertyPath.from(selection, returnedType.getDomainType()), true));
Expand All @@ -251,7 +247,7 @@ private JpqlQueryBuilder.Select doSelect(Sort sort) {

} else {

List<PathAndOrigin> paths = entityType.getIdClassAttributes().stream()//
List<JpqlQueryBuilder.PathExpression> paths = entityType.getIdClassAttributes().stream()//
.map(it -> JpqlUtils.toExpressionRecursively(metamodel, entity, entityType,
PropertyPath.from(it.getName(), returnedType.getDomainType()), true))
.toList();
Expand Down Expand Up @@ -320,7 +316,7 @@ public JpqlQueryBuilder.Predicate build() {
PropertyPath property = part.getProperty();
Type type = part.getType();

PathAndOrigin pas = JpqlUtils.toExpressionRecursively(metamodel, entity, entityType, property);
JpqlQueryBuilder.PathExpression pas = JpqlUtils.toExpressionRecursively(metamodel, entity, entityType, property);
JpqlQueryBuilder.WhereStep where = JpqlQueryBuilder.where(pas);
JpqlQueryBuilder.WhereStep whereIgnoreCase = JpqlQueryBuilder.where(potentiallyIgnoreCase(pas));

Expand Down Expand Up @@ -385,7 +381,7 @@ public JpqlQueryBuilder.Predicate build() {
return type.equals(SIMPLE_PROPERTY) ? where.isNull() : where.isNotNull();
}

JpqlQueryBuilder.Expression expression = potentiallyIgnoreCase(property, placeholder(metadata));
JpqlQueryBuilder.Expression expression = potentiallyIgnoreCase(property, placeholder(simple));
return type.equals(SIMPLE_PROPERTY) ? whereIgnoreCase.eq(expression) : whereIgnoreCase.neq(expression);
case IS_EMPTY:
case IS_NOT_EMPTY:
Expand Down Expand Up @@ -420,8 +416,8 @@ private <T> JpqlQueryBuilder.Expression potentiallyIgnoreCase(JpqlQueryBuilder.O
* @param path must not be {@literal null}.
* @return
*/
private <T> JpqlQueryBuilder.Expression potentiallyIgnoreCase(PathAndOrigin path) {
return potentiallyIgnoreCase(path.path(), JpqlQueryBuilder.expression(path));
private <T> JpqlQueryBuilder.Expression potentiallyIgnoreCase(JpqlQueryBuilder.PathExpression path) {
return potentiallyIgnoreCase(path.getPropertyPath(), path);
}

/**
Expand Down
Loading

0 comments on commit be963f6

Please sign in to comment.