Skip to content

Commit 9207e22

Browse files
committed
Return the number of SpEL expressions created by SpelExtractor.
Closes #2885
1 parent e3fbc9b commit 9207e22

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

src/main/java/org/springframework/data/repository/query/SpelQueryContext.java

+32-17
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
*/
1616
package org.springframework.data.repository.query;
1717

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-
2318
import java.util.ArrayList;
2419
import java.util.Arrays;
2520
import java.util.Collection;
@@ -33,24 +28,33 @@
3328
import java.util.regex.Pattern;
3429
import java.util.stream.Stream;
3530

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+
3636
/**
37-
* A {@literal SpelQueryContext} is able to find SpEL expressions in a query string and to replace them with bind variables.
37+
* A {@literal SpelQueryContext} is able to find SpEL expressions in a query string and to replace them with bind
38+
* variables.
3839
* <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
40+
* Result o the parse process is a {@link SpelExtractor} which offers the transformed query string. Alternatively and
41+
* preferred one may provide a {@link QueryMethodEvaluationContextProvider} via
4142
* {@link #withEvaluationContextProvider(QueryMethodEvaluationContextProvider)} which will yield the more powerful
4243
* {@link EvaluatingSpelQueryContext}.
4344
* <p>
4445
* Typical usage looks like
45-
* <pre><code>
46+
*
47+
* <pre>
48+
* <code>
4649
SpelQueryContext.EvaluatingSpelQueryContext queryContext = SpelQueryContext
4750
.of((counter, expression) -> String.format("__$synthetic$__%d", counter), String::concat)
4851
.withEvaluationContextProvider(evaluationContextProvider);
4952
5053
SpelEvaluator spelEvaluator = queryContext.parse(query, queryMethod.getParameters());
5154
5255
spelEvaluator.evaluate(objects).forEach(parameterMap::addValue);
53-
* </code></pre>
56+
* </code>
57+
* </pre>
5458
*
5559
* @author Jens Schauder
5660
* @author Gerrit Meier
@@ -79,7 +83,7 @@ public class SpelQueryContext {
7983
private final BiFunction<String, String, String> replacementSource;
8084

8185
private SpelQueryContext(BiFunction<Integer, String, String> parameterNameSource,
82-
BiFunction<String, String, String> replacementSource) {
86+
BiFunction<String, String, String> replacementSource) {
8387

8488
Assert.notNull(parameterNameSource, "Parameter name source must not be null");
8589
Assert.notNull(replacementSource, "Replacement source must not be null");
@@ -89,7 +93,7 @@ private SpelQueryContext(BiFunction<Integer, String, String> parameterNameSource
8993
}
9094

9195
public static SpelQueryContext of(BiFunction<Integer, String, String> parameterNameSource,
92-
BiFunction<String, String, String> replacementSource) {
96+
BiFunction<String, String, String> replacementSource) {
9397
return new SpelQueryContext(parameterNameSource, replacementSource);
9498
}
9599

@@ -105,7 +109,7 @@ public static SpelQueryContext of(BiFunction<Integer, String, String> parameterN
105109
*
106110
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
107111
* @return A {@link SpelExtractor} which makes the query with SpEL expressions replaced by bind parameters and a map
108-
* from bind parameter to SpEL expression available. Guaranteed to be not {@literal null}.
112+
* from bind parameter to SpEL expression available. Guaranteed to be not {@literal null}.
109113
*/
110114
public SpelExtractor parse(String query) {
111115
return new SpelExtractor(query);
@@ -141,11 +145,11 @@ public static class EvaluatingSpelQueryContext extends SpelQueryContext {
141145
* parameter name source and replacement source.
142146
*
143147
* @param evaluationContextProvider must not be {@literal null}.
144-
* @param parameterNameSource must not be {@literal null}.
145-
* @param replacementSource must not be {@literal null}.
148+
* @param parameterNameSource must not be {@literal null}.
149+
* @param replacementSource must not be {@literal null}.
146150
*/
147151
private EvaluatingSpelQueryContext(QueryMethodEvaluationContextProvider evaluationContextProvider,
148-
BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) {
152+
BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) {
149153

150154
super(parameterNameSource, replacementSource);
151155

@@ -162,7 +166,7 @@ private EvaluatingSpelQueryContext(QueryMethodEvaluationContextProvider evaluati
162166
* with prefix being the character ':' or '?'. Parsing honors quoted {@literal String}s enclosed in single or double
163167
* quotation marks.
164168
*
165-
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
169+
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
166170
* @param parameters a {@link Parameters} instance describing query method parameters
167171
* @return A {@link SpelEvaluator} which allows to evaluate the SpEL expressions. Will never be {@literal null}.
168172
*/
@@ -180,6 +184,7 @@ public SpelEvaluator parse(String query, Parameters<?, ?> parameters) {
180184
*
181185
* @author Jens Schauder
182186
* @author Oliver Gierke
187+
* @author Mark Paluch
183188
* @since 2.1
184189
*/
185190
public class SpelExtractor {
@@ -264,6 +269,16 @@ public String getParameter(String name) {
264269
return expressions.get(name);
265270
}
266271

272+
/**
273+
* Returns the number of expressions in this extractor.
274+
*
275+
* @return the number of expressions in this extractor.
276+
* @since 3.1.3
277+
*/
278+
public int size() {
279+
return expressions.size();
280+
}
281+
267282
/**
268283
* A {@literal Map} from parameter name to SpEL expression.
269284
*

src/test/java/org/springframework/data/repository/query/SpelQueryContextUnitTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void createsEvaluatingContextUsingProvider() {
6565
assertThat(context.withEvaluationContextProvider(EVALUATION_CONTEXT_PROVIDER)).isNotNull();
6666
}
6767

68-
@Test // DATACMNS-1683
68+
@Test // DATACMNS-1683, GH-
6969
void reportsQuotationCorrectly() {
7070

7171
var context = SpelQueryContext.of(PARAMETER_NAME_SOURCE, REPLACEMENT_SOURCE);
@@ -77,5 +77,6 @@ void reportsQuotationCorrectly() {
7777
"select n from NetworkServer n where (LOWER(n.name) LIKE LOWER(NULLIF(text(concat('%',:__$synthetic$__0,'%')), '')) OR :__$synthetic$__1 IS NULL )");
7878
assertThat(extractor.isQuoted(extractor.getQueryString().indexOf(":__$synthetic$__0"))).isFalse();
7979
assertThat(extractor.isQuoted(extractor.getQueryString().indexOf(":__$synthetic$__1"))).isFalse();
80+
assertThat(extractor.size()).isEqualTo(2);
8081
}
8182
}

0 commit comments

Comments
 (0)