01: package liquibase.database.template;
02:
03: import liquibase.exception.JDBCException;
04:
05: import java.sql.PreparedStatement;
06: import java.sql.SQLException;
07: import java.sql.Types;
08: import java.util.Collection;
09:
10: /**
11: * Simple adapter for PreparedStatementSetter that applies
12: * given arrays of arguments and JDBC argument types.
13: *
14: * @author Spring Framework
15: */
16: class ArgTypePreparedStatementSetter implements PreparedStatementSetter {
17:
18: private final Object[] args;
19:
20: private final int[] argTypes;
21:
22: /**
23: * Create a new ArgTypePreparedStatementSetter for the given arguments.
24: *
25: * @param args the arguments to set
26: * @param argTypes the corresponding SQL types of the arguments
27: */
28: public ArgTypePreparedStatementSetter(Object[] args, int[] argTypes)
29: throws JDBCException {
30: if ((args != null && argTypes == null)
31: || (args == null && argTypes != null)
32: || (args != null && args.length != argTypes.length)) {
33: throw new JDBCException(
34: "args and argTypes parameters must match");
35: }
36: this .args = args;
37: this .argTypes = argTypes;
38: }
39:
40: public void setValues(PreparedStatement ps) throws SQLException {
41: int argIndx = 1;
42: if (this .args != null) {
43: for (int i = 0; i < this .args.length; i++) {
44: Object arg = this .args[i];
45: if (arg instanceof Collection
46: && this .argTypes[i] != Types.ARRAY) {
47: Collection entries = (Collection) arg;
48: for (Object entry : entries) {
49: StatementCreatorUtils.setParameterValue(ps,
50: argIndx++, this.argTypes[i], entry);
51: }
52: } else {
53: StatementCreatorUtils.setParameterValue(ps,
54: argIndx++, this.argTypes[i], arg);
55: }
56: }
57: }
58: }
59:
60: }
|