Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize PreparedStatementCreatorFactory in getPreparedStatementCreator [SPR-16050] #20599

Closed
spring-projects-issues opened this issue Oct 3, 2017 · 1 comment
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 3, 2017

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:

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

I've extracted a common getPreparedStatementCreator variant with an optional Consumer<PreparedStatementCreatorFactory> argument. We're using that internally not only from the query but also from the update operations now where we need to customize the generated key handling via PreparedStatementCreatorFactory. In custom operations like yours, you may call that same getPreparedStatementCreator variant with a customizer lambda expression that adapts the result set type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants