from typing import Any, AsyncIterator, Generic, List, Iterator, Optional
from typing import Sequence, Type, Tuple, TYPE_CHECKING
+from . import pq
from . import sql
from . import errors as e
-from .pq import Format
from .cursor import BaseCursor, execute
from .proto import ConnectionType, Query, Params, PQGen
self.name = name
self.described = False
+ def _repr(self, cur: BaseCursor[ConnectionType]) -> str:
+ cls = f"{cur.__class__.__module__}.{cur.__class__.__qualname__}"
+ info = pq.misc.connection_summary(cur._conn.pgconn)
+ if cur._closed:
+ status = "closed"
+ elif not cur._pgresult:
+ status = "no result"
+ else:
+ status = pq.ExecStatus(cur._pgresult.status).name
+ return f"<{cls} {self.name!r} [{status}] {info} at 0x{id(cur):x}>"
+
def _declare_gen(
self,
cur: BaseCursor[ConnectionType],
connection: "Connection",
name: str,
*,
- format: Format = Format.TEXT,
+ format: pq.Format = pq.Format.TEXT,
):
super().__init__(connection, format=format)
self._helper: ServerCursorHelper["Connection"] = ServerCursorHelper(
ResourceWarning,
)
+ def __repr__(self) -> str:
+ return self._helper._repr(self)
+
def __enter__(self) -> "ServerCursor":
return self
connection: "AsyncConnection",
name: str,
*,
- format: Format = Format.TEXT,
+ format: pq.Format = pq.Format.TEXT,
):
super().__init__(connection, format=format)
self._helper: ServerCursorHelper["AsyncConnection"]
ResourceWarning,
)
+ def __repr__(self) -> str:
+ return self._helper._repr(self)
+
async def __aenter__(self) -> "AsyncServerCursor":
return self
assert cur.name == "1-2-3"
+async def test_repr(aconn):
+ cur = aconn.cursor("my-name")
+ assert "AsyncServerCursor" in repr(cur)
+ assert "my-name" in repr(cur)
+
+
async def test_connection(aconn):
cur = aconn.cursor("foo")
assert cur.connection is aconn