Skip to content

Commit 4ae3ab1

Browse files
committed
Avoid unnecessary wrapping for SqlParameterValue
Closes gh-26471
1 parent 81f0d76 commit 4ae3ab1

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -36,6 +36,7 @@
3636
*
3737
* @author Thomas Risberg
3838
* @author Juergen Hoeller
39+
* @author Yanming Zhou
3940
* @since 2.0
4041
*/
4142
public abstract class NamedParameterUtils {
@@ -83,7 +84,7 @@ public static ParsedSql parseSqlStatement(final String sql) {
8384
Assert.notNull(sql, "SQL must not be null");
8485

8586
Set<String> namedParameters = new HashSet<>();
86-
String sqlToUse = sql;
87+
StringBuilder sqlToUse = new StringBuilder(sql);
8788
List<ParameterHolder> parameterList = new ArrayList<>();
8889

8990
char[] statement = sql.toCharArray();
@@ -155,7 +156,7 @@ public static ParsedSql parseSqlStatement(final String sql) {
155156
int j = i + 1;
156157
if (j < statement.length && statement[j] == ':') {
157158
// escaped ":" should be skipped
158-
sqlToUse = sqlToUse.substring(0, i - escapes) + sqlToUse.substring(i - escapes + 1);
159+
sqlToUse.deleteCharAt(i - escapes);
159160
escapes++;
160161
i = i + 2;
161162
continue;
@@ -174,7 +175,7 @@ public static ParsedSql parseSqlStatement(final String sql) {
174175
}
175176
i++;
176177
}
177-
ParsedSql parsedSql = new ParsedSql(sqlToUse);
178+
ParsedSql parsedSql = new ParsedSql(sqlToUse.toString());
178179
for (ParameterHolder ph : parameterList) {
179180
parsedSql.addNamedParameter(ph.getParameterName(), ph.getStartIndex(), ph.getEndIndex());
180181
}
@@ -346,8 +347,14 @@ public static Object[] buildValueArray(
346347
String paramName = paramNames.get(i);
347348
try {
348349
SqlParameter param = findParameter(declaredParams, paramName, i);
349-
paramArray[i] = (param != null ? new SqlParameterValue(param, paramSource.getValue(paramName)) :
350-
SqlParameterSourceUtils.getTypedValue(paramSource, paramName));
350+
Object paramValue = paramSource.getValue(paramName);
351+
if (paramValue instanceof SqlParameterValue) {
352+
paramArray[i] = paramValue;
353+
}
354+
else {
355+
paramArray[i] = (param != null ? new SqlParameterValue(param, paramValue) :
356+
SqlParameterSourceUtils.getTypedValue(paramSource, paramName));
357+
}
351358
}
352359
catch (IllegalArgumentException ex) {
353360
throw new InvalidDataAccessApiUsageException(

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -23,6 +23,7 @@
2323
import org.junit.jupiter.api.Test;
2424

2525
import org.springframework.dao.InvalidDataAccessApiUsageException;
26+
import org.springframework.jdbc.core.SqlParameterValue;
2627

2728
import static org.assertj.core.api.Assertions.assertThat;
2829
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -32,6 +33,7 @@
3233
* @author Juergen Hoeller
3334
* @author Rick Evans
3435
* @author Artur Geraschenko
36+
* @author Yanming Zhou
3537
*/
3638
public class NamedParameterUtilsTests {
3739

@@ -96,6 +98,20 @@ public void convertTypeMapToArray() {
9698
.buildSqlTypeArray(NamedParameterUtils.parseSqlStatement("xxx :a :b :c xx :a :b"), namedParams)[4]).isEqualTo(2);
9799
}
98100

101+
@Test
102+
public void convertSqlParameterValueToArray() {
103+
SqlParameterValue sqlParameterValue = new SqlParameterValue(2, "b");
104+
Map<String, Object> paramMap = new HashMap<>();
105+
paramMap.put("a", "a");
106+
paramMap.put("b", sqlParameterValue);
107+
paramMap.put("c", "c");
108+
assertThat(NamedParameterUtils.buildValueArray("xxx :a :b :c xx :a :b", paramMap)[4]).isSameAs(sqlParameterValue);
109+
MapSqlParameterSource namedParams = new MapSqlParameterSource();
110+
namedParams.addValue("a", "a", 1).addValue("b", sqlParameterValue).addValue("c", "c", 3);
111+
assertThat(NamedParameterUtils
112+
.buildValueArray(NamedParameterUtils.parseSqlStatement("xxx :a :b :c xx :a :b"), namedParams, null)[4]).isSameAs(sqlParameterValue);
113+
}
114+
99115
@Test
100116
public void convertTypeMapToSqlParameterList() {
101117
MapSqlParameterSource namedParams = new MapSqlParameterSource();

0 commit comments

Comments
 (0)