From 1b9f4bafdd310d45f93c1ab7628877c78dea1684 Mon Sep 17 00:00:00 2001 From: Geoffrey GREBERT Date: Thu, 14 Nov 2024 01:21:26 +0100 Subject: [PATCH] Allow panache sorting by Sort.Column 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 = sorts.stream() .map(fields -> fields.startsWith("-") ? new Sort.Column(fields.substring(1), Sort.Direction.Descending) : new Sort.Column(fields, Sort.Direction.Ascending)) .reduce(Sort::by); ``` --- .../java/io/quarkus/panache/common/Sort.java | 23 ++++++++++++++++- .../it/panache/defaultpu/TestEndpoint.java | 25 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) 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 a503da211661c4..3118f69ec565d8 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 @@ -1,6 +1,7 @@ package io.quarkus.panache.common; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; /** @@ -175,6 +176,16 @@ 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... columns) { + return new Sort().and(columns); + } + /** * Sort by the given columns, in ascending order. Equivalent to {@link #by(String...)}. * @@ -290,7 +301,17 @@ public Sort and(String name, NullPrecedence nullPrecedence) { * @see #and(String) */ public Sort and(String name, Direction direction, NullPrecedence nullPrecedence) { - columns.add(new Column(name, direction, nullPrecedence)); + return and(new Column(name, direction, nullPrecedence)); + } + + /** + * Add a sort column + * + * @param column the column to sort on with his properties + * @return this instance, modified. + */ + public Sort and(Column... columns) { + this.columns.addAll(Arrays.asList(columns)); return this; } 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 440aa962ac3f0e..4498f7dc3d2d50 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,22 @@ private void testSorting() { Sort sort1 = Sort.by("name", "status"); List order1 = Arrays.asList(person3, person2, person1); + List columns = Arrays.asList( + 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(columns.stream().reduce(Sort::by)).list(); + Assertions.assertEquals(order1, list); + list = Person.listAll(sort1); Assertions.assertEquals(order1, list); @@ -534,6 +546,19 @@ 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.by(columns), "stef").list(); + Assertions.assertEquals(order2, list); + + list = Person.findAll(columns.stream().reduce(Sort::by)).list(); + Assertions.assertEquals(order2, list); + + list = Person.find("name", Sort.empty().and(columns), "stef").list(); + Assertions.assertEquals(order2, list); + list = Person.list("name", sort2, "stef"); Assertions.assertEquals(order2, list);