-
Notifications
You must be signed in to change notification settings - Fork 55
Fix sorting issue #39
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
Merged
Changes from 3 commits
Commits
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
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
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
19 changes: 19 additions & 0 deletions
19
...-data-jdbc-querydsl/src/test/java/com/infobip/spring/data/jdbc/sorting/SortingEntity.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,19 @@ | ||
| package com.infobip.spring.data.jdbc.sorting; | ||
|
|
||
| import lombok.*; | ||
| import org.springframework.data.annotation.Id; | ||
| import org.springframework.data.relational.core.mapping.Column; | ||
| import org.springframework.data.relational.core.mapping.Table; | ||
|
|
||
| @Value | ||
| @Table("sorting_entity") | ||
| public class SortingEntity { | ||
|
|
||
| @With | ||
| @Id | ||
| @Column("id") | ||
| private final Long id; | ||
|
|
||
| @Column("foo_bar") | ||
| private final String fooBar; | ||
| } |
7 changes: 7 additions & 0 deletions
7
...dsl/src/test/java/com/infobip/spring/data/jdbc/sorting/SortingEntityPagingRepository.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,7 @@ | ||
| package com.infobip.spring.data.jdbc.sorting; | ||
|
|
||
| import com.infobip.spring.data.jdbc.QuerydslJdbcRepository; | ||
|
|
||
| public interface SortingEntityPagingRepository extends QuerydslJdbcRepository<SortingEntity, Long> { | ||
|
|
||
| } |
92 changes: 92 additions & 0 deletions
92
...src/test/java/com/infobip/spring/data/jdbc/sorting/SortingEntityPagingRepositoryTest.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,92 @@ | ||
| package com.infobip.spring.data.jdbc.sorting; | ||
|
|
||
| import com.infobip.spring.data.jdbc.TestBase; | ||
| import lombok.AllArgsConstructor; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Sort; | ||
| import org.springframework.data.domain.Sort.Order; | ||
| import org.springframework.data.querydsl.QPageRequest; | ||
|
|
||
| import static com.infobip.spring.data.jdbc.sorting.QSortingEntity.sortingEntity; | ||
| import static org.assertj.core.api.BDDAssertions.then; | ||
|
|
||
| @AllArgsConstructor | ||
| public class SortingEntityPagingRepositoryTest extends TestBase { | ||
|
|
||
| private final SortingEntityPagingRepository repository; | ||
|
|
||
| @Test | ||
| void shouldFindByPage() { | ||
| // given | ||
| SortingEntity givenEntity = repository.save(new SortingEntity(null, "givenValue")); | ||
|
|
||
| // when | ||
| Page<SortingEntity> actual = repository.findAll(PageRequest.of(0, 1)); | ||
|
|
||
| then(actual.getContent()).containsExactly(givenEntity); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldSortUsingQSort() { | ||
| // given | ||
| SortingEntity givenEntityA = repository.save(new SortingEntity(null, "A")); | ||
| SortingEntity givenEntityC = repository.save(new SortingEntity(null, "C")); | ||
| SortingEntity givenEntityB = repository.save(new SortingEntity(null, "B")); | ||
|
|
||
| // when | ||
| Page<SortingEntity> actual = repository.findAll(QPageRequest.of(0, 5, sortingEntity.fooBar.desc())); | ||
|
|
||
| then(actual.getContent()).containsExactly(givenEntityC, givenEntityB, givenEntityA); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldSortUsingSort() { | ||
| // given | ||
| SortingEntity givenEntityA = repository.save(new SortingEntity(null, "A")); | ||
| SortingEntity givenEntityC = repository.save(new SortingEntity(null, "C")); | ||
| SortingEntity givenEntityB = repository.save(new SortingEntity(null, "B")); | ||
|
|
||
| // when | ||
| Page<SortingEntity> actual = repository.findAll(PageRequest.of(0, 5, Sort.by(Order.desc("fooBar")))); | ||
|
|
||
| then(actual.getContent()).containsExactly(givenEntityC, givenEntityB, givenEntityA); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldSortUsingQSortCombinedWithPredicate() { | ||
| // given | ||
| SortingEntity givenEntityA = repository.save(new SortingEntity(null, "1A")); | ||
| SortingEntity givenEntityC = repository.save(new SortingEntity(null, "1C")); | ||
| SortingEntity givenEntityB = repository.save(new SortingEntity(null, "1B")); | ||
| SortingEntity otherEntity = repository.save(new SortingEntity(null, "2")); | ||
|
|
||
| // when | ||
| Page<SortingEntity> actual = repository.findAll( | ||
| sortingEntity.fooBar.startsWith("1"), | ||
| // does not work because generated classes in this project uses different naming strategy then is used in this project | ||
| QPageRequest.of(0, 5, sortingEntity.fooBar.desc()) | ||
| ); | ||
|
|
||
| then(actual.getContent()).containsExactly(givenEntityC, givenEntityB, givenEntityA); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldSortUsingSortCombinedWithPredicate() { | ||
| // given | ||
| SortingEntity givenEntityA = repository.save(new SortingEntity(null, "1A")); | ||
| SortingEntity givenEntityC = repository.save(new SortingEntity(null, "1C")); | ||
| SortingEntity givenEntityB = repository.save(new SortingEntity(null, "1B")); | ||
| SortingEntity otherEntity = repository.save(new SortingEntity(null, "2")); | ||
|
|
||
| // when | ||
| Page<SortingEntity> actual = repository.findAll( | ||
| sortingEntity.fooBar.startsWith("1"), | ||
| PageRequest.of(0, 5, Sort.by(Order.desc("fooBar"))) | ||
| ); | ||
|
|
||
| then(actual.getContent()).containsExactly(givenEntityC, givenEntityB, givenEntityA); | ||
| } | ||
| } | ||
|
|
||
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
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 |
|---|---|---|
|
|
@@ -18,11 +18,14 @@ | |
| import com.infobip.spring.data.common.Querydsl; | ||
| import com.infobip.spring.data.common.QuerydslExpressionFactory; | ||
| import com.querydsl.core.types.ConstructorExpression; | ||
| import com.querydsl.core.types.dsl.PathBuilder; | ||
| import com.querydsl.sql.*; | ||
| import org.springframework.data.mapping.context.MappingContext; | ||
| import org.springframework.data.r2dbc.convert.R2dbcConverter; | ||
| import org.springframework.data.r2dbc.core.R2dbcEntityOperations; | ||
| import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory; | ||
| import org.springframework.data.relational.core.mapping.RelationalMappingContext; | ||
| import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; | ||
| import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; | ||
| import org.springframework.data.repository.core.RepositoryMetadata; | ||
| import org.springframework.data.repository.core.support.RepositoryComposition; | ||
| import org.springframework.data.repository.core.support.RepositoryFragment; | ||
|
|
@@ -85,9 +88,15 @@ private RepositoryFragment<Object> createSimpleQuerydslR2dbcFragment(RelationalP | |
| return RepositoryFragment.implemented(simpleJPAQuerydslFragment); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be moved above line 97 to minimize the scope (since it's merged I'll do it on master) |
||
| private RepositoryFragment<Object> createQuerydslJdbcPredicateExecutor(ConstructorExpression<?> constructorExpression, | ||
| RelationalPathBase<?> path) { | ||
| Querydsl querydsl = new Querydsl(sqlQueryFactory, new PathBuilder<>(path.getType(), path.getMetadata())); | ||
|
|
||
|
|
||
| MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context = converter.getMappingContext(); | ||
| RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(constructorExpression.getType()); | ||
|
|
||
| Querydsl querydsl = new Querydsl(sqlQueryFactory, entity); | ||
| Object querydslJdbcPredicateExecutor = getTargetRepositoryViaReflection( | ||
| ReactiveQuerydslR2dbcPredicateExecutor.class, | ||
| constructorExpression, | ||
|
|
||
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.