diff --git a/extensions/panache/panache-common/runtime/src/main/java/io/quarkus/panache/common/Sort.java b/extensions/panache/panache-common/runtime/src/main/java/io/quarkus/panache/common/Sort.java index a503da211661c..d53a461130841 100644 --- a/extensions/panache/panache-common/runtime/src/main/java/io/quarkus/panache/common/Sort.java +++ b/extensions/panache/panache-common/runtime/src/main/java/io/quarkus/panache/common/Sort.java @@ -175,6 +175,36 @@ public static Sort by(String... columns) { return sort; } + /** + * Sort by the given columns. + * + * @param columns the columns to sort on. + * @return a new Sort instance which sorts on the given columns. + */ + public static Sort by(Column column, Column... columns) { + return new Sort().and(column, columns); + } + + /** + * Sort by the given columns. + * + * @param columns the columns to sort on. + * @return a new Sort instance which sorts on the given columns. + */ + public static Sort by(Column column) { + return new Sort().and(column); + } + + /** + * Sort by the given columns. + * + * @param columns the columns to sort on. + * @return a new Sort instance which sorts on the given columns. + */ + public static Sort by(List columns) { + return new Sort().and(columns); + } + /** * Sort by the given columns, in ascending order. Equivalent to {@link #by(String...)}. * @@ -243,16 +273,54 @@ public Sort direction(Direction direction) { 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) { + this.columns.add(column); + 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, Column... columns) { + this.columns.add(column); + if (columns != null) { + for (Column c : columns) { + this.columns.add(c); + } + } + return this; + } + + /** + * Add a sort column + * + * @param column the column to sort on with his properties + * @return this instance, modified. + */ + public Sort and(List columns) { + this.columns.addAll(columns); + return this; + } + /** * Adds a sort column, in ascending order. * * @param name the new column to sort on, in ascending order. * @return this instance, modified. * @see #and(String, Direction) + * @see #and(Column...) */ public Sort and(String name) { - columns.add(new Column(name)); - return this; + return and(new Column(name)); } /** @@ -262,10 +330,10 @@ public Sort and(String name) { * @param direction the direction to sort on. * @return this instance, modified. * @see #and(String) + * @see #and(Column...) */ public Sort and(String name, Direction direction) { - columns.add(new Column(name, direction)); - return this; + return and(new Column(name, direction)); } /** @@ -275,6 +343,7 @@ public Sort and(String name, Direction direction) { * @param nullPrecedence the null precedence to use. * @return this instance, modified. * @see #and(String) + * @see #and(Column...) */ public Sort and(String name, NullPrecedence nullPrecedence) { return and(name, Direction.Ascending, nullPrecedence); @@ -288,10 +357,10 @@ public Sort and(String name, NullPrecedence nullPrecedence) { * @param nullPrecedence the null precedence to use. * @return this instance, modified. * @see #and(String) + * @see #and(Column...) */ public Sort and(String name, Direction direction, NullPrecedence nullPrecedence) { - columns.add(new Column(name, direction, nullPrecedence)); - return this; + return and(new Column(name, direction, nullPrecedence)); } /** diff --git a/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/defaultpu/TestEndpoint.java b/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/defaultpu/TestEndpoint.java index 440aa962ac3f0..dc60459d9eff2 100644 --- a/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/defaultpu/TestEndpoint.java +++ b/integration-tests/hibernate-orm-panache/src/main/java/io/quarkus/it/panache/defaultpu/TestEndpoint.java @@ -518,10 +518,31 @@ private void testSorting() { Sort sort1 = Sort.by("name", "status"); List order1 = Arrays.asList(person3, person2, person1); + Sort.Column[] columns = { + new Sort.Column("name", Sort.Direction.Descending), + new Sort.Column("status", Sort.Direction.Ascending) }; + List columnList = new ArrayList( + new Sort.Column("name", Sort.Direction.Descending), + new Sort.Column("status", Sort.Direction.Ascending)); List list = Person.findAll(sort1).list(); Assertions.assertEquals(order1, list); + list = Person.findAll(Sort.by(columns)).list(); + Assertions.assertEquals(order1, list); + + list = Person.findAll(Sort.empty().and(columns)).list(); + Assertions.assertEquals(order1, list); + + list = Person.findAll(Sort.by(columnList)).list(); + Assertions.assertEquals(order1, list); + + list = Person.findAll(Sort.empty().and(columnList)).list(); + Assertions.assertEquals(order1, list); + + list = Person.findAll(columnList.stream().reduce(Sort::by)).list(); + Assertions.assertEquals(order1, list); + list = Person.listAll(sort1); Assertions.assertEquals(order1, list); @@ -534,6 +555,26 @@ private void testSorting() { list = Person.find("name", sort2, "stef").list(); Assertions.assertEquals(order2, list); + columns={new Sort.Column("name",Sort.Direction.Ascending),new Sort.Column("status",Sort.Direction.Descending)}; + columnList = new ArrayList( + new Sort.Column("name", Sort.Direction.Ascending), + new Sort.Column("status", Sort.Direction.Descending)); + + list = Person.find("name", Sort.by(columns), "stef").list(); + Assertions.assertEquals(order2, list); + + list = Person.find("name", Sort.empty().and(columns), "stef").list(); + Assertions.assertEquals(order2, list); + + list = Person.find("name", Sort.by(columnList), "stef").list(); + Assertions.assertEquals(order2, list); + + list = Person.find("name", Sort.empty().and(columnList), "stef").list(); + Assertions.assertEquals(order2, list); + + list = Person.findAll(columnList.stream().reduce(Sort::by)).list(); + Assertions.assertEquals(order2, list); + list = Person.list("name", sort2, "stef"); Assertions.assertEquals(order2, list);