]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added PQping
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 14 Mar 2020 05:39:11 +0000 (18:39 +1300)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 14 Mar 2020 05:39:11 +0000 (18:39 +1300)
psycopg3/_pq_ctypes.py
psycopg3/pq_ctypes.py
psycopg3/pq_enums.py
tests/test_pq.py

index 0a09c52d3aba5c388a8d03f34186c0aeb873e025..20078d967cdc163aea750c8552c259ae0af88a41 100644 (file)
@@ -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
 
index 1e1d02bc57c704003454bd0430a5bca7590213df..5e784e00be8dd4a26b4c34aebedef129c563a420 100644 (file)
@@ -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
index 1eaa262ea7c3ccb4ec99dcf217711670d4d9ebba..83edca782b2071e36fb23ff0daf5a674cb6730cd 100644 (file)
@@ -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()
index 3fd94ba3af9b999ad02b4f4de68973d3a88c120a..c524ea62f15ace29036d01792b75170c755423c0 100644 (file)
@@ -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