NamedParameterJdbcTemplate treats arrays/Iterables like Collections

Closes gh-22981
This commit is contained in:
Juergen Hoeller
2019-06-11 18:26:40 +02:00
parent 9f92b42d69
commit eeb79c8dde
3 changed files with 31 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
@@ -32,6 +31,7 @@ import java.util.Set;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Helper class that efficiently creates multiple {@link PreparedStatementCreator}
@@ -268,13 +268,19 @@ public class PreparedStatementCreatorFactory {
}
declaredParameter = declaredParameters.get(i);
}
if (in instanceof Collection && declaredParameter.getSqlType() != Types.ARRAY) {
Collection<?> entries = (Collection<?>) in;
if (in != null && in.getClass().isArray()) {
in = Arrays.asList(ObjectUtils.toObjectArray(in));
}
if (in instanceof Iterable && declaredParameter.getSqlType() != Types.ARRAY) {
Iterable<?> entries = (Iterable<?>) in;
for (Object entry : entries) {
if (entry instanceof Object[]) {
Object[] valueArray = ((Object[])entry);
for (Object argValue : valueArray) {
StatementCreatorUtils.setParameterValue(ps, sqlColIndx++, declaredParameter, argValue);
if (entry != null && entry.getClass().isArray()) {
entry = Arrays.asList(ObjectUtils.toObjectArray(entry));
}
if (entry instanceof Iterable) {
Iterable<?> values = (Iterable<?>) entry;
for (Object value : values) {
StatementCreatorUtils.setParameterValue(ps, sqlColIndx++, declaredParameter, value);
}
}
else {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,7 @@
package org.springframework.jdbc.core.namedparam;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@@ -29,6 +29,7 @@ import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlParameterValue;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Helper methods for named parameter parsing.
@@ -284,8 +285,11 @@ public abstract class NamedParameterUtils {
if (value instanceof SqlParameterValue) {
value = ((SqlParameterValue) value).getValue();
}
if (value instanceof Collection) {
Iterator<?> entryIter = ((Collection<?>) value).iterator();
if (value != null && value.getClass().isArray()) {
value = Arrays.asList(ObjectUtils.toObjectArray(value));
}
if (value instanceof Iterable) {
Iterator<?> entryIter = ((Iterable<?>) value).iterator();
int k = 0;
while (entryIter.hasNext()) {
if (k > 0) {