Added ConnectionInfo.parameter_status()

This commit is contained in:
Daniele Varrazzo 2018-10-13 02:40:17 +01:00
parent 9f6a3a5e96
commit d138e42ee5
4 changed files with 97 additions and 39 deletions

View File

@ -613,45 +613,6 @@ The ``connection`` class
.. versionadded:: 2.8.0 .. 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:: .. index::
pair: Connection; Parameters pair: Connection; Parameters
@ -847,6 +808,51 @@ The ``connection`` class
.. versionadded:: 2.0.12 .. 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:: .. testcode::
:hide: :hide:

View File

@ -167,6 +167,7 @@ introspection etc.
.. autoattribute:: options .. autoattribute:: options
.. autoattribute:: status .. autoattribute:: status
.. autoattribute:: transaction_status .. autoattribute:: transaction_status
.. automethod:: parameter_status(name)
.. autoattribute:: protocol_version .. autoattribute:: protocol_version

View File

@ -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[] = static const char protocol_version_doc[] =
"The frontend/backend protocol being used.\n" "The frontend/backend protocol being used.\n"
"\n" "\n"
@ -468,6 +502,8 @@ static struct PyGetSetDef connInfoObject_getsets[] = {
static struct PyMethodDef connInfoObject_methods[] = { static struct PyMethodDef connInfoObject_methods[] = {
{"ssl_attribute", (PyCFunction)ssl_attribute, {"ssl_attribute", (PyCFunction)ssl_attribute,
METH_VARARGS|METH_KEYWORDS, ssl_attribute_doc}, METH_VARARGS|METH_KEYWORDS, ssl_attribute_doc},
{"parameter_status", (PyCFunction)parameter_status,
METH_VARARGS|METH_KEYWORDS, parameter_status_doc},
{NULL} {NULL}
}; };

View File

@ -1736,6 +1736,21 @@ class TestConnectionInfo(ConnectingTestCase):
self.assertEqual(self.conn.info.transaction_status, 2) self.assertEqual(self.conn.info.transaction_status, 2)
self.assertEqual(self.bconn.info.transaction_status, 4) 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): def test_protocol_version(self):
self.assertEqual(self.conn.info.protocol_version, 3) self.assertEqual(self.conn.info.protocol_version, 3)
self.assertEqual(self.bconn.info.protocol_version, 0) self.assertEqual(self.bconn.info.protocol_version, 0)