From: Daniele Varrazzo Date: Fri, 16 Apr 2021 18:17:03 +0000 (+0100) Subject: Add ConnectionInfo.server_version, protocol_version X-Git-Tag: 3.0.dev0~66^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6c3769c8f3b74cf95e1479d58f8e288cadb5c9a7;p=thirdparty%2Fpsycopg.git Add ConnectionInfo.server_version, protocol_version --- diff --git a/docs/api/connections.rst b/docs/api/connections.rst index 7a0612661..5a9f6dfb0 100644 --- a/docs/api/connections.rst +++ b/docs/api/connections.rst @@ -253,21 +253,26 @@ Connection support objects The object is usually returned by `Connection.info`. + .. autoproperty:: status + .. autoproperty:: transaction_status + .. autoproperty:: server_version + .. automethod:: get_parameters + .. autoproperty:: host .. autoproperty:: port .. autoproperty:: dbname .. autoproperty:: user .. autoproperty:: password .. autoproperty:: options - .. autoproperty:: status - .. autoproperty:: transaction_status - .. automethod:: get_parameters .. automethod:: parameter_status Example of parameters are ``server_version``, ``standard_conforming_string``... See :pq:`PQparameterStatus()` for all the available parameters. + .. autoproperty:: protocol_version + + .. rubric:: Objects involved in :ref:`transactions` .. autoclass:: Transaction() diff --git a/psycopg3/psycopg3/conninfo.py b/psycopg3/psycopg3/conninfo.py index 292846e89..a05884f9e 100644 --- a/psycopg3/psycopg3/conninfo.py +++ b/psycopg3/psycopg3/conninfo.py @@ -179,6 +179,23 @@ class ConnectionInfo: res = self.pgconn.parameter_status(param_name.encode(self._pyenc)) return res.decode(self._pyenc) if res is not None else None + @property + def server_version(self) -> int: + """An integer representing the server version. + + The number is formed by converting the major, minor, and revision + numbers into two-decimal-digit numbers and appending them together. + After PostgreSQL 10 the minor version was dropped, so the second group + of digits is always 00. For example, version 9.3.5 will be returned as + 90305, version 10.2 as 100002. See :pq:`PQserverVersion()`. + """ + return self.pgconn.server_version + + @property + def protocol_version(self) -> int: + """The frontend/backend protocol currently used.""" + return self.pgconn.protocol_version + def _get_pgconn_attr(self, name: str) -> str: value: bytes = getattr(self.pgconn, name) return value.decode(self._pyenc) diff --git a/tests/test_conninfo.py b/tests/test_conninfo.py index e6af8be71..b53436284 100644 --- a/tests/test_conninfo.py +++ b/tests/test_conninfo.py @@ -153,3 +153,9 @@ class TestConnectionInfo: tz = conn.info.parameter_status("TimeZone") assert tz and isinstance(tz, str) assert tz == conn.execute("show timezone").fetchone()[0] + + def test_server_version(self, conn): + assert conn.info.server_version == conn.pgconn.server_version + + def test_protocol_version(self, conn): + assert conn.info.protocol_version >= 3