From 5dbeeba0f2760eb2344a34f1b25c952bfb4adb32 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 13 Oct 2010 00:28:42 +0100 Subject: [PATCH] Raise NotSuppoertdError if tpc is used with PostgreSQL < 8.1 --- psycopg/connection.h | 9 +++++++++ psycopg/connection_type.c | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/psycopg/connection.h b/psycopg/connection.h index 85e2d26b..5d056c5e 100644 --- a/psycopg/connection.h +++ b/psycopg/connection.h @@ -147,6 +147,15 @@ HIDDEN PyObject *conn_tpc_recover(connectionObject *self); "in asynchronous mode"); \ return NULL; } +#define EXC_IF_TPC_NOT_SUPPORTED(self) \ + if ((self)->server_version < 80100) { \ + PyErr_Format(NotSupportedError, \ + "server version %d: " \ + "two-phase transactions not supported", \ + (self)->server_version); \ + return NULL; \ + } + #define EXC_IF_TPC_BEGIN(self, cmd) if ((self)->tpc_xid) { \ PyErr_Format(ProgrammingError, "%s cannot be used " \ "during a two-phase transaction", #cmd); \ diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c index 48f27807..bc3aa876 100644 --- a/psycopg/connection_type.c +++ b/psycopg/connection_type.c @@ -179,6 +179,7 @@ static PyObject * psyco_conn_xid(connectionObject *self, PyObject *args, PyObject *kwargs) { EXC_IF_CONN_CLOSED(self); + EXC_IF_TPC_NOT_SUPPORTED(self); return PyObject_Call((PyObject *)&XidType, args, kwargs); } @@ -196,6 +197,7 @@ psyco_conn_tpc_begin(connectionObject *self, PyObject *args) EXC_IF_CONN_CLOSED(self); EXC_IF_CONN_ASYNC(self, tpc_begin); + EXC_IF_TPC_NOT_SUPPORTED(self); if (!PyArg_ParseTuple(args, "O", &oxid)) { goto exit; @@ -363,6 +365,7 @@ psyco_conn_tpc_commit(connectionObject *self, PyObject *args) { EXC_IF_CONN_CLOSED(self); EXC_IF_CONN_ASYNC(self, tpc_commit); + EXC_IF_TPC_NOT_SUPPORTED(self); return _psyco_conn_tpc_finish(self, args, conn_commit, "COMMIT PREPARED"); @@ -376,6 +379,7 @@ psyco_conn_tpc_rollback(connectionObject *self, PyObject *args) { EXC_IF_CONN_CLOSED(self); EXC_IF_CONN_ASYNC(self, tpc_rollback); + EXC_IF_TPC_NOT_SUPPORTED(self); return _psyco_conn_tpc_finish(self, args, conn_rollback, "ROLLBACK PREPARED"); @@ -390,6 +394,7 @@ psyco_conn_tpc_recover(connectionObject *self, PyObject *args) EXC_IF_CONN_CLOSED(self); EXC_IF_CONN_ASYNC(self, tpc_recover); EXC_IF_TPC_PREPARED(self, tpc_recover); + EXC_IF_TPC_NOT_SUPPORTED(self); if (!PyArg_ParseTuple(args, "")) { return NULL; }