From issues-return-6696-apmail-commons-issues-archive=commons.apache.org@commons.apache.org Thu Feb 05 16:12:25 2009 Return-Path: Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: (qmail 70436 invoked from network); 5 Feb 2009 16:12:25 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 5 Feb 2009 16:12:25 -0000 Received: (qmail 22468 invoked by uid 500); 5 Feb 2009 16:12:24 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 22365 invoked by uid 500); 5 Feb 2009 16:12:23 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 22014 invoked by uid 99); 5 Feb 2009 16:12:23 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 05 Feb 2009 08:12:23 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 05 Feb 2009 16:12:21 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id D6606234C4A7 for ; Thu, 5 Feb 2009 08:11:59 -0800 (PST) Message-ID: <911952733.1233850319877.JavaMail.jira@brutus> Date: Thu, 5 Feb 2009 08:11:59 -0800 (PST) From: "Dan Fabulich (JIRA)" To: issues@commons.apache.org Subject: [jira] Updated: (DBUTILS-48) Maintaining a parallel Java 1.5 version of DButils In-Reply-To: <1444610128.1228169144508.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org [ https://issues.apache.org/jira/browse/DBUTILS-48?page=3Dcom.atlassia= n.jira.plugin.system.issuetabpanels:all-tabpanel ] Dan Fabulich updated DBUTILS-48: -------------------------------- Attachment: generics.patch Here's a patch to update DbUtils for 1.5. It's backwards-compatible with t= he old API; in fact, most of my changes are type-erased at compile time. The biggest change was to change the order of the QueryRunner.query method = signatures (var-args have to go at the end). The old functions are left in= , but deprecated. I also added a helper static method ResultSetIterator.iterable, which retur= ns an Iterable, for use in for-each loops. You'd use it like this: for (Object[] row : ResultSetIterator.iterable(resultSet) { // do stuff } > Maintaining a parallel Java 1.5 version of DButils > -------------------------------------------------- > > Key: DBUTILS-48 > URL: https://issues.apache.org/jira/browse/DBUTILS-48 > Project: Commons DbUtils > Issue Type: New Feature > Reporter: Olivier Gr=C3=A9goire > Priority: Minor > Fix For: 1.2 > > Attachments: generics.patch > > > Well, DButils was a great step forward when managing a database. But Java= 5 came and added a lot of new features to the language, including generics= , which I think is easy to put in place. > The base change of all this is changing the signature of ResultSetHandler= : > public interface ResultSetHandler { > public T handle(ResultSet rs) throws SQLException; > } > With this code, we can easily provide directly from the QueryRunner a spe= cific object without having to cast it. > Furthermore, we can also make the queries much more efficient, and declar= e only 2 method instead of three as it is currently the case: > =09public T query(String sql, ResultSetHandler handler, Object... = params) throws SQLException { > =09=09Connection connection =3D this.prepareConnection(); > =09=09try { > =09=09=09return this.query(connection, sql, handler, params); > =09=09} finally { > =09=09=09close(connection); > =09=09} > =09} > =09public T query(Connection connection, String sql, ResultSetHandler= handler, Object... params) throws SQLException { > =09=09PreparedStatement statement =3D null; > =09=09ResultSet resultSet =3D null; > =09=09try { > =09=09=09statement =3D this.prepareStatement(connection, sql); > =09=09=09this.fillStatement(statement, params); > =09=09=09resultSet =3D this.wrap(statement.executeQuery()); > =09=09=09return handler.handle(resultSet); > =09=09} catch (SQLException e) { > =09=09=09throw this.nestException(e, sql, params); // nestException creat= es an exception to be thrown here, instead of rethrow(e, sql, params); > =09=09} finally { > =09=09=09try { > =09=09=09=09close(resultSet); > =09=09=09} finally { > =09=09=09=09close(statement); > =09=09=09} > =09=09} > =09} > I don't know for you, but I find that code much more readable and maintai= nable. In fact, I already did this implementation and use it in prod withou= t any issue, since it is only basic language adaptation. > And using it is also very easy: > Book book =3D queryRunner.query("SELECT * FROM book WHERE title =3D ? AND= author =3D ?", new BeanHandler(Book.class), title, author); > instead of=20 > Book book =3D (Book)queryRunner.query("SELECT * FROM book WHERE title =3D= ? AND author =3D ?", new BeanHandler(Book.class), new Object[]{title, auth= or}); > However, I wrote an original time estimation of 1 day, because all classe= s have to be adapted, especially those included in org.apache.common.dbutil= s.handlers. > As I estimate, it is minor, it is just a request in order to have more be= autiful code, for a downside of providing a Java 1.5 compatible version of = DBUtils. Maintaining this should be very rough as well, since DBUtils is no= t really big (but it's amazing how so little code can provide so much code = improvements!) --=20 This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.