Search in sources :

Example 1 with SqlParameterSource

use of org.springframework.jdbc.core.namedparam.SqlParameterSource in project camel by apache.

the class ElsqlEndpoint method createConsumer.

@Override
public Consumer createConsumer(final Processor processor) throws Exception {
    final SqlProcessingStrategy proStrategy = new ElsqlSqlProcessingStrategy(elSql);
    final SqlPrepareStatementStrategy preStategy = new ElsqlSqlPrepareStatementStrategy();
    final SqlParameterSource param = new EmptySqlParameterSource();
    final String sql = elSql.getSql(elsqlName, new SpringSqlParams(param));
    LOG.debug("ElsqlConsumer @{} using sql: {}", elsqlName, sql);
    final ElsqlConsumer consumer = new ElsqlConsumer(this, processor, namedJdbcTemplate, sql, param, preStategy, proStrategy);
    consumer.setMaxMessagesPerPoll(getMaxMessagesPerPoll());
    consumer.setOnConsume(getOnConsume());
    consumer.setOnConsumeFailed(getOnConsumeFailed());
    consumer.setOnConsumeBatchComplete(getOnConsumeBatchComplete());
    consumer.setBreakBatchOnConsumeFail(isBreakBatchOnConsumeFail());
    consumer.setExpectedUpdateCount(getExpectedUpdateCount());
    consumer.setUseIterator(isUseIterator());
    consumer.setRouteEmptyResultSet(isRouteEmptyResultSet());
    configureConsumer(consumer);
    return consumer;
}
Also used : SqlProcessingStrategy(org.apache.camel.component.sql.SqlProcessingStrategy) EmptySqlParameterSource(org.springframework.jdbc.core.namedparam.EmptySqlParameterSource) SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) SqlPrepareStatementStrategy(org.apache.camel.component.sql.SqlPrepareStatementStrategy) DefaultSqlPrepareStatementStrategy(org.apache.camel.component.sql.DefaultSqlPrepareStatementStrategy) EmptySqlParameterSource(org.springframework.jdbc.core.namedparam.EmptySqlParameterSource) SpringSqlParams(com.opengamma.elsql.SpringSqlParams)

Example 2 with SqlParameterSource

use of org.springframework.jdbc.core.namedparam.SqlParameterSource in project camel by apache.

the class ElsqlProducer method process.

