Skip to content

Commit

Permalink
Issue #2 fix order by clause for multiple fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Andriichuk committed Oct 12, 2021
1 parent fef3eb6 commit ff63bc4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.bitbucket.andriichukandrii.hybris.flexiblesearchbuilder;

import static org.bitbucket.andriichukandrii.hybris.flexiblesearchbuilder.FlexibleSearchBuilderFieldUtils.buildFieldsQueryString;
import static org.bitbucket.andriichukandrii.hybris.flexiblesearchbuilder.FlexibleSearchQueryConstants.FIELD_SEPARATOR;
import static org.bitbucket.andriichukandrii.hybris.flexiblesearchbuilder.FlexibleSearchQueryConstants.ORDER_BY;
import static org.bitbucket.andriichukandrii.hybris.flexiblesearchbuilder.FlexibleSearchQueryConstants.SPACE;

import java.util.List;
import java.util.stream.Collectors;


public class OrderByClause extends TerminateQueryChainElement
Expand All @@ -25,7 +26,12 @@ protected void appendQuery(final StringBuilder sb)
{
super.appendQuery(sb);

sb.append(SPACE).append(ORDER_BY).append(SPACE).append(buildFieldsQueryString(fields)).append(SPACE)
.append(sortingType.getOperator());
sb.append(SPACE).append(ORDER_BY).append(SPACE).append(joinFields()).append(SPACE).append(sortingType.getOperator());
}

private String joinFields()
{
final String delimiter = SPACE + sortingType.getOperator() + FIELD_SEPARATOR;
return fields.stream().map(Object::toString).collect(Collectors.joining(delimiter));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

//This test relies on the constants and current implementation. However, it serves as a point of safety and a reference now.
@UnitTest
@SuppressWarnings({"LawOfDemeter"})
public class FlexibleSearchBuilderBasicUsageTest
{

Expand Down Expand Up @@ -285,7 +286,7 @@ public void testSelectWithOrderBy()
selectFrom(ProductModel.class)
.orderByAsc(ProductModel.CODE, ProductModel.CATALOGVERSION)
.build();
assertEquals("Query does not match", "SELECT {pk} FROM {Product} ORDER BY {code},{catalogVersion} ASC", fQuery.getQuery());
assertEquals("Query does not match", "SELECT {pk} FROM {Product} ORDER BY {code} ASC,{catalogVersion} ASC", fQuery.getQuery());
assertEquals("Wrong number of query parameters", 0, fQuery.getQueryParameters().size());
}

Expand Down Expand Up @@ -356,7 +357,7 @@ public void testSelectWithJoinAndDifferentConditions()

assertEquals("Query does not match", "SELECT {o.pk} FROM {Order AS o LEFT JOIN OrderEntry AS e ON " +
"{o.pk}={e.order} JOIN Product AS p ON {p.pk}={e.product}} WHERE {p.code}=?code1 AND {o.totalPrice}>?totalPrice1" +
" GROUP BY {o.pk},{o.code},{e.entryNumber} ORDER BY {o.code},{e.entryNumber} DESC",
" GROUP BY {o.pk},{o.code},{e.entryNumber} ORDER BY {o.code} DESC,{e.entryNumber} DESC",
fQuery.getQuery());
assertEquals("Wrong number of query parameters", 2, fQuery.getQueryParameters().size());
assertEquals("Query parameter doesn't match", productCode, fQuery.getQueryParameters().get("code1"));
Expand Down

0 comments on commit ff63bc4

Please sign in to comment.