Added helper methods to set a result into a connection/cursor

This commit is contained in:
Daniele Varrazzo 2019-03-17 03:10:10 +00:00
parent e740c21ee6
commit 97220eadc6
5 changed files with 32 additions and 20 deletions

View File

@ -174,6 +174,7 @@ RAISES_NEG HIDDEN int conn_tpc_begin(connectionObject *self, xidObject *xid);
RAISES_NEG HIDDEN int conn_tpc_command(connectionObject *self, RAISES_NEG HIDDEN int conn_tpc_command(connectionObject *self,
const char *cmd, xidObject *xid); const char *cmd, xidObject *xid);
HIDDEN PyObject *conn_tpc_recover(connectionObject *self); HIDDEN PyObject *conn_tpc_recover(connectionObject *self);
HIDDEN void conn_set_result(connectionObject *self, PGresult *pgres);
/* exception-raising macros */ /* exception-raising macros */
#define EXC_IF_CONN_CLOSED(self) if ((self)->closed > 0) { \ #define EXC_IF_CONN_CLOSED(self) if ((self)->closed > 0) { \

View File

@ -1119,8 +1119,7 @@ conn_poll(connectionObject *self)
break; break;
} }
PQclear(curs->pgres); curs_set_result(curs, self->pgres);
curs->pgres = self->pgres;
self->pgres = NULL; self->pgres = NULL;
/* fetch the tuples (if there are any) and build the result. We /* fetch the tuples (if there are any) and build the result. We
@ -1487,3 +1486,11 @@ exit:
return rv; return rv;
} }
void
conn_set_result(connectionObject *self, PGresult *pgres)
{
PQclear(self->pgres);
self->pgres = pgres;
}

View File

@ -96,6 +96,7 @@ HIDDEN void curs_reset(cursorObject *self);
RAISES_NEG HIDDEN int psyco_curs_withhold_set(cursorObject *self, PyObject *pyvalue); RAISES_NEG HIDDEN int psyco_curs_withhold_set(cursorObject *self, PyObject *pyvalue);
RAISES_NEG HIDDEN int psyco_curs_scrollable_set(cursorObject *self, PyObject *pyvalue); RAISES_NEG HIDDEN int psyco_curs_scrollable_set(cursorObject *self, PyObject *pyvalue);
HIDDEN PyObject *psyco_curs_validate_sql_basic(cursorObject *self, PyObject *sql); HIDDEN PyObject *psyco_curs_validate_sql_basic(cursorObject *self, PyObject *sql);
HIDDEN void curs_set_result(cursorObject *self, PGresult *pgres);
/* exception-raising macros */ /* exception-raising macros */
#define EXC_IF_CURS_CLOSED(self) \ #define EXC_IF_CURS_CLOSED(self) \

View File

@ -160,3 +160,11 @@ exit:
Py_XDECREF(comp); Py_XDECREF(comp);
return rv; return rv;
} }
void
curs_set_result(cursorObject *self, PGresult *pgres)
{
PQclear(self->pgres);
self->pgres = pgres;
}

View File

