Skip to content

Commit d8b7a59

Browse files
committed
Individually apply the SQL type from each SqlParameterSource argument
Closes gh-26071
1 parent d3d8f1a commit d8b7a59

File tree

4 files changed

+17
-21
lines changed

4 files changed

+17
-21
lines changed

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

Lines changed: 2 additions & 4 deletions
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-2020 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.
@@ -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: 3 additions & 3 deletions
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-2020 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.
@@ -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: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -44,7 +44,6 @@ public abstract class SqlParameterSourceUtils {
4444
* @see BeanPropertySqlParameterSource
4545
* @see NamedParameterJdbcTemplate#batchUpdate(String, SqlParameterSource[])
4646
*/
47-
@SuppressWarnings("unchecked")
4847
public static SqlParameterSource[] createBatch(Object... candidates) {
4948
return createBatch(Arrays.asList(candidates));
5049
}
@@ -93,17 +92,13 @@ public static SqlParameterSource[] createBatch(Map<String, ?>[] valueMaps) {
9392
* @param source the source of parameter values and type information
9493
* @param parameterName the name of the parameter
9594
* @return the value object
95+
* @see SqlParameterValue
9696
*/
9797
@Nullable
9898
public static Object getTypedValue(SqlParameterSource source, String parameterName) {
9999
int sqlType = source.getSqlType(parameterName);
100100
if (sqlType != SqlParameterSource.TYPE_UNKNOWN) {
101-
if (source.getTypeName(parameterName) != null) {
102-
return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName));
103-
}
104-
else {
105-
return new SqlParameterValue(sqlType, source.getValue(parameterName));
106-
}
101+
return new SqlParameterValue(sqlType, source.getTypeName(parameterName), source.getValue(parameterName));
107102
}
108103
else {
109104
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
@@ -526,24 +526,27 @@ public void testBatchUpdateWithInClause() throws Exception {
526526

527527
@Test
528528
public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception {
529-
SqlParameterSource[] ids = new SqlParameterSource[2];
530-
ids[0] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
531-
ids[1] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
532-
final int[] rowsAffected = new int[] {1, 2};
529+
SqlParameterSource[] ids = new SqlParameterSource[3];
530+
ids[0] = new MapSqlParameterSource().addValue("id", null, Types.NULL);
531+
ids[1] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
532+
ids[2] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
533+
final int[] rowsAffected = new int[] {1, 2, 3};
533534

534535
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
535536
given(connection.getMetaData()).willReturn(databaseMetaData);
536537
namedParameterTemplate = new NamedParameterJdbcTemplate(new JdbcTemplate(dataSource, false));
537538

538539
int[] actualRowsAffected = namedParameterTemplate.batchUpdate(
539540
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
540-
assertThat(actualRowsAffected.length == 2).as("executed 2 updates").isTrue();
541+
assertThat(actualRowsAffected.length == 3).as("executed 3 updates").isTrue();
541542
assertThat(actualRowsAffected[0]).isEqualTo(rowsAffected[0]);
542543
assertThat(actualRowsAffected[1]).isEqualTo(rowsAffected[1]);
544+
assertThat(actualRowsAffected[2]).isEqualTo(rowsAffected[2]);
543545
verify(connection).prepareStatement("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?");
546+
verify(preparedStatement).setNull(1, Types.NULL);
544547
verify(preparedStatement).setObject(1, 100, Types.NUMERIC);
545548
verify(preparedStatement).setObject(1, 200, Types.NUMERIC);
546-
verify(preparedStatement, times(2)).addBatch();
549+
verify(preparedStatement, times(3)).addBatch();
547550
verify(preparedStatement, atLeastOnce()).close();
548551
verify(connection, atLeastOnce()).close();
549552
}

0 commit comments

Comments
 (0)