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.sql.Types;
022import java.util.Collection;
023
024import org.springframework.dao.InvalidDataAccessApiUsageException;
025
026/**
027 * Simple adapter for {@link PreparedStatementSetter} that applies
028 * given arrays of arguments and JDBC argument types.
029 *
030 * @author Juergen Hoeller
031 * @since 3.2.3
032 */
033public class ArgumentTypePreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer {
034
035        private final Object[] args;
036
037        private final int[] argTypes;
038
039
040        /**
041         * Create a new ArgTypePreparedStatementSetter for the given arguments.
042         * @param args the arguments to set
043         * @param argTypes the corresponding SQL types of the arguments
044         */
045        public ArgumentTypePreparedStatementSetter(Object[] args, int[] argTypes) {
046                if ((args != null && argTypes == null) || (args == null && argTypes != null) ||
047                                (args != null && args.length != argTypes.length)) {
048                        throw new InvalidDataAccessApiUsageException("args and argTypes parameters must match");
049                }
050                this.args = args;
051                this.argTypes = argTypes;
052        }
053
054
055        @Override
056        public void setValues(PreparedStatement ps) throws SQLException {
057                int parameterPosition = 1;
058                if (this.args != null) {
059                        for (int i = 0; i < this.args.length; i++) {
060                                Object arg = this.args[i];
061                                if (arg instanceof Collection && this.argTypes[i] != Types.ARRAY) {
062                                        Collection<?> entries = (Collection<?>) arg;
063                                        for (Object entry : entries) {
064                                                if (entry instanceof Object[]) {
065                                                        Object[] valueArray = ((Object[]) entry);
066                                                        for (Object argValue : valueArray) {
067                                                                doSetValue(ps, parameterPosition, this.argTypes[i], argValue);
068                                                                parameterPosition++;
069                                                        }
070                                                }
071                                                else {
072                                                        doSetValue(ps, parameterPosition, this.argTypes[i], entry);
073                                                        parameterPosition++;
074                                                }
075                                        }
076                                }
077                                else {
078                                        doSetValue(ps, parameterPosition, this.argTypes[i], arg);
079                                        parameterPosition++;
080                                }
081                        }
082                }
083        }
084
085        /**
086         * Set the value for the prepared statement's specified parameter position using the passed in
087         * value and type. This method can be overridden by sub-classes if needed.
088         * @param ps the PreparedStatement
089         * @param parameterPosition index of the parameter position
090         * @param argType the argument type
091         * @param argValue the argument value
092         * @throws SQLException if thrown by PreparedStatement methods
093         */
094        protected void doSetValue(PreparedStatement ps, int parameterPosition, int argType, Object argValue)
095                        throws SQLException {
096
097                StatementCreatorUtils.setParameterValue(ps, parameterPosition, argType, argValue);
098        }
099
100        @Override
101        public void cleanupParameters() {
102                StatementCreatorUtils.cleanupParameters(this.args);
103        }
104
105}