pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Jowett <oliver@opencloud.com>
To: Thomas Kellerer <spam_eater@gmx.net>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: DELETE ... RETURNING
Date: Tue, 14 Jul 2009 10:35:31 +1200
Message-ID: <4A5BB6B3.3020002@opencloud.com> (raw)
In-Reply-To: <h3gafc$51d$1@ger.gmane.org>
References: <h3g2rn$dn8$1@ger.gmane.org>
	<226a19190907131251m6c3786f5id8e8025f5c53cf66@mail.gmail.com>
	<h3g536$kq4$1@ger.gmane.org>
	<4A5BAA29.9000004@opencloud.com>
	<h3gafc$51d$1@ger.gmane.org>

Thomas Kellerer wrote:

> The following code will print "deleted: -1" and nothing more.

> stmt.execute("delete from test_delete returning id");
> System.out.println("deleted: " + stmt.getUpdateCount());
> if (stmt.getMoreResults())
> {
>   System.out.println("has result");
>   rs = stmt.getResultSet();
>   while (rs.next())
>   {
>     System.out.println(rs.getObject(1));
>   }
> }

I took another look at this, and your code assumes that the update count
is the first result. It's not. In the case where both a resultset and an
update count are present in a single query, the driver puts the
resultset result first (so that executeQuery() works nicely).

So your first call to getUpdateCount() returns -1 because the current
result is a resultset, not an update count (see the javadoc). Then you
call getMoreResults() which moves to the 2nd result (the update count)
and returns false because there's no resultset (again, see the javadoc).

If you want a general-purpose result processing loop, you want something
like this:

boolean hasResultSet = stmt.execute("...");
while (hasResultSet || stmt.getUpdateCount() != -1) {
  if (hasResultSet) {
    ResultSet rs = stmt.getResultSet();
    // .. process it ..
  } else {
    int updateCount = stmt.getUpdateCount();
    // .. process it ..
  }
  hasResultSet = stmt.getMoreResults();
}

-O



view thread (10+ messages)  latest in thread

Message-ID: <4A5BB6B3.3020002@opencloud.com>
Permalink:  ../4A5BB6B3.3020002@opencloud.com/
Also on:    postgresql.org/message-id/4A5BB6B3.3020002@opencloud.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: oliver@opencloud.com, spam_eater@gmx.net
  Subject: Re: DELETE ... RETURNING
  In-Reply-To: <4A5BB6B3.3020002@opencloud.com>
  References: <h3g2rn$dn8$1@ger.gmane.org> <226a19190907131251m6c3786f5id8e8025f5c53cf66@mail.gmail.com> <h3g536$kq4$1@ger.gmane.org> <4A5BAA29.9000004@opencloud.com> <h3gafc$51d$1@ger.gmane.org> <4A5BB6B3.3020002@opencloud.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