Skip to content

Commit 085c771

Browse files
michael-simonsschauder
authored andcommitted
Escape JDBC styled parameters.
Closes #2228 Original pull request #2229
1 parent 3787976 commit 085c771

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535
* @author Thomas Darimont
3636
* @author Oliver Gierke
3737
* @author Tom Hombergs
38+
* @author Michael J. Simons
3839
*/
3940
class ExpressionBasedStringQuery extends StringQuery {
4041

41-
private static final String EXPRESSION_PARAMETER = "?#{";
42-
private static final String QUOTED_EXPRESSION_PARAMETER = "?__HASH__{";
42+
private static final String EXPRESSION_PARAMETER = "$1#{";
43+
private static final String QUOTED_EXPRESSION_PARAMETER = "$1__HASH__{";
4344

44-
private static final Pattern EXPRESSION_PARAMETER_QUOTING = Pattern.compile(Pattern.quote(EXPRESSION_PARAMETER));
45-
private static final Pattern EXPRESSION_PARAMETER_UNQUOTING = Pattern.compile(Pattern
46-
.quote(QUOTED_EXPRESSION_PARAMETER));
45+
private static final Pattern EXPRESSION_PARAMETER_QUOTING = Pattern.compile("([:\\?])#\\{");
46+
private static final Pattern EXPRESSION_PARAMETER_UNQUOTING = Pattern.compile("([:\\?])__HASH__\\{");
4747

4848
private static final String ENTITY_NAME = "entityName";
4949
private static final String ENTITY_NAME_VARIABLE = "#" + ENTITY_NAME;

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @author Oliver Gierke
3232
* @author Jens Schauder
3333
* @author Mark Paluch
34+
* @author Michael J. Simons
3435
*/
3536
@RunWith(MockitoJUnitRunner.class)
3637
public class ExpressionBasedStringQueryUnitTests {
@@ -62,7 +63,7 @@ public void renderAliasInExpressionQueryCorrectly() {
6263
public void shouldDetectBindParameterCountCorrectly() {
6364

6465
StringQuery query = new ExpressionBasedStringQuery(
65-
"select n from NetworkServer n where (LOWER(n.name) LIKE LOWER(NULLIF(text(concat('%',:#{#networkRequest.name},'%')), '')) OR :#{#networkRequest.name} IS NULL )\"\n"
66+
"select n from #{#entityName} n where (LOWER(n.name) LIKE LOWER(NULLIF(text(concat('%',:#{#networkRequest.name},'%')), '')) OR :#{#networkRequest.name} IS NULL )\"\n"
6667
+ "+ \"AND (LOWER(n.server) LIKE LOWER(NULLIF(text(concat('%',:#{#networkRequest.server},'%')), '')) OR :#{#networkRequest.server} IS NULL)\"\n"
6768
+ "+ \"AND (n.createdAt >= :#{#networkRequest.createdTime.startDateTime}) AND (n.createdAt <=:#{#networkRequest.createdTime.endDateTime})\"\n"
6869
+ "+ \"AND (n.updatedAt >= :#{#networkRequest.updatedTime.startDateTime}) AND (n.updatedAt <=:#{#networkRequest.updatedTime.endDateTime})",
@@ -71,4 +72,17 @@ public void shouldDetectBindParameterCountCorrectly() {
7172
assertThat(query.getParameterBindings()).hasSize(8);
7273
}
7374

75+
@Test // GH-2228
76+
void shouldDetectBindParameterCountCorrectlyWithJDBCStyleParameters() {
77+
78+
StringQuery query = new ExpressionBasedStringQuery(
79+
"select n from #{#entityName} n where (LOWER(n.name) LIKE LOWER(NULLIF(text(concat('%',?#{#networkRequest.name},'%')), '')) OR ?#{#networkRequest.name} IS NULL )\"\n"
80+
+ "+ \"AND (LOWER(n.server) LIKE LOWER(NULLIF(text(concat('%',?#{#networkRequest.server},'%')), '')) OR ?#{#networkRequest.server} IS NULL)\"\n"
81+
+ "+ \"AND (n.createdAt >= ?#{#networkRequest.createdTime.startDateTime}) AND (n.createdAt <=?#{#networkRequest.createdTime.endDateTime})\"\n"
82+
+ "+ \"AND (n.updatedAt >= ?#{#networkRequest.updatedTime.startDateTime}) AND (n.updatedAt <=?#{#networkRequest.updatedTime.endDateTime})",
83+
metadata, SPEL_PARSER);
84+
85+
assertThat(query.getParameterBindings()).hasSize(8);
86+
}
87+
7488
}

0 commit comments

Comments
 (0)