Search in sources :

Example 1 with ArgumentPreparedStatementSetter

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);
}
Also used : CompositeIterator(org.springframework.util.CompositeIterator) SQLException(java.sql.SQLException) Iterator(java.util.Iterator) CompositeIterator(org.springframework.util.CompositeIterator) ArgumentPreparedStatementSetter(org.springframework.jdbc.core.ArgumentPreparedStatementSetter)

Aggregations

SQLException (java.sql.SQLException)1 Iterator (java.util.Iterator)1 ArgumentPreparedStatementSetter (org.springframework.jdbc.core.ArgumentPreparedStatementSetter)1 CompositeIterator (org.springframework.util.CompositeIterator)1