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.support.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.closeConnection;
import static org.springframework.jdbc.support.JdbcUtils.closeResultSet;
import static org.springframework.jdbc.support.JdbcUtils.closeStatement;
public class SqlProducer extends DefaultProducer {
private final String query;
private String resolvedQuery;
private final JdbcTemplate jdbcTemplate;
private final boolean batch;
private final boolean alwaysPopulateStatement;
private final SqlPrepareStatementStrategy sqlPrepareStatementStrategy;
private final boolean useMessageBodyForSql;
private int parametersCount;
public SqlProducer(SqlEndpoint endpoint, String query, JdbcTemplate jdbcTemplate, SqlPrepareStatementStrategy sqlPrepareStatementStrategy,
boolean batch, boolean alwaysPopulateStatement, boolean useMessageBodyForSql) {
super(endpoint);
this.jdbcTemplate = jdbcTemplate;
this.sqlPrepareStatementStrategy = sqlPrepareStatementStrategy;
this.query = query;
this.batch = batch;
this.alwaysPopulateStatement = alwaysPopulateStatement;
this.useMessageBodyForSql = useMessageBodyForSql;
}
@Override
public SqlEndpoint getEndpoint() {
return (SqlEndpoint) super.getEndpoint();
}
@Override
protected void doStart() throws Exception {
super.doStart();
String placeholder = getEndpoint().isUsePlaceholder() ? getEndpoint().getPlaceholder() : null;
resolvedQuery = SqlHelper.resolveQuery(getEndpoint().getCamelContext(), query, placeholder);
}
public void process(final Exchange exchange) throws Exception {
final String sql;
if (useMessageBodyForSql) {
sql = exchange.getIn().getBody(String.class);
} else {
String queryHeader = exchange.getIn().getHeader(SqlConstants.SQL_QUERY, String.class);
sql = queryHeader != null ? queryHeader : resolvedQuery;
}
final String preparedQuery = sqlPrepareStatementStrategy.prepareQuery(sql, getEndpoint().isAllowNamedParameters(), exchange);
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());
}
}
}
};
// special for processing stream list (batch not supported)
SqlOutputType outputType = getEndpoint().getOutputType();
if (outputType == SqlOutputType.StreamList) {
processStreamList(exchange, statementCreator, sql, preparedQuery);
return;
}
log.trace("jdbcTemplate.execute: {}", preparedQuery);
jdbcTemplate.execute(statementCreator, new PreparedStatementCallback