001 package com.alexkasko.springjdbc.iterable;
002
003 import org.springframework.dao.DataAccessException;
004 import org.springframework.jdbc.core.ColumnMapRowMapper;
005 import org.springframework.jdbc.core.RowMapper;
006 import org.springframework.jdbc.core.SingleColumnRowMapper;
007 import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
008 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
009 import org.springframework.jdbc.core.namedparam.SqlParameterSource;
010
011 import javax.sql.DataSource;
012 import java.util.Map;
013
014 /**
015 * {@code NamedParameterJdbcTemplate} extension. All methods, that return {@code List}
016 * mirrored with {@code queryForIter} methods that return {@link CloseableIterator}.
017 *
018 * @author alexkasko
019 * Date: 11/7/12
020 */
021 public class IterableNamedParameterJdbcTemplate extends NamedParameterJdbcTemplate implements IterableNamedParameterJdbcOperations {
022
023 /**
024 * Constructor
025 *
026 * @param dataSource data source
027 */
028 public IterableNamedParameterJdbcTemplate(DataSource dataSource) {
029 super(new IterableJdbcTemplate(dataSource));
030 }
031
032 /**
033 * Constructor, takes {@code fetchSize}
034 *
035 * @param dataSource data source
036 * @param fetchSize <a href=http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/jdbc/core/JdbcTemplate.html#setFetchSize%28int%29>fetchSize</a> property value
037 */
038 public IterableNamedParameterJdbcTemplate(DataSource dataSource, int fetchSize) {
039 super(new IterableJdbcTemplate(dataSource, fetchSize));
040 }
041
042 /**
043 * {@inheritDoc}
044 */
045 @Override
046 public IterableJdbcOperations getIterableJdbcOperations() {
047 return (IterableJdbcOperations) getJdbcOperations();
048 }
049
050 /**
051 * {@inheritDoc}
052 */
053 @Override
054 public <T> CloseableIterator<T> queryForIter(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper) throws DataAccessException {
055 return getIterableJdbcOperations().queryForIter(getPreparedStatementCreator(sql, paramSource), rowMapper);
056 }
057
058 /**
059 * {@inheritDoc}
060 */
061 @Override
062 public <T> CloseableIterator<T> queryForIter(String sql, Map<String, ?> paramMap, RowMapper<T> rowMapper) throws DataAccessException {
063 return queryForIter(sql, new MapSqlParameterSource(paramMap), rowMapper);
064 }
065
066 /**
067 * {@inheritDoc}
068 */
069 @Override
070 public <T> CloseableIterator<T> queryForIter(String sql, SqlParameterSource paramSource, Class<T> elementType) throws DataAccessException {
071 return queryForIter(sql, paramSource, new SingleColumnRowMapper<T>(elementType));
072 }
073
074 /**
075 * {@inheritDoc}
076 */
077 @Override
078 public <T> CloseableIterator<T> queryForIter(String sql, Map<String, ?> paramMap, Class<T> elementType) throws DataAccessException {
079 return queryForIter(sql, new MapSqlParameterSource(paramMap), elementType);
080 }
081
082 /**
083 * {@inheritDoc}
084 */
085 @Override
086 public CloseableIterator<Map<String, Object>> queryForIter(String sql, SqlParameterSource paramSource) throws DataAccessException {
087 return queryForIter(sql, paramSource, new ColumnMapRowMapper());
088 }
089
090 /**
091 * {@inheritDoc}
092 */
093 @Override
094 public CloseableIterator<Map<String, Object>> queryForIter(String sql, Map<String, ?> paramMap) throws DataAccessException {
095 return queryForIter(sql, new MapSqlParameterSource(paramMap));
096 }
097 }