Skip to content

Commit 2fc8b13

Browse files
committed
Add support for MySQL backticks
This commit makes sure that content within backticks are skipped when parsing a SQL statement using NamedParameterUtils. This harmonizes the current behavior of ignoring special characters that are wrapped in backticks. Closes gh-31944
1 parent e73bbd4 commit 2fc8b13

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,12 +44,12 @@ public abstract class NamedParameterUtils {
4444
/**
4545
* Set of characters that qualify as comment or quotes starting characters.
4646
*/
47-
private static final String[] START_SKIP = new String[] {"'", "\"", "--", "/*"};
47+
private static final String[] START_SKIP = new String[] {"'", "\"", "--", "/*", "`"};
4848

4949
/**
5050
* Set of characters that at are the corresponding comment or quotes ending characters.
5151
*/
52-
private static final String[] STOP_SKIP = new String[] {"'", "\"", "\n", "*/"};
52+
private static final String[] STOP_SKIP = new String[] {"'", "\"", "\n", "*/", "`"};
5353

5454
/**
5555
* Set of characters that qualify as parameter separators,

spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterUtilsTests.java

+19-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
2121
import java.util.Map;
2222

2323
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.ValueSource;
2426

2527
import org.springframework.dao.InvalidDataAccessApiUsageException;
2628
import org.springframework.jdbc.core.SqlParameterValue;
@@ -285,25 +287,14 @@ public void variableAssignmentOperator() {
285287
assertThat(newSql).isEqualTo(expectedSql);
286288
}
287289

288-
@Test // SPR-8280
289-
public void parseSqlStatementWithQuotedSingleQuote() {
290-
String sql = "SELECT ':foo'':doo', :xxx FROM DUAL";
291-
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
292-
assertThat(parsedSql.getTotalParameterCount()).isEqualTo(1);
293-
assertThat(parsedSql.getParameterNames()).containsExactly("xxx");
294-
}
295-
296-
@Test
297-
void parseSqlStatementWithQuotesAndCommentBefore() {
298-
String sql = "SELECT /*:doo*/':foo', :xxx FROM DUAL";
299-
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
300-
assertThat(parsedSql.getTotalParameterCount()).isEqualTo(1);
301-
assertThat(parsedSql.getParameterNames()).containsExactly("xxx");
302-
}
303-
304-
@Test
305-
void parseSqlStatementWithQuotesAndCommentAfter() {
306-
String sql = "SELECT ':foo'/*:doo*/, :xxx FROM DUAL";
290+
@ParameterizedTest // SPR-8280 and others
291+
@ValueSource(strings = {
292+
"SELECT ':foo'':doo', :xxx FROM DUAL",
293+
"SELECT /*:doo*/':foo', :xxx FROM DUAL",
294+
"SELECT ':foo'/*:doo*/, :xxx FROM DUAL",
295+
"SELECT \":foo\"\":doo\", :xxx FROM DUAL",
296+
"SELECT `:foo``:doo`, :xxx FROM DUAL",})
297+
void parseSqlStatementWithParametersInsideQuote(String sql) {
307298
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
308299
assertThat(parsedSql.getTotalParameterCount()).isEqualTo(1);
309300
assertThat(parsedSql.getParameterNames()).containsExactly("xxx");
@@ -361,6 +352,14 @@ public Map<String, Object> getHeaders() {
361352
assertThat(sqlToUse).isEqualTo("insert into foos (id) values (?)");
362353
}
363354

355+
@Test // gh-31944
356+
void parseSqlStatementWithBackticks() {
357+
String sql = "select * from `tb&user` where id = :id";
358+
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
359+
assertThat(parsedSql.getParameterNames()).containsExactly("id");
360+
assertThat(substituteNamedParameters(parsedSql)).isEqualTo("select * from `tb&user` where id = ?");
361+
}
362+
364363
private static String substituteNamedParameters(ParsedSql parsedSql) {
365364
return NamedParameterUtils.substituteNamedParameters(parsedSql, null);
366365
}

0 commit comments

Comments
 (0)