Skip to content

Commit ddd8aaa

Browse files
author
ansipunk
committed
S01E05
1 parent d7ff8e8 commit ddd8aaa

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

databases/backends/common/records.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from sqlalchemy.engine.row import Row as SQLRow
55
from sqlalchemy.sql.compiler import _CompileLabel
66
from sqlalchemy.sql.schema import Column
7-
from sqlalchemy.sql.sqltypes import JSON
87
from sqlalchemy.types import TypeEngine
98

109
from databases.interfaces import Record as RecordInterface

databases/backends/psycopg.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import psycopg
44
import psycopg_pool
5+
from psycopg.rows import namedtuple_row
56
from sqlalchemy.engine.interfaces import Dialect
67
from sqlalchemy.dialects.postgresql.psycopg import PGDialect_psycopg
78
from sqlalchemy.sql import ClauseElement
@@ -58,12 +59,11 @@ def connection(self) -> "PsycopgConnection":
5859
class 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

Comments
 (0)