Skip to content

Commit 2643a3e

Browse files
committed
Add utilities for Pageable to be used with Doma.
1 parent bb6c896 commit 2643a3e

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

doma-spring-boot-autoconfigure/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
<groupId>org.springframework.boot</groupId>
3333
<artifactId>spring-boot-starter-jdbc</artifactId>
3434
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.data</groupId>
37+
<artifactId>spring-data-commons</artifactId>
38+
</dependency>
3539
<dependency>
3640
<groupId>org.springframework.boot</groupId>
3741
<artifactId>spring-boot-configuration-processor</artifactId>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,39 @@
1+
/*
2+
* Copyright (C) 2004-2016 the Seasar Foundation and the Others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
116
package org.seasar.doma.boot;
217

18+
import org.seasar.doma.jdbc.SelectOptions;
19+
import org.springframework.data.domain.Pageable;
20+
21+
/**
22+
* Converts Utilities for {@link Pageable} to be used with Doma.
23+
*
24+
* @author Toshiaki Maki
25+
*/
326
public final class Pageables {
27+
/**
28+
* Converts {@link Pageable} to {@link SelectOptions}
29+
*
30+
* @param pageable {@link Pageable} object to convert
31+
* @return {@link SelectOptions} object corresponds to the given {@link Pageable}
32+
* object.
33+
*/
34+
public static SelectOptions toSelectOptions(Pageable pageable) {
35+
final int offset = pageable.getPageNumber() * pageable.getPageSize();
36+
final int limit = pageable.getPageSize();
37+
return SelectOptions.get().offset(offset).limit(limit);
38+
}
439
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2004-2016 the Seasar Foundation and the Others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
16+
package org.seasar.doma.boot;
17+
18+
import org.junit.Test;
19+
import org.seasar.doma.jdbc.SelectOptions;
20+
import org.seasar.doma.jdbc.SelectOptionsAccessor;
21+
import org.springframework.data.domain.PageRequest;
22+
23+
import static org.hamcrest.CoreMatchers.is;
24+
import static org.junit.Assert.assertThat;
25+
26+
public class PageablesTest {
27+
28+
@Test
29+
public void testToSelectOptions() throws Exception {
30+
SelectOptions options = Pageables.toSelectOptions(new PageRequest(0, 10));
31+
assertThat(SelectOptionsAccessor.getOffset(options), is(0L));
32+
assertThat(SelectOptionsAccessor.getLimit(options), is(10L));
33+
}
34+
35+
@Test
36+
public void testToSelectOptions2() throws Exception {
37+
SelectOptions options = Pageables.toSelectOptions(new PageRequest(2, 10));
38+
assertThat(SelectOptionsAccessor.getOffset(options), is(20L));
39+
assertThat(SelectOptionsAccessor.getLimit(options), is(10L));
40+
}
41+
42+
@Test
43+
public void testToSelectOptions3() throws Exception {
44+
SelectOptions options = Pageables.toSelectOptions(new PageRequest(2, 5));
45+
assertThat(SelectOptionsAccessor.getOffset(options), is(10L));
46+
assertThat(SelectOptionsAccessor.getLimit(options), is(5L));
47+
}
48+
}

0 commit comments

Comments
 (0)