From 0813bc2af61437de79af8284582b59bf12a9673c Mon Sep 17 00:00:00 2001 From: "Greg L. Turnquist" Date: Tue, 4 Jan 2022 11:56:01 -0600 Subject: [PATCH] Properly handle ignoreCase for aliased fields. When writing a select with a field alias, properly handle Sort.Order's ignoreCase. Closes #2280 Original pull request #2399 --- .../data/jpa/repository/query/QueryUtils.java | 8 ++++++-- .../jpa/repository/query/QueryUtilsUnitTests.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java index 84a8b70e0..213e21f03 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java @@ -38,11 +38,11 @@ import javax.persistence.criteria.From; import javax.persistence.criteria.Join; import javax.persistence.criteria.JoinType; import javax.persistence.metamodel.Attribute; +import javax.persistence.metamodel.Attribute.PersistentAttributeType; import javax.persistence.metamodel.Bindable; import javax.persistence.metamodel.ManagedType; import javax.persistence.metamodel.PluralAttribute; import javax.persistence.metamodel.SingularAttribute; -import javax.persistence.metamodel.Attribute.PersistentAttributeType; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.dao.InvalidDataAccessApiUsageException; @@ -73,6 +73,7 @@ import org.springframework.util.StringUtils; * @author Mohammad Hewedy * @author Andriy Redko * @author Peter Großmann + * @author Greg Turnquist */ public abstract class QueryUtils { @@ -291,7 +292,9 @@ public abstract class QueryUtils { checkSortExpression(order); if (selectionAlias.contains(property)) { - return String.format("%s %s", property, toJpaDirection(order)); + return String.format("%s %s", // + order.isIgnoreCase() ? String.format("lower(%s)", property) : property, // + toJpaDirection(order)); } boolean qualifyReference = !property.contains("("); // ( indicates a function @@ -465,6 +468,7 @@ public abstract class QueryUtils { * @param originalQuery must not be {@literal null}. * @param countProjection may be {@literal null}. * @return a query String to be used a count query for pagination. Guaranteed to be not {@literal null}. + * @return a query String to be used a count query for pagination. Guaranteed to be not {@literal null}. * @since 1.6 * @deprecated use {@link DeclaredQuery#deriveCountQuery(String, String)} instead. */ diff --git a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java index 9040cfabf..00794830a 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java @@ -39,6 +39,7 @@ import org.springframework.data.jpa.domain.JpaSort; * @author Florian Lüdiger * @author Grégoire Druant * @author Mohammad Hewedy + * @author Greg Turnquist */ class QueryUtilsUnitTests { @@ -444,6 +445,18 @@ class QueryUtilsUnitTests { assertThat(fullQuery).endsWith("order by authorName asc"); } + @Test // GH-2280 + void appliesOrderingCorrectlyForFieldAliasWithIgnoreCase() { + + String query = "SELECT customer.id as id, customer.name as name FROM CustomerEntity customer"; + Sort sort = Sort.by(Order.by("name").ignoreCase()); + + String fullQuery = applySorting(query, sort); + + assertThat(fullQuery).isEqualTo( + "SELECT customer.id as id, customer.name as name FROM CustomerEntity customer order by lower(name) asc"); + } + @Test // DATAJPA-1061 void appliesSortCorrectlyForFunctionAliases() {