Skip to content

Commit 2adf72c

Browse files
committed
DATACMNS-1258 - Added infrastructure for SpEL handling in queries.
The goal is to extract the parts of query parsing that are generic into commons in order to avoid slightly different behavior in modules. Extracted the QuotationMap from Spring Data JPA as well as the part of the parser that deals with extracting SpEL expressions. Added the SpelEvaluator for evaluating the SpEL expressions. Added enough configurability to satisfy the needs of Neo4j and JPA. Mainly to support the different formats for bind parameter :name vs {name}.
1 parent 2e3c28a commit 2adf72c

File tree

5 files changed

+589
-0
lines changed

5 files changed

+589
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.repository.query.parser;
17+
18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.HashSet;
21+
import java.util.List;
22+
import java.util.Set;
23+
24+
import org.springframework.data.domain.Range;
25+
import org.springframework.lang.Nullable;
26+
27+
/**
28+
* Value object to analyze a String to determine the parts of the String that are quoted and offers an API to query that
29+
* information.
30+
*
31+
* @author Jens Schauder
32+
* @since 2.0.3
33+
*/
34+
public class QuotationMap {
35+
36+
private static final Set<Character> QUOTING_CHARACTERS = new HashSet<>(Arrays.asList('"', '\''));
37+
38+
private final List<Range<Integer>> quotedRanges = new ArrayList<>();
39+
40+
public QuotationMap(@Nullable String query) {
41+
42+
if (query == null) {
43+
return;
44+
}
45+
46+
Character inQuotation = null;
47+
int start = 0;
48+
49+
for (int i = 0; i < query.length(); i++) {
50+
51+
char currentChar = query.charAt(i);
52+
53+
if (QUOTING_CHARACTERS.contains(currentChar)) {
54+
55+
if (inQuotation == null) {
56+
57+
inQuotation = currentChar;
58+
start = i;
59+
60+
} else if (currentChar == inQuotation) {
61+
62+
inQuotation = null;
63+
quotedRanges.add(Range.of(Range.Bound.inclusive(start), Range.Bound.inclusive(i)));
64+
}
65+
}
66+
}
67+
68+
if (inQuotation != null) {
69+
throw new IllegalArgumentException(
70+
String.format("The string <%s> starts a quoted range at %d, but never ends it.", query, start));
71+
}
72+
}
73+
74+
/**
75+
* Checks if a given index is within a quoted range.
76+
*
77+
* @param index to check if it is part of a quoted range.
78+
* @return whether the query contains a quoted range at {@literal index}.
79+
*/
80+
public boolean isQuoted(int index) {
81+
return quotedRanges.stream().anyMatch(r -> r.contains(index));
82+
}
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.repository.query.parser;
17+
18+
import lombok.NonNull;
19+
import lombok.RequiredArgsConstructor;
20+
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
import org.springframework.data.repository.query.EvaluationContextProvider;
25+
import org.springframework.data.repository.query.Parameters;
26+
import org.springframework.expression.EvaluationContext;
27+
import org.springframework.expression.spel.standard.SpelExpressionParser;
28+
import org.springframework.lang.Nullable;
29+
import org.springframework.util.Assert;
30+
31+
/**
32+
* Evaluates SpEL expressions as extracted by the SpelExtractor based on parameter information from a method and
33+
* parameter values from a method call.
34+
*
35+
* @author Jens Schauder
36+
* @author Gerrit Meier
37+
*/
38+
@RequiredArgsConstructor
39+
public class SpelEvaluator {
40+
41+
private final static SpelExpressionParser PARSER = new SpelExpressionParser();
42+
43+
@NonNull private final EvaluationContextProvider evaluationContextProvider;
44+
@NonNull private final Parameters<?, ?> parameters;
45+
46+
/**
47+
* A map from parameter name to SpEL expression as returned by {@link SpelQueryContext.SpelExtractor#parameterNameToSpelMap()}.
48+
*/
49+
@NonNull private final Map<String, String> parameterNameToSpelMap;
50+
51+
/**
52+
* Evaluate all the SpEL expressions in {@link #parameterNameToSpelMap} based on values provided as an argument.
53+
*
54+
* @param values Parameter values. Must not be {@literal null}.
55+
* @return a map from parameter name to evaluated value. Guaranteed to be not {@literal null}.
56+
*/
57+
public Map<String, Object> evaluate(Object[] values) {
58+
59+
Assert.notNull(values, "Values must not be null.");
60+
61+
HashMap<String, Object> spelExpressionResults = new HashMap<>();
62+
EvaluationContext evaluationContext = evaluationContextProvider.getEvaluationContext(parameters, values);
63+
for (Map.Entry<String, String> parameterNameToSpel : parameterNameToSpelMap.entrySet()) {
64+
65+
Object spElValue = getSpElValue(evaluationContext, parameterNameToSpel.getValue());
66+
spelExpressionResults.put(parameterNameToSpel.getKey(), spElValue);
67+
}
68+
69+
return spelExpressionResults;
70+
}
71+
72+
@Nullable
73+
private Object getSpElValue(EvaluationContext evaluationContext, String expression) {
74+
return PARSER.parseExpression(expression).getValue(evaluationContext, Object.class);
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.repository.query.parser;
17+
18+
import lombok.NonNull;
19+
import lombok.RequiredArgsConstructor;
20+
21+
import java.util.Collections;
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
import java.util.function.BiFunction;
25+
import java.util.regex.Matcher;
26+
import java.util.regex.Pattern;
27+
28+
import org.springframework.util.Assert;
29+
30+
/**
31+
* Source of {@link SpelExtractor} encapsulating configuration often common for all queries.
32+
*
33+
* @author Jens Schauder
34+
* @author Gerrit Meier
35+
*/
36+
@RequiredArgsConstructor
37+
public class SpelQueryContext {
38+
39+
private final static String SPEL_PATTERN_STRING = "([:?])#\\{([^}]+)}";
40+
private final static Pattern SPEL_PATTERN = Pattern.compile(SPEL_PATTERN_STRING);
41+
42+
/**
43+
* A function from the index of a SpEL expression in a query and the actual SpEL expression to the parameter name to
44+
* be used in place of the SpEL expression. A typical implementation is expected to look like
45+
* <code>(index, spel) -> "__some_placeholder_" + index</code>
46+
*/
47+
@NonNull private final BiFunction<Integer, String, String> parameterNameSource;
48+
49+
/**
50+
* A function from a prefix used to demarcate a SpEL expression in a query and a parameter name as returned from
51+
* {@link #parameterNameSource} to a {@literal String} to be used as a replacement of the SpEL in the query. The
52+
* returned value should normally be interpretable as a bind parameter by the underlying persistence mechanism. A
53+
* typical implementation is expected to look like <code>(prefix, name) -> prefix + name</code> or
54+
* <code>(prefix, name) -> "{" + name + "}"</code>
55+
*/
56+
@NonNull private final BiFunction<String, String, String> replacementSource;
57+
58+
/**
59+
* Parses the query for SpEL expressions using the pattern
60+
*
61+
* <pre>
62+
* &lt;prefix&gt;#{&lt;spel&gt;}
63+
* </pre>
64+
*
65+
* with prefix being the character ':' or '?'. Parsing honors quoted {@literal String}s enclosed in single or double
66+
* quotation marks.
67+
*
68+
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
69+
* @return A {@link SpelExtractor} which makes the query with SpEL expressions replaced by bind parameters and a map
70+
* from bind parameter to SpEL expression available. Guaranteed to be not {@literal null}.
71+
*/
72+
public SpelExtractor parse(String query) {
73+
return new SpelExtractor(query);
74+
}
75+
76+
/**
77+
* Parses a query string, identifies the contained SpEL expressions, replaces them with bind parameters and offers a
78+
* {@link Map} from those bind parameters to the spel expression.
79+
* <p>
80+
* The parser detects quoted parts of the query string and does not detect SpEL expressions inside such quoted parts
81+
* of the query.
82+
*
83+
* @author Jens Schauder
84+
*/
85+
public class SpelExtractor {
86+
87+
private static final int PREFIX_GROUP_INDEX = 1;
88+
private static final int EXPRESSION_GROUP_INDEX = 2;
89+
90+
private final String query;
91+
private final Map<String, String> expressions;
92+
93+
/**
94+
* Creates a SpelExtractor from a query String.
95+
*
96+
* @param query Must not be {@literal null}.
97+
*/
98+
private SpelExtractor(String query) {
99+
100+
Assert.notNull(query, "Query must not be null");
101+
102+
HashMap<String, String> expressions = new HashMap<>();
103+
104+
Matcher matcher = SPEL_PATTERN.matcher(query);
105+
106+
StringBuilder resultQuery = new StringBuilder();
107+
108+
QuotationMap quotedAreas = new QuotationMap(query);
109+
110+
int expressionCounter = 0;
111+
int matchedUntil = 0;
112+
113+
while (matcher.find()) {
114+
115+
if (quotedAreas.isQuoted(matcher.start())) {
116+
117+
resultQuery.append(query.substring(matchedUntil, matcher.end()));
118+
119+
} else {
120+
121+
String spelExpression = matcher.group(EXPRESSION_GROUP_INDEX);
122+
String prefix = matcher.group(PREFIX_GROUP_INDEX);
123+
124+
String parameterName = parameterNameSource.apply(expressionCounter, spelExpression);
125+
String replacement = replacementSource.apply(prefix, parameterName);
126+
127+
resultQuery.append(query.substring(matchedUntil, matcher.start()));
128+
resultQuery.append(replacement);
129+
130+
expressions.put(parameterName, spelExpression);
131+
expressionCounter++;
132+
}
133+
134+
matchedUntil = matcher.end();
135+
}
136+
137+
resultQuery.append(query.substring(matchedUntil));
138+
139+
this.expressions = Collections.unmodifiableMap(expressions);
140+
this.query = resultQuery.toString();
141+
}
142+
143+
/**
144+
* The query with all the SpEL expressions replaced with bind parameters.
145+
*
146+
* @return Guaranteed to be not {@literal null}.
147+
*/
148+
public String query() {
149+
return query;
150+
}
151+
152+
/**
153+
* A {@literal Map} from parameter name to SpEL expression.
154+
*
155+
* @return Guaranteed to be not {@literal null}.
156+
*/
157+
public Map<String, String> parameterNameToSpelMap() {
158+
return expressions;
159+
}
160+
}
161+
}

0 commit comments

Comments
 (0)