From: Denis Laxalde Date: Thu, 11 Feb 2021 16:25:34 +0000 (+0100) Subject: Add row_factory argument to connection.execute() X-Git-Tag: 3.0.dev0~106^2~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=325ee499be74b5dfd1ea81932528d8608953dff6;p=thirdparty%2Fpsycopg.git Add row_factory argument to connection.execute() --- diff --git a/psycopg3/psycopg3/connection.py b/psycopg3/psycopg3/connection.py index 0caca8bca..8f0c93281 100644 --- a/psycopg3/psycopg3/connection.py +++ b/psycopg3/psycopg3/connection.py @@ -470,9 +470,10 @@ class Connection(BaseConnection): query: Query, params: Optional[Params] = None, prepare: Optional[bool] = None, + row_factory: Optional[RowFactory] = None, ) -> "Cursor": """Execute a query and return a cursor to read its results.""" - cur = self.cursor() + cur = self.cursor(row_factory=row_factory) return cur.execute(query, params, prepare=prepare) def commit(self) -> None: @@ -612,8 +613,9 @@ class AsyncConnection(BaseConnection): query: Query, params: Optional[Params] = None, prepare: Optional[bool] = None, + row_factory: Optional[RowFactory] = None, ) -> "AsyncCursor": - cur = await self.cursor() + cur = await self.cursor(row_factory=row_factory) return await cur.execute(query, params, prepare=prepare) async def commit(self) -> None: diff --git a/tests/test_connection.py b/tests/test_connection.py index 398900793..aef5825ac 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -481,6 +481,9 @@ def test_execute(conn): cur = conn.execute("select 12, 22") assert cur.fetchone() == (12, 22) + cur = conn.execute("select 1, 2, 1, 2", row_factory=lambda cur: set) + assert cur.fetchone() == {1, 2} + def test_str(conn): assert "[IDLE]" in str(conn) diff --git a/tests/test_connection_async.py b/tests/test_connection_async.py index 71bfb6a1d..06da3ecfc 100644 --- a/tests/test_connection_async.py +++ b/tests/test_connection_async.py @@ -499,6 +499,9 @@ async def test_execute(aconn): cur = await aconn.execute("select 12, 22") assert await cur.fetchone() == (12, 22) + cur = await aconn.execute("select 1, 2, 1, 2", row_factory=lambda cur: set) + assert await cur.fetchone() == {1, 2} + async def test_str(aconn): assert "[IDLE]" in str(aconn)