Align OffsetScrolling to zero-based indexes.

Closes #1764
This commit is contained in:
Mark Paluch
2024-04-10 15:56:28 +02:00
parent ad8ca20438
commit ae272e2a1e
4 changed files with 13 additions and 6 deletions

View File

@@ -102,7 +102,11 @@ class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<S, R> {
if (scrollPosition instanceof OffsetScrollPosition osp) {
Query query = createQuery().sort(getSort()).offset(osp.getOffset());
Query query = createQuery().sort(getSort());
if (!osp.isInitial()) {
query = query.offset(osp.getOffset() + 1);
}
if (getLimit() > 0) {
query = query.limit(getLimit());

View File

@@ -54,7 +54,7 @@ public class ScrollDelegate {
List<T> result = queryFunction.apply(query);
if (scrollPosition instanceof OffsetScrollPosition offset) {
return createWindow(result, limit, OffsetScrollPosition.positionFunction(offset.getOffset()));
return createWindow(result, limit, offset.positionFunction());
}
throw new UnsupportedOperationException("ScrollPosition " + scrollPosition + " not supported");

View File

@@ -1137,7 +1137,7 @@ public class JdbcRepositoryIntegrationTests {
assertThat(first.map(DummyEntity::getName)).containsExactly("one", "three");
Window<DummyEntity> second = repository.findBy(example, q -> q.limit(2).sortBy(Sort.by("name")))
.scroll(ScrollPosition.offset(2));
.scroll(ScrollPosition.offset(1));
assertThat(second.map(DummyEntity::getName)).containsExactly("two");
WindowIterator<DummyEntity> iterator = WindowIterator.of(

View File

@@ -409,7 +409,11 @@ public class SimpleR2dbcRepository<T, ID> implements R2dbcRepository<T, ID> {
int limit = getLimit();
return createQuery(q -> {
Query queryToUse = q.offset(osp.getOffset());
Query queryToUse = q;
if (!osp.isInitial()) {
queryToUse = queryToUse.offset(osp.getOffset() + 1);
}
if (limit > 0) {
queryToUse = queryToUse.limit(limit + 1);
@@ -419,8 +423,7 @@ public class SimpleR2dbcRepository<T, ID> implements R2dbcRepository<T, ID> {
}).all() //
.collectList() //
.map(content -> {
return ScrollDelegate.createWindow(content, limit,
OffsetScrollPosition.positionFunction(osp.getOffset()));
return ScrollDelegate.createWindow(content, limit, osp.positionFunction());
});
}