22
33import psycopg
44import psycopg_pool
5+ from psycopg .rows import namedtuple_row
56from sqlalchemy .engine .interfaces import Dialect
67from sqlalchemy .dialects .postgresql .psycopg import PGDialect_psycopg
78from sqlalchemy .sql import ClauseElement
@@ -58,12 +59,11 @@ def connection(self) -> "PsycopgConnection":
5859class PsycopgConnection (ConnectionBackend ):
5960 _database : PsycopgBackend
6061 _dialect : Dialect
61- _connection : typing .Optional [psycopg .AsyncConnection ]
62+ _connection : typing .Optional [psycopg .AsyncConnection ] = None
6263
6364 def __init__ (self , database : PsycopgBackend , dialect : Dialect ) -> None :
6465 self ._database = database
6566 self ._dialect = dialect
66- self ._connection = None
6767
6868 async def acquire (self ) -> None :
6969 if self ._connection is not None :
@@ -74,6 +74,7 @@ async def acquire(self) -> None:
7474
7575 # TODO: Add configurable timeouts
7676 self ._connection = await self ._database ._pool .getconn ()
77+ await self ._connection .set_autocommit (True )
7778
7879 async def release (self ) -> None :
7980 if self ._connection is None :
@@ -88,7 +89,7 @@ async def fetch_all(self, query: ClauseElement) -> typing.List[RecordInterface]:
8889
8990 query_str , args , result_columns = self ._compile (query )
9091
91- async with self ._connection .cursor () as cursor :
92+ async with self ._connection .cursor (row_factory = namedtuple_row ) as cursor :
9293 await cursor .execute (query_str , args )
9394 rows = await cursor .fetchall ()
9495
@@ -101,7 +102,7 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[RecordInterfa
101102
102103 query_str , args , result_columns = self ._compile (query )
103104
104- async with self ._connection .cursor () as cursor :
105+ async with self ._connection .cursor (row_factory = namedtuple_row ) as cursor :
105106 await cursor .execute (query_str , args )
106107 row = await cursor .fetchone ()
107108
@@ -127,7 +128,7 @@ async def execute(self, query: ClauseElement) -> typing.Any:
127128
128129 query_str , args , _ = self ._compile (query )
129130
130- async with self ._connection .cursor () as cursor :
131+ async with self ._connection .cursor (row_factory = namedtuple_row ) as cursor :
131132 await cursor .execute (query_str , args )
132133
133134 async def execute_many (self , queries : typing .List [ClauseElement ]) -> None :
@@ -144,7 +145,7 @@ async def iterate(
144145 query_str , args , result_columns = self ._compile (query )
145146 column_maps = create_column_maps (result_columns )
146147
147- async with self ._connection .cursor () as cursor :
148+ async with self ._connection .cursor (row_factory = namedtuple_row ) as cursor :
148149 await cursor .execute (query_str , args )
149150
150151 while True :
0 commit comments