commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Dan Fabulich (JIRA)" <>
Subject [jira] Commented: (DBUTILS-54) Generated key handling for updates
Date Tue, 03 Nov 2009 20:39:32 GMT


Dan Fabulich commented on DBUTILS-54:

I have to -1 this patch as-is.  It's just too much code to add to QueryRunner.  The patch
is 500 lines long (the file today is only 723 lines) and it adds 21 methods, more than doubling
the size of the API.  This will make the class harder to read and understand.

QueryRunner adds very little of value in INSERT/UPDATE statements except for its ability to
fill out the parameters of prepared statements.  But you can use QueryRunner#fillStatement
today in 1.2 without having QueryRunner actually prepare your statement.

Users that need to prepare their own statements (as eugen does) or needs to do something with
the resulting statement can prepare the statement themselves just as easily as we can (if
not easier) without complicating the API.

Fleshing out eugen's example above:

stmt = con.prepareStatement(sql,new String[]{"personID"}); 
queryRunner.fillStatement(stmt, "foo", "bar", "baz");
rs = stmt.getGeneratedKeys();

This is easy code to write.  (That was just five lines of code.)  I don't think adding lots
of code on our end makes it any easier; if anything, it makes it harder to understand and
use the API.

> Generated key handling for updates
> ----------------------------------
>                 Key: DBUTILS-54
>                 URL:
>             Project: Commons DbUtils
>          Issue Type: Improvement
>    Affects Versions: 1.2, Nightly Builds
>            Reporter: Michael V
>            Priority: Minor
>         Attachments: 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.
> 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...
>         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.

View raw message