findAll(CriteriaDefinition criteria);
27 |
28 | /**
29 | * @see R2dbcEntityTemplate#select(Class)
30 | */
31 | Mono
findOne(CriteriaDefinition criteria, Class
projection);
32 |
33 | /**
34 | * @see R2dbcEntityTemplate#select(Class)
35 | */
36 |
Flux
findAll(CriteriaDefinition criteria, Class
projection);
37 |
38 | /**
39 | * @see R2dbcEntityTemplate#count(Query, Class)
40 | */
41 | Mono count(CriteriaDefinition criteria);
42 |
43 | /**
44 | * @see R2dbcEntityTemplate#select(Query, Class)
45 | */
46 | Mono> findAll(CriteriaDefinition criteria, Pageable pageable);
47 |
48 | /**
49 | * @see R2dbcEntityTemplate#select(Query, Class)
50 | */
51 | Mono> findAll(CriteriaDefinition criteria, Pageable pageable, Class projection);
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/spring-data-criteria-r2dbc/src/main/java/io/github/holmofy/data/r2dbc/EnhancedR2dbcRepository.java:
--------------------------------------------------------------------------------
1 | package io.github.holmofy.data.r2dbc;
2 |
3 | import org.springframework.data.domain.Page;
4 | import org.springframework.data.domain.PageImpl;
5 | import org.springframework.data.domain.Pageable;
6 | import org.springframework.data.r2dbc.convert.R2dbcConverter;
7 | import org.springframework.data.r2dbc.core.R2dbcEntityOperations;
8 | import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
9 | import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
10 | import org.springframework.data.r2dbc.repository.support.SimpleR2dbcRepository;
11 | import org.springframework.data.relational.core.query.CriteriaDefinition;
12 | import org.springframework.data.relational.core.query.Query;
13 | import org.springframework.data.relational.repository.query.RelationalEntityInformation;
14 | import org.springframework.r2dbc.core.DatabaseClient;
15 | import reactor.core.publisher.Flux;
16 | import reactor.core.publisher.Mono;
17 |
18 | import java.util.List;
19 |
20 | public class EnhancedR2dbcRepository extends SimpleR2dbcRepository implements CriteriaExecutor, R2dbcSupport {
21 |
22 | private final RelationalEntityInformation entity;
23 | private final R2dbcEntityOperations entityOperations;
24 |
25 | public EnhancedR2dbcRepository(RelationalEntityInformation entity,
26 | R2dbcEntityOperations entityOperations,
27 | R2dbcConverter converter) {
28 | super(entity, entityOperations, converter);
29 | this.entity = entity;
30 | this.entityOperations = entityOperations;
31 | }
32 |
33 | public EnhancedR2dbcRepository(RelationalEntityInformation entity,
34 | DatabaseClient databaseClient,
35 | R2dbcConverter converter,
36 | ReactiveDataAccessStrategy accessStrategy) {
37 | this(entity, new R2dbcEntityTemplate(databaseClient, accessStrategy), converter);
38 | }
39 |
40 | @Override
41 | public Mono findOne(CriteriaDefinition criteria) {
42 | return entityOperations.selectOne(Query.query(criteria), entity.getJavaType());
43 | }
44 |
45 | @Override
46 | public Flux findAll(CriteriaDefinition criteria) {
47 | return entityOperations.select(Query.query(criteria), entity.getJavaType());
48 | }
49 |
50 | @Override
51 | public Mono
findOne(CriteriaDefinition criteria, Class
projection) {
52 | return entityOperations.select(entity.getJavaType())
53 | .from(entity.getTableName())
54 | .as(projection)
55 | .matching(Query.query(criteria))
56 | .one();
57 | }
58 |
59 | @Override
60 | public
Flux
findAll(CriteriaDefinition criteria, Class
projection) {
61 | return entityOperations.select(entity.getJavaType())
62 | .from(entity.getTableName())
63 | .as(projection)
64 | .matching(Query.query(criteria))
65 | .all();
66 | }
67 |
68 | @Override
69 | public Mono count(CriteriaDefinition criteria) {
70 | return entityOperations.count(Query.query(criteria), entity.getJavaType());
71 | }
72 |
73 | @Override
74 | public Mono> findAll(CriteriaDefinition criteria, Pageable pageable) {
75 | Query query = Query.query(criteria);
76 | Mono count = entityOperations.count(query, entity.getJavaType());
77 | Mono> list = entityOperations.select(query.with(pageable), entity.getJavaType())
78 | .collectList();
79 | return Mono.zip(list, count).map(tuple -> new PageImpl<>(tuple.getT1(), pageable, tuple.getT2()));
80 | }
81 |
82 | @Override
83 | public Mono> findAll(CriteriaDefinition criteria, Pageable pageable, Class projection) {
84 | Query query = Query.query(criteria);
85 | Mono count = entityOperations.count(query, entity.getJavaType());
86 | Mono> list = entityOperations.select(entity.getJavaType())
87 | .from(entity.getTableName())
88 | .as(projection)
89 | .matching(query.with(pageable))
90 | .all()
91 | .collectList();
92 | return Mono.zip(list, count).map(tuple -> new PageImpl<>(tuple.getT1(), pageable, tuple.getT2()));
93 | }
94 |
95 | @Override
96 | public R2dbcEntityOperations r2dbcTemplate() {
97 | return entityOperations;
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/spring-data-criteria-r2dbc/src/main/java/io/github/holmofy/data/r2dbc/R2dbcSupport.java:
--------------------------------------------------------------------------------
1 | package io.github.holmofy.data.r2dbc;
2 |
3 | import org.springframework.data.r2dbc.core.R2dbcEntityOperations;
4 | import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
5 |
6 | public interface R2dbcSupport {
7 |
8 | /**
9 | * @return R2dbcEntityTemplate
10 | * @see R2dbcEntityTemplate
11 | */
12 | R2dbcEntityOperations r2dbcTemplate();
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/spring-data-criteria-relational/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | spring-data-criteria
7 | io.github.holmofy
8 | ${reversion}
9 |
10 | 4.0.0
11 |
12 | spring-data-criteria-relational
13 |
14 |
15 |
16 | org.springframework.data
17 | spring-data-relational
18 |
19 |
20 | org.projectlombok
21 | lombok
22 | provided
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/spring-data-criteria-relational/src/main/java/io/github/holmofy/data/relational/MoreCriteria.java:
--------------------------------------------------------------------------------
1 | package io.github.holmofy.data.relational;
2 |
3 | import lombok.experimental.UtilityClass;
4 | import org.springframework.data.relational.core.query.Criteria;
5 | import org.springframework.util.CollectionUtils;
6 |
7 | import java.util.Arrays;
8 | import java.util.Collection;
9 |
10 | @UtilityClass
11 | public class MoreCriteria {
12 |
13 | public static Criteria isNull(String column) {
14 | return Criteria.where(column).isNull();
15 | }
16 |
17 | public static Criteria isNotNull(String column) {
18 | return Criteria.where(column).isNotNull();
19 | }
20 |
21 | public static Criteria eq(String column, T obj) {
22 | return obj == null ? Criteria.empty() : Criteria.where(column).is(obj);
23 | }
24 |
25 | public static Criteria lt(String column, T obj) {
26 | return obj == null ? Criteria.empty() : Criteria.where(column).lessThan(obj);
27 | }
28 |
29 | public static Criteria lte(String column, T obj) {
30 | return obj == null ? Criteria.empty() : Criteria.where(column).lessThanOrEquals(obj);
31 | }
32 |
33 | public static Criteria gt(String column, T obj) {
34 | return obj == null ? Criteria.empty() : Criteria.where(column).greaterThan(obj);
35 | }
36 |
37 | public static Criteria gte(String column, T obj) {
38 | return obj == null ? Criteria.empty() : Criteria.where(column).greaterThanOrEquals(obj);
39 | }
40 |
41 | public static Criteria between(String column, T begin, T end) {
42 | if (begin == null && end == null) {
43 | return Criteria.empty();
44 | }
45 | if (begin != null && end != null) {
46 | return Criteria.where(column).between(begin, end);
47 | }
48 | return begin == null ? lte(column, end) : gte(column, begin);
49 | }
50 |
51 | public static Criteria like(String column, T obj) {
52 | return obj == null || obj.length() == 0 ? Criteria.empty() : Criteria.where(column).like(obj);
53 | }
54 |
55 | public static Criteria startWith(String column, T obj) {
56 | return obj == null || obj.length() == 0 ? Criteria.empty() : Criteria.where(column).like(escapeLikeClause(obj) + "%");
57 | }
58 |
59 | @SafeVarargs
60 | public static Criteria in(String column, T... objs) {
61 | return objs == null ? Criteria.empty() : Criteria.where(column).in(Arrays.asList(objs));
62 | }
63 |
64 | @SafeVarargs
65 | public static Criteria notIn(String column, T... objs) {
66 | return objs == null ? Criteria.empty() : Criteria.where(column).notIn(Arrays.asList(objs));
67 | }
68 |
69 | public static Criteria in(String column, Collection collection) {
70 | return CollectionUtils.isEmpty(collection) ? Criteria.empty() : Criteria.where(column).in(collection);
71 | }
72 |
73 | public static Criteria notIn(String column, Collection collection) {
74 | return CollectionUtils.isEmpty(collection) ? Criteria.empty() : Criteria.where(column).notIn(collection);
75 | }
76 |
77 | private static String escapeLikeClause(CharSequence expression) {
78 | return expression.toString().replace("%", "\\%").replace("_", "\\_");
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------