Skip to content

Commit 3ddccda

Browse files
authored
Merge pull request #305 from wwwjfy/t291
fix #291, process JSON/JSONB scalar result
2 parents f94a81b + 6e929b4 commit 3ddccda

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

gino/dialects/asyncpg.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,6 @@ class AsyncpgDialect(PGDialect, base.AsyncDialectMixin):
291291
statement_compiler = AsyncpgCompiler
292292
execution_ctx_cls = AsyncpgExecutionContext
293293
cursor_cls = DBAPICursor
294-
dbapi_type_map = {
295-
114: JSON(),
296-
3802: JSONB(),
297-
}
298294
init_kwargs = set(itertools.chain(
299295
*[inspect.getfullargspec(f).kwonlydefaults.keys() for f in
300296
[asyncpg.create_pool, asyncpg.connect]]))

gino/dialects/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
import weakref
33

44
from sqlalchemy import util
5+
from sqlalchemy.dialects.postgresql import JSON, JSONB
56

67
# noinspection PyProtectedMember
78
from ..engine import _SAConnection, _SAEngine, _DBAPIConnection
89
from ..loader import Loader
910

1011
DEFAULT = object()
12+
JSON_COLTYPE = 114
13+
JSONB_COLTYPE = 3802
1114

1215

1316
class BaseDBAPI:
@@ -372,6 +375,13 @@ def _init_statement_prepared(cls, dialect, connection, dbapi_connection,
372375
self.cursor = self.create_cursor()
373376
return self
374377

378+
def get_result_processor(self, type_, colname, coltype):
379+
if coltype == JSON_COLTYPE:
380+
return JSON().result_processor(self.dialect, coltype)
381+
if coltype == JSONB_COLTYPE:
382+
return JSONB().result_processor(self.dialect, coltype)
383+
return super().get_result_processor(type_, colname, coltype)
384+
375385

376386
class AsyncDialectMixin:
377387
cursor_cls = DBAPICursor

tests/test_json.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,28 @@ class Test(db.Model):
173173
__tablename__ = 'tests_no_profile'
174174

175175
age = db.IntegerProperty(default=18)
176+
177+
178+
async def test_t291(bind):
179+
from gino.dialects.asyncpg import JSON, JSONB
180+
181+
class PropsTest(db.Model):
182+
__tablename__ = 'props_test_291'
183+
profile = db.Column(JSONB(), nullable=False, server_default='{}')
184+
profile1 = db.Column(JSON(), nullable=False, server_default='{}')
185+
186+
bool = db.BooleanProperty()
187+
bool1 = db.BooleanProperty(column_name='profile1')
188+
189+
await PropsTest.gino.create()
190+
try:
191+
t = await PropsTest.create(
192+
bool=True,
193+
bool1=True,
194+
)
195+
profile = await bind.scalar('SELECT profile FROM props_test_291')
196+
assert isinstance(profile, dict)
197+
profile1 = await bind.scalar('SELECT profile1 FROM props_test_291')
198+
assert isinstance(profile1, dict)
199+
finally:
200+
await PropsTest.gino.drop()

0 commit comments

Comments
 (0)