Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support NullHandling in QueryUtils #3811

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
* @author Pranav HS
* @author Eduard Dudar
* @author Yanming Zhou
* @author Alim Naizabek
*/
public abstract class QueryUtils {

Expand Down Expand Up @@ -428,7 +429,13 @@ static Set<String> getFunctionAliases(String query) {
}

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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ void shouldApplySorting() {

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");
}
}