@ -273,9 +273,9 @@ pq_clear_async(connectionObject *conn)
finalize asynchronous processing so the connection will be ready to finalize asynchronous processing so the connection will be ready to
accept another query */ accept another query */
while ((pgres = PQgetResult(conn->pgconn)) != NULL) { while ((pgres = PQgetResult(conn->pgconn))) {
Dprintf("pq_clear_async: clearing PGresult at %p", pgres); Dprintf("pq_clear_async: clearing PGresult at %p", pgres);
CLEARPGRES(pgres); PQclear(pgres);
} }
Py_CLEAR(conn->async_cursor); Py_CLEAR(conn->async_cursor);
} }
@ -321,12 +321,11 @@ pq_execute_command_locked(connectionObject *conn, const char *query,
conn->pgconn, query); conn->pgconn, query);
*error = NULL; *error = NULL;
CLEARPGRES(conn->pgres);
if (!psyco_green()) { if (!psyco_green()) {
conn->pgres = PQexec(conn->pgconn, query); conn_set_result(conn, PQexec(conn->pgconn, query));
} else { } else {
PyEval_RestoreThread(*tstate); PyEval_RestoreThread(*tstate);
conn->pgres = psyco_exec_green(conn, query); conn_set_result(conn, psyco_exec_green(conn, query));
*tstate = PyEval_SaveThread(); *tstate = PyEval_SaveThread();
} }
if (conn->pgres == NULL) { if (conn->pgres == NULL) {
@ -647,13 +646,12 @@ pq_get_guc_locked(
Dprintf("pq_get_guc_locked: pgconn = %p, query = %s", conn->pgconn, query); Dprintf("pq_get_guc_locked: pgconn = %p, query = %s", conn->pgconn, query);
*error = NULL; *error = NULL;
CLEARPGRES(conn->pgres);
if (!psyco_green()) { if (!psyco_green()) {
conn->pgres = PQexec(conn->pgconn, query); conn_set_result(conn, PQexec(conn->pgconn, query));
} else { } else {
PyEval_RestoreThread(*tstate); PyEval_RestoreThread(*tstate);
conn->pgres = psyco_exec_green(conn, query); conn_set_result(conn, psyco_exec_green(conn, query));
*tstate = PyEval_SaveThread(); *tstate = PyEval_SaveThread();
} }
@ -826,8 +824,7 @@ pq_get_result_async(connectionObject *conn)
PQclear(res); PQclear(res);
} }
else { else {
PQclear(conn->pgres); conn_set_result(conn, res);
conn->pgres = res;
} }
switch (status) { switch (status) {
@ -903,15 +900,14 @@ _pq_execute_sync(cursorObject *curs, const char *query, int no_result, int no_be
return -1; return -1;
} }
CLEARPGRES(conn->pgres);
Dprintf("pq_execute: executing SYNC query: pgconn = %p", conn->pgconn); Dprintf("pq_execute: executing SYNC query: pgconn = %p", conn->pgconn);
Dprintf(" %-.200s", query); Dprintf(" %-.200s", query);
if (!psyco_green()) { if (!psyco_green()) {
conn->pgres = PQexec(conn->pgconn, query); conn_set_result(conn, PQexec(conn->pgconn, query));
} }
else { else {
Py_BLOCK_THREADS; Py_BLOCK_THREADS;
conn->pgres = psyco_exec_green(conn, query); conn_set_result(conn, psyco_exec_green(conn, query));
Py_UNBLOCK_THREADS; Py_UNBLOCK_THREADS;
} }
@ -932,7 +928,7 @@ _pq_execute_sync(cursorObject *curs, const char *query, int no_result, int no_be
Py_BLOCK_THREADS; Py_BLOCK_THREADS;
/* assign the result back to the cursor now that we have the GIL */ /* assign the result back to the cursor now that we have the GIL */
curs->pgres = conn->pgres; curs_set_result(curs, conn->pgres);
conn->pgres = NULL; conn->pgres = NULL;
/* Process notifies here instead of when fetching the tuple as we are /* Process notifies here instead of when fetching the tuple as we are
@ -1422,7 +1418,7 @@ _pq_copy_in_v3(cursorObject *curs)
/* and finally we grab the operation result from the backend */ /* and finally we grab the operation result from the backend */
for (;;) { for (;;) {
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
curs->pgres = PQgetResult(curs->conn->pgconn); curs_set_result(curs, PQgetResult(curs->conn->pgconn));
Py_END_ALLOW_THREADS; Py_END_ALLOW_THREADS;
if (NULL == curs->pgres) if (NULL == curs->pgres)
@ -1503,10 +1499,9 @@ _pq_copy_out_v3(cursorObject *curs)
} }
/* and finally we grab the operation result from the backend */ /* and finally we grab the operation result from the backend */
CLEARPGRES(curs->pgres);
for (;;) { for (;;) {
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
curs->pgres = PQgetResult(curs->conn->pgconn); curs_set_result(curs, PQgetResult(curs->conn->pgconn));
Py_END_ALLOW_THREADS; Py_END_ALLOW_THREADS;
if (NULL == curs->pgres) if (NULL == curs->pgres)
@ -1585,7 +1580,7 @@ retry:
} }
if (len == -1) { if (len == -1) {
/* EOF */ /* EOF */
curs->pgres = PQgetResult(pgconn); curs_set_result(curs, PQgetResult(pgconn));
if (curs->pgres && PQresultStatus(curs->pgres) == PGRES_FATAL_ERROR) { if (curs->pgres && PQresultStatus(curs->pgres) == PGRES_FATAL_ERROR) {
pq_raise(conn, curs, NULL); pq_raise(conn, curs, NULL);