Skip to content

Commit

Permalink
add sql param converter for psycopg2._json.Json
Browse files Browse the repository at this point in the history
  • Loading branch information
voidZXL committed Dec 26, 2024
1 parent fe380a3 commit 38c262c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,6 @@ def command_process(db_using):
env = os.environ.copy()
env['UTILMETA_OPERATIONS_DATABASE_ENGINE'] = db_using
server = subprocess.Popen(cmd, env=env, cwd=str(cwd or os.getcwd()))

try:
if port:
import socket
Expand Down
4 changes: 2 additions & 2 deletions tests/test_7_ops/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
)

retry = RetryPlugin(
max_retries=3, max_retries_timeout=15, retry_interval=1
max_retries=5, max_retries_timeout=15, retry_interval=1.5
)

OPS_WAIT = 1.0
OPS_WAIT = 2.0
# add wait to make sure that the operations data are all setup


Expand Down
24 changes: 15 additions & 9 deletions utilmeta/core/orm/databases/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,24 @@ async def disconnect(self):
@property
def _param_converter(self):
if self.async_engine == 'asyncpg':
json_types = []
try:
from psycopg.types.json import Json, Jsonb
json_types.extend([Json, Jsonb])
except (ModuleNotFoundError, ImportError):
return lambda x: x

json_types = (Json, Jsonb)

def converter(x):
if isinstance(x, json_types):
return json_dumps(x.obj)
return x
return converter
pass
try:
from psycopg2._json import Json
json_types.append(Json)
except (ModuleNotFoundError, ImportError):
pass
json_types = tuple(json_types)
if json_types:
def converter(x):
if isinstance(x, json_types):
return json_dumps(getattr(x, 'obj', getattr(x, 'adapted', None)))
return x
return converter
return lambda x: x

def _parse_sql_params(self, sql: str, params=None):
Expand Down
13 changes: 12 additions & 1 deletion utilmeta/core/orm/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@
from psycopg2._json import Json # noqa

@register_encoder(Json)
def from_iterable(encoder, data):
def from_psycopg2_json(encoder, data):
return data.adapted

except (ImportError, ModuleNotFoundError):
pass


try:
from psycopg.types.json import Json, Jsonb

@register_encoder(Json, Jsonb)
def from_psycopg_json(encoder, data):
return data.obj

except (ImportError, ModuleNotFoundError):
pass

0 comments on commit 38c262c

Please sign in to comment.