21
21
import java .util .List ;
22
22
23
23
import org .reactivestreams .Publisher ;
24
-
24
+ import org . springframework . data . domain . Example ;
25
25
import org .springframework .data .domain .Sort ;
26
26
import org .springframework .data .r2dbc .convert .R2dbcConverter ;
27
27
import org .springframework .data .r2dbc .core .R2dbcEntityOperations ;
28
28
import org .springframework .data .r2dbc .core .R2dbcEntityTemplate ;
29
29
import org .springframework .data .r2dbc .core .ReactiveDataAccessStrategy ;
30
+ import org .springframework .data .r2dbc .repository .R2dbcRepository ;
30
31
import org .springframework .data .relational .core .mapping .RelationalPersistentProperty ;
31
32
import org .springframework .data .relational .core .query .Criteria ;
32
33
import org .springframework .data .relational .core .query .Query ;
33
34
import org .springframework .data .relational .repository .query .RelationalEntityInformation ;
35
+ import org .springframework .data .relational .repository .query .RelationalExampleMapper ;
34
36
import org .springframework .data .repository .reactive .ReactiveSortingRepository ;
35
37
import org .springframework .data .util .Lazy ;
36
38
import org .springframework .data .util .Streamable ;
45
47
* @author Jens Schauder
46
48
* @author Mingyuan Wu
47
49
* @author Stephen Cohen
50
+ * @author Greg Turnquist
48
51
*/
49
52
@ Transactional (readOnly = true )
50
- public class SimpleR2dbcRepository <T , ID > implements ReactiveSortingRepository <T , ID > {
53
+ public class SimpleR2dbcRepository <T , ID > implements R2dbcRepository <T , ID > {
51
54
52
55
private final RelationalEntityInformation <T , ID > entity ;
53
56
private final R2dbcEntityOperations entityOperations ;
54
57
private final Lazy <RelationalPersistentProperty > idProperty ;
58
+ private final RelationalExampleMapper exampleMapper ;
55
59
56
60
/**
57
61
* Create a new {@link SimpleR2dbcRepository}.
@@ -70,6 +74,7 @@ public SimpleR2dbcRepository(RelationalEntityInformation<T, ID> entity, R2dbcEnt
70
74
.getMappingContext () //
71
75
.getRequiredPersistentEntity (this .entity .getJavaType ()) //
72
76
.getRequiredIdProperty ());
77
+ this .exampleMapper = new RelationalExampleMapper (converter .getMappingContext ());
73
78
}
74
79
75
80
/**
@@ -90,6 +95,7 @@ public SimpleR2dbcRepository(RelationalEntityInformation<T, ID> entity, Database
90
95
.getMappingContext () //
91
96
.getRequiredPersistentEntity (this .entity .getJavaType ()) //
92
97
.getRequiredIdProperty ());
98
+ this .exampleMapper = new RelationalExampleMapper (converter .getMappingContext ());
93
99
}
94
100
95
101
/**
@@ -112,6 +118,7 @@ public SimpleR2dbcRepository(RelationalEntityInformation<T, ID> entity,
112
118
.getMappingContext () //
113
119
.getRequiredPersistentEntity (this .entity .getJavaType ()) //
114
120
.getRequiredIdProperty ());
121
+ this .exampleMapper = new RelationalExampleMapper (converter .getMappingContext ());
115
122
}
116
123
117
124
// -------------------------------------------------------------------------
@@ -372,6 +379,59 @@ public Flux<T> findAll(Sort sort) {
372
379
return this .entityOperations .select (Query .empty ().sort (sort ), this .entity .getJavaType ());
373
380
}
374
381
382
+ // -------------------------------------------------------------------------
383
+ // Methods from ReactiveQueryByExampleExecutor
384
+ // -------------------------------------------------------------------------
385
+
386
+ @ Override
387
+ public <S extends T > Mono <S > findOne (Example <S > example ) {
388
+
389
+ Assert .notNull (example , "Example must not be null!" );
390
+
391
+ Query query = this .exampleMapper .getMappedExample (example );
392
+
393
+ return this .entityOperations .selectOne (query , example .getProbeType ());
394
+ }
395
+
396
+ @ Override
397
+ public <S extends T > Flux <S > findAll (Example <S > example ) {
398
+
399
+ Assert .notNull (example , "Example must not be null!" );
400
+
401
+ return findAll (example , Sort .unsorted ());
402
+ }
403
+
404
+ @ Override
405
+ public <S extends T > Flux <S > findAll (Example <S > example , Sort sort ) {
406
+
407
+ Assert .notNull (example , "Example must not be null!" );
408
+ Assert .notNull (sort , "Sort must not be null!" );
409
+
410
+ Query query = this .exampleMapper .getMappedExample (example ).sort (sort );
411
+
412
+ return this .entityOperations .select (query , example .getProbeType ());
413
+ }
414
+
415
+ @ Override
416
+ public <S extends T > Mono <Long > count (Example <S > example ) {
417
+
418
+ Assert .notNull (example , "Example must not be null!" );
419
+
420
+ Query query = this .exampleMapper .getMappedExample (example );
421
+
422
+ return this .entityOperations .count (query , example .getProbeType ());
423
+ }
424
+
425
+ @ Override
426
+ public <S extends T > Mono <Boolean > exists (Example <S > example ) {
427
+
428
+ Assert .notNull (example , "Example must not be null!" );
429
+
430
+ Query query = this .exampleMapper .getMappedExample (example );
431
+
432
+ return this .entityOperations .exists (query , example .getProbeType ());
433
+ }
434
+
375
435
private RelationalPersistentProperty getIdProperty () {
376
436
return this .idProperty .get ();
377
437
}
0 commit comments