Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 09c1b2b

Browse files
author
ansipunk
committed
S01E11
1 parent 6910288 commit 09c1b2b

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

databases/backends/common/records.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ def __init__(
3939

4040
@property
4141
def _mapping(self) -> typing.Mapping:
42-
if hasattr(self._row, "_asdict"):
43-
return self._row._asdict()
44-
4542
return self._row
4643

4744
def keys(self) -> typing.KeysView:
@@ -52,10 +49,7 @@ def values(self) -> typing.ValuesView:
5249

5350
def __getitem__(self, key: typing.Any) -> typing.Any:
5451
if len(self._column_map) == 0:
55-
try:
56-
return self._row[key]
57-
except TypeError:
58-
return self._mapping[key]
52+
return self._row[key]
5953
elif isinstance(key, Column):
6054
idx, datatype = self._column_map_full[str(key)]
6155
elif isinstance(key, int):

databases/backends/dialects/psycopg.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88
import typing
99

10+
from sqlalchemy import types, util
1011
from sqlalchemy.dialects.postgresql.base import PGDialect, PGExecutionContext
1112
from sqlalchemy.engine import processors
12-
from sqlalchemy.types import Numeric
13+
from sqlalchemy.types import Float, Numeric
1314

1415

1516
class PGExecutionContext_psycopg(PGExecutionContext):
@@ -32,10 +33,13 @@ def result_processor(
3233

3334

3435
class PGDialect_psycopg(PGDialect):
35-
colspecs = {
36-
**PGDialect.colspecs,
37-
Numeric: PGNumeric,
38-
}
36+
colspecs = util.update_copy(
37+
PGDialect.colspecs,
38+
{
39+
types.Numeric: PGNumeric,
40+
types.Float: Float,
41+
},
42+
)
3943
execution_ctx_cls = PGExecutionContext_psycopg
4044

4145

databases/backends/psycopg.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import typing
22

3+
import orjson
34
import psycopg
5+
import psycopg.adapt
6+
import psycopg.types
47
import psycopg_pool
58
from psycopg.rows import namedtuple_row
69
from sqlalchemy.dialects.postgresql.psycopg import PGDialect_psycopg
@@ -18,6 +21,16 @@
1821
)
1922

2023

24+
class JsonLoader(psycopg.adapt.Loader):
25+
def load(self, data):
26+
return orjson.loads(data)
27+
28+
29+
class JsonDumper(psycopg.adapt.Dumper):
30+
def dump(self, data):
31+
return orjson.dumps(data)
32+
33+
2134
class PsycopgBackend(DatabaseBackend):
2235
_database_url: DatabaseURL
2336
_options: typing.Dict[str, typing.Any]
@@ -73,8 +86,13 @@ async def acquire(self) -> None:
7386
raise RuntimeError("PsycopgBackend is not running")
7487

7588
# TODO: Add configurable timeouts
76-
self._connection = await self._database._pool.getconn()
77-
await self._connection.set_autocommit(True)
89+
connection = await self._database._pool.getconn()
90+
connection.adapters.register_loader("json", JsonLoader)
91+
connection.adapters.register_loader("jsonb", JsonLoader)
92+
connection.adapters.register_dumper(dict, JsonDumper)
93+
connection.adapters.register_dumper(list, JsonDumper)
94+
await connection.set_autocommit(True)
95+
self._connection = connection
7896

7997
async def release(self) -> None:
8098
if self._connection is None:

0 commit comments

Comments
 (0)