mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-02-17 09:30:33 +03:00
Added 'pq_get_last_result()' function.
The function reads the last result after an asynchronous query.
This commit is contained in:
parent
5be0fc52ca
commit
a66de9808f
|
@ -29,6 +29,7 @@
|
||||||
#include "psycopg/psycopg.h"
|
#include "psycopg/psycopg.h"
|
||||||
#include "psycopg/green.h"
|
#include "psycopg/green.h"
|
||||||
#include "psycopg/connection.h"
|
#include "psycopg/connection.h"
|
||||||
|
#include "psycopg/pqpath.h"
|
||||||
|
|
||||||
HIDDEN PyObject *wait_callback = NULL;
|
HIDDEN PyObject *wait_callback = NULL;
|
||||||
|
|
||||||
|
@ -143,8 +144,7 @@ psyco_wait(connectionObject *conn)
|
||||||
PGresult *
|
PGresult *
|
||||||
psyco_exec_green(connectionObject *conn, const char *command)
|
psyco_exec_green(connectionObject *conn, const char *command)
|
||||||
{
|
{
|
||||||
PGconn *pgconn = conn->pgconn;
|
PGresult *result = NULL;
|
||||||
PGresult *result = NULL, *res;
|
|
||||||
PyObject *cb, *pyrv;
|
PyObject *cb, *pyrv;
|
||||||
|
|
||||||
if (!(cb = have_wait_callback())) {
|
if (!(cb = have_wait_callback())) {
|
||||||
|
@ -153,7 +153,7 @@ psyco_exec_green(connectionObject *conn, const char *command)
|
||||||
|
|
||||||
/* Send the query asynchronously */
|
/* Send the query asynchronously */
|
||||||
Dprintf("psyco_exec_green: sending query async");
|
Dprintf("psyco_exec_green: sending query async");
|
||||||
if (0 == PQsendQuery(pgconn, command)) {
|
if (0 == PQsendQuery(conn->pgconn, command)) {
|
||||||
Dprintf("psyco_exec_green: PQsendQuery returned 0");
|
Dprintf("psyco_exec_green: PQsendQuery returned 0");
|
||||||
goto clear;
|
goto clear;
|
||||||
}
|
}
|
||||||
|
@ -172,19 +172,8 @@ psyco_exec_green(connectionObject *conn, const char *command)
|
||||||
}
|
}
|
||||||
Py_DECREF(pyrv);
|
Py_DECREF(pyrv);
|
||||||
|
|
||||||
/* Now we can read the data without fear of blocking.
|
/* Now we can read the data without fear of blocking. */
|
||||||
* Read until PQgetResult gives a NULL */
|
result = pq_get_last_result(conn);
|
||||||
while (NULL != (res = PQgetResult(pgconn))) {
|
|
||||||
if (result) {
|
|
||||||
/* TODO too bad: we are discarding results from all the queries
|
|
||||||
* except the last. We could have populated `nextset()` with it
|
|
||||||
* but it would be an incompatible change (apps currently issue
|
|
||||||
* groups of queries expecting to receive the last result: they
|
|
||||||
* would start receiving the first instead). */
|
|
||||||
PQclear(result);
|
|
||||||
}
|
|
||||||
result = res;
|
|
||||||
}
|
|
||||||
|
|
||||||
clear:
|
clear:
|
||||||
conn->async_status = ASYNC_DONE;
|
conn->async_status = ASYNC_DONE;
|
||||||
|
|
|
@ -782,6 +782,33 @@ pq_execute(cursorObject *curs, const char *query, int async)
|
||||||
return 1-async;
|
return 1-async;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Return the last result available on the connection.
|
||||||
|
*
|
||||||
|
* The function will block will block only if a command is active and the
|
||||||
|
* necessary response data has not yet been read by PQconsumeInput.
|
||||||
|
*
|
||||||
|
* The result should be disposed using PQclear()
|
||||||
|
*/
|
||||||
|
PGresult *
|
||||||
|
pq_get_last_result(connectionObject *conn)
|
||||||
|
{
|
||||||
|
PGresult *result = NULL, *res;
|
||||||
|
|
||||||
|
/* Read until PQgetResult gives a NULL */
|
||||||
|
while (NULL != (res = PQgetResult(conn->pgconn))) {
|
||||||
|
if (result) {
|
||||||
|
/* TODO too bad: we are discarding results from all the queries
|
||||||
|
* except the last. We could have populated `nextset()` with it
|
||||||
|
* but it would be an incompatible change (apps currently issue
|
||||||
|
* groups of queries expecting to receive the last result: they
|
||||||
|
* would start receiving the first instead). */
|
||||||
|
PQclear(result);
|
||||||
|
}
|
||||||
|
result = res;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/* pq_fetch - fetch data after a query
|
/* pq_fetch - fetch data after a query
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
#define CLEARPGRES(pgres) PQclear(pgres); pgres = NULL
|
#define CLEARPGRES(pgres) PQclear(pgres); pgres = NULL
|
||||||
|
|
||||||
/* exported functions */
|
/* exported functions */
|
||||||
|
HIDDEN PGresult *pq_get_last_result(connectionObject *conn);
|
||||||
HIDDEN int pq_fetch(cursorObject *curs);
|
HIDDEN int pq_fetch(cursorObject *curs);
|
||||||
HIDDEN int pq_execute(cursorObject *curs, const char *query, int async);
|
HIDDEN int pq_execute(cursorObject *curs, const char *query, int async);
|
||||||
HIDDEN int pq_begin_locked(connectionObject *conn, PGresult **pgres,
|
HIDDEN int pq_begin_locked(connectionObject *conn, PGresult **pgres,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user