From d138e42ee5ac39797ae6b563ddd87eca3166b458 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 13 Oct 2018 02:40:17 +0100 Subject: [PATCH] Added ConnectionInfo.parameter_status() --- doc/src/connection.rst | 84 +++++++++++++++++++++------------------- doc/src/extensions.rst | 1 + psycopg/conninfo_type.c | 36 +++++++++++++++++ tests/test_connection.py | 15 +++++++ 4 files changed, 97 insertions(+), 39 deletions(-) diff --git a/doc/src/connection.rst b/doc/src/connection.rst index 9c88b77c..5310458b 100644 --- a/doc/src/connection.rst +++ b/doc/src/connection.rst @@ -613,45 +613,6 @@ The ``connection`` class .. versionadded:: 2.8.0 - .. index:: - pair: Backend; PID - - .. method:: get_backend_pid() - - Returns the process ID (PID) of the backend server process handling - this connection. - - Note that the PID belongs to a process executing on the database - server host, not the local host! - - .. seealso:: libpq docs for `PQbackendPID()`__ for details. - - .. __: https://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQBACKENDPID - - .. versionadded:: 2.0.8 - - - .. index:: - pair: Server; Parameters - - .. method:: get_parameter_status(parameter) - - Look up a current parameter setting of the server. - - Potential values for ``parameter`` are: ``server_version``, - ``server_encoding``, ``client_encoding``, ``is_superuser``, - ``session_authorization``, ``DateStyle``, ``TimeZone``, - ``integer_datetimes``, and ``standard_conforming_strings``. - - If server did not report requested parameter, return `!None`. - - .. seealso:: libpq docs for `PQparameterStatus()`__ for details. - - .. __: https://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQPARAMETERSTATUS - - .. versionadded:: 2.0.12 - - .. index:: pair: Connection; Parameters @@ -847,6 +808,51 @@ The ``connection`` class .. versionadded:: 2.0.12 + .. index:: + pair: Backend; PID + + .. method:: get_backend_pid() + + Also available as `~connection.info`\ `!.`\ + `~psycopg2.extensions.ConnectionInfo.backend_pid`. + + Returns the process ID (PID) of the backend server process handling + this connection. + + Note that the PID belongs to a process executing on the database + server host, not the local host! + + .. seealso:: libpq docs for `PQbackendPID()`__ for details. + + .. __: https://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQBACKENDPID + + .. versionadded:: 2.0.8 + + + .. index:: + pair: Server; Parameters + + .. method:: get_parameter_status(parameter) + + Also available as `~connection.info`\ `!.`\ + `~psycopg2.extensions.ConnectionInfo.parameter_status()`. + + Look up a current parameter setting of the server. + + Potential values for ``parameter`` are: ``server_version``, + ``server_encoding``, ``client_encoding``, ``is_superuser``, + ``session_authorization``, ``DateStyle``, ``TimeZone``, + ``integer_datetimes``, and ``standard_conforming_strings``. + + If server did not report requested parameter, return `!None`. + + .. seealso:: libpq docs for `PQparameterStatus()`__ for details. + + .. __: https://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQPARAMETERSTATUS + + .. versionadded:: 2.0.12 + + .. testcode:: :hide: diff --git a/doc/src/extensions.rst b/doc/src/extensions.rst index 02937409..10ed81a6 100644 --- a/doc/src/extensions.rst +++ b/doc/src/extensions.rst @@ -167,6 +167,7 @@ introspection etc. .. autoattribute:: options .. autoattribute:: status .. autoattribute:: transaction_status + .. automethod:: parameter_status(name) .. autoattribute:: protocol_version diff --git a/psycopg/conninfo_type.c b/psycopg/conninfo_type.c index c7a21110..ed89c6c8 100644 --- a/psycopg/conninfo_type.c +++ b/psycopg/conninfo_type.c @@ -208,6 +208,40 @@ transaction_status_get(connInfoObject *self) } +static const char parameter_status_doc[] = +"Looks up a current parameter setting of the server.\n" +"\n" +":param name: The name of the parameter to return.\n" +":type name: `!str`\n" +":return: The parameter value, `!None` if the parameter is unknown.\n" +":rtype: `!str`\n" +"\n" +".. seealso:: libpq docs for `PQparameterStatus()`__ for details.\n" +".. __: https://www.postgresql.org/docs/current/static/libpq-status.html" + "#LIBPQ-PQPARAMETERSTATUS"; + +static PyObject * +parameter_status(connInfoObject *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = {"name", NULL}; + const char *name; + const char *val; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name)) { + return NULL; + } + + val = PQparameterStatus(self->conn->pgconn, name); + + if (!val) { + Py_RETURN_NONE; + } + else { + return conn_text_from_chars(self->conn, val); + } +} + + static const char protocol_version_doc[] = "The frontend/backend protocol being used.\n" "\n" @@ -468,6 +502,8 @@ static struct PyGetSetDef connInfoObject_getsets[] = { static struct PyMethodDef connInfoObject_methods[] = { {"ssl_attribute", (PyCFunction)ssl_attribute, METH_VARARGS|METH_KEYWORDS, ssl_attribute_doc}, + {"parameter_status", (PyCFunction)parameter_status, + METH_VARARGS|METH_KEYWORDS, parameter_status_doc}, {NULL} }; diff --git a/tests/test_connection.py b/tests/test_connection.py index 0c8d40d6..0b683892 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -1736,6 +1736,21 @@ class TestConnectionInfo(ConnectingTestCase): self.assertEqual(self.conn.info.transaction_status, 2) self.assertEqual(self.bconn.info.transaction_status, 4) + def test_parameter_status(self): + cur = self.conn.cursor() + try: + cur.execute("show server_version") + except psycopg2.DatabaseError: + self.assertIsInstance( + self.conn.info.parameter_status('server_version'), str) + else: + self.assertEqual( + self.conn.info.parameter_status('server_version'), + cur.fetchone()[0]) + + self.assertIsNone(self.conn.info.parameter_status('wat')) + self.assertIsNone(self.bconn.info.parameter_status('server_version')) + def test_protocol_version(self): self.assertEqual(self.conn.info.protocol_version, 3) self.assertEqual(self.bconn.info.protocol_version, 0)