Support NullHandling in QueryUtils.

Closes #3811

Signed-off-by: Alim <alim.naizabek@gmail.com>
This commit is contained in:
Alim
2025-03-12 22:16:46 +05:00
committed by Mark Paluch
parent 2e40704835
commit aa29081369
2 changed files with 18 additions and 1 deletions

View File

@@ -85,6 +85,7 @@ import org.springframework.util.StringUtils;
* @author Pranav HS
* @author Eduard Dudar
* @author Yanming Zhou
* @author Alim Naizabek
*/
public abstract class QueryUtils {
@@ -428,7 +429,13 @@ public abstract class QueryUtils {
}
private static String toJpaDirection(Order order) {
return order.getDirection().name().toLowerCase(Locale.US);
String direction = order.getDirection().name().toLowerCase(Locale.US);
if (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) {
direction += " nulls first";
} else if (order.getNullHandling() == Sort.NullHandling.NULLS_LAST) {
direction += " nulls last";
}
return direction;
}
/**

View File

@@ -48,4 +48,14 @@ public class DefaultQueryEnhancerUnitTests extends QueryEnhancerTckTests {
assertThat(sql).isEqualTo("SELECT e FROM Employee e order by e.foo asc, e.bar asc");
}
@Test
void shouldApplySortingWithNullHandling() {
QueryEnhancer enhancer = createQueryEnhancer(DeclaredQuery.of("SELECT e FROM Employee e", true));
String sql = enhancer.applySorting(Sort.by(Sort.Order.asc("foo").nullsFirst(), Sort.Order.asc("bar").nullsLast()));
assertThat(sql).isEqualTo("SELECT e FROM Employee e order by e.foo asc nulls first, e.bar asc nulls last");
}
}