You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Julien Aymé (JIRA)" <ji...@apache.org> on 2009/11/05 13:29:32 UTC
[jira] Issue Comment Edited: (DBUTILS-54) Generated key handling
for updates
[ https://issues.apache.org/jira/browse/DBUTILS-54?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12773915#action_12773915 ]
Julien Aymé edited comment on DBUTILS-54 at 11/5/09 12:28 PM:
--------------------------------------------------------------
Attaching updated version of GenKeyQueryRunner, no need of GeneratedKeysHandler.java anymore.
The new version is used simply this way:
{code}
GenKeyQueryRunner<Object> runner = new GenKeyQueryRunner(ds, new ScalarHandler(), "id");
// The key column to use for auto-generated key retrieval will be the column named "id"
int updates = runner.update("INSERT INTO person(name, height) VALUES (?, ?)", "John Doe", 1.80);
// This will insert a John Doe into the Person table,
// and the RDBMS will generate an auto-incremented id
Long key = (Long) runner.getGeneratedKeys();
// Use the key here
...
{code}
This allow the reuse of existing ResultSetHandler implementations, and the users clearly see that either keyColsByIndex or keyColsByName can be used, but not both in the same time.
was (Author: julien.ayme@gmail.com):
Attaching updated version of GenKeyQueryRunner, no need of GeneratedKeysHandler.java anymore.
The new version is used simply this way:
{code}
GenKeyQueryRunner<Object> runner = new GenKeyQueryRunner(ds, new ScalarHandler(), "id");
// The key column to use for auto-generated key retrieval will be the column named "id"
int updates = runner.update("INSERT INTO person(name, height) VALUES (?, ?)", "John Doe", 1.80);
// This will insert a John Doe into the Person table,
// and the RDBMS will generate an auto-incremented id
Long key = (Long) runner.getGeneratedKeys();
// Use the key here
...
{code}
This allow the reuse of existing ResultSetHandler implementations, and the users clearly see that either keyColsByIndex or keyColsByName can be used, but not both in the same time.
> Generated key handling for updates
> ----------------------------------
>
> Key: DBUTILS-54
> URL: https://issues.apache.org/jira/browse/DBUTILS-54
> Project: Commons DbUtils
> Issue Type: Improvement
> Affects Versions: 1.2, Nightly Builds
> Reporter: Michael V
> Priority: Minor
> Attachments: GeneratedKeysHandler.java, GenKeyQueryRunner.java, GenKeyQueryRunner.java, QueryRunner.patch
>
>
> It would be great (and fairly easy to do) to provide a way to get autogenerated keys from QueryRunner.update. There was an email thread about this in 2004 but it seems it never was actually implemented.
> http://mail-archives.apache.org/mod_mbox/commons-dev/200406.mbox/%3C20040602024627.21604.qmail@web50606.mail.yahoo.com%3E
> The thought is to provide an ability to recover generated keys, for instance by providing a result set handler, in which case prepared statement would be generated with RETURN_GENERATED_KEYS and getGeneratedKeys() would be passed to the result handler.
> It seems that in 1.2 there is a way to get PreparedStatement and work with QueryRunner more as a support to JDBC but IMO it would be cool to add this feature.
> example solution:
> {noformat}
> public int update(Connection conn, String sql, Object... params)
> throws SQLException {
> update(sql, null, params);
> }
> protected PreparedStatement prepareStatement(Connection conn, String sql, int autoGeneratedKeys)
> throws SQLException {
> return conn.prepareStatement(sql, autoGeneratedKeys);
> }
> public int update(Connection conn, String sql, ResultSetHandler<?> rsh, Object... params)
> throws SQLException {
> PreparedStatement stmt = null;
> int rows = 0;
> try {
> stmt = this.prepareStatement(conn, sql, rsh==null?Statement.NO_GENERATED_KEY:Statement.RETURN_GENERATED_KEYS);
> this.fillStatement(stmt, params);
> rows = stmt.executeUpdate();
> if(rsh!=null)
> rsh.handle(stmt.getGeneratedKeys());
> } catch (SQLException e) {
> this.rethrow(e, sql, params);
> } finally {
> close(stmt);
> }
> return rows;
> }
> {noformat}
> Thanks!
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.