From 75f9d519fc66f68d06d521d495343f5a6f6d1d5b Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Fri, 29 Jan 2021 10:34:32 +0800 Subject: [PATCH] Avoid unnecessary wrapping for SqlParameterValue Fix https://github.com/spring-projects/spring-framework/issues/26467 --- .../core/namedparam/NamedParameterUtils.java | 13 ++++++++++--- .../namedparam/NamedParameterUtilsTests.java | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java index 4d4c414ea7a4..f289cdb2d402 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,7 @@ * * @author Thomas Risberg * @author Juergen Hoeller + * @author Yanming Zhou * @since 2.0 */ public abstract class NamedParameterUtils { @@ -346,8 +347,14 @@ public static Object[] buildValueArray( String paramName = paramNames.get(i); try { SqlParameter param = findParameter(declaredParams, paramName, i); - paramArray[i] = (param != null ? new SqlParameterValue(param, paramSource.getValue(paramName)) : - SqlParameterSourceUtils.getTypedValue(paramSource, paramName)); + Object paramValue = paramSource.getValue(paramName); + if (paramValue instanceof SqlParameterValue) { + paramArray[i] = paramValue; + } + else { + paramArray[i] = (param != null ? new SqlParameterValue(param, paramValue) : + SqlParameterSourceUtils.getTypedValue(paramSource, paramName)); + } } catch (IllegalArgumentException ex) { throw new InvalidDataAccessApiUsageException( diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterUtilsTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterUtilsTests.java index 685d2f3aaeb6..7a1df2928a57 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterUtilsTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test; import org.springframework.dao.InvalidDataAccessApiUsageException; +import org.springframework.jdbc.core.SqlParameterValue; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -32,6 +33,7 @@ * @author Juergen Hoeller * @author Rick Evans * @author Artur Geraschenko + * @author Yanming Zhou */ public class NamedParameterUtilsTests { @@ -96,6 +98,20 @@ public void convertTypeMapToArray() { .buildSqlTypeArray(NamedParameterUtils.parseSqlStatement("xxx :a :b :c xx :a :b"), namedParams)[4]).isEqualTo(2); } + @Test + public void convertSqlParameterValueToArray() { + SqlParameterValue sqlParameterValue = new SqlParameterValue(2, "b"); + Map paramMap = new HashMap<>(); + paramMap.put("a", "a"); + paramMap.put("b", sqlParameterValue); + paramMap.put("c", "c"); + assertThat(NamedParameterUtils.buildValueArray("xxx :a :b :c xx :a :b", paramMap)[4]).isSameAs(sqlParameterValue); + MapSqlParameterSource namedParams = new MapSqlParameterSource(); + namedParams.addValue("a", "a", 1).addValue("b", sqlParameterValue).addValue("c", "c", 3); + assertThat(NamedParameterUtils + .buildValueArray(NamedParameterUtils.parseSqlStatement("xxx :a :b :c xx :a :b"), namedParams, null)[4]).isSameAs(sqlParameterValue); + } + @Test public void convertTypeMapToSqlParameterList() { MapSqlParameterSource namedParams = new MapSqlParameterSource();