-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0beaad8
commit b29a238
Showing
17 changed files
with
386 additions
and
192 deletions.
There are no files selected for viewing
This file contains 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 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 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
9 changes: 2 additions & 7 deletions
9
...oot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/repository/PostRepository.java
This file contains 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 |
---|---|---|
@@ -1,13 +1,8 @@ | ||
package com.example.jooq.r2dbc.repository; | ||
|
||
import com.example.jooq.r2dbc.entities.Post; | ||
import com.example.jooq.r2dbc.repository.custom.CustomPostRepository; | ||
import java.util.UUID; | ||
import org.springframework.data.r2dbc.repository.Query; | ||
import org.springframework.data.r2dbc.repository.R2dbcRepository; | ||
import reactor.core.publisher.Flux; | ||
|
||
public interface PostRepository extends R2dbcRepository<Post, UUID> { | ||
|
||
@Query("SELECT * FROM posts where title like :title") | ||
public Flux<Post> findByTitleContains(String title); | ||
} | ||
public interface PostRepository extends R2dbcRepository<Post, UUID>, CustomPostRepository {} |
7 changes: 0 additions & 7 deletions
7
...dbc-sample/src/main/java/com/example/jooq/r2dbc/repository/PostTagRelationRepository.java
This file was deleted.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
...boot-jooq-r2dbc-sample/src/main/java/com/example/jooq/r2dbc/repository/TagRepository.java
This file contains 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
package com.example.jooq.r2dbc.repository; | ||
|
||
import com.example.jooq.r2dbc.entities.Tags; | ||
import com.example.jooq.r2dbc.repository.custom.CustomTagRepository; | ||
import java.util.UUID; | ||
import org.springframework.data.r2dbc.repository.R2dbcRepository; | ||
|
||
public interface TagRepository extends R2dbcRepository<Tags, UUID> {} | ||
public interface TagRepository extends R2dbcRepository<Tags, UUID>, CustomTagRepository {} |
10 changes: 10 additions & 0 deletions
10
...c-sample/src/main/java/com/example/jooq/r2dbc/repository/custom/CustomPostRepository.java
This file contains 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,10 @@ | ||
package com.example.jooq.r2dbc.repository.custom; | ||
|
||
import com.example.jooq.r2dbc.model.response.PostResponse; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface CustomPostRepository { | ||
Mono<Page<PostResponse>> findByKeyword(String keyword, Pageable pageable); | ||
} |
11 changes: 11 additions & 0 deletions
11
...bc-sample/src/main/java/com/example/jooq/r2dbc/repository/custom/CustomTagRepository.java
This file contains 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,11 @@ | ||
package com.example.jooq.r2dbc.repository.custom; | ||
|
||
import com.example.jooq.r2dbc.entities.Tags; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface CustomTagRepository { | ||
|
||
Mono<Page<Tags>> findAll(Pageable pageable); | ||
} |
89 changes: 89 additions & 0 deletions
89
...src/main/java/com/example/jooq/r2dbc/repository/custom/impl/CustomPostRepositoryImpl.java
This file contains 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,89 @@ | ||
package com.example.jooq.r2dbc.repository.custom.impl; | ||
|
||
import static com.example.jooq.r2dbc.testcontainersflyway.db.Tables.*; | ||
import static com.example.jooq.r2dbc.testcontainersflyway.db.Tables.POSTS; | ||
import static org.jooq.impl.DSL.multiset; | ||
import static org.jooq.impl.DSL.select; | ||
|
||
import com.example.jooq.r2dbc.model.response.PostCommentResponse; | ||
import com.example.jooq.r2dbc.model.response.PostResponse; | ||
import com.example.jooq.r2dbc.repository.custom.CustomPostRepository; | ||
import com.example.jooq.r2dbc.testcontainersflyway.db.tables.PostComments; | ||
import com.example.jooq.r2dbc.testcontainersflyway.db.tables.Posts; | ||
import org.jooq.Condition; | ||
import org.jooq.DSLContext; | ||
import org.jooq.Record1; | ||
import org.jooq.impl.DSL; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.util.StringUtils; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class CustomPostRepositoryImpl extends JooqSorting implements CustomPostRepository { | ||
|
||
private final DSLContext dslContext; | ||
|
||
public CustomPostRepositoryImpl(DSLContext dslContext) { | ||
this.dslContext = dslContext; | ||
} | ||
|
||
@Override | ||
public Mono<Page<PostResponse>> findByKeyword(String keyword, Pageable pageable) { | ||
Condition where = DSL.trueCondition(); | ||
if (StringUtils.hasText(keyword)) { | ||
where = where.and(POSTS.TITLE.likeIgnoreCase("%" + keyword + "%")); | ||
} | ||
var dataSql = | ||
dslContext | ||
.select( | ||
POSTS.ID, | ||
POSTS.TITLE, | ||
POSTS.CONTENT, | ||
multiset( | ||
select( | ||
PostComments.POST_COMMENTS.ID, | ||
PostComments.POST_COMMENTS.CONTENT, | ||
PostComments.POST_COMMENTS | ||
.CREATED_AT) | ||
.from(PostComments.POST_COMMENTS) | ||
.where( | ||
PostComments.POST_COMMENTS.POST_ID | ||
.eq(Posts.POSTS.ID))) | ||
.as("comments") | ||
.convertFrom( | ||
record3s -> | ||
record3s.into(PostCommentResponse.class)), | ||
multiset( | ||
select(TAGS.NAME) | ||
.from(TAGS) | ||
.join(POSTS_TAGS) | ||
.on(TAGS.ID.eq(POSTS_TAGS.TAG_ID)) | ||
.where(POSTS_TAGS.POST_ID.eq(POSTS.ID))) | ||
.as("tags") | ||
.convertFrom(record -> record.map(Record1::value1))) | ||
.from(POSTS.leftJoin(POST_COMMENTS).on(POST_COMMENTS.POST_ID.eq(POSTS.ID))) | ||
.where(where) | ||
.groupBy(POSTS.ID) | ||
.orderBy(getSortFields(pageable.getSort(), POSTS)) | ||
.limit(pageable.getPageSize()) | ||
.offset(pageable.getOffset()); | ||
|
||
var countSql = dslContext.selectCount().from(POSTS).where(where); | ||
|
||
return Mono.zip( | ||
Flux.from(dataSql) | ||
.map( | ||
r -> | ||
new PostResponse( | ||
r.value1(), | ||
r.value2(), | ||
r.value3(), | ||
r.value4(), | ||
r.value5())) | ||
.collectList(), | ||
Mono.from(countSql).map(Record1::value1)) | ||
.map(it -> new PageImpl<>(it.getT1(), pageable, it.getT2())); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
.../src/main/java/com/example/jooq/r2dbc/repository/custom/impl/CustomTagRepositoryImpl.java
This file contains 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,37 @@ | ||
package com.example.jooq.r2dbc.repository.custom.impl; | ||
|
||
import static com.example.jooq.r2dbc.testcontainersflyway.db.Tables.TAGS; | ||
|
||
import com.example.jooq.r2dbc.entities.Tags; | ||
import com.example.jooq.r2dbc.repository.custom.CustomTagRepository; | ||
import org.jooq.*; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageImpl; | ||
import org.springframework.data.domain.Pageable; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class CustomTagRepositoryImpl extends JooqSorting implements CustomTagRepository { | ||
|
||
private final DSLContext dslContext; | ||
|
||
public CustomTagRepositoryImpl(DSLContext dslContext) { | ||
this.dslContext = dslContext; | ||
} | ||
|
||
@Override | ||
public Mono<Page<Tags>> findAll(Pageable pageable) { | ||
var dataSql = | ||
dslContext | ||
.select(TAGS.ID, TAGS.NAME) | ||
.from(TAGS) | ||
.orderBy(getSortFields(pageable.getSort(), TAGS)) | ||
.limit(pageable.getPageSize()) | ||
.offset(pageable.getOffset()); | ||
var countSql = dslContext.selectCount().from(TAGS); | ||
return Mono.zip( | ||
Flux.from(dataSql).map(r -> new Tags(r.value1(), r.value2())).collectList(), | ||
Mono.from(countSql).map(Record1::value1)) | ||
.map(it -> new PageImpl<>(it.getT1(), pageable, it.getT2())); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...r2dbc-sample/src/main/java/com/example/jooq/r2dbc/repository/custom/impl/JooqSorting.java
This file contains 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,54 @@ | ||
package com.example.jooq.r2dbc.repository.custom.impl; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import org.jooq.SortField; | ||
import org.jooq.TableField; | ||
import org.springframework.dao.InvalidDataAccessApiUsageException; | ||
import org.springframework.data.domain.Sort; | ||
|
||
public class JooqSorting { | ||
|
||
<T> List<SortField<?>> getSortFields(Sort sortSpecification, T tableType) { | ||
List<SortField<?>> querySortFields = new ArrayList<>(); | ||
|
||
if (sortSpecification == null) { | ||
return querySortFields; | ||
} | ||
|
||
for (Sort.Order specifiedField : sortSpecification) { | ||
String sortFieldName = specifiedField.getProperty(); | ||
Sort.Direction sortDirection = specifiedField.getDirection(); | ||
|
||
TableField tableField = getTableField(sortFieldName, tableType); | ||
SortField<?> querySortField = convertTableFieldToSortField(tableField, sortDirection); | ||
querySortFields.add(querySortField); | ||
} | ||
|
||
return querySortFields; | ||
} | ||
|
||
private <T> TableField getTableField(String sortFieldName, T tableType) { | ||
TableField sortField; | ||
try { | ||
Field tableField = | ||
tableType.getClass().getField(sortFieldName.toUpperCase(Locale.ROOT)); | ||
sortField = (TableField) tableField.get(tableType); | ||
} catch (NoSuchFieldException | IllegalAccessException ex) { | ||
String errorMessage = String.format("Could not find table field: %s", sortFieldName); | ||
throw new InvalidDataAccessApiUsageException(errorMessage, ex); | ||
} | ||
return sortField; | ||
} | ||
|
||
private SortField<?> convertTableFieldToSortField( | ||
TableField tableField, Sort.Direction sortDirection) { | ||
if (sortDirection == Sort.Direction.ASC) { | ||
return tableField.asc(); | ||
} else { | ||
return tableField.desc(); | ||
} | ||
} | ||
} |
Oops, something went wrong.