pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeppe Sommer <jso@trifork.com>
To: pgsql-jdbc@postgresql.org <pgsql-jdbc@postgresql.org>
Subject: Re: Here's a fix to AbstractJdbc3Statement.getGeneratedKeys
Date: Fri, 24 Apr 2009 10:19:34 +0200
Message-ID: <49F17616.9030801@trifork.com> (raw)
In-Reply-To: <49F175A6.9080401@trifork.com>
References: <49F175A6.9080401@trifork.com>

Sorry, I forgot to attach the patch :-(

Here it is.

Jeppe Sommer skrev:
> Frustrations over a 4 hour database script with MySQL forced me into 
> this :-)
>
> Porting a project from MySQL to PostgreSQL, I discovered that the jdbc3 
> facility of returning the generated keys from an insert statement does 
> not work. On a fresh cvs checkout, there is partial support, however, 
> all fields are returned, not only the field that are autogenerated.
>
> Here is a patch that fixes this. I hope someone will take a look, and 
> consider whether it can be adopted into the project. Or improved. It 
> works for me :-)
>
> The strategy of the patch is simply to inspect the metadata of the 
> returned (initially full) resultset, and then strip out any fields that 
> are not marked as autoincrement.
>
> BTW the 4 hour db script finishes after 2m 19s on PostgreSQL.
>
>
> Best Regards,
> Jeppe
>
>
>   

Attachments:

pgjdbc.diff (text/plain, 3.9K) download Index: org/postgresql/jdbc2/AbstractJdbc2ResultSet.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v retrieving revision 1.105 diff -u -r1.105 AbstractJdbc2ResultSet.java --- org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 30 Sep 2008 04:34:51 -0000 1.105 +++ org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 24 Apr 2009 07:54:23 -0000 @@ -55,7 +55,7 @@ private int fetchdirection = ResultSet.FETCH_UNKNOWN; protected final BaseConnection connection; // the connection we belong to protected final BaseStatement statement; // the statement we belong to - protected final Field fields[]; // Field metadata for this resultset. + protected Field fields[]; // Field metadata for this resultset. protected final Query originalQuery; // Query we originated from protected final int maxRows; // Maximum rows in this resultset (might be 0). @@ -2909,6 +2909,18 @@ public String getValue() { return null; } - }; + } + + public Vector getRows() { + return rows; + } + + public Field[] getFields() { + return fields; + } + + public void setFields(Field[] fields) { + this.fields = fields; + }; } Index: org/postgresql/jdbc3/AbstractJdbc3Statement.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc3/AbstractJdbc3Statement.java,v retrieving revision 1.24 diff -u -r1.24 AbstractJdbc3Statement.java --- org/postgresql/jdbc3/AbstractJdbc3Statement.java 28 Jan 2009 09:50:21 -0000 1.24 +++ org/postgresql/jdbc3/AbstractJdbc3Statement.java 24 Apr 2009 07:54:23 -0000 @@ -11,16 +11,19 @@ import java.math.BigDecimal; import java.sql.*; +import java.util.ArrayList; import java.util.Calendar; +import java.util.HashSet; import java.util.Vector; -import org.postgresql.util.PSQLException; -import org.postgresql.util.PSQLState; -import org.postgresql.core.Utils; -import org.postgresql.core.QueryExecutor; -import org.postgresql.core.Field; import org.postgresql.core.BaseConnection; +import org.postgresql.core.Field; +import org.postgresql.core.QueryExecutor; +import org.postgresql.core.Utils; +import org.postgresql.jdbc2.AbstractJdbc2ResultSet; import org.postgresql.util.GT; +import org.postgresql.util.PSQLException; +import org.postgresql.util.PSQLState; /** * This class defines methods of the jdbc3 specification. This class extends @@ -30,6 +33,7 @@ public abstract class AbstractJdbc3Statement extends org.postgresql.jdbc2.AbstractJdbc2Statement { private final int rsHoldability; + public HashSet<String> specifiedColumns; public AbstractJdbc3Statement (AbstractJdbc3Connection c, int rsType, int rsConcurrency, int rsHoldability) throws SQLException { @@ -111,7 +115,34 @@ if (generatedKeys == null || generatedKeys.getResultSet() == null) return createDriverResultSet(new Field[0], new Vector()); - return generatedKeys.getResultSet(); + AbstractJdbc2ResultSet rs = (AbstractJdbc2ResultSet) generatedKeys.getResultSet(); + + ResultSetMetaData metaData = rs.getMetaData(); + + final ArrayList<Field> fields = new ArrayList<Field>(); + ArrayList<byte[]> tuples = new ArrayList<byte[]>(); + int columnCount = metaData.getColumnCount(); + + byte[][] origTuples = (byte[][]) rs.getRows().get(0); + Field[] origFields = rs.getFields(); + + for (int c = 0; c < columnCount; c++) { + if (metaData.isAutoIncrement(c + 1)) { + tuples.add(origTuples[c]); + fields.add(origFields[c]); + } + } + + Field[] f = new Field[fields.size()]; + fields.toArray(f); + + byte[][] t = new byte[tuples.size()][]; + tuples.toArray(t); + + rs.getRows().set(0, t); + rs.setFields(f); + + return rs; } /**

view thread (8+ messages)  latest in thread

Message-ID: <49F17616.9030801@trifork.com>
Permalink:  ../49F17616.9030801@trifork.com/
Also on:    postgresql.org/message-id/49F17616.9030801@trifork.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: jso@trifork.com
  Subject: Re: Here's a fix to AbstractJdbc3Statement.getGeneratedKeys
  In-Reply-To: <49F17616.9030801@trifork.com>
  References: <49F175A6.9080401@trifork.com> <49F17616.9030801@trifork.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