15
15
*/
16
16
package org .springframework .data .repository .query ;
17
17
18
+ import org .springframework .data .domain .Range ;
19
+ import org .springframework .data .domain .Range .Bound ;
20
+ import org .springframework .lang .Nullable ;
21
+ import org .springframework .util .Assert ;
22
+
18
23
import java .util .ArrayList ;
19
24
import java .util .Arrays ;
20
25
import java .util .Collection ;
28
33
import java .util .regex .Pattern ;
29
34
import java .util .stream .Stream ;
30
35
31
- import org .springframework .data .domain .Range ;
32
- import org .springframework .data .domain .Range .Bound ;
33
- import org .springframework .lang .Nullable ;
34
- import org .springframework .util .Assert ;
35
-
36
36
/**
37
- * Source of {@link SpelExtractor} encapsulating configuration often common for all queries.
37
+ * A {@literal SpelQueryContext} is able to find SpEL expressions in a query string and to replace them with bind variables.
38
+ * <p>
39
+ * Result o the parse process is a {@link SpelExtractor} which offers the transformed query string.
40
+ * Alternatively and preferred one may provide a {@link QueryMethodEvaluationContextProvider} via
41
+ * {@link #withEvaluationContextProvider(QueryMethodEvaluationContextProvider)} which will yield the more powerful
42
+ * {@link EvaluatingSpelQueryContext}.
43
+ * <p>
44
+ * Typical usage looks like
45
+ * <pre><code>
46
+ SpelQueryContext.EvaluatingSpelQueryContext queryContext = SpelQueryContext
47
+ .of((counter, expression) -> String.format("__$synthetic$__%d", counter), String::concat)
48
+ .withEvaluationContextProvider(evaluationContextProvider);
49
+
50
+ SpelEvaluator spelEvaluator = queryContext.parse(query, queryMethod.getParameters());
51
+
52
+ spelEvaluator.evaluate(objects).forEach(parameterMap::addValue);
53
+ * </code></pre>
38
54
*
39
55
* @author Jens Schauder
40
56
* @author Gerrit Meier
@@ -63,7 +79,7 @@ public class SpelQueryContext {
63
79
private final BiFunction <String , String , String > replacementSource ;
64
80
65
81
private SpelQueryContext (BiFunction <Integer , String , String > parameterNameSource ,
66
- BiFunction <String , String , String > replacementSource ) {
82
+ BiFunction <String , String , String > replacementSource ) {
67
83
68
84
Assert .notNull (parameterNameSource , "Parameter name source must not be null" );
69
85
Assert .notNull (replacementSource , "Replacement source must not be null" );
@@ -73,7 +89,7 @@ private SpelQueryContext(BiFunction<Integer, String, String> parameterNameSource
73
89
}
74
90
75
91
public static SpelQueryContext of (BiFunction <Integer , String , String > parameterNameSource ,
76
- BiFunction <String , String , String > replacementSource ) {
92
+ BiFunction <String , String , String > replacementSource ) {
77
93
return new SpelQueryContext (parameterNameSource , replacementSource );
78
94
}
79
95
@@ -83,13 +99,13 @@ public static SpelQueryContext of(BiFunction<Integer, String, String> parameterN
83
99
* <pre>
84
100
* <prefix>#{<spel>}
85
101
* </pre>
86
- *
102
+ * <p>
87
103
* with prefix being the character ':' or '?'. Parsing honors quoted {@literal String}s enclosed in single or double
88
104
* quotation marks.
89
105
*
90
106
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
91
107
* @return A {@link SpelExtractor} which makes the query with SpEL expressions replaced by bind parameters and a map
92
- * from bind parameter to SpEL expression available. Guaranteed to be not {@literal null}.
108
+ * from bind parameter to SpEL expression available. Guaranteed to be not {@literal null}.
93
109
*/
94
110
public SpelExtractor parse (String query ) {
95
111
return new SpelExtractor (query );
@@ -125,11 +141,11 @@ public static class EvaluatingSpelQueryContext extends SpelQueryContext {
125
141
* parameter name source and replacement source.
126
142
*
127
143
* @param evaluationContextProvider must not be {@literal null}.
128
- * @param parameterNameSource must not be {@literal null}.
129
- * @param replacementSource must not be {@literal null}.
144
+ * @param parameterNameSource must not be {@literal null}.
145
+ * @param replacementSource must not be {@literal null}.
130
146
*/
131
147
private EvaluatingSpelQueryContext (QueryMethodEvaluationContextProvider evaluationContextProvider ,
132
- BiFunction <Integer , String , String > parameterNameSource , BiFunction <String , String , String > replacementSource ) {
148
+ BiFunction <Integer , String , String > parameterNameSource , BiFunction <String , String , String > replacementSource ) {
133
149
134
150
super (parameterNameSource , replacementSource );
135
151
@@ -142,11 +158,11 @@ private EvaluatingSpelQueryContext(QueryMethodEvaluationContextProvider evaluati
142
158
* <pre>
143
159
* <prefix>#{<spel>}
144
160
* </pre>
145
- *
161
+ * <p>
146
162
* with prefix being the character ':' or '?'. Parsing honors quoted {@literal String}s enclosed in single or double
147
163
* quotation marks.
148
164
*
149
- * @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
165
+ * @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
150
166
* @param parameters a {@link Parameters} instance describing query method parameters
151
167
* @return A {@link SpelEvaluator} which allows to evaluate the SpEL expressions. Will never be {@literal null}.
152
168
*/
0 commit comments