From a7e097d3d1e11fe3c06b102d53cced85e58ee0f3 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 20 Jul 2023 14:06:17 +0200 Subject: [PATCH] Return the number of SpEL expressions created by `SpelExtractor`. Closes #2885 --- .../repository/query/SpelQueryContext.java | 49 ++++++++++++------- .../query/SpelQueryContextUnitTests.java | 3 +- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/query/SpelQueryContext.java b/src/main/java/org/springframework/data/repository/query/SpelQueryContext.java index f03b5a9ef7..72eb8532ce 100644 --- a/src/main/java/org/springframework/data/repository/query/SpelQueryContext.java +++ b/src/main/java/org/springframework/data/repository/query/SpelQueryContext.java @@ -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.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. *

- * 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}. *

* Typical usage looks like - *


+ *
+ * 
+ * 
      SpelQueryContext.EvaluatingSpelQueryContext queryContext = SpelQueryContext
          .of((counter, expression) -> String.format("__$synthetic$__%d", counter), String::concat)
          .withEvaluationContextProvider(evaluationContextProvider);
@@ -50,7 +53,8 @@
      SpelEvaluator spelEvaluator = queryContext.parse(query, queryMethod.getParameters());
 
      spelEvaluator.evaluate(objects).forEach(parameterMap::addValue);
- * 
+ *
+ *
* * @author Jens Schauder * @author Gerrit Meier @@ -79,7 +83,7 @@ public class SpelQueryContext { private final BiFunction replacementSource; private SpelQueryContext(BiFunction parameterNameSource, - BiFunction replacementSource) { + BiFunction replacementSource) { Assert.notNull(parameterNameSource, "Parameter name source must not be null"); Assert.notNull(replacementSource, "Replacement source must not be null"); @@ -89,7 +93,7 @@ private SpelQueryContext(BiFunction parameterNameSource } public static SpelQueryContext of(BiFunction parameterNameSource, - BiFunction replacementSource) { + BiFunction replacementSource) { return new SpelQueryContext(parameterNameSource, replacementSource); } @@ -105,7 +109,7 @@ public static SpelQueryContext of(BiFunction parameterN * * @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 static class EvaluatingSpelQueryContext extends 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 parameterNameSource, BiFunction replacementSource) { + BiFunction parameterNameSource, BiFunction replacementSource) { super(parameterNameSource, replacementSource); @@ -162,7 +166,7 @@ private EvaluatingSpelQueryContext(QueryMethodEvaluationContextProvider evaluati * 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 SpelEvaluator parse(String query, Parameters parameters) { * * @author Jens Schauder * @author Oliver Gierke + * @author Mark Paluch * @since 2.1 */ public class SpelExtractor { @@ -264,6 +269,16 @@ public String getParameter(String name) { return expressions.get(name); } + /** + * Returns the number of expressions in this extractor. + * + * @return the number of expressions in this extractor. + * @since 3.0.9 + */ + public int size() { + return expressions.size(); + } + /** * A {@literal Map} from parameter name to SpEL expression. * diff --git a/src/test/java/org/springframework/data/repository/query/SpelQueryContextUnitTests.java b/src/test/java/org/springframework/data/repository/query/SpelQueryContextUnitTests.java index f57a355c7d..f4cf0e32f4 100644 --- a/src/test/java/org/springframework/data/repository/query/SpelQueryContextUnitTests.java +++ b/src/test/java/org/springframework/data/repository/query/SpelQueryContextUnitTests.java @@ -65,7 +65,7 @@ void createsEvaluatingContextUsingProvider() { 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 @@ void reportsQuotationCorrectly() { "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); } }