1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29
30
31
32
33 public class ArgumentTypePreparedStatementSetter implements PreparedStatementSetter, ParameterDisposer {
34
35 private final Object[] args;
36
37 private final int[] argTypes;
38
39
40
41
42
43
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
87
88
89
90
91
92
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 }