added batchUpdate methods to NamedParameterJdbcTemplate (SPR-3322)

This commit is contained in:
Thomas Risberg
2009-05-28 03:15:47 +00:00
parent 88e32a3cfe
commit 4105957596
8 changed files with 292 additions and 172 deletions

View File

@@ -0,0 +1,68 @@
/*
* Copyright 2002-2008 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.core;
import java.util.List;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* Generic utility methods for working with JDBC batch statements. Mainly for internal use
* within the framework.
*
* @author Thomas Risberg
*/
public abstract class BatchUpdateUtils {
public static int[] executeBatchUpdate(String sql, final List<Object[]> batchValues, final int[] columnTypes, JdbcOperations jdbcOperations) {
return jdbcOperations.batchUpdate(
sql,
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
Object[] values = batchValues.get(i);
setStatementParameters(values, ps, columnTypes);
}
public int getBatchSize() {
return batchValues.size();
}
});
}
protected static void setStatementParameters(Object[] values, PreparedStatement ps, int[] columnTypes) throws SQLException {
int colIndex = 0;
for (Object value : values) {
colIndex++;
if (value instanceof SqlParameterValue) {
SqlParameterValue paramValue = (SqlParameterValue) value;
StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue());
}
else {
int colType;
if (columnTypes == null || columnTypes.length < colIndex) {
colType = SqlTypeValue.TYPE_UNKNOWN;
}
else {
colType = columnTypes[colIndex - 1];
}
StatementCreatorUtils.setParameterValue(ps, colIndex, colType, value);
}
}
}
}

View File

@@ -0,0 +1,40 @@
package org.springframework.jdbc.core.namedparam;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.jdbc.core.BatchUpdateUtils;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
/**
* Generic utility methods for working with JDBC batch statements. Mainly for internal use
* within the framework.
*
* @author Thomas Risberg
*/
public class NamedParameterBatchUpdateUtils extends BatchUpdateUtils {
public static int[] executeBatchUpdateWithNamedParameters(String sql, final SqlParameterSource[] batchArgs, JdbcOperations jdbcOperations) {
if (batchArgs.length <= 0) {
return new int[] {0};
}
final ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);
return jdbcOperations.batchUpdate(
sqlToUse,
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]);
setStatementParameters(values, ps, columnTypes);
}
public int getBatchSize() {
return batchArgs.length;
}
});
}
}

View File

@@ -493,4 +493,20 @@ public interface NamedParameterJdbcOperations {
int update(String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames)
throws DataAccessException;
/**
* Executes a batch using the supplied SQL statement with the batch of supplied arguments.
* @param sql the SQL statement to execute
* @param batchValues the array of Maps containing the batch of arguments for the query
* @return an array containing the numbers of rows affected by each update in the batch
*/
public int[] batchUpdate(String sql, Map<String, Object>[] batchValues);
/**
* Execute a batch using the supplied SQL statement with the batch of supplied arguments.
* @param sql the SQL statement to execute
* @param batchArgs the array of {@link SqlParameterSource} containing the batch of arguments for the query
* @return an array containing the numbers of rows affected by each update in the batch
*/
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs);
}

View File

@@ -261,6 +261,19 @@ public class NamedParameterJdbcTemplate implements NamedParameterJdbcOperations
return getJdbcOperations().update(pscf.newPreparedStatementCreator(params), generatedKeyHolder);
}
public int[] batchUpdate(String sql, Map<String, Object>[] batchValues) {
SqlParameterSource[] batchArgs = new SqlParameterSource[batchValues.length];
int i = 0;
for (Map<String, Object> values : batchValues) {
batchArgs[i] = new MapSqlParameterSource(values);
i++;
}
return batchUpdate(sql, batchArgs);
}
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) {
return NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters(sql, batchArgs, getJdbcOperations());
}
/**
* Build a PreparedStatementCreator based on the given SQL and named parameters.

View File

@@ -16,26 +16,20 @@
package org.springframework.jdbc.core.simple;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.SqlParameterValue;
import org.springframework.jdbc.core.SqlTypeValue;
import org.springframework.jdbc.core.StatementCreatorUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.BatchUpdateUtils;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterUtils;
import org.springframework.jdbc.core.namedparam.ParsedSql;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterBatchUpdateUtils;
import org.springframework.util.ObjectUtils;
/**
@@ -241,11 +235,11 @@ public class SimpleJdbcTemplate implements SimpleJdbcOperations {
}
public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
return doExecuteBatchUpdate(sql, batchArgs, new int[0]);
return BatchUpdateUtils.executeBatchUpdate(sql, batchArgs, new int[0], getJdbcOperations());
}
public int[] batchUpdate(String sql, List<Object[]> batchArgs, int[] argTypes) {
return doExecuteBatchUpdate(sql, batchArgs, argTypes);
return batchUpdate(sql, batchArgs, argTypes);
}
public int[] batchUpdate(String sql, Map<String, Object>[] batchValues) {
@@ -255,71 +249,11 @@ public class SimpleJdbcTemplate implements SimpleJdbcOperations {
batchArgs[i] = new MapSqlParameterSource(values);
i++;
}
return doExecuteBatchUpdateWithNamedParameters(sql, batchArgs);
return batchUpdate(sql, batchArgs);
}
public int[] batchUpdate(String sql, SqlParameterSource[] batchArgs) {
return doExecuteBatchUpdateWithNamedParameters(sql, batchArgs);
}
private int[] doExecuteBatchUpdate(String sql, final List<Object[]> batchValues, final int[] columnTypes) {
return getJdbcOperations().batchUpdate(
sql,
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
Object[] values = batchValues.get(i);
doSetStatementParameters(values, ps, columnTypes);
}
public int getBatchSize() {
return batchValues.size();
}
});
}
private int[] doExecuteBatchUpdateWithNamedParameters(String sql, final SqlParameterSource[] batchArgs) {
if (batchArgs.length <= 0) {
return new int[] {0};
}
final ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, batchArgs[0]);
return getJdbcOperations().batchUpdate(
sqlToUse,
new BatchPreparedStatementSetter() {
public void setValues(PreparedStatement ps, int i) throws SQLException {
Object[] values = NamedParameterUtils.buildValueArray(parsedSql, batchArgs[i], null);
int[] columnTypes = NamedParameterUtils.buildSqlTypeArray(parsedSql, batchArgs[i]);
doSetStatementParameters(values, ps, columnTypes);
}
public int getBatchSize() {
return batchArgs.length;
}
});
}
private void doSetStatementParameters(Object[] values, PreparedStatement ps, int[] columnTypes) throws SQLException {
int colIndex = 0;
for (Object value : values) {
colIndex++;
if (value instanceof SqlParameterValue) {
SqlParameterValue paramValue = (SqlParameterValue) value;
StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue());
}
else {
int colType;
if (columnTypes == null || columnTypes.length < colIndex) {
colType = SqlTypeValue.TYPE_UNKNOWN;
}
else {
colType = columnTypes[colIndex - 1];
}
StatementCreatorUtils.setParameterValue(ps, colIndex, colType, value);
}
}
return NamedParameterBatchUpdateUtils.executeBatchUpdateWithNamedParameters(sql, batchArgs, getJdbcOperations());
}