pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
To: Heikki Hiltunen <heikki.hiltunen@smilehouse.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: Problem with prepareStatement and Statement.RETURN_GENERATED_KEYS in PostgreSQL JDBC driver 8.4
Date: Fri, 23 Apr 2010 18:57:11 +0300
Message-ID: <4BD1C357.90506@enterprisedb.com> (raw)
In-Reply-To: <4BD196B4.3040607@smilehouse.com>
References: <4BD196B4.3040607@smilehouse.com>

Heikki Hiltunen wrote:
> When using 8.4 JDBC drivers, calling prepareStatement(String sql, int
> autoGeneratedKeys) with Statement.RETURN_GENERATED_KEYS seems to add
> "RETURNING *" to the end of the SQL even with select statements.
> According to Javadoc for prepareStatement(String sql, int
> autoGeneratedKeys) in java.sql.Connection:
> 
> "The given constant tells the driver whether it should make
> auto-generated keys available for retrieval. This parameter is ignored
> if the SQL statement is not an INSERT statement, or an SQL statement
> able to return auto-generated keys (the list of such statements is
> vendor-specific). "

Yeah, the driver just blindly tacks a " RETURNING *" to the end of the
SQL string if you specify RETURN_GENERATED_KEYS. I'm tempted to do
something like this:

inline patch 1: AbstractJdbc3Connection.java (+17/-0 lines) download inline-1.patch*** AbstractJdbc3Connection.java 23 Dec 2009 10:28:40 +0200 1.21 --- AbstractJdbc3Connection.java 23 Apr 2010 18:49:44 +0300 *************** *** 359,364 **** --- 359,381 ---- throws SQLException { checkClosed(); + + /* + * We implement fetching auto-generated keys by tacking a + * " RETURNING *" to the end of the string. Don't try to do that + * with other statements than INSERT/UPDATE/DELETE. + * + * XXX this gets fooled by comments at the beginning of the SQL string + */ + if (autoGeneratedKeys != Statement.NO_GENERATED_KEYS) + { + String trimmedSql = sql.trim(); + if (!trimmedSql.regionMatches(true, 0, "INSERT", 0, 6) && + !trimmedSql.regionMatches(true, 0, "UPDATE", 0, 6) && + !trimmedSql.regionMatches(true, 0, "DELETE", 0, 6)) + autoGeneratedKeys = Statement.NO_GENERATED_KEYS; + } + if (autoGeneratedKeys != Statement.NO_GENERATED_KEYS) sql = AbstractJdbc3Statement.addReturning(this, sql, new
String[]{"*"}, false); But that's not very bullet-proof, and will fail to detect the statement as an INSERT if it e.g begins with a comment. We could add a mini-parser to detect comments too, but it's not a very robust approach. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com

view thread (4+ messages)  latest in thread

Message-ID: <4BD1C357.90506@enterprisedb.com>
Permalink:  ../4BD1C357.90506@enterprisedb.com/
Also on:    postgresql.org/message-id/4BD1C357.90506@enterprisedb.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: heikki.linnakangas@enterprisedb.com, heikki.hiltunen@smilehouse.com
  Subject: Re: Problem with prepareStatement and Statement.RETURN_GENERATED_KEYS in PostgreSQL JDBC driver 8.4
  In-Reply-To: <4BD1C357.90506@enterprisedb.com>
  References: <4BD196B4.3040607@smilehouse.com> <4BD1C357.90506@enterprisedb.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