Skip to content

Commit dbe935c

Browse files
rchigvintsevmp911de
authored andcommitted
#282 - Add support for query derivation.
We now support query derivation for R2DBC repositories: interface ReactivePersonRepository extends ReactiveSortingRepository<Person, String> { Flux<Person> findByFirstname(String firstname); Flux<Person> findByFirstname(Publisher<String> firstname); Mono<Person> findByFirstnameAndLastname(String firstname, String lastname); Flux<Person> findFirstByLastnameLike(String pattern); } Original pull request: #295.
1 parent 6dcd878 commit dbe935c

16 files changed

+1933
-22
lines changed

Diff for: src/main/java/org/springframework/data/r2dbc/core/DefaultStatementMapper.java

+18
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ public PreparedOperation<Delete> getMappedObject(DeleteSpec deleteSpec) {
226226
return getMappedObject(deleteSpec, null);
227227
}
228228

229+
/*
230+
* (non-Javadoc)
231+
* @see org.springframework.data.r2dbc.function.StatementMapper#getRenderContext()
232+
*/
233+
@Override
234+
public RenderContext getRenderContext() {
235+
return renderContext;
236+
}
237+
229238
private PreparedOperation<Delete> getMappedObject(DeleteSpec deleteSpec,
230239
@Nullable RelationalPersistentEntity<?> entity) {
231240

@@ -375,5 +384,14 @@ public PreparedOperation<?> getMappedObject(UpdateSpec updateSpec) {
375384
public PreparedOperation<?> getMappedObject(DeleteSpec deleteSpec) {
376385
return DefaultStatementMapper.this.getMappedObject(deleteSpec, this.entity);
377386
}
387+
388+
/*
389+
* (non-Javadoc)
390+
* @see org.springframework.data.r2dbc.function.StatementMapper#getRenderContext()
391+
*/
392+
@Override
393+
public RenderContext getRenderContext() {
394+
return DefaultStatementMapper.this.getRenderContext();
395+
}
378396
}
379397
}

Diff for: src/main/java/org/springframework/data/r2dbc/core/StatementMapper.java

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.data.relational.core.sql.Expression;
3535
import org.springframework.data.relational.core.sql.SqlIdentifier;
3636
import org.springframework.data.relational.core.sql.Table;
37+
import org.springframework.data.relational.core.sql.render.RenderContext;
3738
import org.springframework.lang.Nullable;
3839

3940
/**
@@ -177,6 +178,16 @@ default DeleteSpec createDelete(SqlIdentifier table) {
177178
return DeleteSpec.create(table);
178179
}
179180

181+
/**
182+
* Returns {@link RenderContext}.
183+
*
184+
* @return {@link RenderContext} instance or {@literal null} if {@link RenderContext} is not available
185+
*/
186+
@Nullable
187+
default RenderContext getRenderContext() {
188+
return null;
189+
}
190+
180191
/**
181192
* {@code SELECT} specification.
182193
*/

Diff for: src/main/java/org/springframework/data/r2dbc/query/Criteria.java

