Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 0F6076329AA for ; Fri, 1 May 2009 17:40:25 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 31984-04 for ; Fri, 1 May 2009 17:40:23 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail3.videx.com (mail3.videx.com [65.122.251.14]) by mail.postgresql.org (Postfix) with ESMTP id 431B0632856 for ; Fri, 1 May 2009 17:40:21 -0300 (ADT) Received: from [191.10.58.1] ([191.10.58.1]) (authenticated user adamb@videx.com) by mail3.videx.com for pgsql-jdbc@postgresql.org; Fri, 1 May 2009 13:40:18 -0700 Message-ID: <49FB5E2D.4020105@videx.com> Date: Fri, 01 May 2009 13:40:13 -0700 From: Adam B User-Agent: Thunderbird 2.0.0.21 (X11/20090318) MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: A basic Statement.getGeneratedKeys() implementation (patch attached) Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200905/5 X-Sequence-Number: 19555 Hello all=2C Our webapp relies heavily on JDBC=27s Statement=2EgetGeneratedKeys=28=29=20= abstraction=2E We are investigating switching from Mysql to Postgre so I= =20 created a basic implementation of this feature in the 8=2E3-604 version of= =20 the driver=2E The diff patch is attached=2E On Linux you can apply it by= =3A 1=29 Download and extract=3A postgresql-jdbc-8=2E3-604=2Esrc=2Etar=2Egz 2=29 Copy the attached patch into the folder 3=29 Open a terminal to the folder and run=3A patch -p1 =3C=20 getGeneratedKeys=2Epatch 4=29 ant jar Here=27s an example usage =28nothing special=29=3A PreparedStatement ps =3D con=2EprepareStatement=28=22INSERT INTO foo=20= =28name=29=20 VALUES =28=3F=29=22=2C Statement=2ERETURN=5FGENERATED=5FKEYS=29=3B ps=2EsetString=281=2C =22sweet!=22=29=3B ps=2EexecuteUpdate=28=29=3B ResultSet rs =3D ps=2EgetGeneratedKeys=28=29=3B rs=2Enext=28=29=3B System=2Eout=2Eprintln=28rs=2EgetInt=281=29=29=3B ps=2Eclose=28=29=3B The patch modifies the following files=3A org/postgresql/core/Query=2Ejava org/postgresql/core/v2/V2Query=2Ejava org/postgresql/core/v3/CompositeQuery=2Ejava org/postgresql/core/v3/SimpleQuery=2Ejava org/postgresql/jdbc2/AbstractJdbc2Statement=2Ejava org/postgresql/jdbc3/AbstractJdbc3Connection=2Ejava org/postgresql/jdbc3/AbstractJdbc3Statement=2Ejava Limitations of this implementation=3A * You must use PreparedStatement =28not Statement=29 * Your table can only have one auto generated column * Column must have default value LIKE =27nextval=28=25seq=25=29=27 =20= =28this is=20 the case of SERIAL columns=29 * Must use JDBC3 or higher have =28ie =3E=3D java 1=2E5=29 * Must be running against a version of Postgre that supports the=20 =22RETURNING=22 clause =28eg INSERT INTO foo =28name=29 VALUES=28=27hello= =27=29 RETURNING=20 foo=5Fid=29 =20 I=27m hoping this patch will be included in the official driver=2E I=20 realize that it has some limitations but it will cover 95=25 of user needs= =2E Let me know what you think=2E Improvements are welcome=2E - Adam Videx Inc=2E 1105 N=2E E=2E Circle Blvd=2E Corvallis OR 97330 =28541=29 758= -0521 CONFIDENTIAL COMMUNICATION=3A The email message and any attachments are int= ended only for the addressee=2E They may be privileged=2C confidential=2C= and protected from disclosure=2E If you are not the intended recipient=2C= any dissemination=2C distribution=2C or copying is expressly prohibited=2E= If you received this email message in error=2C please notify the sender i= mmediately by replying to this e-mail message or by telephone