Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.sql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultProducer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.PreparedStatementCreator;
import static org.springframework.jdbc.support.JdbcUtils.closeResultSet;
public class SqlProducer extends DefaultProducer {
private String query;
private JdbcTemplate jdbcTemplate;
private boolean batch;
private boolean alwaysPopulateStatement;
private SqlPrepareStatementStrategy sqlPrepareStatementStrategy;
private int parametersCount;
public SqlProducer(SqlEndpoint endpoint, String query, JdbcTemplate jdbcTemplate, SqlPrepareStatementStrategy sqlPrepareStatementStrategy,
boolean batch, boolean alwaysPopulateStatement) {
super(endpoint);
this.jdbcTemplate = jdbcTemplate;
this.sqlPrepareStatementStrategy = sqlPrepareStatementStrategy;
this.query = query;
this.batch = batch;
this.alwaysPopulateStatement = alwaysPopulateStatement;
}
@Override
public SqlEndpoint getEndpoint() {
return (SqlEndpoint) super.getEndpoint();
}
public void process(final Exchange exchange) throws Exception {
String queryHeader = exchange.getIn().getHeader(SqlConstants.SQL_QUERY, String.class);
final String sql = queryHeader != null ? queryHeader : query;
final String preparedQuery = sqlPrepareStatementStrategy.prepareQuery(sql, getEndpoint().isAllowNamedParameters());
// CAMEL-7313 - check whether to return generated keys
final Boolean shouldRetrieveGeneratedKeys =
exchange.getIn().getHeader(SqlConstants.SQL_RETRIEVE_GENERATED_KEYS, false, Boolean.class);
PreparedStatementCreator statementCreator = new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
if (!shouldRetrieveGeneratedKeys) {
return con.prepareStatement(preparedQuery);
} else {
Object expectedGeneratedColumns = exchange.getIn().getHeader(SqlConstants.SQL_GENERATED_COLUMNS);
if (expectedGeneratedColumns == null) {
return con.prepareStatement(preparedQuery, Statement.RETURN_GENERATED_KEYS);
} else if (expectedGeneratedColumns instanceof String[]) {
return con.prepareStatement(preparedQuery, (String[]) expectedGeneratedColumns);
} else if (expectedGeneratedColumns instanceof int[]) {
return con.prepareStatement(preparedQuery, (int[]) expectedGeneratedColumns);
} else {
throw new IllegalArgumentException(
"Header specifying expected returning columns isn't an instance of String[] or int[] but "
+ expectedGeneratedColumns.getClass());
}
}
}
};
jdbcTemplate.execute(statementCreator, new PreparedStatementCallback