Skip to content

Commit

Permalink
Return the number of SpEL expressions created by SpelExtractor.
Browse files Browse the repository at this point in the history
Closes #2885
  • Loading branch information
mp911de committed Jul 20, 2023
1 parent 7e2a653 commit a7e097d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,24 +28,33 @@
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);
SpelEvaluator spelEvaluator = queryContext.parse(query, queryMethod.getParameters());
spelEvaluator.evaluate(objects).forEach(parameterMap::addValue);
* </code></pre>
* </code>
* </pre>
*
* @author Jens Schauder
* @author Gerrit Meier
Expand Down Expand Up @@ -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");
Expand All @@ -89,7 +93,7 @@ private SpelQueryContext(BiFunction<Integer, String, String> parameterNameSource
}

public static SpelQueryContext of(BiFunction<Integer, String, String> parameterNameSource,
BiFunction<String, String, String> replacementSource) {
BiFunction<String, String, String> replacementSource) {
return new SpelQueryContext(parameterNameSource, replacementSource);
}

Expand All @@ -105,7 +109,7 @@ public static SpelQueryContext of(BiFunction<Integer, String, String> 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);
Expand Down Expand Up @@ -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<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) {
BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) {

super(parameterNameSource, replacementSource);

Expand All @@ -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}.
*/
Expand All @@ -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 {
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}

0 comments on commit a7e097d

Please sign in to comment.