.. autoattribute:: closed
.. autoattribute:: broken
-
.. method:: cursor(*, binary: bool = False, row_factory: Optional[RowFactory] = None) -> Cursor
.. method:: cursor(name: str, *, binary: bool = False, row_factory: Optional[RowFactory] = None) -> ServerCursor
:noindex:
loader. See :ref:`binary-data` for details.
:param row_factory: If specified override the `row_factory` set on the
connection. See :ref:`row-factories` for details.
+ :return: A cursor of the class specified by `cursor_factory` (or
+ `server_cursor_factory` if *name* is specified).
.. note:: You can use :ref:`with conn.cursor(): ...<usage>`
to close the cursor automatically when the block is exited.
+ .. autoattribute:: cursor_factory
+
+ The type, of factory function, returned by `cursor()` and `execute()`.
+
+ Default is `psycopg.Cursor`.
+
+ .. autoattribute:: server_cursor_factory
+
+ The type, of factory function, returned by `cursor()` when a name is
+ specified.
+
+ Default is `psycopg.ServerCursor`.
+
.. automethod:: execute(query, params=None, prepare=None) -> Cursor
:param query: The query to execute.
.. note:: You can use ``async with conn.cursor() as cur: ...`` to
close the cursor automatically when the block is exited.
+ .. autoattribute:: cursor_factory
+
+ Default is `psycopg.AsyncCursor`.
+
+ .. autoattribute:: server_cursor_factory
+
+ Default is `psycopg.AsyncServerCursor`.
+
.. automethod:: execute(query, params=None, prepare=None) -> AsyncCursor
.. automethod:: commit
.. automethod:: rollback
__module__ = "psycopg"
+ cursor_factory: Type[Cursor[Row]]
+ server_cursor_factory: Type[ServerCursor[Row]]
+
def __init__(self, pgconn: "PGconn", row_factory: RowFactory[Row]):
super().__init__(pgconn, row_factory)
self.lock = threading.Lock()
+ self.cursor_factory = Cursor
+ self.server_cursor_factory = ServerCursor
@overload
@classmethod
cur: Union[Cursor[Any], ServerCursor[Any]]
if name:
- cur = ServerCursor(self, name=name, row_factory=row_factory)
+ cur = self.server_cursor_factory(
+ self, name=name, row_factory=row_factory
+ )
else:
- cur = Cursor(self, row_factory=row_factory)
+ cur = self.cursor_factory(self, row_factory=row_factory)
if binary:
cur.format = Format.BINARY
__module__ = "psycopg"
+ cursor_factory: Type[AsyncCursor[Row]]
+ server_cursor_factory: Type[AsyncServerCursor[Row]]
+
def __init__(self, pgconn: "PGconn", row_factory: RowFactory[Row]):
super().__init__(pgconn, row_factory)
self.lock = asyncio.Lock()
+ self.cursor_factory = AsyncCursor
+ self.server_cursor_factory = AsyncServerCursor
@overload
@classmethod
cur: Union[AsyncCursor[Any], AsyncServerCursor[Any]]
if name:
- cur = AsyncServerCursor(self, name=name, row_factory=row_factory)
+ cur = self.server_cursor_factory(
+ self, name=name, row_factory=row_factory
+ )
else:
- cur = AsyncCursor(self, row_factory=row_factory)
+ cur = self.cursor_factory(self, row_factory=row_factory)
if binary:
cur.format = Format.BINARY
conn.close()
with pytest.raises(psycopg.OperationalError):
conn.fileno()
+
+
+def test_cursor_factory(conn):
+ assert conn.cursor_factory is psycopg.Cursor
+
+ class MyCursor(psycopg.Cursor):
+ pass
+
+ conn.cursor_factory = MyCursor
+ with conn.cursor() as cur:
+ assert isinstance(cur, MyCursor)
+
+ with conn.execute("select 1") as cur:
+ assert isinstance(cur, MyCursor)
+
+
+def test_server_cursor_factory(conn):
+ assert conn.server_cursor_factory is psycopg.ServerCursor
+
+ class MyServerCursor(psycopg.ServerCursor):
+ pass
+
+ conn.server_cursor_factory = MyServerCursor
+ with conn.cursor(name="n") as cur:
+ assert isinstance(cur, MyServerCursor)
await aconn.close()
with pytest.raises(psycopg.OperationalError):
aconn.fileno()
+
+
+async def test_cursor_factory(aconn):
+ assert aconn.cursor_factory is psycopg.AsyncCursor
+
+ class MyCursor(psycopg.AsyncCursor):
+ pass
+
+ aconn.cursor_factory = MyCursor
+ async with aconn.cursor() as cur:
+ assert isinstance(cur, MyCursor)
+
+ async with (await aconn.execute("select 1")) as cur:
+ assert isinstance(cur, MyCursor)
+
+
+async def test_server_cursor_factory(aconn):
+ assert aconn.server_cursor_factory is psycopg.AsyncServerCursor
+
+ class MyServerCursor(psycopg.AsyncServerCursor):
+ pass
+
+ aconn.server_cursor_factory = MyServerCursor
+ async with aconn.cursor(name="n") as cur:
+ assert isinstance(cur, MyServerCursor)