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