Skip to content

Commit 40a79a8

Browse files
committed
Individually apply the SQL type from each SqlParameterSource argument
Closes gh-26071
1 parent 65460db commit 40a79a8

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
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.
@@ -31,7 +31,6 @@
3131

3232
import org.springframework.dao.InvalidDataAccessApiUsageException;
3333
import org.springframework.lang.Nullable;
34-
import org.springframework.util.Assert;
3534

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

202201
public PreparedStatementCreatorImpl(String actualSql, List<?> parameters) {
203202
this.actualSql = actualSql;
204-
Assert.notNull(parameters, "Parameters List must not be null");
205203
this.parameters = parameters;
206-
if (this.parameters.size() != declaredParameters.size()) {
204+
if (parameters.size() != declaredParameters.size()) {
207205
// Account for named parameters being used multiple times
208206
Set<String> names = new HashSet<>();
209207
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-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.
@@ -346,9 +346,9 @@ public static Object[] buildValueArray(
346346
for (int i = 0; i < paramNames.size(); i++) {
347347
String paramName = paramNames.get(i);
348348
try {
349-
Object value = paramSource.getValue(paramName);
350349
SqlParameter param = findParameter(declaredParams, paramName, i);
351-
paramArray[i] = (param != null ? new SqlParameterValue(param, value) : value);
350+
paramArray[i] = (param != null ? new SqlParameterValue(param, paramSource.getValue(paramName)) :
351+
SqlParameterSourceUtils.getTypedValue(paramSource, paramName));
352352
}
353353
catch (IllegalArgumentException ex) {
354354
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: 10 additions & 7 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.
@@ -508,24 +508,27 @@ public void testBatchUpdateWithInClause() throws Exception {
508508

509509
@Test
510510
public void testBatchUpdateWithSqlParameterSourcePlusTypeInfo() throws Exception {
511-
SqlParameterSource[] ids = new SqlParameterSource[2];
512-
ids[0] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
513-
ids[1] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
514-
final int[] rowsAffected = new int[] {1, 2};
511+
SqlParameterSource[] ids = new SqlParameterSource[3];
512+
ids[0] = new MapSqlParameterSource().addValue("id", null, Types.NULL);
513+
ids[1] = new MapSqlParameterSource().addValue("id", 100, Types.NUMERIC);
514+
ids[2] = new MapSqlParameterSource().addValue("id", 200, Types.NUMERIC);
515+
final int[] rowsAffected = new int[] {1, 2, 3};
515516

516517
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
517518
given(connection.getMetaData()).willReturn(databaseMetaData);
518519
namedParameterTemplate = new NamedParameterJdbcTemplate(new JdbcTemplate(dataSource, false));
519520

520521
int[] actualRowsAffected = namedParameterTemplate.batchUpdate(
521522
"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = :id", ids);
522-
assertTrue("executed 2 updates", actualRowsAffected.length == 2);
523+
assertTrue("executed 3 updates", actualRowsAffected.length == 3);
523524
assertEquals(rowsAffected[0], actualRowsAffected[0]);
524525
assertEquals(rowsAffected[1], actualRowsAffected[1]);
526+
assertEquals(rowsAffected[2], actualRowsAffected[2]);
525527
verify(connection).prepareStatement("UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?");
528+
verify(preparedStatement).setNull(1, Types.NULL);
526529
verify(preparedStatement).setObject(1, 100, Types.NUMERIC);
527530
verify(preparedStatement).setObject(1, 200, Types.NUMERIC);
528-
verify(preparedStatement, times(2)).addBatch();
531+
verify(preparedStatement, times(3)).addBatch();
529532
verify(preparedStatement, atLeastOnce()).close();
530533
verify(connection, atLeastOnce()).close();
531534
}

0 commit comments

Comments
 (0)