001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.component.sql; 018 019 import java.sql.PreparedStatement; 020 import java.sql.SQLException; 021 import java.util.Iterator; 022 import java.util.List; 023 024 import org.apache.camel.Exchange; 025 import org.apache.camel.NoTypeConversionAvailableException; 026 import org.apache.camel.impl.DefaultProducer; 027 import org.springframework.dao.DataAccessException; 028 import org.springframework.jdbc.core.ColumnMapRowMapper; 029 import org.springframework.jdbc.core.JdbcTemplate; 030 import org.springframework.jdbc.core.PreparedStatementCallback; 031 import org.springframework.jdbc.core.RowMapperResultSetExtractor; 032 033 public class SqlProducer extends DefaultProducer { 034 public static final String UPDATE_COUNT = "org.apache.camel.sql.update-count"; 035 private String query; 036 private JdbcTemplate jdbcTemplate; 037 038 public SqlProducer(SqlEndpoint endpoint, String query, JdbcTemplate jdbcTemplate) { 039 super(endpoint); 040 this.jdbcTemplate = jdbcTemplate; 041 this.query = query; 042 } 043 044 public void process(final Exchange exchange) throws Exception { 045 jdbcTemplate.execute(query, new PreparedStatementCallback() { 046 public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, 047 DataAccessException { 048 int argNumber = 1; 049 try { 050 Iterator<?> iterator = exchange.getIn().getBody(Iterator.class); 051 while (iterator != null && iterator.hasNext()) { 052 ps.setObject(argNumber++, iterator.next()); 053 } 054 } catch (NoTypeConversionAvailableException e) { 055 // ignored - assumed no parameters have to be used 056 } 057 058 // number of parameters must match 059 int expected = ps.getParameterMetaData().getParameterCount(); 060 if (argNumber - 1 != expected) { 061 throw new SQLException("Number of parameters mismatch. Expected: " + expected + ", was:" + (argNumber - 1)); 062 } 063 064 boolean isResultSet = ps.execute(); 065 066 if (isResultSet) { 067 RowMapperResultSetExtractor mapper = new RowMapperResultSetExtractor(new ColumnMapRowMapper()); 068 List<?> result = (List<?>) mapper.extractData(ps.getResultSet()); 069 exchange.getOut().setBody(result); 070 // preserve headers 071 exchange.getOut().setHeaders(exchange.getIn().getHeaders()); 072 } else { 073 exchange.getIn().setHeader(UPDATE_COUNT, ps.getUpdateCount()); 074 } 075 076 // data is set on exchange so return null 077 return null; 078 } 079 }); 080 } 081 082 }