summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--psycopg/connection_int.c22
-rw-r--r--psycopg/cursor.h1
-rw-r--r--psycopg/cursor_int.c42
3 files changed, 18 insertions, 47 deletions
diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c
index 0d9da09..ebd710f 100644
--- a/psycopg/connection_int.c
+++ b/psycopg/connection_int.c
@@ -699,7 +699,8 @@ conn_poll_send(connectionObject *self)
/* conn_poll_fetch - poll the connection when reading results from the backend
*
- * Assume self->async_cursor is not null: use such cursor to store results.
+ * If self_curs is available, use it to store the result of the last query.
+ * Also unlink it when finished.
*/
PyObject *
@@ -725,10 +726,23 @@ conn_poll_fetch(connectionObject *self)
because of asynchronous NOTIFYs that can be sent by the backend
even if the user didn't asked for them */
- if (self->async_status == ASYNC_READ)
- last_result = curs_get_last_result((cursorObject *)self->async_cursor);
- else
+ if (self->async_status == ASYNC_READ && self->async_cursor) {
+ cursorObject *curs = (cursorObject *)self->async_cursor;
+ IFCLEARPGRES(curs->pgres);
+ curs->pgres = pq_get_last_result(self);
+
+ /* fetch the tuples (if there are any) and build the result. We don't
+ * care if pq_fetch return 0 or 1, but if there was an error, we want to
+ * signal it to the caller. */
+ last_result = pq_fetch(curs) == -1 ? -1 : 0;
+
+ /* We have finished with our async_cursor */
+ Py_XDECREF(self->async_cursor);
+ self->async_cursor = NULL;
+ }
+ else {
last_result = 0;
+ }
if (last_result == 0) {
Dprintf("conn_poll_fetch: returning %d", PSYCO_POLL_OK);
diff --git a/psycopg/cursor.h b/psycopg/cursor.h
index 27a8e42..6fff4ae 100644
--- a/psycopg/cursor.h
+++ b/psycopg/cursor.h
@@ -85,7 +85,6 @@ typedef struct {
/* C-callable functions in cursor_int.c and cursor_ext.c */
HIDDEN void curs_reset(cursorObject *self);
-HIDDEN int curs_get_last_result(cursorObject *self);
/* exception-raising macros */
#define EXC_IF_CURS_CLOSED(self) \
diff --git a/psycopg/cursor_int.c b/psycopg/cursor_int.c
index 8f60ca7..004ba82 100644
--- a/psycopg/cursor_int.c
+++ b/psycopg/cursor_int.c
@@ -54,45 +54,3 @@ curs_reset(cursorObject *self)
self->casts = NULL;
Py_XDECREF(tmp);
}
-
-/*
- * curs_get_last_result
- *
- * read all results from the connection, save the last one
- * returns 0 if all results were read, 1 if there are remaining results, but
- * their retrieval would block, -1 if there was an error
- */
-
-int
-curs_get_last_result(cursorObject *self) {
- PGresult *pgres;
-
- Py_BEGIN_ALLOW_THREADS;
- pthread_mutex_lock(&(self->conn->lock));
- /* read one result, there can be multiple if the client sent multiple
- statements */
- while ((pgres = PQgetResult(self->conn->pgconn)) != NULL) {
- if (PQisBusy(self->conn->pgconn) == 1) {
- /* there is another result waiting, need to tell the client to
- wait more */
- Dprintf("curs_get_last_result: got result, but more are pending");
- IFCLEARPGRES(self->pgres);
- self->pgres = pgres;
- pthread_mutex_unlock(&(self->conn->lock));
- Py_BLOCK_THREADS;
- return 1;
- }
- Dprintf("curs_get_last_result: got result %p", pgres);
- IFCLEARPGRES(self->pgres);
- self->pgres = pgres;
- }
- Py_XDECREF(self->conn->async_cursor);
- self->conn->async_cursor = NULL;
- pthread_mutex_unlock(&(self->conn->lock));
- Py_END_ALLOW_THREADS;
- /* fetch the tuples (if there are any) and build the result. We don't care
- if pq_fetch return 0 or 1, but if there was an error, we want to signal
- it to the caller. */
- return pq_fetch(self) == -1 ? -1 : 0;
-}
-