11import logging
22import typing
3+ from collections .abc import Mapping
34
45import asyncpg
56from sqlalchemy .dialects .postgresql import pypostgresql
@@ -63,7 +64,7 @@ def connection(self) -> "PostgresConnection":
6364 return PostgresConnection (self , self ._dialect )
6465
6566
66- class Record :
67+ class Record ( Mapping ) :
6768 def __init__ (self , row : tuple , result_columns : tuple , dialect : Dialect ) -> None :
6869 self ._row = row
6970 self ._result_columns = result_columns
@@ -86,6 +87,12 @@ def __getitem__(self, key: str) -> typing.Any:
8687 return processor (raw )
8788 return raw
8889
90+ def __iter__ (self ) -> typing .Iterator :
91+ return iter (self ._column_map )
92+
93+ def __len__ (self ) -> int :
94+ return len (self ._column_map )
95+
8996
9097class PostgresConnection (ConnectionBackend ):
9198 def __init__ (self , database : PostgresBackend , dialect : Dialect ):
@@ -104,13 +111,13 @@ async def release(self) -> None:
104111 self ._connection = await self ._database ._pool .release (self ._connection )
105112 self ._connection = None
106113
107- async def fetch_all (self , query : ClauseElement ) -> typing .Any :
114+ async def fetch_all (self , query : ClauseElement ) -> typing .List [ Record ] :
108115 assert self ._connection is not None , "Connection is not acquired"
109116 query , args , result_columns = self ._compile (query )
110117 rows = await self ._connection .fetch (query , * args )
111118 return [Record (row , result_columns , self ._dialect ) for row in rows ]
112119
113- async def fetch_one (self , query : ClauseElement ) -> typing .Any :
120+ async def fetch_one (self , query : ClauseElement ) -> typing .Optional [ Record ] :
114121 assert self ._connection is not None , "Connection is not acquired"
115122 query , args , result_columns = self ._compile (query )
116123 row = await self ._connection .fetchrow (query , * args )
0 commit comments