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
This commit is contained in:
Greg L. Turnquist
2022-01-04 11:56:01 -06:00
committed by Jens Schauder
parent 2ca827338b
commit 0813bc2af6
2 changed files with 19 additions and 2 deletions

View File

@@ -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.
*/

View File

@@ -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() {