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: getUdateCount() vs. RETURNING clause
Date: Thu, 26 Nov 2009 01:16:20 +1300
Message-ID: <4B0D2014.3060703@opencloud.com> (raw)
In-Reply-To: <hej5r1$aqs$1@ger.gmane.org>
References: <hegh0m$j0f$1@ger.gmane.org>
	<4B0C7CFE.50106@opencloud.com>
	<heimgq$sdo$1@ger.gmane.org>
	<4B0D1164.1060006@opencloud.com>
	<hej5r1$aqs$1@ger.gmane.org>

Thomas Kellerer wrote:
> Oliver Jowett, 25.11.2009 12:13:
>>
>> You've done some selective editing there. The javadoc I referred to is
>> this (from the Java 6 javadoc):
>>
>> getResultSet():
>>
>> Retrieves the current result as a ResultSet object. This method should
>> be called only once per result.
> 
> Correct, once per *result* not per statement. If the statement returns
> more than one result, I should be allowed to call it multiple time.

That's right, but you need a call to getMoreResults() to step through
the results between calls, as I suggested in my original response.

> I think the base of my (mis)understanding is that the term "current"
> lead me to believe that the "stack" of results a statement can hold,
> could look like this:
> 
> resultSet
> update count = 3
> update count = 2
> resultSet
> reslutSet

Yes, you can have that. You step through the results by calling
getMoreResults(). At any particular point, the current result is either
a resultset or an update count, but never both.

> So if I create a loop using the condition stated in the Javadocs the
> program flow would be as follows:
> 
> 1) stmt.execute() returns true, so I call getResultSet()
> 2) getMoreResults() returns false, but getUpdateCount() returns 3 ==> go on
> 3) getMoreResults() returns false, but getUpdateCount() returns 2 ==> go on
> 4) getMoreResults() returns true, so getResultSet() returns a result set
> ==> go on
> 5) getMoreResults() returns true, so getResultSet() returns a result set
> ==> go on
> 6) getMoreResults() returns false, getUpdateCount() returns -1 ==>
> everything was processed.

Yes, this is correct. It will look something like this:

boolean hasResultSet = stmt.execute();
int updateCount = stmt.getUpdateCount();
while (hasResultSet || updateCount != -1) {
  if (hasResultSet) {
    ResultSet rs = stmt.getResultSet();
    // This result is a resultset, process rs.
  } else {
    // This result is an update count, process updateCount.
  }

  hasResultSet = stmt.getMoreResults();
  updateCount = stmt.getUpdateCount();
}

-O



view thread (10+ messages)  latest in thread

Message-ID: <4B0D2014.3060703@opencloud.com>
Permalink:  ../4B0D2014.3060703@opencloud.com/
Also on:    postgresql.org/message-id/4B0D2014.3060703@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: getUdateCount() vs. RETURNING clause
  In-Reply-To: <4B0D2014.3060703@opencloud.com>
  References: <hegh0m$j0f$1@ger.gmane.org> <4B0C7CFE.50106@opencloud.com> <heimgq$sdo$1@ger.gmane.org> <4B0D1164.1060006@opencloud.com> <hej5r1$aqs$1@ger.gmane.org> <4B0D2014.3060703@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