diff --git a/src/connection.py b/src/connection.py index ec455124..16ed38bf 100644 --- a/src/connection.py +++ b/src/connection.py @@ -1,6 +1,4 @@ # coding: utf-8 -import logging - # we support both pg8000 and psycopg2 try: import psycopg2 as pglib @@ -106,16 +104,14 @@ def rollback(self): def execute(self, query, *args): self.cursor.execute(query, args) - try: - # pg8000 might return tuples - res = [tuple(t) for t in self.cursor.fetchall()] - return res - except ProgrammingError: - return None - except Exception as e: - logging.error("Error executing query: {}\n {}".format(repr(e), query)) + + if self.cursor.description is None: return None + # pg8000 might return tuples + res = [tuple(t) for t in self.cursor.fetchall()] + return res + def close(self): self.cursor.close() self.connection.close() diff --git a/tests/test_testgres_common.py b/tests/test_testgres_common.py index 93effdb2..4aa52a9a 100644 --- a/tests/test_testgres_common.py +++ b/tests/test_testgres_common.py @@ -3343,6 +3343,30 @@ def test_node_app__make_empty_and_pgconf(self, node_svc: PostgresNodeService): assert node.status() == NodeStatus.Running return + def test_node_connection__execute( + self, + node_svc: PostgresNodeService, + ): + assert isinstance(node_svc, PostgresNodeService) + + with __class__.helper__get_node(node_svc) as node: + node.init() + node.start() + + with node.connect("postgres") as cn: + assert cn.execute("create table ttt (id integer);") is None + assert cn.execute("insert into ttt values(1);") is None + assert cn.execute("update ttt set id=2;") is None + assert cn.execute("select id from ttt;") == [(2, )] + assert cn.execute("select id from ttt where id=%s;", '2') == [(2, )] + assert cn.execute("select id from ttt where id=%s and (id + 1) = %s;", '2', 3) == [(2, )] + assert cn.execute("delete from ttt where id=2;") is None + assert cn.execute("select id from ttt;") == [] + assert cn.execute("drop table ttt;") is None + pass + pass + return + @staticmethod def helper__get_node( node_svc: PostgresNodeService,