Skip to content

Commit 5e1d1b2

Browse files
committed
#335 - Consider Pageable in derived queries.
We now consider Pageable arguments in derived queries. Previously only limiting queries (findFirst10) were considered.
1 parent e7214cf commit 5e1d1b2

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/main/java/org/springframework/data/r2dbc/repository/query/R2dbcQueryCreator.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.List;
1919
import java.util.stream.Collectors;
2020

21+
import org.springframework.data.domain.Pageable;
2122
import org.springframework.data.domain.Sort;
2223
import org.springframework.data.r2dbc.core.PreparedOperation;
2324
import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
@@ -43,6 +44,7 @@
4344
public class R2dbcQueryCreator extends RelationalQueryCreator<PreparedOperation<?>> {
4445

4546
private final PartTree tree;
47+
private final RelationalParameterAccessor accessor;
4648
private final ReactiveDataAccessStrategy dataAccessStrategy;
4749
private final RelationalEntityMetadata<?> entityMetadata;
4850

@@ -58,11 +60,13 @@ public class R2dbcQueryCreator extends RelationalQueryCreator<PreparedOperation<
5860
public R2dbcQueryCreator(PartTree tree, ReactiveDataAccessStrategy dataAccessStrategy,
5961
RelationalEntityMetadata<?> entityMetadata, RelationalParameterAccessor accessor) {
6062
super(tree, accessor);
61-
this.tree = tree;
6263

6364
Assert.notNull(dataAccessStrategy, "Data access strategy must not be null");
6465
Assert.notNull(entityMetadata, "Relational entity metadata must not be null");
6566

67+
this.tree = tree;
68+
this.accessor = accessor;
69+
6670
this.dataAccessStrategy = dataAccessStrategy;
6771
this.entityMetadata = entityMetadata;
6872
}
@@ -87,6 +91,11 @@ protected PreparedOperation<?> complete(Criteria criteria, Sort sort) {
8791
selectSpec = selectSpec.limit(tree.getMaxResults());
8892
}
8993

94+
Pageable pageable = accessor.getPageable();
95+
if (pageable.isPaged()) {
96+
selectSpec = selectSpec.limit(pageable.getPageSize()).offset(pageable.getOffset());
97+
}
98+
9099
if (criteria != null) {
91100
selectSpec = selectSpec.withCriteria(criteria);
92101
}

src/test/java/org/springframework/data/r2dbc/repository/AbstractR2dbcRepositoryIntegrationTests.java

+54
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Arrays;
3030
import java.util.Collections;
3131
import java.util.Map;
32+
import java.util.stream.IntStream;
3233

3334
import javax.sql.DataSource;
3435

@@ -40,6 +41,8 @@
4041
import org.springframework.dao.DataAccessException;
4142
import org.springframework.data.annotation.Id;
4243
import org.springframework.data.annotation.PersistenceConstructor;
44+
import org.springframework.data.domain.PageRequest;
45+
import org.springframework.data.domain.Pageable;
4346
import org.springframework.data.r2dbc.connectionfactory.R2dbcTransactionManager;
4447
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
4548
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
@@ -179,6 +182,53 @@ public void shouldDeleteUsingQueryMethod() {
179182
assertThat(count).hasEntrySatisfying("count", numberOf(1));
180183
}
181184

185+
@Test // gh-335
186+
public void shouldFindByPageable() {
187+
188+
Flux<LegoSet> sets = Flux.fromStream(IntStream.range(0, 100).mapToObj(value -> {
189+
return new LegoSet(null, "Set " + value, value);
190+
}));
191+
192+
repository.saveAll(sets) //
193+
.as(StepVerifier::create) //
194+
.expectNextCount(100) //
195+
.verifyComplete();
196+
197+
repository.findAllByOrderByManual(PageRequest.of(0, 10)) //
198+
.collectList() //
199+
.as(StepVerifier::create) //
200+
.consumeNextWith(actual -> {
201+
202+
assertThat(actual).hasSize(10).extracting(LegoSet::getManual).containsSequence(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
203+
}).verifyComplete();
204+
205+
repository.findAllByOrderByManual(PageRequest.of(19, 5)) //
206+
.collectList() //
207+
.as(StepVerifier::create) //
208+
.consumeNextWith(actual -> {
209+
210+
assertThat(actual).hasSize(5).extracting(LegoSet::getManual).containsSequence(95, 96, 97, 98, 99);
211+
}).verifyComplete();
212+
}
213+
214+
@Test // gh-335
215+
public void shouldFindTop10() {
216+
217+
Flux<LegoSet> sets = Flux.fromStream(IntStream.range(0, 100).mapToObj(value -> {
218+
return new LegoSet(null, "Set " + value, value);
219+
}));
220+
221+
repository.saveAll(sets) //
222+
.as(StepVerifier::create) //
223+
.expectNextCount(100) //
224+
.verifyComplete();
225+
226+
repository.findFirst10By() //
227+
.as(StepVerifier::create) //
228+
.expectNextCount(10) //
229+
.verifyComplete();
230+
}
231+
182232
@Test
183233
public void shouldInsertItemsTransactional() {
184234

@@ -212,6 +262,10 @@ interface LegoSetRepository extends ReactiveCrudRepository<LegoSet, Integer> {
212262

213263
Flux<LegoSet> findByNameContains(String name);
214264

265+
Flux<LegoSet> findFirst10By();
266+
267+
Flux<LegoSet> findAllByOrderByManual(Pageable pageable);
268+
215269
Flux<Named> findAsProjection();
216270

217271
Mono<LegoSet> findByManual(int manual);

0 commit comments

Comments
 (0)