]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Define Connection's row_factory as a property
authorDenis Laxalde <denis.laxalde@dalibo.com>
Fri, 23 Apr 2021 13:41:41 +0000 (15:41 +0200)
committerDenis Laxalde <denis.laxalde@dalibo.com>
Tue, 27 Apr 2021 08:58:23 +0000 (10:58 +0200)
This is similar to Cursor class and will make things easier in following
changes, making Connection generic on row type.

docs/api/connections.rst
psycopg3/psycopg3/connection.py

index 7590e8ff0db8667a6b108e157fd24b8e8218b6e6..113dec25b76c14dd00fc802f750012a33f318dba 100644 (file)
@@ -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
index a5b4ad6baa743d3ba42f51a4f3f150f389e8d526..96440bc7180f8177a0b601e671aa9d9d01224a71 100644 (file)
@@ -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