From a779c8ef993e2b5106c27865d111ccf82b983445 Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Wed, 25 Apr 2007 22:42:36 +0000 Subject: [PATCH] .get_transaction_status() implementation. --- ChangeLog | 7 +++++++ lib/extensions.py | 10 ++++++++-- psycopg/connection_type.c | 21 +++++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 75011f5e..2fdd3f1c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2007-04-25 Federico Di Gregorio + + * psycopg/connection_type.c: added support for a new method + .get_transaction_status() that returns the backend transaction + status as libpq knows. Also added some symbolic constants to + psycopg.extensions module (TRANSACTION_STATUS_XXX). + 2007-04-14 Federico Di Gregorio * psycopg/psycopg.h: fixed probable typo in definition of diff --git a/lib/extensions.py b/lib/extensions.py index c17878dc..fe613039 100644 --- a/lib/extensions.py +++ b/lib/extensions.py @@ -51,8 +51,8 @@ ISOLATION_LEVEL_SERIALIZABLE = 2 ISOLATION_LEVEL_REPEATABLE_READ = ISOLATION_LEVEL_SERIALIZABLE ISOLATION_LEVEL_READ_UNCOMMITTED = ISOLATION_LEVEL_READ_COMMITTED -"""Transaction status values.""" -STATUS_SETUP = 0 +"""psycopg connection status values.""" +STATUS_SETUP = 0 STATUS_READY = 1 STATUS_BEGIN = 2 STATUS_SYNC = 3 @@ -61,6 +61,12 @@ STATUS_ASYNC = 4 # This is a usefull mnemonic to check if the connection is in a transaction STATUS_IN_TRANSACTION = STATUS_BEGIN +"""Backend transaction status values.""" +TRANSACTION_STATUS_IDLE = 0 +TRANSACTION_STATUS_ACTIVE = 1 +TRANSACTION_STATUS_INTRANS = 2 +TRANSACTION_STATUS_INERROR = 3 +TRANSACTION_STATUS_UNKNOWN = 4 def register_adapter(typ, callable): """Register 'callable' as an ISQLQuote adapter for type 'typ'.""" diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c index 4abb08d0..ae737b57 100644 --- a/psycopg/connection_type.c +++ b/psycopg/connection_type.c @@ -146,6 +146,7 @@ psyco_conn_rollback(connectionObject *self, PyObject *args) #ifdef PSYCOPG_EXTENSIONS + /* set_isolation_level method - switch connection isolation level */ #define psyco_conn_set_isolation_level_doc \ @@ -173,8 +174,6 @@ psyco_conn_set_isolation_level(connectionObject *self, PyObject *args) return Py_None; } - - /* set_isolation_level method - switch connection isolation level */ #define psyco_conn_set_client_encoding_doc \ @@ -210,6 +209,22 @@ psyco_conn_set_client_encoding(connectionObject *self, PyObject *args) return NULL; } } + +/* set_isolation_level method - switch connection isolation level */ + +#define psyco_conn_get_transaction_status_doc \ +"get_transaction_status() -- Get backend transaction status." + +static PyObject * +psyco_conn_get_transaction_status(connectionObject *self, PyObject *args) +{ + EXC_IF_CONN_CLOSED(self); + + if (!PyArg_ParseTuple(args, "")) return NULL; + + return PyInt_FromLong((long)PQtransactionStatus(self->pgconn)); +} + #endif @@ -232,6 +247,8 @@ static struct PyMethodDef connectionObject_methods[] = { METH_VARARGS, psyco_conn_set_isolation_level_doc}, {"set_client_encoding", (PyCFunction)psyco_conn_set_client_encoding, METH_VARARGS, psyco_conn_set_client_encoding_doc}, + {"get_transaction_status", (PyCFunction)psyco_conn_get_transaction_status, + METH_VARARGS, psyco_conn_get_transaction_status_doc}, #endif {NULL} };