From: Daniele Varrazzo Date: Sat, 14 Mar 2020 05:39:11 +0000 (+1300) Subject: Added PQping X-Git-Tag: 3.0.dev0~727 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bbab77bccdf803b05237d3106221c2917f5ca71e;p=thirdparty%2Fpsycopg.git Added PQping --- diff --git a/psycopg3/_pq_ctypes.py b/psycopg3/_pq_ctypes.py index 0a09c52d3..20078d967 100644 --- a/psycopg3/_pq_ctypes.py +++ b/psycopg3/_pq_ctypes.py @@ -86,6 +86,10 @@ PQresetPoll = pq.PQresetPoll PQresetPoll.argtypes = [PGconn_ptr] PQresetPoll.restype = c_int +PQping = pq.PQping +PQping.argtypes = [c_char_p] +PQping.restype = c_int + # 33.2. Connection Status Functions diff --git a/psycopg3/pq_ctypes.py b/psycopg3/pq_ctypes.py index 1e1d02bc5..5e784e00b 100644 --- a/psycopg3/pq_ctypes.py +++ b/psycopg3/pq_ctypes.py @@ -10,7 +10,7 @@ implementation. from collections import namedtuple -from .pq_enums import ConnStatus, PostgresPollingStatus +from .pq_enums import ConnStatus, PostgresPollingStatus, PGPing from . import _pq_ctypes as impl @@ -92,6 +92,14 @@ class PGconn: rv = impl.PQstatus(self.pgconn_ptr) return ConnStatus(rv) + @classmethod + def ping(self, conninfo): + if isinstance(conninfo, str): + conninfo = conninfo.encode("utf8") + + rv = impl.PQping(conninfo) + return PGPing(rv) + @property def error_message(self): # TODO: decode diff --git a/psycopg3/pq_enums.py b/psycopg3/pq_enums.py index 1eaa262ea..83edca782 100644 --- a/psycopg3/pq_enums.py +++ b/psycopg3/pq_enums.py @@ -30,3 +30,10 @@ class PostgresPollingStatus(IntEnum): PGRES_POLLING_WRITING = auto() PGRES_POLLING_OK = auto() PGRES_POLLING_ACTIVE = auto() + + +class PGPing(IntEnum): + PQPING_OK = 0 + PQPING_REJECT = auto() + PQPING_NO_RESPONSE = auto() + PQPING_NO_ATTEMPT = auto() diff --git a/tests/test_pq.py b/tests/test_pq.py index 3fd94ba3a..c524ea62f 100644 --- a/tests/test_pq.py +++ b/tests/test_pq.py @@ -3,7 +3,7 @@ from select import select import pytest -from psycopg3.pq_enums import ConnStatus, PostgresPollingStatus +from psycopg3.pq_enums import ConnStatus, PostgresPollingStatus, PGPing def test_connectdb(pq, dsn): @@ -118,3 +118,11 @@ def test_reset_async(pq, dsn): assert rv == PostgresPollingStatus.PGRES_POLLING_OK assert conn.status == ConnStatus.CONNECTION_OK + + +def test_ping(pq, dsn): + rv = pq.PGconn.ping(dsn) + assert rv == PGPing.PQPING_OK + + rv = pq.PGconn.ping("port=99999") + assert rv == PGPing.PQPING_NO_RESPONSE