#148 - Consider sorting of PageRequest.

Issuing a paged SELECT with paging now considers the Sort order of PageRequest.
This commit is contained in:
Mark Paluch
2019-07-16 12:00:05 +02:00
parent 929c4dfab4
commit 861a2b194f
3 changed files with 34 additions and 8 deletions

View File

@@ -188,7 +188,12 @@ public interface StatementMapper {
* @return the {@link SelectSpec}.
*/
public SelectSpec withSort(Sort sort) {
return new SelectSpec(this.table, this.projectedFields, this.criteria, sort, this.page);
if (sort.isSorted()) {
return new SelectSpec(this.table, this.projectedFields, this.criteria, sort, this.page);
}
return new SelectSpec(this.table, this.projectedFields, this.criteria, this.sort, this.page);
}
/**

View File

@@ -443,10 +443,19 @@ public abstract class AbstractDatabaseClientIntegrationTests extends R2dbcIntegr
databaseClient.select().from(LegoSet.class) //
.orderBy(Sort.by(desc("id"))) //
.page(PageRequest.of(1, 1)).fetch().all() //
.page(PageRequest.of(2, 1)) //
.fetch().all() //
.map(LegoSet::getId) //
.as(StepVerifier::create) //
.expectNext(42064) //
.expectNext(42055) //
.verifyComplete();
databaseClient.select().from(LegoSet.class) //
.page(PageRequest.of(2, 1, Sort.by(Sort.Direction.ASC, "id"))) //
.fetch().all() //
.map(LegoSet::getId) //
.as(StepVerifier::create) //
.expectNext(42068) //
.verifyComplete();
}

View File

@@ -18,12 +18,12 @@ package org.springframework.data.r2dbc.core;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Collections;
import org.junit.Test;
import org.springframework.data.r2dbc.core.DefaultReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.core.DefaultStatementMapper;
import org.springframework.data.r2dbc.core.PreparedOperation;
import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
import org.springframework.data.r2dbc.core.StatementMapper;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.r2dbc.core.StatementMapper.UpdateSpec;
import org.springframework.data.r2dbc.dialect.BindTarget;
import org.springframework.data.r2dbc.dialect.PostgresDialect;
@@ -69,4 +69,16 @@ public class StatementMapperUnitTests {
verify(bindTarget).bind(0, "value");
verify(bindTarget).bind(1, "bar");
}
@Test // gh-148
public void shouldMapSelectWithPage() {
StatementMapper.SelectSpec selectSpec = StatementMapper.SelectSpec.create("table")
.withProjection(Collections.singletonList("*"))
.withPage(PageRequest.of(1, 2, Sort.by(Sort.Direction.DESC, "id")));
PreparedOperation<?> preparedOperation = mapper.getMappedObject(selectSpec);
assertThat(preparedOperation.toQuery()).isEqualTo("SELECT table.* FROM table ORDER BY id DESC LIMIT 2 OFFSET 2");
}
}