Skip to content

Commit 2fbfd8a

Browse files
quaffjhoeller
authored andcommitted
Avoid unnecessary wrapping for SqlParameterValue
Fix #26467
1 parent 42c15be commit 2fbfd8a

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

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

+10-3
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 {
@@ -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

+17-1
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)