01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: import java.util.Iterator;
24:
25: import org.springframework.dao.InvalidDataAccessApiUsageException;
26:
27: /**
28: * Simple adapter for PreparedStatementSetter that applies
29: * given arrays of arguments and JDBC argument types.
30: *
31: * @author Juergen Hoeller
32: */
33: class ArgTypePreparedStatementSetter implements
34: PreparedStatementSetter, ParameterDisposer {
35:
36: private final Object[] args;
37:
38: private final int[] argTypes;
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 ArgTypePreparedStatementSetter(Object[] args, int[] argTypes) {
46: if ((args != null && argTypes == null)
47: || (args == null && argTypes != null)
48: || (args != null && args.length != argTypes.length)) {
49: throw new InvalidDataAccessApiUsageException(
50: "args and argTypes parameters must match");
51: }
52: this .args = args;
53: this .argTypes = argTypes;
54: }
55:
56: public void setValues(PreparedStatement ps) throws SQLException {
57: int argIndx = 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
62: && this .argTypes[i] != Types.ARRAY) {
63: Collection entries = (Collection) arg;
64: for (Iterator it = entries.iterator(); it.hasNext();) {
65: Object entry = it.next();
66: if (entry instanceof Object[]) {
67: Object[] valueArray = ((Object[]) entry);
68: for (int k = 0; k < valueArray.length; k++) {
69: Object argValue = valueArray[k];
70: StatementCreatorUtils
71: .setParameterValue(ps,
72: argIndx++,
73: this .argTypes[i],
74: argValue);
75: }
76: } else {
77: StatementCreatorUtils.setParameterValue(ps,
78: argIndx++, this .argTypes[i], entry);
79: }
80: }
81: } else {
82: StatementCreatorUtils.setParameterValue(ps,
83: argIndx++, this .argTypes[i], arg);
84: }
85: }
86: }
87: }
88:
89: public void cleanupParameters() {
90: StatementCreatorUtils.cleanupParameters(this.args);
91: }
92:
93: }
|