Skip to content

Commit eeb79c8

Browse files
committed
NamedParameterJdbcTemplate treats arrays/Iterables like Collections
Closes gh-22981
1 parent 9f92b42 commit eeb79c8

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

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

+14-8
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-2019 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.
@@ -22,7 +22,6 @@
2222
import java.sql.SQLException;
2323
import java.sql.Types;
2424
import java.util.Arrays;
25-
import java.util.Collection;
2625
import java.util.Collections;
2726
import java.util.HashSet;
2827
import java.util.LinkedList;
@@ -32,6 +31,7 @@
3231
import org.springframework.dao.InvalidDataAccessApiUsageException;
3332
import org.springframework.lang.Nullable;
3433
import org.springframework.util.Assert;
34+
import org.springframework.util.ObjectUtils;
3535

3636
/**
3737
* Helper class that efficiently creates multiple {@link PreparedStatementCreator}
@@ -268,13 +268,19 @@ public void setValues(PreparedStatement ps) throws SQLException {
268268
}
269269
declaredParameter = declaredParameters.get(i);
270270
}
271-
if (in instanceof Collection && declaredParameter.getSqlType() != Types.ARRAY) {
272-
Collection<?> entries = (Collection<?>) in;
271+
if (in != null && in.getClass().isArray()) {
272+
in = Arrays.asList(ObjectUtils.toObjectArray(in));
273+
}
274+
if (in instanceof Iterable && declaredParameter.getSqlType() != Types.ARRAY) {
275+
Iterable<?> entries = (Iterable<?>) in;
273276
for (Object entry : entries) {
274-
if (entry instanceof Object[]) {
275-
Object[] valueArray = ((Object[])entry);
276-
for (Object argValue : valueArray) {
277-
StatementCreatorUtils.setParameterValue(ps, sqlColIndx++, declaredParameter, argValue);
277+
if (entry != null && entry.getClass().isArray()) {
278+
entry = Arrays.asList(ObjectUtils.toObjectArray(entry));
279+
}
280+
if (entry instanceof Iterable) {
281+
Iterable<?> values = (Iterable<?>) entry;
282+
for (Object value : values) {
283+
StatementCreatorUtils.setParameterValue(ps, sqlColIndx++, declaredParameter, value);
278284
}
279285
}
280286
else {

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

+8-4
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-2019 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.
@@ -17,7 +17,7 @@
1717
package org.springframework.jdbc.core.namedparam;
1818

1919
import java.util.ArrayList;
20-
import java.util.Collection;
20+
import java.util.Arrays;
2121
import java.util.HashSet;
2222
import java.util.Iterator;
2323
import java.util.List;
@@ -29,6 +29,7 @@
2929
import org.springframework.jdbc.core.SqlParameterValue;
3030
import org.springframework.lang.Nullable;
3131
import org.springframework.util.Assert;
32+
import org.springframework.util.ObjectUtils;
3233

3334
/**
3435
* Helper methods for named parameter parsing.
@@ -284,8 +285,11 @@ public static String substituteNamedParameters(ParsedSql parsedSql, @Nullable Sq
284285
if (value instanceof SqlParameterValue) {
285286
value = ((SqlParameterValue) value).getValue();
286287
}
287-
if (value instanceof Collection) {
288-
Iterator<?> entryIter = ((Collection<?>) value).iterator();
288+
if (value != null && value.getClass().isArray()) {
289+
value = Arrays.asList(ObjectUtils.toObjectArray(value));
290+
}
291+
if (value instanceof Iterable) {
292+
Iterator<?> entryIter = ((Iterable<?>) value).iterator();
289293
int k = 0;
290294
while (entryIter.hasNext()) {
291295
if (k > 0) {

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,12 @@ public void testBatchUpdateWithSqlParameterSource() throws Exception {
475475
@Test
476476
public void testBatchUpdateWithInClause() throws Exception {
477477
@SuppressWarnings("unchecked")
478-
Map<String, Object>[] parameters = new Map[2];
478+
Map<String, Object>[] parameters = new Map[3];
479479
parameters[0] = Collections.singletonMap("ids", Arrays.asList(1, 2));
480-
parameters[1] = Collections.singletonMap("ids", Arrays.asList(3, 4));
480+
parameters[1] = Collections.singletonMap("ids", new Integer[] {3, 4});
481+
parameters[2] = Collections.singletonMap("ids", (Iterable<Integer>) () -> Arrays.asList(5, 6).iterator());
481482

482-
final int[] rowsAffected = new int[] {1, 2};
483+
final int[] rowsAffected = new int[] {1, 2, 3};
483484
given(preparedStatement.executeBatch()).willReturn(rowsAffected);
484485
given(connection.getMetaData()).willReturn(databaseMetaData);
485486

@@ -491,7 +492,7 @@ public void testBatchUpdateWithInClause() throws Exception {
491492
parameters
492493
);
493494

494-
assertThat(actualRowsAffected.length).as("executed 2 updates").isEqualTo(2);
495+
assertThat(actualRowsAffected.length).as("executed 3 updates").isEqualTo(3);
495496

496497
InOrder inOrder = inOrder(preparedStatement);
497498

@@ -503,6 +504,10 @@ public void testBatchUpdateWithInClause() throws Exception {
503504
inOrder.verify(preparedStatement).setObject(2, 4);
504505
inOrder.verify(preparedStatement).addBatch();
505506

507+
inOrder.verify(preparedStatement).setObject(1, 5);
508+
inOrder.verify(preparedStatement).setObject(2, 6);
509+
inOrder.verify(preparedStatement).addBatch();
510+
506511
inOrder.verify(preparedStatement, atLeastOnce()).close();
507512
verify(connection, atLeastOnce()).close();
508513
}

0 commit comments

Comments
 (0)