Return the number of SpEL expressions created by SpelExtractor.

Closes #2885
This commit is contained in:
Mark Paluch
2023-07-20 14:06:17 +02:00
parent c449942053
commit 0f08311b2e
2 changed files with 34 additions and 18 deletions

View File

@@ -15,11 +15,6 @@
*/
package org.springframework.data.repository.query;
import org.springframework.data.domain.Range;
import org.springframework.data.domain.Range.Bound;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -33,16 +28,24 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.springframework.data.domain.Range;
import org.springframework.data.domain.Range.Bound;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* A {@literal SpelQueryContext} is able to find SpEL expressions in a query string and to replace them with bind variables.
* A {@literal SpelQueryContext} is able to find SpEL expressions in a query string and to replace them with bind
* variables.
* <p>
* Result o the parse process is a {@link SpelExtractor} which offers the transformed query string.
* Alternatively and preferred one may provide a {@link QueryMethodEvaluationContextProvider} via
* Result o the parse process is a {@link SpelExtractor} which offers the transformed query string. Alternatively and
* preferred one may provide a {@link QueryMethodEvaluationContextProvider} via
* {@link #withEvaluationContextProvider(QueryMethodEvaluationContextProvider)} which will yield the more powerful
* {@link EvaluatingSpelQueryContext}.
* <p>
* Typical usage looks like
* <pre><code>
*
* <pre>
* <code>
SpelQueryContext.EvaluatingSpelQueryContext queryContext = SpelQueryContext
.of((counter, expression) -> String.format("__$synthetic$__%d", counter), String::concat)
.withEvaluationContextProvider(evaluationContextProvider);
@@ -50,7 +53,8 @@ import java.util.stream.Stream;
SpelEvaluator spelEvaluator = queryContext.parse(query, queryMethod.getParameters());
spelEvaluator.evaluate(objects).forEach(parameterMap::addValue);
* </code></pre>
* </code>
* </pre>
*
* @author Jens Schauder
* @author Gerrit Meier
@@ -79,7 +83,7 @@ public class SpelQueryContext {
private final BiFunction<String, String, String> replacementSource;
private SpelQueryContext(BiFunction<Integer, String, String> parameterNameSource,
BiFunction<String, String, String> replacementSource) {
BiFunction<String, String, String> replacementSource) {
Assert.notNull(parameterNameSource, "Parameter name source must not be null");
Assert.notNull(replacementSource, "Replacement source must not be null");
@@ -89,7 +93,7 @@ public class SpelQueryContext {
}
public static SpelQueryContext of(BiFunction<Integer, String, String> parameterNameSource,
BiFunction<String, String, String> replacementSource) {
BiFunction<String, String, String> replacementSource) {
return new SpelQueryContext(parameterNameSource, replacementSource);
}
@@ -105,7 +109,7 @@ public class SpelQueryContext {
*
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
* @return A {@link SpelExtractor} which makes the query with SpEL expressions replaced by bind parameters and a map
* from bind parameter to SpEL expression available. Guaranteed to be not {@literal null}.
* from bind parameter to SpEL expression available. Guaranteed to be not {@literal null}.
*/
public SpelExtractor parse(String query) {
return new SpelExtractor(query);
@@ -141,11 +145,11 @@ public class SpelQueryContext {
* parameter name source and replacement source.
*
* @param evaluationContextProvider must not be {@literal null}.
* @param parameterNameSource must not be {@literal null}.
* @param replacementSource must not be {@literal null}.
* @param parameterNameSource must not be {@literal null}.
* @param replacementSource must not be {@literal null}.
*/
private EvaluatingSpelQueryContext(QueryMethodEvaluationContextProvider evaluationContextProvider,
BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) {
BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) {
super(parameterNameSource, replacementSource);
@@ -162,7 +166,7 @@ public class SpelQueryContext {
* with prefix being the character ':' or '?'. Parsing honors quoted {@literal String}s enclosed in single or double
* quotation marks.
*
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
* @param parameters a {@link Parameters} instance describing query method parameters
* @return A {@link SpelEvaluator} which allows to evaluate the SpEL expressions. Will never be {@literal null}.
*/
@@ -180,6 +184,7 @@ public class SpelQueryContext {
*
* @author Jens Schauder
* @author Oliver Gierke
* @author Mark Paluch
* @since 2.1
*/
public class SpelExtractor {
@@ -264,6 +269,16 @@ public class SpelQueryContext {
return expressions.get(name);
}
/**
* Returns the number of expressions in this extractor.
*
* @return the number of expressions in this extractor.
* @since 3.1.3
*/
public int size() {
return expressions.size();
}
/**
* A {@literal Map} from parameter name to SpEL expression.
*

View File

@@ -65,7 +65,7 @@ class SpelQueryContextUnitTests {
assertThat(context.withEvaluationContextProvider(EVALUATION_CONTEXT_PROVIDER)).isNotNull();
}
@Test // DATACMNS-1683
@Test // DATACMNS-1683, GH-
void reportsQuotationCorrectly() {
var context = SpelQueryContext.of(PARAMETER_NAME_SOURCE, REPLACEMENT_SOURCE);
@@ -77,5 +77,6 @@ class SpelQueryContextUnitTests {
"select n from NetworkServer n where (LOWER(n.name) LIKE LOWER(NULLIF(text(concat('%',:__$synthetic$__0,'%')), '')) OR :__$synthetic$__1 IS NULL )");
assertThat(extractor.isQuoted(extractor.getQueryString().indexOf(":__$synthetic$__0"))).isFalse();
assertThat(extractor.isQuoted(extractor.getQueryString().indexOf(":__$synthetic$__1"))).isFalse();
assertThat(extractor.size()).isEqualTo(2);
}
}