Avoid unnecessary wrapping for SqlParameterValue
Closes gh-26471
This commit is contained in:
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -36,6 +36,7 @@ import org.springframework.util.Assert;
|
|||||||
*
|
*
|
||||||
* @author Thomas Risberg
|
* @author Thomas Risberg
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Yanming Zhou
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public abstract class NamedParameterUtils {
|
public abstract class NamedParameterUtils {
|
||||||
@@ -83,7 +84,7 @@ public abstract class NamedParameterUtils {
|
|||||||
Assert.notNull(sql, "SQL must not be null");
|
Assert.notNull(sql, "SQL must not be null");
|
||||||
|
|
||||||
Set<String> namedParameters = new HashSet<>();
|
Set<String> namedParameters = new HashSet<>();
|
||||||
String sqlToUse = sql;
|
StringBuilder sqlToUse = new StringBuilder(sql);
|
||||||
List<ParameterHolder> parameterList = new ArrayList<>();
|
List<ParameterHolder> parameterList = new ArrayList<>();
|
||||||
|
|
||||||
char[] statement = sql.toCharArray();
|
char[] statement = sql.toCharArray();
|
||||||
@@ -155,7 +156,7 @@ public abstract class NamedParameterUtils {
|
|||||||
int j = i + 1;
|
int j = i + 1;
|
||||||
if (j < statement.length && statement[j] == ':') {
|
if (j < statement.length && statement[j] == ':') {
|
||||||
// escaped ":" should be skipped
|
// escaped ":" should be skipped
|
||||||
sqlToUse = sqlToUse.substring(0, i - escapes) + sqlToUse.substring(i - escapes + 1);
|
sqlToUse.deleteCharAt(i - escapes);
|
||||||
escapes++;
|
escapes++;
|
||||||
i = i + 2;
|
i = i + 2;
|
||||||
continue;
|
continue;
|
||||||
@@ -174,7 +175,7 @@ public abstract class NamedParameterUtils {
|
|||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
ParsedSql parsedSql = new ParsedSql(sqlToUse);
|
ParsedSql parsedSql = new ParsedSql(sqlToUse.toString());
|
||||||
for (ParameterHolder ph : parameterList) {
|
for (ParameterHolder ph : parameterList) {
|
||||||
parsedSql.addNamedParameter(ph.getParameterName(), ph.getStartIndex(), ph.getEndIndex());
|
parsedSql.addNamedParameter(ph.getParameterName(), ph.getStartIndex(), ph.getEndIndex());
|
||||||
}
|
}
|
||||||
@@ -346,8 +347,14 @@ public abstract class NamedParameterUtils {
|
|||||||
String paramName = paramNames.get(i);
|
String paramName = paramNames.get(i);
|
||||||
try {
|
try {
|
||||||
SqlParameter param = findParameter(declaredParams, paramName, i);
|
SqlParameter param = findParameter(declaredParams, paramName, i);
|
||||||
paramArray[i] = (param != null ? new SqlParameterValue(param, paramSource.getValue(paramName)) :
|
Object paramValue = paramSource.getValue(paramName);
|
||||||
SqlParameterSourceUtils.getTypedValue(paramSource, paramName));
|
if (paramValue instanceof SqlParameterValue) {
|
||||||
|
paramArray[i] = paramValue;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
paramArray[i] = (param != null ? new SqlParameterValue(param, paramValue) :
|
||||||
|
SqlParameterSourceUtils.getTypedValue(paramSource, paramName));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException ex) {
|
catch (IllegalArgumentException ex) {
|
||||||
throw new InvalidDataAccessApiUsageException(
|
throw new InvalidDataAccessApiUsageException(
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -23,6 +23,7 @@ import java.util.Map;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
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.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
@@ -32,6 +33,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Rick Evans
|
* @author Rick Evans
|
||||||
* @author Artur Geraschenko
|
* @author Artur Geraschenko
|
||||||
|
* @author Yanming Zhou
|
||||||
*/
|
*/
|
||||||
public class NamedParameterUtilsTests {
|
public class NamedParameterUtilsTests {
|
||||||
|
|
||||||
@@ -96,6 +98,20 @@ public class NamedParameterUtilsTests {
|
|||||||
.buildSqlTypeArray(NamedParameterUtils.parseSqlStatement("xxx :a :b :c xx :a :b"), namedParams)[4]).isEqualTo(2);
|
.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<String, Object> 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
|
@Test
|
||||||
public void convertTypeMapToSqlParameterList() {
|
public void convertTypeMapToSqlParameterList() {
|
||||||
MapSqlParameterSource namedParams = new MapSqlParameterSource();
|
MapSqlParameterSource namedParams = new MapSqlParameterSource();
|
||||||
|
|||||||
Reference in New Issue
Block a user