From: Denis Laxalde Date: Fri, 23 Apr 2021 13:41:41 +0000 (+0200) Subject: Define Connection's row_factory as a property X-Git-Tag: 3.0.dev0~63^2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=412af3419bb19d1615a66dc5e159a9c5393b715d;p=thirdparty%2Fpsycopg.git Define Connection's row_factory as a property This is similar to Cursor class and will make things easier in following changes, making Connection generic on row type. --- diff --git a/docs/api/connections.rst b/docs/api/connections.rst index 7590e8ff0..113dec25b 100644 --- a/docs/api/connections.rst +++ b/docs/api/connections.rst @@ -101,9 +101,7 @@ The `!Connection` class queries. .. autoattribute:: row_factory - :annotation: RowFactory - Writable attribute to control how result rows are formed. See :ref:`row-factories` for details. .. rubric:: Transaction management methods diff --git a/psycopg3/psycopg3/connection.py b/psycopg3/psycopg3/connection.py index a5b4ad6ba..96440bc71 100644 --- a/psycopg3/psycopg3/connection.py +++ b/psycopg3/psycopg3/connection.py @@ -98,10 +98,9 @@ class BaseConnection(AdaptContext): ConnStatus = pq.ConnStatus TransactionStatus = pq.TransactionStatus - row_factory: RowFactory[Any] = tuple_row - - def __init__(self, pgconn: "PGconn"): + def __init__(self, pgconn: "PGconn", row_factory: RowFactory[Any]): self.pgconn = pgconn # TODO: document this + self._row_factory = row_factory self._autocommit = False self._adapters = adapt.AdaptersMap(adapt.global_adapters) self._notice_handlers: List[NoticeHandler] = [] @@ -229,6 +228,15 @@ class BaseConnection(AdaptContext): # implement the AdaptContext protocol return self + @property + def row_factory(self) -> RowFactory[Any]: + """Writable attribute to control how result rows are formed.""" + return self._row_factory + + @row_factory.setter + def row_factory(self, row_factory: RowFactory[Any]) -> None: + self._row_factory = row_factory + def fileno(self) -> int: """Return the file descriptor of the connection. @@ -350,11 +358,10 @@ class BaseConnection(AdaptContext): """Generator to connect to the database and create a new instance.""" conninfo = make_conninfo(conninfo, **kwargs) pgconn = yield from connect(conninfo) - conn = cls(pgconn) + if not row_factory: + row_factory = tuple_row + conn = cls(pgconn, row_factory) conn._autocommit = autocommit - if row_factory is None: - row_factory = cls.row_factory - conn.row_factory = row_factory return conn def _exec_command(self, command: Query) -> PQGen["PGresult"]: @@ -435,8 +442,8 @@ class Connection(BaseConnection): __module__ = "psycopg3" - def __init__(self, pgconn: "PGconn"): - super().__init__(pgconn) + def __init__(self, pgconn: "PGconn", row_factory: RowFactory[Any]): + super().__init__(pgconn, row_factory) self.lock = threading.Lock() @classmethod @@ -626,8 +633,8 @@ class AsyncConnection(BaseConnection): __module__ = "psycopg3" - def __init__(self, pgconn: "PGconn"): - super().__init__(pgconn) + def __init__(self, pgconn: "PGconn", row_factory: RowFactory[Any]): + super().__init__(pgconn, row_factory) self.lock = asyncio.Lock() @classmethod