Skip to content

Commit a5a9332

Browse files
committed
DATAJPA-1267 - Polishing.
Adapt to polished API for DATACMNS-1258. Related tickets: DATACMNS-1258.
1 parent b1b2371 commit a5a9332

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

src/main/java/org/springframework/data/jpa/repository/query/StringQuery.java

+14-19
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import java.util.regex.Matcher;
2828
import java.util.regex.Pattern;
2929

30+
import org.springframework.data.repository.query.SpelQueryContext;
31+
import org.springframework.data.repository.query.SpelQueryContext.SpelExtractor;
3032
import org.springframework.data.repository.query.parser.Part.Type;
31-
import org.springframework.data.repository.query.parser.QuotationMap;
32-
import org.springframework.data.repository.query.parser.SpelQueryContext;
3333
import org.springframework.lang.Nullable;
3434
import org.springframework.util.Assert;
3535
import org.springframework.util.ObjectUtils;
@@ -202,26 +202,22 @@ enum ParameterBindingParser {
202202
private String parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(String query,
203203
List<ParameterBinding> bindings) {
204204

205-
SpelQueryContext.SpelExtractor spelExtractor = createSpelExtractor(query);
205+
SpelExtractor spelExtractor = createSpelExtractor(query);
206206

207-
String resultingQuery = spelExtractor.query();
208-
209-
Matcher matcher = PARAMETER_BINDING_PATTERN.matcher(spelExtractor.query());
210-
211-
QuotationMap quotationMap = new QuotationMap(spelExtractor.query());
207+
String resultingQuery = spelExtractor.getQueryString();
208+
Matcher matcher = PARAMETER_BINDING_PATTERN.matcher(resultingQuery);
212209

213210
while (matcher.find()) {
214211

215-
if (quotationMap.isQuoted(matcher.start())) {
212+
if (spelExtractor.isQuoted(matcher.start())) {
216213
continue;
217214
}
218215

219216
String parameterIndexString = matcher.group(INDEXED_PARAMETER_GROUP);
220217
String parameterName = parameterIndexString != null ? null : matcher.group(NAMED_PARAMETER_GROUP);
221218
Integer parameterIndex = parameterIndexString == null ? null : Integer.valueOf(parameterIndexString);
222219
String typeSource = matcher.group(COMPARISION_TYPE_GROUP);
223-
String expression = spelExtractor.parameterNameToSpelMap()
224-
.get(parameterName == null ? parameterIndexString : parameterName);
220+
String expression = spelExtractor.getParameter(parameterName == null ? parameterIndexString : parameterName);
225221
String replacement = null;
226222

227223
Assert.isTrue(parameterIndex != null || parameterName != null, "We need either a name or an index.");
@@ -269,7 +265,8 @@ private String parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(St
269265
return resultingQuery;
270266
}
271267

272-
private SpelQueryContext.SpelExtractor createSpelExtractor(String queryWithSpel) {
268+
private SpelExtractor createSpelExtractor(String queryWithSpel) {
269+
273270
int greatestParameterIndex = tryFindGreatestParameterIndexIn(queryWithSpel);
274271

275272
boolean parametersShouldBeAccessedByIndex = greatestParameterIndex != -1;
@@ -296,7 +293,7 @@ private SpelQueryContext.SpelExtractor createSpelExtractor(String queryWithSpel)
296293

297294
BiFunction<String, String, String> parameterNameToReplacement = (prefix, name) -> fixedPrefix + name;
298295

299-
return new SpelQueryContext(indexToParameterName, parameterNameToReplacement).parse(queryWithSpel);
296+
return SpelQueryContext.of(indexToParameterName, parameterNameToReplacement).parse(queryWithSpel);
300297
}
301298

302299
private String replaceFirst(String text, String substring, String replacement) {
@@ -322,13 +319,11 @@ private int tryFindGreatestParameterIndexIn(String query) {
322319
return greatestParameterIndex;
323320
}
324321

325-
private void checkAndRegister(ParameterBinding binding, List<ParameterBinding> bindings) {
322+
private static void checkAndRegister(ParameterBinding binding, List<ParameterBinding> bindings) {
326323

327-
for (ParameterBinding existing : bindings) {
328-
if (existing.hasName(binding.getName()) || existing.hasPosition(binding.getPosition())) {
329-
Assert.isTrue(existing.equals(binding), String.format(MESSAGE, existing, binding));
330-
}
331-
}
324+
bindings.stream() //
325+
.filter(it -> it.hasName(binding.getName()) || it.hasPosition(binding.getPosition())) //
326+
.forEach(it -> Assert.isTrue(it.equals(binding), String.format(MESSAGE, it, binding)));
332327

333328
if (!bindings.contains(binding)) {
334329
bindings.add(binding);

src/test/java/org/springframework/data/jpa/repository/query/ParameterBindingParserUnitTests.java

-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18-
import java.util.ArrayList;
19-
import java.util.List;
20-
2118
import org.assertj.core.api.SoftAssertions;
2219
import org.junit.Test;
23-
import org.springframework.data.jpa.repository.query.StringQuery.ParameterBinding;
2420
import org.springframework.data.jpa.repository.query.StringQuery.ParameterBindingParser;
2521

2622
/**

0 commit comments

Comments
 (0)