Skip to content

Commit

Permalink
Properly handle ignoreCase for aliased fields.
Browse files Browse the repository at this point in the history
When writing a select with a field alias, properly handle Sort.Order's ignoreCase.

Closes #2280
Original pull request #2399
  • Loading branch information
gregturn authored and schauder committed Jan 5, 2022
1 parent bca2adc commit 27f6866
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
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;
Expand Down Expand Up @@ -73,6 +73,7 @@
* @author Mohammad Hewedy
* @author Andriy Redko
* @author Peter Großmann
* @author Greg Turnquist
*/
public abstract class QueryUtils {

Expand Down Expand Up @@ -291,7 +292,9 @@ private static String getOrderClause(Set<String> joinAliases, Set<String> select
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
Expand Down Expand Up @@ -465,6 +468,7 @@ public static String createCountQueryFor(String originalQuery) {
* @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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* @author Florian Lüdiger
* @author Grégoire Druant
* @author Mohammad Hewedy
* @author Greg Turnquist
*/
class QueryUtilsUnitTests {

Expand Down Expand Up @@ -444,6 +445,18 @@ void appliesSortCorrectlyForFieldAliases() {
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() {

Expand Down

0 comments on commit 27f6866

Please sign in to comment.