Skip to content

Commit 66292cd

Browse files
committed
Individually apply the SQL type from each SqlParameterSource argument
Closes gh-26071
1 parent 6eec1ac commit 66292cd

File tree

4 files changed

+14
-17
lines changed

4 files changed

+14
-17
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/PreparedStatementCreatorFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import org.springframework.dao.InvalidDataAccessApiUsageException;
3232
import org.springframework.lang.Nullable;
33-
import org.springframework.util.Assert;
3433

3534
/**
3635
* Helper class that efficiently creates multiple {@link PreparedStatementCreator}
@@ -200,9 +199,8 @@ public PreparedStatementCreatorImpl(List<?> parameters) {
200199

201200
public PreparedStatementCreatorImpl(String actualSql, List<?> parameters) {
202201
this.actualSql = actualSql;
203-
Assert.notNull(parameters, "Parameters List must not be null");
204202
this.parameters = parameters;
205-
if (this.parameters.size() != declaredParameters.size()) {
203+
if (parameters.size() != declaredParameters.size()) {
206204
// Account for named parameters being used multiple times
207205
Set<String> names = new HashSet<>();
208206
for (int i = 0; i < parameters.size(); i++) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ public static Object[] buildValueArray(
345345
for (int i = 0; i < paramNames.size(); i++) {
346346
String paramName = paramNames.get(i);
347347
try {
348-
Object value = paramSource.getValue(paramName);
349348
SqlParameter param = findParameter(declaredParams, paramName, i);
350-
paramArray[i] = (param != null ? new SqlParameterValue(param, value) : value);
349+
paramArray[i] = (param != null ? new SqlParameterValue(param, paramSource.getValue(paramName)) :
350+
SqlParameterSourceUtils.getTypedValue(paramSource, paramName));
351351
}
352352
catch (IllegalArgumentException ex) {
353353
throw new InvalidDataAccessApiUsageException(

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,13 @@ public static SqlParameterSource[] createBatch(Map<String, ?>[] valueMaps) {
9292
* @param source the source of parameter values and type information
9393
* @param parameterName the name of the parameter
9494
* @return the value object
95+
* @see SqlParameterValue
9596
*/
9697
@Nullable
9798
public static Object getTypedValue(SqlParameterSource source, String parameterName) {
9899
int sqlType = source.getSqlType(parameterName);
99100
if (sqlType != SqlParameterSource.TYPE_UNKNOWN) {
100-
if (source.getTypeName(parameterName) != null) {
101-
return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName));
102-
}
103-
else {
104-
return new SqlParameterValue(sqlType, source.getValue(parameterName));
105-
}
101+
return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName));
106102
}
107103
else {
108104
return source.getValue(parameterName);

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -561,24 +561,27 @@ public void testBatchUpdateWithInClause() throws Exception {
561561

562562
@Test
563563
public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception {
564-
SqlParameterSource[] ids = new SqlParameterSource[2];
565-
ids[0] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
566-
ids[1] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
567-
final int[] rowsAffected = new int[] {1, 2};
564+
SqlParameterSource[] ids = new SqlParameterSource[3];
565+
ids[0] = new MapSqlParameterSource().addValue("id", null, Types.NULL);
566+
ids[1] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
567+
ids[2] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
568+
final int[] rowsAffected = new int[] {1, 2, 3};
568569

569570
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
570571
given(connection.getMetaData()).willReturn(databaseMetaData);
571572
namedParameterTemplate = new NamedParameterJdbcTemplate(new JdbcTemplate(dataSource, false));
572573

573574
int[] actualRowsAffected = namedParameterTemplate.batchUpdate(
574575
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
575-
assertThat(actualRowsAffected.length == 2).as("executed 2 updates").isTrue();
576+
assertThat(actualRowsAffected.length == 3).as("executed 3 updates").isTrue();
576577
assertThat(actualRowsAffected[0]).isEqualTo(rowsAffected[0]);
577578
assertThat(actualRowsAffected[1]).isEqualTo(rowsAffected[1]);
579+
assertThat(actualRowsAffected[2]).isEqualTo(rowsAffected[2]);
578580
verify(connection).prepareStatement("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?");
581+
verify(preparedStatement).setNull(1, Types.NULL);
579582
verify(preparedStatement).setObject(1, 100, Types.NUMERIC);
580583
verify(preparedStatement).setObject(1, 200, Types.NUMERIC);
581-
verify(preparedStatement, times(2)).addBatch();
584+
verify(preparedStatement, times(3)).addBatch();
582585
verify(preparedStatement, atLeastOnce()).close();
583586
verify(connection, atLeastOnce()).close();
584587
}

0 commit comments

Comments
 (0)