isLast() and empty ResultSet

Lists: pgsql-jdbc
From: "Ruediger Herrmann" <ruediger(dot)herrmann(at)gmx(dot)de>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: isLast() and empty ResultSet
Date: 2005-04-21 21:10:09
Message-ID: 6626.1114117809@www66.gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Hello,

I implemented an Iterator interface iterating over a ResultSet. Therefore
I rely on isLast() to implement the Iterator#hasNext() method. This works
fine unless the whole ResultSet is empty.
For empty RresultSets, isLast always returns true.
Stepping through the code I found the reason therefore. Line 544 of
AbstractJdbc2ResultSet says
if (rows_size == 0)
return false; // No rows.
At least to me this is suspicious as I would return the opposite. Might that
be a bug or is there any other reason to behave like this?

I am using JDBC Driver 8.0 Build 311 and Server Version 8.0.2.

Regards
RĂ¼diger

--
+++ NEU: GMX DSL_Flatrate! Schon ab 14,99 EUR/Monat! +++

GMX Garantie: Surfen ohne Tempo-Limit! http://www.gmx.net/de/go/dsl


From: Kris Jurka <books(at)ejurka(dot)com>
To: Ruediger Herrmann <ruediger(dot)herrmann(at)gmx(dot)de>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: isLast() and empty ResultSet
Date: 2005-04-21 22:05:54
Message-ID: Pine.BSO.4.56.0504211700090.24801@leary.csoft.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

On Thu, 21 Apr 2005, Ruediger Herrmann wrote:

> I implemented an Iterator interface iterating over a ResultSet. Therefore
> I rely on isLast() to implement the Iterator#hasNext() method. This works
> fine unless the whole ResultSet is empty.
> For empty RresultSets, isLast always returns true.

It always returns false because isLast implies that you are on the last
row of the ResultSet. If the ResultSet is empty you aren't on a row,
so you can't be on the last row.

Kris Jurka


From: Oliver Jowett <oliver(at)opencloud(dot)com>
To: Ruediger Herrmann <ruediger(dot)herrmann(at)gmx(dot)de>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: isLast() and empty ResultSet
Date: 2005-04-21 22:16:37
Message-ID: 42682645.4030104@opencloud.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-jdbc

Ruediger Herrmann wrote:
> Hello,
>
> I implemented an Iterator interface iterating over a ResultSet. Therefore
> I rely on isLast() to implement the Iterator#hasNext() method. This works
> fine unless the whole ResultSet is empty.
> For empty RresultSets, isLast always returns true.
> Stepping through the code I found the reason therefore. Line 544 of
> AbstractJdbc2ResultSet says
> if (rows_size == 0)
> return false; // No rows.
> At least to me this is suspicious as I would return the opposite. Might that
> be a bug or is there any other reason to behave like this?

It's not a bug, AFAIK. isLast() returns true if the resultset is
positioned *on* the last row of the resultset. This means that if you
have a 5-row resultset, isLast() is true when the 5th row is the
currently active row (and you can retrieve data from that row at that
point).

For a 0-row resultset, we can never be on the last row as there are no
rows at all.

You could try something like this for your hasNext() condition:

rs.isBeforeFirst() || (rs.getRow() != 0 && !rs.isLast())

(note that isBeforeFirst() returns false on an empty resultset, per javadoc)

-O