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

BatchSqlUpdate support for named parameters [SPR-10435] #15068

Closed
spring-projects-issues opened this issue Apr 2, 2013 · 4 comments
Closed

BatchSqlUpdate support for named parameters [SPR-10435] #15068

spring-projects-issues opened this issue Apr 2, 2013 · 4 comments
Labels
in: data status: bulk-closed

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Apr 2, 2013

Giovanni Botta opened SPR-10435 and commented

The BatchSqlUpdate class doesn't override the updateByNamedParam(Map<String,?> paramMap) method. It would be useful to override this so that named parameters would be supported when performing batch updates and the same map could be reused to pass the parameters around (simple and with minimal space overhead).

NamedParameterJdbcTemplate has the batchUpdate(String sql, Map<String,?>[] batchValues) method to do this but, besides having a different interface, it requires an array of maps, with increased overhead, code bloating and complication reusing the maps (http://stackoverflow.com/questions/15277584/spring-batchsqlupdate-vs-namedparameterjdbctemplate-using-named-parameters).

Thanks
Giovanni


Affects: 3.2 GA

Attachments:

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Apr 3, 2013

Giovanni Botta commented

I wrote a few lines of code to work around the lack of an update(Map) method:

// a SQL statement using named parameters, e.g., insert into t(c1,c2) values (:c1,:c2)
String sql = ...;
// list of named parameters, e.g., {new SqlParameter("c1",Types....),new SqlParameter("c2",Types....)}
List<SqlParameter> parameters = ...;
// the data source
DataSource dataSource = ...;

ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql)
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, null);

BatchSqlUpdate batchSqlUpdate = new BatchSqlUpdate(dataSource, sqlToUse);

for (SqlParameter p : parameters)
  batchSqlUpdate.declareParameter(p);

// the following method can be now used:
void update(Map<String, Object> record) {
  MapSqlParameterSource sqlParameterSource = new MapSqlParameterSource(record);
  Object[] params = NamedParameterUtils.buildValueArray(parsedSql, sqlParameterSource, parameters);
  batchSqlUpdate.update(params);
}

Note that my code uses the existing BatchSqlUpdate implementation. However, a new class extending BatchSqlUpdate could be created that overrides update(Map<String, Object> record) in the same fashion. Using inheritance or composition is just a design choice.
Bottom line: this is a very simple change, easy to implement and to test.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented May 14, 2013

Phil Webb commented

#285

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Apr 10, 2014

Giovanni Dall'Oglio Risso commented

Hello, forgive my intromission.

I propose a small refactor (find attached).
Thanks.

@spring-projects-issues spring-projects-issues added status: waiting-for-triage in: data type: enhancement and removed type: enhancement labels Jan 11, 2019
@rstoyanchev rstoyanchev added status: bulk-closed and removed status: waiting-for-triage labels Jan 11, 2019
@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 12, 2019

Bulk closing outdated, unresolved issues. Please, reopen if still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data status: bulk-closed
Projects
None yet
Development

No branches or pull requests

2 participants