From eefb1dc2396705444ebc2acbafc8048e6f32b6ef Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 5 Feb 2025 11:42:37 +0100 Subject: [PATCH] Add support for fluent queries returning a `Slice`. Closes #1011 --- .../modules/ROOT/pages/query-by-example.adoc | 2 +- .../data/repository/query/FluentQuery.java | 32 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/antora/modules/ROOT/pages/query-by-example.adoc b/src/main/antora/modules/ROOT/pages/query-by-example.adoc index 45590079c..cc96f2247 100644 --- a/src/main/antora/modules/ROOT/pages/query-by-example.adoc +++ b/src/main/antora/modules/ROOT/pages/query-by-example.adoc @@ -207,7 +207,7 @@ You do so by invoking the various methods of the `FetchableFluentQuery` in the s `sortBy` lets you specify an ordering for your result. `as` lets you specify the type to which you want the result to be transformed. `project` limits the queried attributes. -`first`, `firstValue`, `one`, `oneValue`, `all`, `page`, `stream`, `count`, and `exists` define what kind of result you get and how the query behaves when more than the expected number of results are available. +`first`, `firstValue`, `one`, `oneValue`, `all`, `page`, `slice`, `stream`, `count`, and `exists` define what kind of result you get and how the query behaves when more than the expected number of results are available. .Use the fluent API to get the last of potentially many results, ordered by lastname. diff --git a/src/main/java/org/springframework/data/repository/query/FluentQuery.java b/src/main/java/org/springframework/data/repository/query/FluentQuery.java index 2e09b684c..551a685f3 100644 --- a/src/main/java/org/springframework/data/repository/query/FluentQuery.java +++ b/src/main/java/org/springframework/data/repository/query/FluentQuery.java @@ -15,7 +15,6 @@ */ package org.springframework.data.repository.query; -import org.springframework.data.domain.Window; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -23,12 +22,15 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Optional; +import java.util.function.Function; import java.util.stream.Stream; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.ScrollPosition; +import org.springframework.data.domain.Slice; import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Window; import org.springframework.lang.Nullable; /** @@ -41,7 +43,7 @@ import org.springframework.lang.Nullable; public interface FluentQuery { /** - * Define the sort order. + * Define the sort order. Multiple calls will add {@link Sort#and(Sort) Sort} specifications. * * @param sort the {@link Sort} specification to sort the results by, may be {@link Sort#unsorted()}, must not be * {@literal null}. @@ -189,6 +191,19 @@ public interface FluentQuery { */ Page page(Pageable pageable); + /** + * Get a slice of matching elements for {@link Pageable}. + * + * @param pageable the pageable to request a sliced result, can be {@link Pageable#unpaged()}, must not be + * {@literal null}. The given {@link Pageable} will override any previously specified {@link Sort sort}. + * Any potentially specified {@link #limit(int)} will be overridden by {@link Pageable#getPageSize()}. + * @return + * @since 3.5 + */ + default Slice slice(Pageable pageable) { + return page(pageable); + } + /** * Stream all matching elements. * @@ -285,6 +300,19 @@ public interface FluentQuery { */ Mono> page(Pageable pageable); + /** + * Get a slice of matching elements for {@link Pageable}. + * + * @param pageable the pageable to request a sliced result, can be {@link Pageable#unpaged()}, must not be + * {@literal null}. The given {@link Pageable} will override any previously specified {@link Sort sort}. + * Any potentially specified {@link #limit(int)} will be overridden by {@link Pageable#getPageSize()}. + * @return + * @since 3.5 + */ + default Mono> slice(Pageable pageable) { + return page(pageable).map(Function.identity()); + } + /** * Get the number of matching elements. *