Skip to content

Commit 4e5bf95

Browse files
committed
#282 - Polishing.
Move query derivation infrastructure to Spring Data Relational. Adapt to newly introduced ValueFunction for deferred value mapping. Use query derivation in integration tests. Tweak javadoc, add since and author tags, reformat code. Related ticket: https://jira.spring.io/browse/DATAJDBC-514 Original pull request: #295.
1 parent a9a3919 commit 4e5bf95

20 files changed

+317
-890
lines changed

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

+52-1
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,23 @@
3434
import org.springframework.data.r2dbc.dialect.MutableBindings;
3535
import org.springframework.data.r2dbc.dialect.R2dbcDialect;
3636
import org.springframework.data.r2dbc.mapping.SettableValue;
37+
import org.springframework.data.relational.core.dialect.Escaper;
3738
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
3839
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
3940
import org.springframework.data.relational.core.query.CriteriaDefinition;
4041
import org.springframework.data.relational.core.query.CriteriaDefinition.Comparator;
42+
import org.springframework.data.relational.core.query.ValueFunction;
4143
import org.springframework.data.relational.core.sql.*;
4244
import org.springframework.data.util.ClassTypeInformation;
45+
import org.springframework.data.util.Pair;
4346
import org.springframework.data.util.TypeInformation;
4447
import org.springframework.lang.Nullable;
4548
import org.springframework.util.Assert;
4649
import org.springframework.util.ClassUtils;
4750

4851
/**
49-
* Maps {@link Criteria} and {@link Sort} objects considering mapping metadata and dialect-specific conversion.
52+
* Maps {@link CriteriaDefinition} and {@link Sort} objects considering mapping metadata and dialect-specific
53+
* conversion.
5054
*
5155
* @author Mark Paluch
5256
* @author Roman Chigvintsev
@@ -344,7 +348,13 @@ private Condition mapCondition(CriteriaDefinition criteria, MutableBindings bind
344348

345349
mappedValue = convertValue(settableValue.getValue(), propertyField.getTypeHint());
346350
typeHint = getTypeHint(mappedValue, actualType.getType(), settableValue);
351+
} else if (criteria.getValue() instanceof ValueFunction) {
347352

353+
ValueFunction<Object> valueFunction = (ValueFunction<Object>) criteria.getValue();
354+
Object value = valueFunction.apply(getEscaper(criteria.getComparator()));
355+
356+
mappedValue = convertValue(value, propertyField.getTypeHint());
357+
typeHint = actualType.getType();
348358
} else {
349359

350360
mappedValue = convertValue(criteria.getValue(), propertyField.getTypeHint());
@@ -354,6 +364,15 @@ private Condition mapCondition(CriteriaDefinition criteria, MutableBindings bind
354364
return createCondition(column, mappedValue, typeHint, bindings, criteria.getComparator(), criteria.isIgnoreCase());
355365
}
356366

367+
private Escaper getEscaper(Comparator comparator) {
368+
369+
if (comparator == Comparator.LIKE || comparator == Comparator.NOT_LIKE) {
370+
return dialect.getLikeEscaper();
371+
}
372+
373+
return Escaper.DEFAULT;
374+
}
375+
357376
/**
358377
* Potentially convert the {@link SettableValue}.
359378
*
@@ -376,6 +395,21 @@ protected Object convertValue(@Nullable Object value, TypeInformation<?> typeInf
376395
return null;
377396
}
378397

398+
if (value instanceof Pair) {
399+
400+
Pair<Object, Object> pair = (Pair<Object, Object>) value;
401+
402+
Object first = convertValue(pair.getFirst(),
403+
typeInformation.getActualType() != null ? typeInformation.getRequiredActualType()
404+
: ClassTypeInformation.OBJECT);
405+
406+
Object second = convertValue(pair.getSecond(),
407+
typeInformation.getActualType() != null ? typeInformation.getRequiredActualType()
408+
: ClassTypeInformation.OBJECT);
409+
410+
return Pair.of(first, second);
411+
}
412+
379413
if (value instanceof Iterable) {
380414

381415
List<Object> mapped = new ArrayList<>();
@@ -456,6 +490,19 @@ private Condition createCondition(Column column, @Nullable Object mappedValue, C
456490
return condition;
457491
}
458492

493+
if (comparator == Comparator.BETWEEN || comparator == Comparator.NOT_BETWEEN) {
494+
495+
Pair<Object, Object> pair = (Pair<Object, Object>) mappedValue;
496+
497+
Expression begin = bind(pair.getFirst(), valueType, bindings,
498+
bindings.nextMarker(column.getName().getReference()), ignoreCase);
499+
Expression end = bind(mappedValue, valueType, bindings, bindings.nextMarker(column.getName().getReference()),
500+
ignoreCase);
501+
502+
return comparator == Comparator.BETWEEN ? Conditions.between(columnExpression, begin, end)
503+
: Conditions.notBetween(columnExpression, begin, end);
504+
}
505+
459506
BindMarker bindMarker = bindings.nextMarker(column.getName().getReference());
460507

461508
switch (comparator) {
@@ -505,6 +552,10 @@ Field createPropertyField(@Nullable RelationalPersistentEntity<?> entity, SqlIde
505552
return entity == null ? new Field(key) : new MetadataBackedField(key, entity, mappingContext);
506553
}
507554

555+
Class<?> getTypeHint(@Nullable Object mappedValue, Class<?> propertyType) {
556+
return propertyType;
557+
}
558+
508559
Class<?> getTypeHint(@Nullable Object mappedValue, Class<?> propertyType, SettableValue settableValue) {
509560

510561
if (mappedValue == null || propertyType.equals(Object.class)) {

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

+13
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import org.springframework.data.r2dbc.dialect.MutableBindings;
2727
import org.springframework.data.r2dbc.dialect.R2dbcDialect;
2828
import org.springframework.data.r2dbc.mapping.SettableValue;
29+
import org.springframework.data.relational.core.dialect.Escaper;
2930
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
31+
import org.springframework.data.relational.core.query.ValueFunction;
3032
import org.springframework.data.relational.core.sql.AssignValue;
3133
import org.springframework.data.relational.core.sql.Assignment;
3234
import org.springframework.data.relational.core.sql.Assignments;
@@ -134,6 +136,17 @@ private Assignment getAssignment(SqlIdentifier columnName, Object value, Mutable
134136
mappedValue = convertValue(settableValue.getValue(), propertyField.getTypeHint());
135137
typeHint = getTypeHint(mappedValue, actualType.getType(), settableValue);
136138

139+
} else if (value instanceof ValueFunction) {
140+
141+
ValueFunction<Object> valueFunction = (ValueFunction<Object>) value;
142+
143+
mappedValue = convertValue(valueFunction.apply(Escaper.DEFAULT), propertyField.getTypeHint());
144+
145+
if (mappedValue == null) {
146+
return Assignments.value(column, SQL.nullLiteral());
147+
}
148+
149+
typeHint = actualType.getType();
137150
} else {
138151

139152
mappedValue = convertValue(value, propertyField.getTypeHint());

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

-167
This file was deleted.

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

-71
This file was deleted.

0 commit comments

Comments
 (0)