New connection methods: get_server_parameter & co

Marko Kreen <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
Attached patch adds following methods to connection object:

  get_parameter_status - wraps PQparameterStatus
  get_server_version - wraps PQserverVersion
  get_protocol_version - wraps PQprotocolVersion

libpq docs: http://www.postgresql.org/docs/8.3/static/libpq-status.html

The get_parameter_status() is really useful for writing admin/replication
tools with psycopg2.  In my case - Skytools/Londiste.

get_server_version() is also needed, although it could be emulated
by issuing get_parameter_version('server_version') and then parsing
resulting string.  But as libpq already provides more easy-to-use API,
I'd prefer to use that.

get_protocol_version() is provided for completeness.  I personally
don't need it, but it will be useful, if Postgres happens to upgrade
the protocol.

-- 
marko

_______________________________________________
Psycopg mailing list
Psycopg-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/psycopg
param_status_and_more.diff (text/x-patch, 3.2 KB)
=== modified file 'psycopg/connection_type.c'
--- psycopg/connection_type.c	2008-11-25 18:18:17 +0000
+++ psycopg/connection_type.c	2009-04-15 10:46:37 +0000
@@ -231,6 +231,72 @@
     return PyInt_FromLong((long)PQtransactionStatus(self->pgconn));
 }
 
+/* get_parameter_status method - Get server parameter status */
+
+#define psyco_conn_get_parameter_status_doc \
+"get_parameter_status(parameter) -- Get backend parameter status.\n\n" \
+"Potential values for ``parameter``:\n" \
+"  server_version, server_encoding, client_encoding, is_superuser,\n" \
+"  session_authorization, DateStyle, TimeZone, integer_datetimes,\n" \
+"  and standard_conforming_strings\n" \
+"If server did not report requested parameter, None is returned.\n\n" \
+"See libpq docs for PQparameterStatus() for further details."
+
+static PyObject *
+psyco_conn_get_parameter_status(connectionObject *self, PyObject *args)
+{
+    const char *param = NULL;
+    const char *val = NULL;
+
+    EXC_IF_CONN_CLOSED(self);
+
+    if (!PyArg_ParseTuple(args, "s", &param)) return NULL;
+
+    val = PQparameterStatus(self->pgconn, param);
+    if (!val) {
+	Py_INCREF(Py_None);
+	return Py_None;
+    }
+    return PyString_FromString(val);
+}
+
+/* get_server_version method - Get numeric backend server version */
+
+#define psyco_conn_get_server_version_doc \
+"get_server_version() -- Get numeric backend server version.\n\n" \
+"Server version is encoded as (MAJOR * 10000) + (MINOR * 100) + PATCHLEVEL\n" \
+"so 8.1.5 would appear as 80105.\n\n" \
+"See libpq docs for PQserverVersion() for further details."
+
+static PyObject *
+psyco_conn_get_server_version(connectionObject *self, PyObject *args)
+{
+    EXC_IF_CONN_CLOSED(self);
+
+    if (!PyArg_ParseTuple(args, "")) return NULL;
+
+    return PyInt_FromLong((long)PQserverVersion(self->pgconn));
+}
+
+/* get_protocol_version method - Get libpq protocol version used. */
+
+#define psyco_conn_get_protocol_version_doc \
+"get_protocol_version() -- Get libpq protocol version used.\n\n" \
+"Potetial values:\n" \
+"  2 - Protocol v2.0\n" \
+"  3 - Protocol v3.0\n\n" \
+"See libpq docs for PQprotocolVersion() for further details."
+
+static PyObject *
+psyco_conn_get_protocol_version(connectionObject *self, PyObject *args)
+{
+    EXC_IF_CONN_CLOSED(self);
+
+    if (!PyArg_ParseTuple(args, "")) return NULL;
+
+    return PyInt_FromLong((long)PQprotocolVersion(self->pgconn));
+}
+
 /* lobject method - allocate a new lobject */
 
 #define psyco_conn_lobject_doc \
@@ -353,6 +419,12 @@
      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},
+    {"get_parameter_status", (PyCFunction)psyco_conn_get_parameter_status,
+     METH_VARARGS, psyco_conn_get_parameter_status_doc},
+    {"get_server_version", (PyCFunction)psyco_conn_get_server_version,
+     METH_VARARGS, psyco_conn_get_server_version_doc},
+    {"get_protocol_version", (PyCFunction)psyco_conn_get_protocol_version,
+     METH_VARARGS, psyco_conn_get_protocol_version_doc},
     {"get_backend_pid", (PyCFunction)psyco_conn_get_backend_pid,
      METH_NOARGS, psyco_conn_get_backend_pid_doc},
     {"lobject", (PyCFunction)psyco_conn_lobject,
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.