DATAJPA-1061 - Polishing.

Tweaked the formatting and the naming of variables.

Original pull request: #276.
This commit is contained in:
Jens Schauder
2019-06-24 14:00:44 +02:00
parent a83c0bf2a5
commit 66834db5a7
2 changed files with 15 additions and 6 deletions

View File

@@ -262,12 +262,12 @@ public abstract class QueryUtils {
builder.append(", ");
}
Set<String> aliases = getOuterJoinAliases(query);
Set<String> fieldAliases = getFunctionAliases(query);
fieldAliases.addAll(getFieldAliases(query));
Set<String> joinAliases = getOuterJoinAliases(query);
Set<String> selectionAliases = getFunctionAliases(query);
selectionAliases.addAll(getFieldAliases(query));
for (Order order : sort) {
builder.append(getOrderClause(aliases, fieldAliases, alias, order)).append(", ");
builder.append(getOrderClause(joinAliases, selectionAliases, alias, order)).append(", ");
}
builder.delete(builder.length() - 2, builder.length());
@@ -284,14 +284,14 @@ public abstract class QueryUtils {
* @param order the order object to build the clause for. Must not be {@literal null}.
* @return a String containing a order clause. Guaranteed to be not {@literal null}.
*/
private static String getOrderClause(Set<String> joinAliases, Set<String> fieldAlias, @Nullable String alias,
private static String getOrderClause(Set<String> joinAliases, Set<String> selectionAlias, @Nullable String alias,
Order order) {
String property = order.getProperty();
checkSortExpression(order);
if (fieldAlias.contains(property)) {
if (selectionAlias.contains(property)) {
return String.format("%s %s", property, toJpaDirection(order));
}

View File

@@ -426,25 +426,34 @@ public class QueryUtilsUnitTests {
@Test // DATAJPA-1061
public void appliesSortCorrectlyForFieldAliases() {
String query = "SELECT m.price, lower(m.title) AS title, a.name as authorName FROM Magazine m INNER JOIN m.author a";
Sort sort = Sort.by("authorName");
String fullQuery = applySorting(query, sort);
assertThat(fullQuery, endsWith("order by authorName asc"));
}
@Test // DATAJPA-1061
public void appliesSortCorrectlyForFunctionAliases() {
String query = "SELECT m.price, lower(m.title) AS title, a.name as authorName FROM Magazine m INNER JOIN m.author a";
Sort sort = Sort.by("title");
String fullQuery = applySorting(query, sort);
assertThat(fullQuery, endsWith("order by title asc"));
}
@Test // DATAJPA-1061
public void appliesSortCorrectlyForSimpleField() {
String query = "SELECT m.price, lower(m.title) AS title, a.name as authorName FROM Magazine m INNER JOIN m.author a";
Sort sort = Sort.by("price");
String fullQuery = applySorting(query, sort);
assertThat(fullQuery, endsWith("order by m.price asc"));
}