use of org.springframework.jdbc.core.ArgumentPreparedStatementSetter in project camel by apache.
the class DefaultSqlPrepareStatementStrategy method populateStatement.
@Override
public void populateStatement(PreparedStatement ps, Iterator<?> iterator, int expectedParams) throws SQLException {
if (expectedParams <= 0) {
return;
}
final Object[] args = new Object[expectedParams];
int i = 0;
int argNumber = 1;
while (iterator != null && iterator.hasNext()) {
Object value = iterator.next();
// special for SQL IN where we need to set dynamic number of values
if (value instanceof CompositeIterator) {
Iterator it = (Iterator) value;
while (it.hasNext()) {
Object val = it.next();
LOG.trace("Setting parameter #{} with value: {}", argNumber, val);
if (argNumber <= expectedParams) {
args[i] = val;
}
argNumber++;
i++;
}
} else {
LOG.trace("Setting parameter #{} with value: {}", argNumber, value);
if (argNumber <= expectedParams) {
args[i] = value;
}
argNumber++;
i++;
}
}
if (argNumber - 1 != expectedParams) {
throw new SQLException("Number of parameters mismatch. Expected: " + expectedParams + ", was: " + (argNumber - 1));
}
// use argument setter as it deals with various JDBC drivers setObject vs setLong/setInteger/setString etc.
ArgumentPreparedStatementSetter setter = new ArgumentPreparedStatementSetter(args);
setter.setValues(ps);
}
Aggregations