Cursors: getting the number of tuples; moving backwards

From: am(at)fx(dot)ro
To: pgsql-general(at)postgresql(dot)org
Subject: Cursors: getting the number of tuples; moving backwards
Date: 2002-11-01 10:43:48
Message-ID: 20021101124348.A344@coto
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers pgsql-jdbc

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(at)fx(dot)ro)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2002-11-01 11:38:12 Re: readline.h and history.h configure error
Previous Message Stefan Stern 2002-11-01 10:40:09 readline.h and history.h configure error

Browse pgsql-hackers by date

  From Date Subject
Next Message Larry Rosenman 2002-11-01 11:54:20 Re: Request for supported platforms
Previous Message Mario Weilguni 2002-11-01 08:38:31 Interesting VACUUM notice

Browse pgsql-jdbc by date

  From Date Subject
Next Message am 2002-11-01 18:14:33 Re: Cursors: getting the number of tuples; moving backwards
Previous Message Gary 2002-11-01 09:06:04 Java.lang.ClassNotFoundException:postgresql.Driver