View Javadoc
1   /*
2    * Copyright 2002-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.jdbc.core;
18  
19  import java.sql.PreparedStatement;
20  import java.sql.SQLException;
21  import java.sql.Types;
22  import java.util.Collection;
23  
24  import org.springframework.dao.InvalidDataAccessApiUsageException;
25  
26  /**
27   * Simple adapter for {@link PreparedStatementSetter} that applies
28   * given arrays of arguments and JDBC argument types.
29   *
30   * @author Juergen Hoeller
31   * @since 3.2.3
32   */
33  public class ArgumentTypePreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer {
34  
35  	private final Object[] args;
36  
37  	private final int[] argTypes;
38  
39  
40  	/**
41  	 * Create a new ArgTypePreparedStatementSetter for the given arguments.
42  	 * @param args the arguments to set
43  	 * @param argTypes the corresponding SQL types of the arguments
44  	 */
45  	public ArgumentTypePreparedStatementSetter(Object[] args, int[] argTypes) {
46  		if ((args != null && argTypes == null) || (args == null && argTypes != null) ||
47  				(args != null && args.length != argTypes.length)) {
48  			throw new InvalidDataAccessApiUsageException("args and argTypes parameters must match");
49  		}
50  		this.args = args;
51  		this.argTypes = argTypes;
52  	}
53  
54  
55  	@Override
56  	public void setValues(PreparedStatement ps) throws SQLException {
57  		int parameterPosition = 1;
58  		if (this.args != null) {
59  			for (int i = 0; i < this.args.length; i++) {
60  				Object arg = this.args[i];
61  				if (arg instanceof Collection && this.argTypes[i] != Types.ARRAY) {
62  					Collection<?> entries = (Collection<?>) arg;
63  					for (Object entry : entries) {
64  						if (entry instanceof Object[]) {
65  							Object[] valueArray = ((Object[]) entry);
66  							for (Object argValue : valueArray) {
67  								doSetValue(ps, parameterPosition, this.argTypes[i], argValue);
68  								parameterPosition++;
69  							}
70  						}
71  						else {
72  							doSetValue(ps, parameterPosition, this.argTypes[i], entry);
73  							parameterPosition++;
74  						}
75  					}
76  				}
77  				else {
78  					doSetValue(ps, parameterPosition, this.argTypes[i], arg);
79  					parameterPosition++;
80  				}
81  			}
82  		}
83  	}
84  
85  	/**
86  	 * Set the value for the prepared statement's specified parameter position using the passed in
87  	 * value and type. This method can be overridden by sub-classes if needed.
88  	 * @param ps the PreparedStatement
89  	 * @param parameterPosition index of the parameter position
90  	 * @param argType the argument type
91  	 * @param argValue the argument value
92  	 * @throws SQLException
93  	 */
94  	protected void doSetValue(PreparedStatement ps, int parameterPosition, int argType, Object argValue)
95  			throws SQLException {
96  
97  		StatementCreatorUtils.setParameterValue(ps, parameterPosition, argType, argValue);
98  	}
99  
100 	@Override
101 	public void cleanupParameters() {
102 		StatementCreatorUtils.cleanupParameters(this.args);
103 	}
104 
105 }