001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.jdbc.core;
018
019import java.sql.PreparedStatement;
020import java.sql.SQLException;
021import java.util.List;
022
023import org.springframework.lang.Nullable;
024
025/**
026 * Generic utility methods for working with JDBC batch statements.
027 * Mainly for internal use within the framework.
028 *
029 * @author Thomas Risberg
030 * @author Juergen Hoeller
031 * @since 3.0
032 * @deprecated as of 5.1.3, not used by {@link JdbcTemplate} anymore
033 */
034@Deprecated
035public abstract class BatchUpdateUtils {
036
037        public static int[] executeBatchUpdate(
038                        String sql, final List<Object[]> batchArgs, final int[] columnTypes, JdbcOperations jdbcOperations) {
039
040                if (batchArgs.isEmpty()) {
041                        return new int[0];
042                }
043
044                return jdbcOperations.batchUpdate(
045                                sql,
046                                new BatchPreparedStatementSetter() {
047                                        @Override
048                                        public void setValues(PreparedStatement ps, int i) throws SQLException {
049                                                Object[] values = batchArgs.get(i);
050                                                setStatementParameters(values, ps, columnTypes);
051                                        }
052                                        @Override
053                                        public int getBatchSize() {
054                                                return batchArgs.size();
055                                        }
056                                });
057        }
058
059        protected static void setStatementParameters(Object[] values, PreparedStatement ps, @Nullable int[] columnTypes)
060                        throws SQLException {
061
062                int colIndex = 0;
063                for (Object value : values) {
064                        colIndex++;
065                        if (value instanceof SqlParameterValue) {
066                                SqlParameterValue paramValue = (SqlParameterValue) value;
067                                StatementCreatorUtils.setParameterValue(ps, colIndex, paramValue, paramValue.getValue());
068                        }
069                        else {
070                                int colType;
071                                if (columnTypes == null || columnTypes.length < colIndex) {
072                                        colType = SqlTypeValue.TYPE_UNKNOWN;
073                                }
074                                else {
075                                        colType = columnTypes[colIndex - 1];
076                                }
077                                StatementCreatorUtils.setParameterValue(ps, colIndex, colType, value);
078                        }
079                }
080        }
081
082}