Skip to content

Commit 861a2b1

Browse files
committed
#148 - Consider sorting of PageRequest.
Issuing a paged SELECT with paging now considers the Sort order of PageRequest.
1 parent 929c4df commit 861a2b1

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/main/java/org/springframework/data/r2dbc/core/StatementMapper.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,12 @@ public SelectSpec withCriteria(Criteria criteria) {
188188
* @return the {@link SelectSpec}.
189189
*/
190190
public SelectSpec withSort(Sort sort) {
191-
return new SelectSpec(this.table, this.projectedFields, this.criteria, sort, this.page);
191+
192+
if (sort.isSorted()) {
193+
return new SelectSpec(this.table, this.projectedFields, this.criteria, sort, this.page);
194+
}
195+
196+
return new SelectSpec(this.table, this.projectedFields, this.criteria, this.sort, this.page);
192197
}
193198

194199
/**

src/test/java/org/springframework/data/r2dbc/core/AbstractDatabaseClientIntegrationTests.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,19 @@ public void selectOrderPaged() {
443443

444444
databaseClient.select().from(LegoSet.class) //
445445
.orderBy(Sort.by(desc("id"))) //
446-
.page(PageRequest.of(1, 1)).fetch().all() //
446+
.page(PageRequest.of(2, 1)) //
447+
.fetch().all() //
447448
.map(LegoSet::getId) //
448449
.as(StepVerifier::create) //
449-
.expectNext(42064) //
450+
.expectNext(42055) //
451+
.verifyComplete();
452+
453+
databaseClient.select().from(LegoSet.class) //
454+
.page(PageRequest.of(2, 1, Sort.by(Sort.Direction.ASC, "id"))) //
455+
.fetch().all() //
456+
.map(LegoSet::getId) //
457+
.as(StepVerifier::create) //
458+
.expectNext(42068) //
450459
.verifyComplete();
451460
}
452461

src/test/java/org/springframework/data/r2dbc/core/StatementMapperUnitTests.java

+17-5
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.mockito.Mockito.*;
2020

21+
import java.util.Collections;
22+
2123
import org.junit.Test;
22-
import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy;
23-
import org.springframework.data.r2dbc.core.DefaultStatementMapper;
24-
import org.springframework.data.r2dbc.core.PreparedOperation;
25-
import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
26-
import org.springframework.data.r2dbc.core.StatementMapper;
24+
25+
import org.springframework.data.domain.PageRequest;
26+
import org.springframework.data.domain.Sort;
2727
import org.springframework.data.r2dbc.core.StatementMapper.UpdateSpec;
2828
import org.springframework.data.r2dbc.dialect.BindTarget;
2929
import org.springframework.data.r2dbc.dialect.PostgresDialect;
@@ -69,4 +69,16 @@ public void shouldMapUpdateWithCriteria() {
6969
verify(bindTarget).bind(0, "value");
7070
verify(bindTarget).bind(1, "bar");
7171
}
72+
73+
@Test // gh-148
74+
public void shouldMapSelectWithPage() {
75+
76+
StatementMapper.SelectSpec selectSpec = StatementMapper.SelectSpec.create("table")
77+
.withProjection(Collections.singletonList("*"))
78+
.withPage(PageRequest.of(1, 2, Sort.by(Sort.Direction.DESC, "id")));
79+
80+
PreparedOperation<?> preparedOperation = mapper.getMappedObject(selectSpec);
81+
82+
assertThat(preparedOperation.toQuery()).isEqualTo("SELECT table.* FROM table ORDER BY id DESC LIMIT 2 OFFSET 2");
83+
}
7284
}

0 commit comments

Comments
 (0)