@Override
public void process(final Exchange exchange) throws Exception {
    final Object data = exchange.getIn().getBody();
    final SqlParameterSource param = new ElsqlSqlMapSource(exchange, data);
    final String sql = elSql.getSql(elSqlName, new SpringSqlParams(param));
    LOG.debug("ElsqlProducer @{} using sql: {}", elSqlName, sql);
    // special for processing stream list (batch not supported)
    final SqlOutputType outputType = getEndpoint().getOutputType();
    if (outputType == SqlOutputType.StreamList) {
        processStreamList(exchange, sql, param);
        return;
    }
    log.trace("jdbcTemplate.execute: {}", sql);
    jdbcTemplate.execute(sql, param, new PreparedStatementCallback<Object>() {

        @Override
        public Object doInPreparedStatement(final PreparedStatement ps) throws SQLException, DataAccessException {
            ResultSet rs = null;
            try {
                boolean isResultSet = false;
                final int expected = ps.getParameterMetaData().getParameterCount();
                if (expected > 0 && batch) {
                    final String sqlForDefaultPreparedStamentStrategy = sql.replaceAll(":", ":\\?");
                    final String preparedQuery = sqlPrepareStatementStrategy.prepareQuery(sqlForDefaultPreparedStamentStrategy, getEndpoint().isAllowNamedParameters(), exchange);
                    final Iterator<?> iterator = exchange.getIn().getBody(Iterator.class);
                    while (iterator != null && iterator.hasNext()) {
                        final Object value = iterator.next();
                        final Iterator<?> i = sqlPrepareStatementStrategy.createPopulateIterator(sqlForDefaultPreparedStamentStrategy, preparedQuery, expected, exchange, value);
                        sqlPrepareStatementStrategy.populateStatement(ps, i, expected);
                        ps.addBatch();
                    }
                }
                // execute the prepared statement and populate the outgoing message
                if (batch) {
                    final int[] updateCounts = ps.executeBatch();
                    int total = 0;
                    for (final int count : updateCounts) {
                        total += count;
                    }
                    exchange.getIn().setHeader(SqlConstants.SQL_UPDATE_COUNT, total);
                } else {
                    isResultSet = ps.execute();
                    if (isResultSet) {
                        rs = ps.getResultSet();
                        // preserve headers first, so we can override the SQL_ROW_COUNT header
                        exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
                        final SqlOutputType outputType = getEndpoint().getOutputType();
                        log.trace("Got result list from query: {}, outputType={}", rs, outputType);
                        if (outputType == SqlOutputType.SelectList) {
                            final List<?> data = getEndpoint().queryForList(rs, true);
                            // for noop=true we still want to enrich with the row count header
                            if (getEndpoint().isNoop()) {
                                exchange.getOut().setBody(exchange.getIn().getBody());
                            } else if (getEndpoint().getOutputHeader() != null) {
                                exchange.getOut().setBody(exchange.getIn().getBody());
                                exchange.getOut().setHeader(getEndpoint().getOutputHeader(), data);
                            } else {
                                exchange.getOut().setBody(data);
                            }
                            exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, data.size());
                        } else if (outputType == SqlOutputType.SelectOne) {
                            final Object data = getEndpoint().queryForObject(rs);
                            if (data != null) {
                                // for noop=true we still want to enrich with the row count header
                                if (getEndpoint().isNoop()) {
                                    exchange.getOut().setBody(exchange.getIn().getBody());
                                } else if (getEndpoint().getOutputHeader() != null) {
                                    exchange.getOut().setBody(exchange.getIn().getBody());
                                    exchange.getOut().setHeader(getEndpoint().getOutputHeader(), data);
                                } else {
                                    exchange.getOut().setBody(data);
                                }
                                exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 1);
                            } else {
                                if (getEndpoint().isNoop()) {
                                    exchange.getOut().setBody(exchange.getIn().getBody());
                                } else if (getEndpoint().getOutputHeader() != null) {
                                    exchange.getOut().setBody(exchange.getIn().getBody());
                                }
                                exchange.getOut().setHeader(SqlConstants.SQL_ROW_COUNT, 0);
                            }
                        } else {
                            throw new IllegalArgumentException("Invalid outputType=" + outputType);
                        }
                    } else {
                        // if we are here, there isResultSet is false. This can happen only if we are doing an update operation or there is no result.
                        // we can simply add the updateCount in this case.
                        exchange.getOut().setHeader(SqlConstants.SQL_UPDATE_COUNT, ps.getUpdateCount());
                    }
                }
            } finally {
                closeResultSet(rs);
            }
            return null;
        }
    });
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) SqlOutputType(org.apache.camel.component.sql.SqlOutputType) SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) ResultSet(java.sql.ResultSet) JdbcUtils.closeResultSet(org.springframework.jdbc.support.JdbcUtils.closeResultSet) Iterator(java.util.Iterator) ResultSetIterator(org.apache.camel.component.sql.ResultSetIterator) List(java.util.List) DataAccessException(org.springframework.dao.DataAccessException) SpringSqlParams(com.opengamma.elsql.SpringSqlParams)

Example 3 with SqlParameterSource

use of org.springframework.jdbc.core.namedparam.SqlParameterSource in project camel by apache.

the class ElsqlSqlProcessingStrategy method commit.

