Skip to content

Commit

Permalink
include order by direction in OQL
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoghisi authored and asereda-gs committed Feb 12, 2020
1 parent 0c5e5e3 commit 5c3d15b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Expand Up @@ -91,8 +91,10 @@ Oql generate(Query query) {

if (!query.collations().isEmpty()) {
oql.append(" ORDER BY ");

final String ascending = isMultiDirectionCollation(query) ? " ASC" : "";
final String orderBy = query.collations().stream()
.map(c -> pathNaming().name(c.path()) + (c.direction().isAscending() ? "" : " DESC"))
.map(c -> pathNaming().name(c.path()) + (c.direction().isAscending() ? ascending : " DESC"))
.collect(Collectors.joining(", "));

oql.append(orderBy);
Expand All @@ -111,6 +113,15 @@ Oql generate(Query query) {
return new Oql(variables, oql.toString());
}

private static boolean isMultiDirectionCollation(Query query) {
final long directionCount = query.collations().stream()
.map(Collation::direction)
.distinct()
.limit(Ordering.Direction.values().length)
.count();
return directionCount > 1;
}

private String toProjection(Expression expression) {
if (expression instanceof AggregationCall) {
AggregationCall aggregation = (AggregationCall) expression;
Expand Down
Expand Up @@ -72,7 +72,7 @@ void orderBy() {
.is("SELECT * FROM /myRegion ORDER BY fullName");

check(generate(query.withCollations((Collation) person.age.desc(), (Collation) person.fullName.asc())))
.is("SELECT * FROM /myRegion ORDER BY age DESC, fullName");
.is("SELECT * FROM /myRegion ORDER BY age DESC, fullName ASC");

check(generate(query.withCollations((Collation) person.fullName.desc()).withLimit(1).withOffset(10)))
.is("SELECT * FROM /myRegion ORDER BY fullName DESC LIMIT 1 OFFSET 10");
Expand Down

0 comments on commit 5c3d15b

Please sign in to comment.