-
Notifications
You must be signed in to change notification settings - Fork 26
Add utilities for Pageable to be used with Doma Criteria. #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
backpaper0
merged 20 commits into
domaframework:master
from
mazeneko:add-pagebles-for-criteria
Mar 30, 2025
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c776c68
Add utilities for Pageable to be used with Doma Criteria.
mazeneko 18b1a4d
Apply format
mazeneko 5f6fe5c
Even if a sort order is specified, use the default order when there a…
mazeneko 547691d
Change class name
mazeneko 9f9d9b8
Create fields
mazeneko 121f407
Create static Constructors
mazeneko c02a12d
WIP: Remove orderBy variations
mazeneko c8796d9
Recreate methods
mazeneko 152ad5f
Create getters
mazeneko 74ed41c
Make the sort configuration required instead of optional
mazeneko e7a08ff
Fix test name
mazeneko 9c0694e
Use @ParameterizedTest
mazeneko a7159f4
Removed an unused @throws tag from the Javadoc of orderBy
mazeneko 275499e
Injectable a handler for missing sort properties in orderBy
mazeneko 18c5122
Use the actual metamodel instead of a mock in the test
mazeneko dd97640
Fixed an issue where the call order of sort specifications was not be…
mazeneko 3d37c73
To better testOrderByWhenNonSort
mazeneko 0522fdf
Add missing tests for `missing properties`
mazeneko cb4e97e
Remove stream side effects
mazeneko 1ed99cc
Fix missing closing bracket in Javadoc
mazeneko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
doma-spring-boot-core/src/main/java/org/seasar/doma/boot/PageablesForCriteria.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package org.seasar.doma.boot; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.seasar.doma.jdbc.criteria.declaration.OrderByNameDeclaration; | ||
| import org.seasar.doma.jdbc.criteria.metamodel.EntityMetamodel; | ||
| import org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel; | ||
| import org.seasar.doma.jdbc.criteria.statement.EntityQueryable; | ||
| import org.springframework.data.domain.Pageable; | ||
|
|
||
| /** | ||
| * Converts Utilities for {@link Pageable} to be used with Doma Criteria. | ||
| * | ||
| * @author mazeneko | ||
| */ | ||
| public class PageablesForCriteria { | ||
| /** | ||
| * Converts {@link Pageable} to {@link EntityQueryable#limit(Integer)} | ||
| * | ||
| * @param pageable {@link Pageable} object to convert | ||
| * @return the limit. | ||
| * if {@link Pageable#isUnpaged()} is {@code true} then null. | ||
| */ | ||
| public static Integer limit(Pageable pageable) { | ||
| return pageable.isUnpaged() ? null : pageable.getPageSize(); | ||
| } | ||
|
|
||
| /** | ||
| * Converts {@link Pageable} to {@link EntityQueryable#offset(Integer)} | ||
| * | ||
| * @param pageable {@link Pageable} object to convert | ||
| * @return the offset. | ||
| * if {@link Pageable#isUnpaged()} is {@code true} then null. | ||
| */ | ||
| public static Integer offset(Pageable pageable) { | ||
| return pageable.isUnpaged() ? null : Math.multiplyExact(pageable.getPageNumber(), pageable.getPageSize()); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an {@link OrderByNameDeclaration} consumer for a single entity based | ||
| * on the {@link Pageable}'s sort information. | ||
| * <p> | ||
| * This method resolves property names for ordering within the specified entity | ||
| * using its metamodel, and generates | ||
| * a consumer that can be used to apply ascending/descending orders. | ||
| * <p> | ||
| * If the {@link Pageable} is unsorted, no ordering is applied. | ||
| * | ||
| * @param pageable the {@link Pageable} containing sorting information | ||
| * @param entityMetamodel the {@link EntityMetamodel} corresponding to the | ||
| * target entity | ||
| * @return a consumer that configures ordering on the target entity | ||
| */ | ||
| public static Consumer<OrderByNameDeclaration> orderBySingleEntity( | ||
| Pageable pageable, | ||
| EntityMetamodel<?> entityMetamodel) { | ||
| return orderBySingleEntity(pageable, entityMetamodel, c -> { | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an {@link OrderByNameDeclaration} consumer for a single entity based | ||
| * on the {@link Pageable}'s sort information. | ||
| * <p> | ||
| * This method resolves property names for ordering within the specified entity | ||
| * using its metamodel, and generates | ||
| * a consumer that can be used to apply ascending/descending orders. | ||
| * <p> | ||
| * a default ordering via {@code defaultOrder} if the given {@link Pageable} is | ||
| * unsorted. | ||
| * | ||
| * @param pageable the {@link Pageable} containing sorting information | ||
| * @param entityMetamodel the {@link EntityMetamodel} corresponding to the | ||
| * target entity | ||
| * @param defaultOrder a consumer that applies default ordering if the | ||
| * {@link Pageable} is unsorted | ||
| * @return a consumer that configures ordering on the target entity | ||
| */ | ||
| public static Consumer<OrderByNameDeclaration> orderBySingleEntity( | ||
| Pageable pageable, | ||
| EntityMetamodel<?> entityMetamodel, | ||
| Consumer<OrderByNameDeclaration> defaultOrder) { | ||
| final var nameToMetamodel = entityMetamodel | ||
| .allPropertyMetamodels() | ||
| .stream() | ||
| .collect(Collectors.toMap(PropertyMetamodel::getName, Function.identity())); | ||
| return orderBy( | ||
| pageable, | ||
| propertyName -> Optional.ofNullable(nameToMetamodel.get(propertyName)), | ||
| defaultOrder); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an {@link OrderByNameDeclaration} consumer based on the | ||
| * {@link Pageable}'s sort information | ||
| * using the provided {@link PropertyMetamodelResolver}. | ||
| * <p> | ||
| * If the {@link Pageable} is unsorted, no ordering is applied. | ||
| * | ||
| * @param pageable the {@link Pageable} containing sorting | ||
| * information | ||
| * @param propertyMetamodelResolver a resolver that maps property names to | ||
| * {@link PropertyMetamodel} | ||
| * @return a consumer that configures ordering based on the resolved | ||
| * {@link PropertyMetamodel} instances | ||
| */ | ||
| public static Consumer<OrderByNameDeclaration> orderBy( | ||
| Pageable pageable, | ||
| PropertyMetamodelResolver propertyMetamodelResolver) { | ||
| return orderBy(pageable, propertyMetamodelResolver, c -> { | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an {@link OrderByNameDeclaration} consumer based on the | ||
| * {@link Pageable}'s sort information | ||
| * using the provided {@link PropertyMetamodelResolver}. | ||
| * <p> | ||
| * a default ordering via {@code defaultOrder} if the given {@link Pageable} is | ||
| * unsorted. | ||
| * | ||
| * @param pageable the {@link Pageable} containing sorting | ||
| * information | ||
| * @param propertyMetamodelResolver a resolver that maps property names to | ||
| * {@link PropertyMetamodel} | ||
| * @param defaultOrder a consumer that applies default ordering if | ||
| * the {@link Pageable} is unsorted | ||
| * @return a consumer that configures ordering based on the resolved | ||
| * {@link PropertyMetamodel} instances | ||
| */ | ||
| public static Consumer<OrderByNameDeclaration> orderBy( | ||
| Pageable pageable, | ||
| PropertyMetamodelResolver propertyMetamodelResolver, | ||
| Consumer<OrderByNameDeclaration> defaultOrder) { | ||
| if (pageable.getSort().isUnsorted()) { | ||
| return defaultOrder; | ||
| } | ||
| final var orderSpecifiers = pageable | ||
| .getSort() | ||
| .flatMap(order -> propertyMetamodelResolver | ||
| .resolve(order.getProperty()) | ||
| .<Consumer<OrderByNameDeclaration>>map(propertyMetamodel -> switch (order.getDirection()) { | ||
| case ASC -> c -> c.asc(propertyMetamodel); | ||
| case DESC -> c -> c.desc(propertyMetamodel); | ||
| }) | ||
| .stream()) | ||
| .toList(); | ||
| return c -> orderSpecifiers.forEach(orderSpecifier -> orderSpecifier.accept(c)); | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
doma-spring-boot-core/src/main/java/org/seasar/doma/boot/PropertyMetamodelResolver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package org.seasar.doma.boot; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel; | ||
|
|
||
| /** | ||
| * A resolver that maps property names to {@link PropertyMetamodel} | ||
| */ | ||
| @FunctionalInterface | ||
| public interface PropertyMetamodelResolver { | ||
| /** | ||
| * Resolves the specified property name into a {@link PropertyMetamodel}. | ||
| * | ||
| * @param propertyName the name of the property to resolve | ||
| * @return an {@link Optional} containing the resolved {@link PropertyMetamodel} | ||
| * if found, | ||
| * or an empty {@link Optional} if the property name cannot be resolved | ||
| */ | ||
| Optional<PropertyMetamodel<?>> resolve(String propertyName); | ||
| } |
142 changes: 142 additions & 0 deletions
142
doma-spring-boot-core/src/test/java/org/seasar/doma/boot/PageablesForCriteriaTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package org.seasar.doma.boot; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.is; | ||
| import static org.hamcrest.CoreMatchers.nullValue; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.function.Consumer; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.seasar.doma.jdbc.criteria.declaration.OrderByNameDeclaration; | ||
| import org.seasar.doma.jdbc.criteria.metamodel.EntityMetamodel; | ||
| import org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Sort; | ||
|
|
||
| public class PageablesForCriteriaTest { | ||
| @Test | ||
| public void testOffsetAndLimit() { | ||
| Pageable pageable = PageRequest.of(0, 10); | ||
| Integer offset = PageablesForCriteria.offset(pageable); | ||
| Integer limit = PageablesForCriteria.limit(pageable); | ||
| assertThat(offset, is(0)); | ||
| assertThat(limit, is(10)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOffsetAndLimit2() { | ||
| Pageable pageable = PageRequest.of(2, 10); | ||
| Integer offset = PageablesForCriteria.offset(pageable); | ||
| Integer limit = PageablesForCriteria.limit(pageable); | ||
| assertThat(offset, is(20)); | ||
| assertThat(limit, is(10)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOffsetAndLimit3() { | ||
| Pageable pageable = PageRequest.of(2, 5); | ||
| Integer offset = PageablesForCriteria.offset(pageable); | ||
| Integer limit = PageablesForCriteria.limit(pageable); | ||
| assertThat(offset, is(10)); | ||
| assertThat(limit, is(5)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOffsetAndLimit4() { | ||
| Pageable pageable = Pageable.unpaged(); | ||
| Integer offset = PageablesForCriteria.offset(pageable); | ||
| Integer limit = PageablesForCriteria.limit(pageable); | ||
| assertThat(offset, nullValue()); | ||
| assertThat(limit, nullValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOrderBy() { | ||
| Pageable pageable = PageRequest.of(0, 10, Sort.by("name").ascending()); | ||
|
|
||
| PropertyMetamodel<?> nameProp = mock(PropertyMetamodel.class); | ||
|
|
||
| Consumer<OrderByNameDeclaration> consumer = PageablesForCriteria.orderBy( | ||
| pageable, | ||
| propertyName -> switch (propertyName) { | ||
| case "name" -> Optional.of(nameProp); | ||
| default -> Optional.empty(); | ||
| }); | ||
| OrderByNameDeclaration orderByNameDeclaration = mock(OrderByNameDeclaration.class); | ||
| consumer.accept(orderByNameDeclaration); | ||
| verify(orderByNameDeclaration, times(1)).asc(nameProp); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOrderBy2() { | ||
| Pageable pageable = PageRequest.of(0, 10, Sort.by("name").descending().and(Sort.by("age").ascending())); | ||
|
|
||
| PropertyMetamodel<?> nameProp = mock(PropertyMetamodel.class); | ||
| PropertyMetamodel<?> ageProp = mock(PropertyMetamodel.class); | ||
|
|
||
| Consumer<OrderByNameDeclaration> consumer = PageablesForCriteria.orderBy( | ||
| pageable, | ||
| propertyName -> switch (propertyName) { | ||
| case "name" -> Optional.of(nameProp); | ||
| case "age" -> Optional.of(ageProp); | ||
| default -> Optional.empty(); | ||
| }); | ||
| OrderByNameDeclaration orderByNameDeclaration = mock(OrderByNameDeclaration.class); | ||
| consumer.accept(orderByNameDeclaration); | ||
| verify(orderByNameDeclaration, times(1)).desc(nameProp); | ||
| verify(orderByNameDeclaration, times(1)).asc(ageProp); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOrderByWhenNonSort() { | ||
| Pageable pageable = PageRequest.of(0, 10); | ||
|
|
||
| Consumer<OrderByNameDeclaration> consumer = PageablesForCriteria.orderBy( | ||
| pageable, | ||
| propertyName -> Optional.empty()); | ||
| OrderByNameDeclaration orderByNameDeclaration = mock(OrderByNameDeclaration.class); | ||
| consumer.accept(orderByNameDeclaration); | ||
| verifyNoMoreInteractions(orderByNameDeclaration); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOrderByWhenNonSortAndSetDefault() { | ||
| Pageable pageable = PageRequest.of(0, 10); | ||
|
|
||
| PropertyMetamodel<?> idProp = mock(PropertyMetamodel.class); | ||
|
|
||
| Consumer<OrderByNameDeclaration> consumer = PageablesForCriteria.orderBy( | ||
| pageable, | ||
| propertyName -> Optional.empty(), | ||
| t -> t.asc(idProp)); | ||
| OrderByNameDeclaration orderByNameDeclaration = mock(OrderByNameDeclaration.class); | ||
| consumer.accept(orderByNameDeclaration); | ||
| verify(orderByNameDeclaration, times(1)).asc(idProp); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOrderBySingleEntity() { | ||
| Pageable pageable = PageRequest.of(0, 10, Sort.by("name").descending().and(Sort.by("age").ascending())); | ||
|
|
||
| PropertyMetamodel<?> nameProp = mock(PropertyMetamodel.class); | ||
| when(nameProp.getName()).thenReturn("name"); | ||
| PropertyMetamodel<?> ageProp = mock(PropertyMetamodel.class); | ||
| when(ageProp.getName()).thenReturn("age"); | ||
| EntityMetamodel<?> entity = mock(EntityMetamodel.class); | ||
| when(entity.allPropertyMetamodels()).thenReturn(List.of(nameProp, ageProp)); | ||
|
|
||
| Consumer<OrderByNameDeclaration> consumer = PageablesForCriteria.orderBySingleEntity(pageable, entity); | ||
| OrderByNameDeclaration orderByNameDeclaration = mock(OrderByNameDeclaration.class); | ||
| consumer.accept(orderByNameDeclaration); | ||
| verify(orderByNameDeclaration, times(1)).desc(nameProp); | ||
| verify(orderByNameDeclaration, times(1)).asc(ageProp); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.