.. automethod:: connect
:param conninfo: The `connection string`__ (a ``postgresql://`` url or
- a list of ``key=value`` pairs) to specify where and
- how to connect.
+ a list of ``key=value`` pairs) to specify where and how to connect.
:param kwargs: Further parameters specifying the connection string.
- They override the ones specified in ``conninfo``.
+ They override the ones specified in ``conninfo``.
:param autocommit: If `!True` don't start transactions automatically.
- See :ref:`transactions` for details.
+ See :ref:`transactions` for details.
:param row_factory: The row factory specifying what type of records
- to create fetching data (default:
- `~psycopg.rows.tuple_row()`). See
- :ref:`row-factories` for details.
- :param prepare_threshold: Set the `prepare_threshold` attribute of the
- connection.
+ to create fetching data (default: `~psycopg.rows.tuple_row()`). See
+ :ref:`row-factories` for details.
+ :param cursor_factory: Initial value for the `cursor_factory` attribute
+ of the connection (new in Psycopg 3.1).
+ :param prepare_threshold: Initial value for the `prepare_threshold`
+ attribute of the connection (new in Psycopg 3.1).
More specialized use:
:param context: A context to copy the initial adapters configuration
- from. It might be an `~psycopg.adapt.AdaptersMap` with
- customized loaders and dumpers, used as a template to
- create several connections. See :ref:`adaptation` for
- further details.
+ from. It might be an `~psycopg.adapt.AdaptersMap` with customized
+ loaders and dumpers, used as a template to create several connections.
+ See :ref:`adaptation` for further details.
.. __: https://www.postgresql.org/docs/current/libpq-connect.html
#LIBPQ-CONNSTRING
.. __: https://www.postgresql.org/docs/current/libpq-envars.html
.. versionchanged:: 3.1
- added ``prepare_threshold`` parameter.
+ added ``prepare_threshold`` and ``cursor_factory`` parameters.
.. automethod:: close
(:ticket:`#145`).
- Add `pq.PGconn.trace()` and related trace functions (:ticket:`#167`).
- Add ``prepare_threshold`` parameter to `Connection` init (:ticket:`#200`).
+- Add ``cursor_factory`` parameter to `Connection` init.
- Add `Error.pgconn` and `Error.pgresult` attributes (:ticket:`#242`).
- Add explicit type cast to values converted by `sql.Literal` (:ticket:`#205`).
- Drop support for Python 3.6.
super().__init__(pgconn)
self.row_factory = row_factory
self.lock = threading.Lock()
- self.cursor_factory = cast("Type[Cursor[Row]]", Cursor)
+ self.cursor_factory = Cursor
self.server_cursor_factory = ServerCursor
@overload
autocommit: bool = False,
row_factory: RowFactory[Row],
prepare_threshold: Optional[int] = 5,
+ cursor_factory: Optional[Type[Cursor[Row]]] = None,
context: Optional[AdaptContext] = None,
**kwargs: Union[None, int, str],
) -> "Connection[Row]":
*,
autocommit: bool = False,
prepare_threshold: Optional[int] = 5,
+ cursor_factory: Optional[Type[Cursor[Any]]] = None,
context: Optional[AdaptContext] = None,
**kwargs: Union[None, int, str],
) -> "Connection[TupleRow]":
autocommit: bool = False,
prepare_threshold: Optional[int] = 5,
row_factory: Optional[RowFactory[Row]] = None,
+ cursor_factory: Optional[Type[Cursor[Row]]] = None,
context: Optional[AdaptContext] = None,
**kwargs: Any,
) -> "Connection[Any]":
if row_factory:
rv.row_factory = row_factory
+ if cursor_factory:
+ rv.cursor_factory = cursor_factory
if context:
rv._adapters = AdaptersMap(context.adapters)
rv.prepare_threshold = prepare_threshold
super().__init__(pgconn)
self.row_factory = row_factory
self.lock = asyncio.Lock()
- self.cursor_factory = cast("Type[AsyncCursor[Row]]", AsyncCursor)
+ self.cursor_factory = AsyncCursor
self.server_cursor_factory = AsyncServerCursor
@overload
autocommit: bool = False,
prepare_threshold: Optional[int] = 5,
row_factory: AsyncRowFactory[Row],
+ cursor_factory: Optional[Type[AsyncCursor[Row]]] = None,
context: Optional[AdaptContext] = None,
**kwargs: Union[None, int, str],
) -> "AsyncConnection[Row]":
*,
autocommit: bool = False,
prepare_threshold: Optional[int] = 5,
+ cursor_factory: Optional[Type[AsyncCursor[Any]]] = None,
context: Optional[AdaptContext] = None,
**kwargs: Union[None, int, str],
) -> "AsyncConnection[TupleRow]":
prepare_threshold: Optional[int] = 5,
context: Optional[AdaptContext] = None,
row_factory: Optional[AsyncRowFactory[Row]] = None,
+ cursor_factory: Optional[Type[AsyncCursor[Row]]] = None,
**kwargs: Any,
) -> "AsyncConnection[Any]":
if row_factory:
rv.row_factory = row_factory
+ if cursor_factory:
+ rv.cursor_factory = cursor_factory
if context:
rv._adapters = AdaptersMap(context.adapters)
rv.prepare_threshold = prepare_threshold
assert isinstance(cur, MyCursor)
+def test_cursor_factory_connect(dsn):
+ class MyCursor(psycopg.Cursor[psycopg.rows.Row]):
+ pass
+
+ with psycopg.connect(dsn, cursor_factory=MyCursor) as conn:
+ assert conn.cursor_factory is MyCursor
+ cur = conn.cursor()
+ assert type(cur) is MyCursor
+
+
def test_server_cursor_factory(conn):
assert conn.server_cursor_factory is psycopg.ServerCursor
assert isinstance(cur, MyCursor)
+async def test_cursor_factory_connect(dsn):
+ class MyCursor(psycopg.AsyncCursor[psycopg.rows.Row]):
+ pass
+
+ async with await psycopg.AsyncConnection.connect(
+ dsn, cursor_factory=MyCursor
+ ) as conn:
+ assert conn.cursor_factory is MyCursor
+ cur = conn.cursor()
+ assert type(cur) is MyCursor
+
+
async def test_server_cursor_factory(aconn):
assert aconn.server_cursor_factory is psycopg.AsyncServerCursor