pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adam B <adamb@videx.com>
To: pgsql-jdbc@postgresql.org
Subject: A basic Statement.getGeneratedKeys() implementation (patch attached)
Date: Fri, 01 May 2009 13:40:13 -0700
Message-ID: <49FB5E2D.4020105@videx.com> (raw)

Hello all,

Our webapp relies heavily on JDBC's Statement.getGeneratedKeys() 
abstraction.  We are investigating switching from Mysql to Postgre so I 
created a basic implementation of this feature in the 8.3-604 version of 
the driver.  The diff patch is attached.  On Linux you can apply it by:

1) Download and extract: postgresql-jdbc-8.3-604.src.tar.gz
2) Copy the attached patch into the folder
3) Open a terminal to the folder and run:  patch -p1 < 
getGeneratedKeys.patch
4) ant jar

Here's an example usage (nothing special):

    PreparedStatement ps = con.prepareStatement("INSERT INTO foo (name) 
VALUES (?)", Statement.RETURN_GENERATED_KEYS);
    ps.setString(1, "sweet!");
    ps.executeUpdate();
    ResultSet rs = ps.getGeneratedKeys();
    rs.next();
    System.out.println(rs.getInt(1));
    ps.close();

The patch modifies the following files:
    org/postgresql/core/Query.java
    org/postgresql/core/v2/V2Query.java
    org/postgresql/core/v3/CompositeQuery.java
    org/postgresql/core/v3/SimpleQuery.java
    org/postgresql/jdbc2/AbstractJdbc2Statement.java
    org/postgresql/jdbc3/AbstractJdbc3Connection.java
    org/postgresql/jdbc3/AbstractJdbc3Statement.java

Limitations of this implementation:
    * You must use PreparedStatement (not Statement)
    * Your table can only have one auto generated column
    * Column must have default value LIKE 'nextval(%seq%)'   (this is 
the case of SERIAL columns)
    * Must use JDBC3 or higher have (ie >= java 1.5)
    * Must be running against a version of Postgre that supports the 
"RETURNING" clause (eg INSERT INTO foo (name) VALUES('hello') RETURNING 
foo_id)
   
I'm hoping this patch will be included in the official driver.  I 
realize that it has some limitations but it will cover 95% of user needs.

Let me know what you think.  Improvements are welcome.
- Adam



Videx Inc. 1105 N. E. Circle Blvd. Corvallis OR 97330 (541) 758-0521
CONFIDENTIAL COMMUNICATION: The email message and any attachments are intended only for the addressee.  They may be privileged, confidential, and protected from disclosure. If you are not the intended recipient, any dissemination, distribution, or copying is expressly prohibited.  If you received this email message in error, please notify the sender immediately by replying to this e-mail message or by telephone




view thread (4+ messages)  latest in thread

Message-ID: <49FB5E2D.4020105@videx.com>
Permalink:  ../49FB5E2D.4020105@videx.com/
Also on:    postgresql.org/message-id/49FB5E2D.4020105@videx.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: adamb@videx.com
  Subject: Re: A basic Statement.getGeneratedKeys() implementation (patch attached)
  In-Reply-To: <49FB5E2D.4020105@videx.com>
  References: <49FB5E2D.4020105@videx.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox