25
25
import org .springframework .data .relational .repository .query .RelationalParameterAccessor ;
26
26
import org .springframework .data .repository .query .Parameter ;
27
27
import org .springframework .data .repository .query .Parameters ;
28
- import org .springframework .data .repository .query .QueryMethodEvaluationContextProvider ;
29
- import org .springframework .expression .EvaluationContext ;
30
- import org .springframework .expression .Expression ;
31
- import org .springframework .expression .spel .standard .SpelExpressionParser ;
32
28
import org .springframework .r2dbc .core .DatabaseClient ;
33
- import org .springframework .util .Assert ;
34
29
35
30
/**
36
31
* {@link ExpressionEvaluatingParameterBinder} allows to evaluate, convert and bind parameters to placeholders within a
41
36
*/
42
37
class ExpressionEvaluatingParameterBinder {
43
38
44
- private final SpelExpressionParser expressionParser ;
45
-
46
- private final QueryMethodEvaluationContextProvider evaluationContextProvider ;
47
-
48
39
private final ExpressionQuery expressionQuery ;
49
40
50
41
private final Map <String , Boolean > namedParameters = new ConcurrentHashMap <>();
51
42
52
43
/**
53
44
* Creates new {@link ExpressionEvaluatingParameterBinder}
54
45
*
55
- * @param expressionParser must not be {@literal null}.
56
- * @param evaluationContextProvider must not be {@literal null}.
57
46
* @param expressionQuery must not be {@literal null}.
58
47
*/
59
- ExpressionEvaluatingParameterBinder (SpelExpressionParser expressionParser ,
60
- QueryMethodEvaluationContextProvider evaluationContextProvider , ExpressionQuery expressionQuery ) {
61
-
62
- Assert .notNull (expressionParser , "ExpressionParser must not be null" );
63
- Assert .notNull (evaluationContextProvider , "EvaluationContextProvider must not be null" );
64
- Assert .notNull (expressionQuery , "ExpressionQuery must not be null" );
65
-
66
- this .expressionParser = expressionParser ;
67
- this .evaluationContextProvider = evaluationContextProvider ;
48
+ ExpressionEvaluatingParameterBinder (ExpressionQuery expressionQuery ) {
68
49
this .expressionQuery = expressionQuery ;
69
50
}
70
51
@@ -74,28 +55,28 @@ class ExpressionEvaluatingParameterBinder {
74
55
*
75
56
* @param bindSpec must not be {@literal null}.
76
57
* @param parameterAccessor must not be {@literal null}.
58
+ * @param evaluator must not be {@literal null}.
77
59
*/
78
- public DatabaseClient .GenericExecuteSpec bind (DatabaseClient .GenericExecuteSpec bindSpec ,
79
- RelationalParameterAccessor parameterAccessor ) {
60
+ DatabaseClient .GenericExecuteSpec bind (DatabaseClient .GenericExecuteSpec bindSpec ,
61
+ RelationalParameterAccessor parameterAccessor , R2dbcSpELExpressionEvaluator evaluator ) {
80
62
81
63
Object [] values = parameterAccessor .getValues ();
82
64
Parameters <?, ?> bindableParameters = parameterAccessor .getBindableParameters ();
83
65
84
- DatabaseClient .GenericExecuteSpec bindSpecToUse = bindExpressions (bindSpec , values , bindableParameters );
66
+ DatabaseClient .GenericExecuteSpec bindSpecToUse = bindExpressions (bindSpec , evaluator );
85
67
bindSpecToUse = bindParameters (bindSpecToUse , parameterAccessor .hasBindableNullValue (), values , bindableParameters );
86
68
87
69
return bindSpecToUse ;
88
70
}
89
71
90
- private DatabaseClient .GenericExecuteSpec bindExpressions (DatabaseClient .GenericExecuteSpec bindSpec , Object [] values ,
91
- Parameters <?, ?> bindableParameters ) {
72
+ private DatabaseClient .GenericExecuteSpec bindExpressions (DatabaseClient .GenericExecuteSpec bindSpec ,
73
+ R2dbcSpELExpressionEvaluator evaluator ) {
92
74
93
75
DatabaseClient .GenericExecuteSpec bindSpecToUse = bindSpec ;
94
76
95
77
for (ParameterBinding binding : expressionQuery .getBindings ()) {
96
78
97
- org .springframework .r2dbc .core .Parameter valueForBinding = getParameterValueForBinding (bindableParameters , values ,
98
- binding );
79
+ org .springframework .r2dbc .core .Parameter valueForBinding = evaluator .evaluate (binding .getExpression ());
99
80
100
81
if (valueForBinding .isEmpty ()) {
101
82
bindSpecToUse = bindSpecToUse .bindNull (binding .getParameterName (), valueForBinding .getType ());
@@ -108,13 +89,11 @@ private DatabaseClient.GenericExecuteSpec bindExpressions(DatabaseClient.Generic
108
89
}
109
90
110
91
private DatabaseClient .GenericExecuteSpec bindParameters (DatabaseClient .GenericExecuteSpec bindSpec ,
111
- boolean bindableNull , Object [] values ,
112
- Parameters <?, ?> bindableParameters ) {
92
+ boolean bindableNull , Object [] values , Parameters <?, ?> bindableParameters ) {
113
93
114
94
DatabaseClient .GenericExecuteSpec bindSpecToUse = bindSpec ;
115
95
int bindingIndex = 0 ;
116
96
117
-
118
97
for (Parameter bindableParameter : bindableParameters ) {
119
98
120
99
Object value = values [bindableParameter .getIndex ()];
@@ -161,37 +140,4 @@ private boolean isNamedParameterUsed(Optional<String> name) {
161
140
});
162
141
}
163
142
164
- /**
165
- * Returns the value to be used for the given {@link ParameterBinding}.
166
- *
167
- * @param parameters must not be {@literal null}.
168
- * @param binding must not be {@literal null}.
169
- * @return the value used for the given {@link ParameterBinding}.
170
- */
171
- private org .springframework .r2dbc .core .Parameter getParameterValueForBinding (Parameters <?, ?> parameters ,
172
- Object [] values ,
173
- ParameterBinding binding ) {
174
- return evaluateExpression (binding .getExpression (), parameters , values );
175
- }
176
-
177
- /**
178
- * Evaluates the given {@code expressionString}.
179
- *
180
- * @param expressionString must not be {@literal null} or empty.
181
- * @param parameters must not be {@literal null}.
182
- * @param parameterValues must not be {@literal null}.
183
- * @return the value of the {@code expressionString} evaluation.
184
- */
185
- private org .springframework .r2dbc .core .Parameter evaluateExpression (String expressionString ,
186
- Parameters <?, ?> parameters ,
187
- Object [] parameterValues ) {
188
-
189
- EvaluationContext evaluationContext = evaluationContextProvider .getEvaluationContext (parameters , parameterValues );
190
- Expression expression = expressionParser .parseExpression (expressionString );
191
-
192
- Object value = expression .getValue (evaluationContext , Object .class );
193
- Class <?> valueType = expression .getValueType (evaluationContext );
194
-
195
- return org .springframework .r2dbc .core .Parameter .fromOrEmpty (value , valueType != null ? valueType : Object .class );
196
- }
197
143
}
0 commit comments