Skip to content

Customize PreparedStatementCreatorFactory in getPreparedStatementCreator [SPR-16050] #20599

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions