Cursors: getting the number of tuples; moving backwards

am@fx.ro

From: am@fx.ro
To: pgsql-general@postgresql.org
Date: 2002-11-01T10:43:48Z
Lists: pgsql-hackers, pgsql-general
Hello everyone!

I have 2 questions:

--1-- Some days ago, I've been trying to get the number of tuples
that FETCH ALL would return,  *before* fetching anything.
(the program is written in C++, using libpq ; PostgreSQL 7.2.3).

The solution i've found was something like:

    int nr_tuples;

    res = PQexec(conn, "MOVE ALL in CURS");    
    sscanf(PQcmdStatus(res),"MOVE %i",&nr_tuples);
    PQclear(res);
    
I'm wondering: is there any better way to get that number?

( just an idea:  maybe it would be useful to make PQcmdTuples
  work for MOVE commands ... ? )


--2-- I found out that if i reach the end of the cursor, and want
to move backwards, i have to increase the MOVE command's argument by 1:
 
  MOVE ALL in CURS    --> i get the number of tuples: 590

  MOVE -590 in CURS
  FETCH ALL           --> i get all tuples except the first one

  MOVE -591 in CURS
  FETCH ALL           --> i get all the tuples

  MOVE -1 in CURS
  FETCH ALL           --> i get nothing !

  MOVE -2 in CURS 
  FETCH ALL           --> i get the last tuple

This happens only if the current position is at the end of the cursor. 
 
Is this the normal behaviour?
 


Best regards,
Adrian Maier
(am@fx.ro)