Class ValueExpressionQueryRewriter
java.lang.Object
org.springframework.data.repository.query.ValueExpressionQueryRewriter
- Direct Known Subclasses:
ValueExpressionQueryRewriter.EvaluatingValueExpressionQueryRewriter
A ValueExpressionQueryRewriter is able to detect Value expressions in a query string and to replace them
with bind variables.
Result of the parse process is a ValueExpressionQueryRewriter.ParsedQuery
which provides the transformed query string. Alternatively and
preferred one may provide a QueryMethodValueEvaluationContextAccessor
via
withEvaluationContextAccessor(QueryMethodValueEvaluationContextAccessor)
which will yield the more powerful
ValueExpressionQueryRewriter.EvaluatingValueExpressionQueryRewriter
.
Typical usage looks like
ValueExpressionQueryRewriter.EvaluatingValueExpressionQueryRewriter rewriter = ValueExpressionQueryRewriter .of(valueExpressionParser, (counter, expression) -> String.format("__$synthetic$__%d", counter), String::concat) .withEvaluationContextAccessor(evaluationContextProviderFactory); ValueExpressionQueryRewriter.QueryExpressionEvaluator evaluator = rewriter.parse(query, queryMethod.getParameters()); evaluator.evaluate(objects).forEach(parameterMap::addValue);
- Since:
- 3.3
- Author:
- Jens Schauder, Gerrit Meier, Mark Paluch
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
An extension ofValueExpressionQueryRewriter
that can createValueExpressionQueryRewriter.QueryExpressionEvaluator
instances as it also knows about aQueryMethodValueEvaluationContextAccessor
.class
Parses a query string, identifies the contained Value expressions, replaces them with bind parameters and offers aMap
from those bind parameters to the value expression.class
Evaluates Value expressions as detected byValueExpressionQueryRewriter.ParsedQuery
based on parameter information from a method and parameter values from a method call. -
Method Summary
Modifier and TypeMethodDescriptionstatic ValueExpressionQueryRewriter
of
(ValueExpressionParser expressionParser, BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) Creates a new ValueExpressionQueryRewriter using the givenValueExpressionParser
and rewrite functions.of
(ValueExpressionDelegate delegate, BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) Creates a new EvaluatingValueExpressionQueryRewriter using the givenValueExpressionDelegate
and rewrite functions.Parses the query forvalue expressions
using the pattern:Creates aValueExpressionQueryRewriter.EvaluatingValueExpressionQueryRewriter
from the current one and the givenQueryMethodValueEvaluationContextAccessor
.
-
Method Details
-
of
public static ValueExpressionQueryRewriter of(ValueExpressionParser expressionParser, BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) Creates a new ValueExpressionQueryRewriter using the givenValueExpressionParser
and rewrite functions.- Parameters:
expressionParser
- the expression parser to use.parameterNameSource
- function to generate parameter names. Typically, a function of the form(index, expression) -> "__some_placeholder_" + index
.replacementSource
- function to generate replacements. Typically, a concatenation of the prefix and the parameter name such asString::concat
.- Returns:
- a ValueExpressionQueryRewriter instance to rewrite queries and extract parsed
ValueExpression
s.
-
of
public static ValueExpressionQueryRewriter.EvaluatingValueExpressionQueryRewriter of(ValueExpressionDelegate delegate, BiFunction<Integer, String, String> parameterNameSource, BiFunction<String, String, String> replacementSource) Creates a new EvaluatingValueExpressionQueryRewriter using the givenValueExpressionDelegate
and rewrite functions.- Parameters:
delegate
- the ValueExpressionDelegate to use for parsing and to obtain EvaluationContextAccessor from.parameterNameSource
- function to generate parameter names. Typically, a function of the form(index, expression) -> "__some_placeholder_" + index
.replacementSource
- function to generate replacements. Typically, a concatenation of the prefix and the parameter name such asString::concat
.- Returns:
- a EvaluatingValueExpressionQueryRewriter instance to rewrite queries and extract parsed
ValueExpression
s. - Since:
- 3.4
-
parse
Parses the query forvalue expressions
using the pattern:<prefix>#{<spel>} <prefix>${<property placeholder>}
with prefix being the character ':' or '?'. Parsing honors quoted Strings enclosed in single or double quotation marks.
- Parameters:
query
- a query containing Value Expressions in the format described above. Must not be null.- Returns:
- A
ValueExpressionQueryRewriter.ParsedQuery
which makes the query with Value Expressions replaced by bind parameters and a map from bind parameter to Value Expression available. Guaranteed to be not null.
-
withEvaluationContextAccessor
public ValueExpressionQueryRewriter.EvaluatingValueExpressionQueryRewriter withEvaluationContextAccessor(QueryMethodValueEvaluationContextAccessor accessor) Creates aValueExpressionQueryRewriter.EvaluatingValueExpressionQueryRewriter
from the current one and the givenQueryMethodValueEvaluationContextAccessor
.- Parameters:
accessor
- must not be null.- Returns:
- EvaluatingValueExpressionQueryRewriter instance to rewrite and evaluate Value Expressions.
-