Skip to content

Commit bdbab71

Browse files
committed
Correctly validate mixed parameter bind marker usage.
We now inspect individual parameters instead of the resulting query whether the query contains JDBC-style bind markers. Previously, we inspected the final (rewritten) query which might have contained question marks in literals leading to improper validation failures. Closes #3125
1 parent 00ee738 commit bdbab71

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

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

+13-10
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,6 @@ private String parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(St
244244
int currentIndex = 0;
245245

246246
boolean usesJpaStyleParameters = false;
247-
if (JDBC_STYLE_PARAM.matcher(resultingQuery).find()) {
248-
queryMeta.usesJdbcStyleParameters = true;
249-
}
250-
251-
if (NUMBERED_STYLE_PARAM.matcher(resultingQuery).find() || NAMED_STYLE_PARAM.matcher(resultingQuery).find()) {
252-
usesJpaStyleParameters = true;
253-
}
254247

255248
while (matcher.find()) {
256249

@@ -262,6 +255,19 @@ private String parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(St
262255
String parameterName = parameterIndexString != null ? null : matcher.group(NAMED_PARAMETER_GROUP);
263256
Integer parameterIndex = getParameterIndex(parameterIndexString);
264257

258+
String match = matcher.group(0);
259+
if (JDBC_STYLE_PARAM.matcher(match).find()) {
260+
queryMeta.usesJdbcStyleParameters = true;
261+
}
262+
263+
if (NUMBERED_STYLE_PARAM.matcher(match).find() || NAMED_STYLE_PARAM.matcher(match).find()) {
264+
usesJpaStyleParameters = true;
265+
}
266+
267+
if (usesJpaStyleParameters && queryMeta.usesJdbcStyleParameters) {
268+
throw new IllegalArgumentException("Mixing of ? parameters and other forms like ?1 is not supported");
269+
}
270+
265271
String typeSource = matcher.group(COMPARISION_TYPE_GROUP);
266272
Assert.isTrue(parameterIndexString != null || parameterName != null,
267273
() -> String.format("We need either a name or an index; Offending query string: %s", query));
@@ -273,9 +279,6 @@ private String parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(St
273279
parameterIndex = expressionParameterIndex;
274280
}
275281

276-
if (usesJpaStyleParameters && queryMeta.usesJdbcStyleParameters) {
277-
throw new IllegalArgumentException("Mixing of ? parameters and other forms like ?1 is not supported");
278-
}
279282

280283
BindingIdentifier queryParameter;
281284
if (parameterIndex != null) {

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/StringQueryUnitTests.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ void questionMarkInStringLiteral() {
629629
assertThat(query.getQueryString()).isEqualTo(queryString);
630630
assertThat(query.hasParameterBindings()).isFalse();
631631
assertThat(query.getParameterBindings()).isEmpty();
632-
632+
assertThat(query.usesJdbcStyleParameters()).isFalse();
633633
}
634634

635635
@Test // DATAJPA-1318
@@ -667,6 +667,18 @@ void isNotDefaultProjection() {
667667
}
668668
}
669669

670+
@Test // GH-3125
671+
void questionMarkInStringLiteralWithParameters() {
672+
673+
String queryString = "SELECT CAST(REGEXP_SUBSTR(itp.template_as_txt, '(?<=templateId\\\\\\\\=)(\\\\\\\\d+)(?:\\\\\\\\R)') AS INT) AS templateId FROM foo itp WHERE bar = ?1 AND baz = 1";
674+
StringQuery query = new StringQuery(queryString, false);
675+
676+
assertThat(query.getQueryString()).isEqualTo(queryString);
677+
assertThat(query.hasParameterBindings()).isTrue();
678+
assertThat(query.getParameterBindings()).hasSize(1);
679+
assertThat(query.usesJdbcStyleParameters()).isFalse();
680+
}
681+
670682
@Test // DATAJPA-1652
671683
void usingPipesWithNamedParameter() {
672684

0 commit comments

Comments
 (0)