Search in sources :

Example 1 with CompositeIterator

use of org.springframework.util.CompositeIterator in project camel by apache.

the class DefaultSqlPrepareStatementStrategy method createInParameterIterator.

@SuppressWarnings("unchecked")
protected static CompositeIterator createInParameterIterator(Object value) {
    Iterator it;
    // if the body is a String then honor quotes etc.
    if (value instanceof String) {
        String[] tokens = StringQuoteHelper.splitSafeQuote((String) value, ',', true);
        List<String> list = Arrays.asList(tokens);
        it = list.iterator();
    } else {
        it = ObjectHelper.createIterator(value, null);
    }
    CompositeIterator ci = new CompositeIterator();
    ci.add(it);
    return ci;
}
Also used : CompositeIterator(org.springframework.util.CompositeIterator) Iterator(java.util.Iterator) CompositeIterator(org.springframework.util.CompositeIterator)

Example 2 with CompositeIterator

use of org.springframework.util.CompositeIterator 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

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