pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
isLast() and empty ResultSet
4+ messages / 3 participants
[nested] [flat]

* isLast() and empty ResultSet
@ 2005-04-21 21:10 Ruediger Herrmann <ruediger.herrmann@gmx.de>
  2005-04-21 22:05 ` Re: isLast() and empty ResultSet Kris Jurka <books@ejurka.com>
  2005-04-21 22:16 ` Re: isLast() and empty ResultSet Oliver Jowett <oliver@opencloud.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Ruediger Herrmann @ 2005-04-21 21:10 UTC (permalink / raw)
  To: 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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: isLast() and empty ResultSet
  2005-04-21 21:10 isLast() and empty ResultSet Ruediger Herrmann <ruediger.herrmann@gmx.de>
@ 2005-04-21 22:05 ` Kris Jurka <books@ejurka.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Kris Jurka @ 2005-04-21 22:05 UTC (permalink / raw)
  To: Ruediger Herrmann <ruediger.herrmann@gmx.de>; +Cc: 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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: isLast() and empty ResultSet
  2005-04-21 21:10 isLast() and empty ResultSet Ruediger Herrmann <ruediger.herrmann@gmx.de>
@ 2005-04-21 22:16 ` Oliver Jowett <oliver@opencloud.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Oliver Jowett @ 2005-04-21 22:16 UTC (permalink / raw)
  To: Ruediger Herrmann <ruediger.herrmann@gmx.de>; +Cc: 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



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: isLast() and empty ResultSet
@ 2005-04-22 08:45 Ruediger Herrmann <ruediger.herrmann@gmx.de>
  0 siblings, 0 replies; 4+ messages in thread

From: Ruediger Herrmann @ 2005-04-22 08:45 UTC (permalink / raw)
  To: pgsql-jdbc


Thanks a lot to all of you for the replies.

I'm now using isBeforeFirst() to test for an empty
ResultSet, wich as far as my tests show seems to work
if it is the first thing done.

Regards
Rüdiger

--- Oliver Jowett <oliver@opencloud.com> wrote:
> 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
> 
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to
> majordomo@postgresql.org
> 
> 
> 

-- 
+++ GMX - Die erste Adresse für Mail, Message, More +++

1 GB Mailbox bereits in GMX FreeMail http://www.gmx.net/de/go/mail



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2005-04-22 08:45 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-04-21 21:10 isLast() and empty ResultSet Ruediger Herrmann <ruediger.herrmann@gmx.de>
2005-04-21 22:05 ` Kris Jurka <books@ejurka.com>
2005-04-21 22:16 ` Oliver Jowett <oliver@opencloud.com>
2005-04-22 08:45 Re: isLast() and empty ResultSet Ruediger Herrmann <ruediger.herrmann@gmx.de>

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