Closed
Description
diego lovison opened SPR-16050 and commented
I would like to create a streamable query using spring jdbc.
public class StreamNamedParameterJdbcTemplate extends NamedParameterJdbcTemplate {
private final DataSource dataSource;
public StreamNamedParameterJdbcTemplate(DataSource dataSource) {
super(dataSource);
this.dataSource = dataSource;
}
@SneakyThrows(SQLException.class)
public <T> Stream<T> streamQuery(String sql, Map<String, Object> parameters, RowMapper<T> rowMapper) {
Connection connection = DataSourceUtils.getConnection(dataSource);
PreparedStatementCreator preparedStatementCreator =
this.getPreparedStatementCreator(sql, new MapSqlParameterSource(parameters));
PreparedStatement preparedStatement = preparedStatementCreator.createPreparedStatement(connection);
StreamableQuery<T> streamableQuery =
new StreamableQuery<>(connection, dataSource, preparedStatement, rowMapper);
return streamableQuery.stream();
}
}
My method is delegating a call to getPreparedStatementCreator in the class NamedParameterJdbcTemplate to avoid copying and pasting the following code:
protected PreparedStatementCreator getPreparedStatementCreator(String sql, SqlParameterSource paramSource) {
ParsedSql parsedSql = getParsedSql(sql);
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
return pscf.newPreparedStatementCreator(params);
}
My project is using DB2 and I need to change the resultSetType parameter value in the class PreparedStatementCreatorFactory.
Today I can only do that copying and past the entire method.
The idea about stream query come from: https://github.com/APNIC-net/spring-jdbctemplate-streams
Issue Links:
- Add iterable JDBC template to process large amount of data [SPR-13900] #18474 Add iterable JDBC template to process large amount of data
- Add Optional Support to JdbcTemplate [SPR-12662] #17262 Add Optional Support to JdbcTemplate
- Creating a SimpleJdbcInsert from a NamedParameterJdbcTemplate [SPR-16241] #20788 Creating a SimpleJdbcInsert from a NamedParameterJdbcTemplate