#335 - Consider Pageable in derived queries.

We now consider Pageable arguments in derived queries. Previously only limiting queries (findFirst10) were considered.
This commit is contained in:
Mark Paluch
2020-04-02 14:20:45 +02:00
parent e7214cfb73
commit 5e1d1b2919
2 changed files with 64 additions and 1 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.data.r2dbc.repository.query;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.r2dbc.core.PreparedOperation;
import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
@@ -43,6 +44,7 @@ import org.springframework.util.Assert;
public class R2dbcQueryCreator extends RelationalQueryCreator<PreparedOperation<?>> {
private final PartTree tree;
private final RelationalParameterAccessor accessor;
private final ReactiveDataAccessStrategy dataAccessStrategy;
private final RelationalEntityMetadata<?> entityMetadata;
@@ -58,11 +60,13 @@ public class R2dbcQueryCreator extends RelationalQueryCreator<PreparedOperation<
public R2dbcQueryCreator(PartTree tree, ReactiveDataAccessStrategy dataAccessStrategy,
RelationalEntityMetadata<?> entityMetadata, RelationalParameterAccessor accessor) {
super(tree, accessor);
this.tree = tree;
Assert.notNull(dataAccessStrategy, "Data access strategy must not be null");
Assert.notNull(entityMetadata, "Relational entity metadata must not be null");
this.tree = tree;
this.accessor = accessor;
this.dataAccessStrategy = dataAccessStrategy;
this.entityMetadata = entityMetadata;
}
@@ -87,6 +91,11 @@ public class R2dbcQueryCreator extends RelationalQueryCreator<PreparedOperation<
selectSpec = selectSpec.limit(tree.getMaxResults());
}
Pageable pageable = accessor.getPageable();
if (pageable.isPaged()) {
selectSpec = selectSpec.limit(pageable.getPageSize()).offset(pageable.getOffset());
}
if (criteria != null) {
selectSpec = selectSpec.withCriteria(criteria);
}

View File

@@ -29,6 +29,7 @@ import reactor.test.StepVerifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.stream.IntStream;
import javax.sql.DataSource;
@@ -40,6 +41,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.r2dbc.connectionfactory.R2dbcTransactionManager;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
@@ -179,6 +182,53 @@ public abstract class AbstractR2dbcRepositoryIntegrationTests extends R2dbcInteg
assertThat(count).hasEntrySatisfying("count", numberOf(1));
}
@Test // gh-335
public void shouldFindByPageable() {
Flux<LegoSet> sets = Flux.fromStream(IntStream.range(0, 100).mapToObj(value -> {
return new LegoSet(null, "Set " + value, value);
}));
repository.saveAll(sets) //
.as(StepVerifier::create) //
.expectNextCount(100) //
.verifyComplete();
repository.findAllByOrderByManual(PageRequest.of(0, 10)) //
.collectList() //
.as(StepVerifier::create) //
.consumeNextWith(actual -> {
assertThat(actual).hasSize(10).extracting(LegoSet::getManual).containsSequence(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
}).verifyComplete();
repository.findAllByOrderByManual(PageRequest.of(19, 5)) //
.collectList() //
.as(StepVerifier::create) //
.consumeNextWith(actual -> {
assertThat(actual).hasSize(5).extracting(LegoSet::getManual).containsSequence(95, 96, 97, 98, 99);
}).verifyComplete();
}
@Test // gh-335
public void shouldFindTop10() {
Flux<LegoSet> sets = Flux.fromStream(IntStream.range(0, 100).mapToObj(value -> {
return new LegoSet(null, "Set " + value, value);
}));
repository.saveAll(sets) //
.as(StepVerifier::create) //
.expectNextCount(100) //
.verifyComplete();
repository.findFirst10By() //
.as(StepVerifier::create) //
.expectNextCount(10) //
.verifyComplete();
}
@Test
public void shouldInsertItemsTransactional() {
@@ -212,6 +262,10 @@ public abstract class AbstractR2dbcRepositoryIntegrationTests extends R2dbcInteg
Flux<LegoSet> findByNameContains(String name);
Flux<LegoSet> findFirst10By();
Flux<LegoSet> findAllByOrderByManual(Pageable pageable);
Flux<Named> findAsProjection();
Mono<LegoSet> findByManual(int manual);