I'm working with the PostgreSQL C API. Reading the documentation it states that a query is finished when PQgetResult returns NULL and PQgetResult will block if PQisBusy does not return 0. However PQisBusy returns 1 if there is no more input, so I can't call PQgetResult and get the NULL. Therefore I can't know if the query is over. Is there any other way to know if the query is done? Did I misunderstand the async API?
----edit-----
The basic idea as C code is:
PQsendQuery(conn, query)
while(true)
{
PQconsumeInput(conn)
if(PQisBusy(conn) == 0)
{
PGresult* res = PQgetResult(conn);
if(res)
{
//print result
}
else
{
//res == NULL
//indicates query is over
break;
}
}
}
The result will be printed but the loop never terminates. Because PQisBusy only returns 0 once.
I think what you need is PQsetSingleRowMode
to let PQgetResult
return one row each time.
from http://www.postgresql.org/docs/9.2/static/libpq-single-row-mode.html
Ordinarily, libpq collects a SQL command's entire result and returns it to the application as a single PGresult. This can be unworkable for commands that return a large number of rows. For such cases, applications can use PQsendQuery and PQgetResult in single-row mode. In this mode, the result row(s) are returned to the application one at a time, as they are received from the server.
If the query returns any rows, they are returned as individual PGresult objects, which look like normal query results except for having status code PGRES_SINGLE_TUPLE instead of PGRES_TUPLES_OK. After the last row, or immediately if the query returns zero rows, a zero-row object with status PGRES_TUPLES_OK is returned; this is the signal that no more rows will arrive.
check out the sample code: https://github.com/markokr/libpq-rowproc-demos/blob/master/demo-onerow-async.c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With