org.simpleflatmapper.jdbc.spring.PreparedStatementCallbackImpl Maven / Gradle / Ivy
package org.simpleflatmapper.jdbc.spring;
import org.simpleflatmapper.jdbc.JdbcMapper;
import org.simpleflatmapper.util.CheckedConsumer;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.ResultSetExtractor;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public final class PreparedStatementCallbackImpl implements PreparedStatementCallback> {
private final ResultSetExtractorImpl resultSetExtractor;
public PreparedStatementCallbackImpl(JdbcMapper mapper) {
this.resultSetExtractor = new ResultSetExtractorImpl(mapper);
}
@Override
public List doInPreparedStatement(PreparedStatement ps) throws SQLException,
DataAccessException {
ResultSet rs = ps.executeQuery();
try {
return resultSetExtractor.extractData(rs);
} finally {
rs.close();
}
}
public > PreparedStatementCallback newPreparedStatementCallback(final H handler) {
return new PreparedStatementCallback() {
@Override
public H doInPreparedStatement(
PreparedStatement ps)
throws SQLException, DataAccessException {
ResultSet rs = ps.executeQuery();
ResultSetExtractor extractor = resultSetExtractor.newResultSetExtractor(handler);
try {
return extractor.extractData(rs);
} finally {
rs.close();
}
}
};
}
}