Consistently return empty array in case of empty batch arguments

Issue: SPR-17476
This commit is contained in:
Juergen Hoeller
2018-11-08 13:43:07 +01:00
parent b6e8674700
commit 362c59c310
4 changed files with 69 additions and 50 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -27,24 +27,29 @@ import org.springframework.lang.Nullable;
* Mainly for internal use within the framework.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 3.0
*/
public abstract class BatchUpdateUtils {
public static int[] executeBatchUpdate(
String sql, final List<Object[]> batchValues, final int[] columnTypes, JdbcOperations jdbcOperations) {
String sql, final List<Object[]> batchArgs, final int[] columnTypes, JdbcOperations jdbcOperations) {
if (batchArgs.isEmpty()) {
return new int[0];
}
return jdbcOperations.batchUpdate(
sql,
new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
Object[] values = batchValues.get(i);
Object[] values = batchArgs.get(i);
setStatementParameters(values, ps, columnTypes);
}
@Override
public int getBatchSize() {
return batchValues.size();
return batchArgs.size();
}
});
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -28,15 +28,16 @@ import org.springframework.jdbc.core.JdbcOperations;
* Mainly for internal use within the framework.
*
* @author Thomas Risberg
* @author Juergen Hoeller
* @since 3.0
*/
public abstract class NamedParameterBatchUpdateUtils extends BatchUpdateUtils {
public static int[] executeBatchUpdateWithNamedParameters(final ParsedSql parsedSql,
final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {
public static int[] executeBatchUpdateWithNamedParameters(
final ParsedSql parsedSql, final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {
if (batchArgs.length <= 0) {
return new int[] {0};
if (batchArgs.length == 0) {
return new int[0];
}
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);