@Override
public int commit(DefaultSqlEndpoint defaultSqlEndpoint, Exchange exchange, Object data, NamedParameterJdbcTemplate jdbcTemplate, SqlParameterSource parameterSource, String query) throws Exception {
    final SqlParameterSource param = new ElsqlSqlMapSource(exchange, data);
    final String sql = elSql.getSql(query, new SpringSqlParams(param));
    LOG.debug("commit @{} using sql: {}", query, sql);
    return jdbcTemplate.execute(sql, param, new PreparedStatementCallback<Integer>() {

        @Override
        public Integer doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.execute();
            int updateCount = ps.getUpdateCount();
            if (LOG.isTraceEnabled()) {
                LOG.trace("Update count {}", updateCount);
            }
            return updateCount;
        }
    });
}
Also used : EmptySqlParameterSource(org.springframework.jdbc.core.namedparam.EmptySqlParameterSource) SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.springframework.dao.DataAccessException) SpringSqlParams(com.opengamma.elsql.SpringSqlParams)

Example 4 with SqlParameterSource

use of org.springframework.jdbc.core.namedparam.SqlParameterSource in project camel by apache.

the class ElsqlSqlProcessingStrategy method commitBatchComplete.

@Override
public int commitBatchComplete(DefaultSqlEndpoint endpoint, NamedParameterJdbcTemplate namedJdbcTemplate, SqlParameterSource parameterSource, String query) throws Exception {
    final SqlParameterSource param = new EmptySqlParameterSource();
    final String sql = elSql.getSql(query, new SpringSqlParams(param));
    LOG.debug("commitBatchComplete @{} using sql: {}", query, sql);
    return namedJdbcTemplate.execute(sql, param, new PreparedStatementCallback<Integer>() {

        @Override
        public Integer doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.execute();
            int updateCount = ps.getUpdateCount();
            if (LOG.isTraceEnabled()) {
                LOG.trace("Update count {}", updateCount);
            }
            return updateCount;
        }
    });
}
Also used : EmptySqlParameterSource(org.springframework.jdbc.core.namedparam.EmptySqlParameterSource) SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) SQLException(java.sql.SQLException) EmptySqlParameterSource(org.springframework.jdbc.core.namedparam.EmptySqlParameterSource) PreparedStatement(java.sql.PreparedStatement) DataAccessException(org.springframework.dao.DataAccessException) SpringSqlParams(com.opengamma.elsql.SpringSqlParams)

Example 5 with SqlParameterSource

use of org.springframework.jdbc.core.namedparam.SqlParameterSource in project spring-framework by spring-projects.

the class AbstractJdbcInsert method doExecuteBatch.

/**
	 * Delegate method that executes a batch insert using the passed-in {@link SqlParameterSource}s.
	 * @param batch array of SqlParameterSource with parameter names and values to be used in insert
	 * @return array of number of rows affected
	 */
protected int[] doExecuteBatch(SqlParameterSource... batch) {
    checkCompiled();
    List<List<Object>> batchValues = new ArrayList<>(batch.length);
    for (SqlParameterSource parameterSource : batch) {
        batchValues.add(matchInParameterValuesWithInsertColumns(parameterSource));
    }
    return executeBatchInternal(batchValues);
}
Also used : SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

SqlParameterSource (org.springframework.jdbc.core.namedparam.SqlParameterSource)5 SpringSqlParams (com.opengamma.elsql.SpringSqlParams)4 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 DataAccessException (org.springframework.dao.DataAccessException)3 EmptySqlParameterSource (org.springframework.jdbc.core.namedparam.EmptySqlParameterSource)3 List (java.util.List)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 DefaultSqlPrepareStatementStrategy (org.apache.camel.component.sql.DefaultSqlPrepareStatementStrategy)1 ResultSetIterator (org.apache.camel.component.sql.ResultSetIterator)1 SqlOutputType (org.apache.camel.component.sql.SqlOutputType)1 SqlPrepareStatementStrategy (org.apache.camel.component.sql.SqlPrepareStatementStrategy)1 SqlProcessingStrategy (org.apache.camel.component.sql.SqlProcessingStrategy)1 JdbcUtils.closeResultSet (org.springframework.jdbc.support.JdbcUtils.closeResultSet)1