org.apache.camel.component.jdbc.DefaultJdbcPrepareStatementStrategy Maven / Gradle / Ivy
/*
* 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.jdbc;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.camel.Exchange;
import org.apache.camel.RuntimeExchangeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Default {@link JdbcPrepareStatementStrategy} which is a copy from the camel-sql component having
* this functionality first.
*/
public class DefaultJdbcPrepareStatementStrategy implements JdbcPrepareStatementStrategy {
private static final Logger LOG = LoggerFactory.getLogger(DefaultJdbcPrepareStatementStrategy.class);
@Override
public String prepareQuery(String query, boolean allowNamedParameters) throws SQLException {
String answer;
if (allowNamedParameters && hasNamedParameters(query)) {
// replace all :?word with just ?
answer = query.replaceAll("\\:\\?\\w+", "\\?");
} else {
answer = query;
}
LOG.trace("Prepared query: {}", answer);
return answer;
}
@Override
public Iterator> createPopulateIterator(final String query, final String preparedQuery, final int expectedParams,
final Exchange exchange, final Object value) throws SQLException {
Map, ?> map = null;
if (exchange.getIn().hasHeaders()) {
if (exchange.getIn().getHeader(JdbcConstants.JDBC_PARAMETERS) != null) {
// header JDBC_PARAMETERS takes precedence over regular headers
map = exchange.getIn().getHeader(JdbcConstants.JDBC_PARAMETERS, Map.class);
} else {
map = exchange.getIn().getHeaders();
}
}
final Map, ?> headerMap = map;
if (hasNamedParameters(query)) {
// create an iterator that returns the value in the named order
try {
return new Iterator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy