Skip to content

Commit

Permalink
Allow panache sorting by Sort.Column
Browse files Browse the repository at this point in the history
Before:

  ```
  var sorts = List.of("-name", "status");
  var sort = Sort.empty();

  sorts.stream()
      .map(fields -> fields.startsWith("-")
          ? new Sort.Column(fields.substring(1), Sort.Direction.Descending)
          : new Sort.Column(fields, Sort.Direction.Ascending))
      .forEach(c -> sort.and(c.getName(), c.getDirection()));
  ```

After:

  ```
  var sorts = List.of("-name", "status");
  var sort = Sort.empty();

  sorts.stream()
      .map(fields -> fields.startsWith("-")
          ? new Sort.Column(fields.substring(1), Sort.Direction.Descending)
          : new Sort.Column(fields, Sort.Direction.Ascending))
      .forEach(sorts::and);
  ```
  • Loading branch information
ggrebert committed Nov 14, 2024
1 parent 7fa4088 commit 96d9fa8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ public Sort and(String name, Direction direction, NullPrecedence nullPrecedence)
return this;
}

/**
* Add a sort column
*
* @param column the column to sort on with his properties
* @return this instance, modified.
*/
public Sort and(Column column) {
columns.add(column);
return this;
}

/**
* Disables escaping of column names with a backticks during HQL Order By clause generation
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,17 @@ private void testSorting() {

Sort sort1 = Sort.by("name", "status");
List<Person> order1 = Arrays.asList(person3, person2, person1);
List<Sort.Column> columns = Arrays.asList(
new Sort.column("name", Sort.Direction.Descending),
new Sort.column("status", Sort.Direction.Ascending)
);

List<Person> list = Person.findAll(sort1).list();
Assertions.assertEquals(order1, list);

list = Person.findAll(Sort.and(column)).list();
Assertions.assertEquals(order1, list);

list = Person.listAll(sort1);
Assertions.assertEquals(order1, list);

Expand All @@ -534,6 +541,13 @@ private void testSorting() {
list = Person.find("name", sort2, "stef").list();
Assertions.assertEquals(order2, list);

columns = Arrays.asList(
new Sort.column("name", Sort.Direction.Descending),
new Sort.column("status", Sort.Direction.Ascending)
);
list = Person.find("name", Sort.and(column), "stef").list();
Assertions.assertEquals(order2, list);

list = Person.list("name", sort2, "stef");
Assertions.assertEquals(order2, list);

Expand Down

0 comments on commit 96d9fa8

Please sign in to comment.