From d131b2b22bec228afd6c15ed679f64965b1fa96b Mon Sep 17 00:00:00 2001 From: Alexey Borzenkov Date: Tue, 9 Jul 2013 23:36:04 +0400 Subject: [PATCH] No implicit transaction on named cursor close Also, don't start an implicit transaction when fetching with named with hold cursor, since it already returns results from a previously committed transaction. --- psycopg/cursor_type.c | 20 ++++++++++---------- psycopg/pqpath.c | 4 ++-- psycopg/pqpath.h | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index ddbed298..8df1df2a 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -63,7 +63,7 @@ psyco_curs_close(cursorObject *self) EXC_IF_NO_MARK(self); PyOS_snprintf(buffer, 127, "CLOSE \"%s\"", self->name); - if (pq_execute(self, buffer, 0, 0) == -1) return NULL; + if (pq_execute(self, buffer, 0, 0, 1) == -1) return NULL; } self->closed = 1; @@ -444,7 +444,7 @@ _psyco_curs_execute(cursorObject *self, /* At this point, the SQL statement must be str, not unicode */ - tmp = pq_execute(self, Bytes_AS_STRING(self->query), async, no_result); + tmp = pq_execute(self, Bytes_AS_STRING(self->query), async, no_result, 0); Dprintf("psyco_curs_execute: res = %d, pgres = %p", tmp, self->pgres); if (tmp < 0) { goto exit; } @@ -766,7 +766,7 @@ psyco_curs_fetchone(cursorObject *self) EXC_IF_ASYNC_IN_PROGRESS(self, fetchone); EXC_IF_TPC_PREPARED(self->conn, fetchone); PyOS_snprintf(buffer, 127, "FETCH FORWARD 1 FROM \"%s\"", self->name); - if (pq_execute(self, buffer, 0, 0) == -1) return NULL; + if (pq_execute(self, buffer, 0, 0, self->withhold) == -1) return NULL; if (_psyco_curs_prefetch(self) < 0) return NULL; } @@ -816,7 +816,7 @@ psyco_curs_next_named(cursorObject *self) PyOS_snprintf(buffer, 127, "FETCH FORWARD %ld FROM \"%s\"", self->itersize, self->name); - if (pq_execute(self, buffer, 0, 0) == -1) return NULL; + if (pq_execute(self, buffer, 0, 0, self->withhold) == -1) return NULL; if (_psyco_curs_prefetch(self) < 0) return NULL; } @@ -885,7 +885,7 @@ psyco_curs_fetchmany(cursorObject *self, PyObject *args, PyObject *kwords) EXC_IF_TPC_PREPARED(self->conn, fetchone); PyOS_snprintf(buffer, 127, "FETCH FORWARD %d FROM \"%s\"", (int)size, self->name); - if (pq_execute(self, buffer, 0, 0) == -1) { goto exit; } + if (pq_execute(self, buffer, 0, 0, self->withhold) == -1) { goto exit; } if (_psyco_curs_prefetch(self) < 0) { goto exit; } } @@ -960,7 +960,7 @@ psyco_curs_fetchall(cursorObject *self) EXC_IF_ASYNC_IN_PROGRESS(self, fetchall); EXC_IF_TPC_PREPARED(self->conn, fetchall); PyOS_snprintf(buffer, 127, "FETCH FORWARD ALL FROM \"%s\"", self->name); - if (pq_execute(self, buffer, 0, 0) == -1) { goto exit; } + if (pq_execute(self, buffer, 0, 0, self->withhold) == -1) { goto exit; } if (_psyco_curs_prefetch(self) < 0) { goto exit; } } @@ -1178,7 +1178,7 @@ psyco_curs_scroll(cursorObject *self, PyObject *args, PyObject *kwargs) else { PyOS_snprintf(buffer, 127, "MOVE %d FROM \"%s\"", value, self->name); } - if (pq_execute(self, buffer, 0, 0) == -1) return NULL; + if (pq_execute(self, buffer, 0, 0, self->withhold) == -1) return NULL; if (_psyco_curs_prefetch(self) < 0) return NULL; } @@ -1391,7 +1391,7 @@ psyco_curs_copy_from(cursorObject *self, PyObject *args, PyObject *kwargs) Py_INCREF(file); self->copyfile = file; - if (pq_execute(self, query, 0, 0) >= 0) { + if (pq_execute(self, query, 0, 0, 0) >= 0) { res = Py_None; Py_INCREF(Py_None); } @@ -1485,7 +1485,7 @@ psyco_curs_copy_to(cursorObject *self, PyObject *args, PyObject *kwargs) Py_INCREF(file); self->copyfile = file; - if (pq_execute(self, query, 0, 0) >= 0) { + if (pq_execute(self, query, 0, 0, 0) >= 0) { res = Py_None; Py_INCREF(Py_None); } @@ -1559,7 +1559,7 @@ psyco_curs_copy_expert(cursorObject *self, PyObject *args, PyObject *kwargs) self->copyfile = file; /* At this point, the SQL statement must be str, not unicode */ - if (pq_execute(self, Bytes_AS_STRING(sql), 0, 0) >= 0) { + if (pq_execute(self, Bytes_AS_STRING(sql), 0, 0, 0) >= 0) { res = Py_None; Py_INCREF(res); } diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 42460f5b..3512f639 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -893,7 +893,7 @@ pq_flush(connectionObject *conn) */ RAISES_NEG int -pq_execute(cursorObject *curs, const char *query, int async, int no_result) +pq_execute(cursorObject *curs, const char *query, int async, int no_result, int no_begin) { PGresult *pgres = NULL; char *error = NULL; @@ -916,7 +916,7 @@ pq_execute(cursorObject *curs, const char *query, int async, int no_result) Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&(curs->conn->lock)); - if (pq_begin_locked(curs->conn, &pgres, &error, &_save) < 0) { + if (!no_begin && pq_begin_locked(curs->conn, &pgres, &error, &_save) < 0) { pthread_mutex_unlock(&(curs->conn->lock)); Py_BLOCK_THREADS; pq_complete_error(curs->conn, &pgres, &error); diff --git a/psycopg/pqpath.h b/psycopg/pqpath.h index 40beea19..bd3293f8 100644 --- a/psycopg/pqpath.h +++ b/psycopg/pqpath.h @@ -36,7 +36,7 @@ HIDDEN PGresult *pq_get_last_result(connectionObject *conn); RAISES_NEG HIDDEN int pq_fetch(cursorObject *curs, int no_result); RAISES_NEG HIDDEN int pq_execute(cursorObject *curs, const char *query, - int async, int no_result); + int async, int no_result, int no_begin); HIDDEN int pq_send_query(connectionObject *conn, const char *query); HIDDEN int pq_begin_locked(connectionObject *conn, PGresult **pgres, char **error, PyThreadState **tstate);