+83-2
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,28 @@ public class Criteria {
5757
private final @Nullable SqlIdentifier column;
5858
private final @Nullable Comparator comparator;
5959
private final @Nullable Object value;
60+
private final boolean ignoreCase;
6061

6162
private Criteria(SqlIdentifier column, Comparator comparator, @Nullable Object value) {
62-
this(null, Combinator.INITIAL, Collections.emptyList(), column, comparator, value);
63+
this(null, Combinator.INITIAL, Collections.emptyList(), column, comparator, value, false);
6364
}
6465

6566
private Criteria(@Nullable Criteria previous, Combinator combinator, List<Criteria> group,
6667
@Nullable SqlIdentifier column, @Nullable Comparator comparator, @Nullable Object value) {
68+
this(previous, combinator, group, column, comparator, value, false);
69+
}
70+
71+
private Criteria(@Nullable Criteria previous, Combinator combinator, List<Criteria> group,
72+
@Nullable SqlIdentifier column, @Nullable Comparator comparator, @Nullable Object value,
73+
boolean ignoreCase) {
6774

6875
this.previous = previous;
6976
this.combinator = previous != null && previous.isEmpty() ? Combinator.INITIAL : combinator;
7077
this.group = group;
7178
this.column = column;
7279
this.comparator = comparator;
7380
this.value = value;
81+
this.ignoreCase = ignoreCase;
7482
}
7583

7684
private Criteria(@Nullable Criteria previous, Combinator combinator, List<Criteria> group) {
@@ -81,6 +89,7 @@ private Criteria(@Nullable Criteria previous, Combinator combinator, List<Criter
8189
this.column = null;
8290
this.comparator = null;
8391
this.value = null;
92+
this.ignoreCase = false;
8493
}
8594

8695
/**
@@ -236,6 +245,19 @@ public Criteria or(List<Criteria> criteria) {
236245
return new Criteria(Criteria.this, Combinator.OR, criteria);
237246
}
238247

248+
/**
249+
* Creates a new {@link Criteria} with the given "ignore case" flag.
250+
*
251+
* @param ignoreCase {@literal true} if comparison should be done in case-insensitive way
252+
* @return a new {@link Criteria} object
253+
*/
254+
public Criteria ignoreCase(boolean ignoreCase) {
255+
if (this.ignoreCase != ignoreCase) {
256+
return new Criteria(previous, combinator, group, column, comparator, value, ignoreCase);
257+
}
258+
return this;
259+
}
260+
239261
/**
240262
* @return the previous {@link Criteria} object. Can be {@literal null} if there is no previous {@link Criteria}.
241263
* @see #hasPrevious()
@@ -338,8 +360,17 @@ Object getValue() {
338360
return value;
339361
}
340362

363+
/**
364+
* Checks whether comparison should be done in case-insensitive way.
365+
*
366+
* @return {@literal true} if comparison should be done in case-insensitive way
367+
*/
368+
boolean isIgnoreCase() {
369+
return ignoreCase;
370+
}
371+
341372
enum Comparator {
342-
INITIAL, EQ, NEQ, LT, LTE, GT, GTE, IS_NULL, IS_NOT_NULL, LIKE, NOT_IN, IN,
373+
INITIAL, EQ, NEQ, LT, LTE, GT, GTE, IS_NULL, IS_NOT_NULL, LIKE, NOT_LIKE, NOT_IN, IN, IS_TRUE, IS_FALSE
343374
}
344375

345376
enum Combinator {
@@ -428,6 +459,14 @@ public interface CriteriaStep {
428459
*/
429460
Criteria like(Object value);
430461

462+
/**
463+
* Creates a {@link Criteria} using {@code NOT LIKE}.
464+
*
465+
* @param value must not be {@literal null}
466+
* @return a new {@link Criteria} object
467+
*/
468+
Criteria notLike(Object value);
469+
431470
/**
432471
* Creates a {@link Criteria} using {@code IS NULL}.
433472
*/
@@ -437,6 +476,20 @@ public interface CriteriaStep {
437476
* Creates a {@link Criteria} using {@code IS NOT NULL}.
438477
*/
439478
Criteria isNotNull();
479+
480+
/**
481+
* Creates a {@link Criteria} using {@code IS TRUE}.
482+
*
483+
* @return a new {@link Criteria} object
484+
*/
485+
Criteria isTrue();
486+
487+
/**
488+
* Creates a {@link Criteria} using {@code IS FALSE}.
489+
*
490+
* @return a new {@link Criteria} object
491+
*/
492+
Criteria isFalse();
440493
}
441494

442495
/**
@@ -596,6 +649,16 @@ public Criteria like(Object value) {
596649
return createCriteria(Comparator.LIKE, value);
597650
}
598651

652+
/*
653+
* (non-Javadoc)
654+
* @see org.springframework.data.r2dbc.function.query.Criteria.CriteriaStep#notLike(java.lang.Object)
655+
*/
656+
@Override
657+
public Criteria notLike(Object value) {
658+
Assert.notNull(value, "Value must not be null!");
659+
return createCriteria(Comparator.NOT_LIKE, value);
660+
}
661+
599662
/*
600663
* (non-Javadoc)
601664
* @see org.springframework.data.r2dbc.function.query.Criteria.CriteriaStep#isNull()
@@ -614,6 +677,24 @@ public Criteria isNotNull() {
614677
return createCriteria(Comparator.IS_NOT_NULL, null);
615678
}
616679

680+
/*
681+
* (non-Javadoc)
682+
* @see org.springframework.data.r2dbc.function.query.Criteria.CriteriaStep#isTrue()
683+
*/
684+
@Override
685+
public Criteria isTrue() {
686+
return createCriteria(Comparator.IS_TRUE, null);
687+
}
688+
689+
/*
690+
* (non-Javadoc)
691+
* @see org.springframework.data.r2dbc.function.query.Criteria.CriteriaStep#isFalse()
692+
*/
693+
@Override
694+
public Criteria isFalse() {
695+
return createCriteria(Comparator.IS_FALSE, null);
696+
}
697+
617698
protected Criteria createCriteria(Comparator comparator, Object value) {
618699
return new Criteria(this.property, comparator, value);
619700
}

0 commit comments

Comments
 (0)