package org.simpleflatmapper.jdbc.spring;
import org.simpleflatmapper.jdbc.JdbcMapper;
import org.simpleflatmapper.jdbc.JdbcMapperFactory;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Provide integration point with Spring JdbcTemplate.
* <p>
* It implements {@link RowMapper}, {@link PreparedStatementCallback} and {@link ResultSetExtractor}.
* Because some JdbcTemplate template signature match against a few of those type you might need to downcast, declare the variable with a specific type or use the type specific method in {@link JdbcTemplateMapperFactory}.
*
* <p>
*
* <code>
* class MyDao {<br>
* private final JdbcTemplateMapper<DbObject> jdbcMapper =<br>
* JdbcTemplateMapperFactory.newInstance().newMapper(DbObject.class);<br>
* private final RowMapper<DbObject> rowMapper = jdbcMapper;<br>
*<br>
* public void doSomething() {<br>
* List<DbObject> results = template.query(DbHelper.TEST_DB_OBJECT_QUERY, rowMapper);<br>
* }<br>
*<br>
* public void doSomethingElse() {<br>
* template<br>
* .query(TEST_DB_OBJECT_QUERY,<br>
* jdbcMapper.newResultSetExtractor((o) -> System.out.println(o.toString())));<br>
* }<br>
* }<br>
*</code>
*
* @param <T> the mapped type
* @see JdbcMapperFactory
* @see JdbcMapper
* @see JdbcTemplateMapperFactory#newPreparedStatementCallback(Class)
* @see JdbcTemplateMapperFactory#newResultSetExtractor(Class)
* @see JdbcTemplateMapperFactory#newRowMapper(Class)
*
*/
public final class RowMapperImpl<T> implements RowMapper<T> {
private final JdbcMapper<T> mapper;
public RowMapperImpl(JdbcMapper<T> mapper) {
this.mapper = mapper;
}
@Override
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
return mapper.map(rs);
}
}