Skip to content

Commit

Permalink
Issue #2 add possibility to do 'order by' with different ordering (AS…
Browse files Browse the repository at this point in the history
…C,DESC) on multiple fields
  • Loading branch information
Andrii Andriichuk committed Oct 12, 2021
1 parent ff63bc4 commit b572293
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import java.util.stream.Collectors;


public class OrderByClause extends TerminateQueryChainElement
public class OrderByClause extends TerminateQueryChainElement implements OrderByAcceptable
{
private final List<FieldRepresentation> fields;
private final OrderBySortingType sortingType;

OrderByClause(final AbstractFlexibleSearchQueryChainElement parent, final List<FieldRepresentation> fields,
final OrderBySortingType sortingType)
final OrderBySortingType sortingType)
{
super(parent);
this.fields = fields;
Expand All @@ -26,7 +26,14 @@ protected void appendQuery(final StringBuilder sb)
{
super.appendQuery(sb);

sb.append(SPACE).append(ORDER_BY).append(SPACE).append(joinFields()).append(SPACE).append(sortingType.getOperator());
if ((parent instanceof OrderByClause))
{
sb.append(FIELD_SEPARATOR);
} else
{
sb.append(SPACE).append(ORDER_BY).append(SPACE);
}
sb.append(joinFields()).append(SPACE).append(sortingType.getOperator());
}

private String joinFields()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,9 @@ public void testSelectWithOrderBy()
final FlexibleSearchQuery fQuery =
selectFrom(ProductModel.class)
.orderByAsc(ProductModel.CODE, ProductModel.CATALOGVERSION)
.orderByDesc(ProductModel.NAME)
.build();
assertEquals("Query does not match", "SELECT {pk} FROM {Product} ORDER BY {code} ASC,{catalogVersion} ASC", fQuery.getQuery());
assertEquals("Query does not match", "SELECT {pk} FROM {Product} ORDER BY {code} ASC,{catalogVersion} ASC,{name} DESC", fQuery.getQuery());
assertEquals("Wrong number of query parameters", 0, fQuery.getQueryParameters().size());
}

Expand Down

0 comments on commit b572293

Please sign in to comment.