Simplified JSONField key transforms by using JSON_OBJECT() on Oracle.
This commit is contained in:
parent
bec415b290
commit
b996424199
|
@ -385,11 +385,11 @@ class KeyTransformIn(lookups.In):
|
||||||
):
|
):
|
||||||
if connection.vendor == 'oracle':
|
if connection.vendor == 'oracle':
|
||||||
value = json.loads(param)
|
value = json.loads(param)
|
||||||
|
sql = "%s(JSON_OBJECT('value' VALUE %%s FORMAT JSON), '$.value')"
|
||||||
if isinstance(value, (list, dict)):
|
if isinstance(value, (list, dict)):
|
||||||
sql = "JSON_QUERY(%s, '$.value')"
|
sql = sql % 'JSON_QUERY'
|
||||||
else:
|
else:
|
||||||
sql = "JSON_VALUE(%s, '$.value')"
|
sql = sql % 'JSON_VALUE'
|
||||||
params = (json.dumps({'value': value}),)
|
|
||||||
elif connection.vendor in {'sqlite', 'mysql'}:
|
elif connection.vendor in {'sqlite', 'mysql'}:
|
||||||
sql = "JSON_EXTRACT(%s, '$')"
|
sql = "JSON_EXTRACT(%s, '$')"
|
||||||
if connection.vendor == 'mysql' and connection.mysql_is_mariadb:
|
if connection.vendor == 'mysql' and connection.mysql_is_mariadb:
|
||||||
|
@ -413,15 +413,14 @@ class KeyTransformExact(JSONExact):
|
||||||
rhs, rhs_params = super().process_rhs(compiler, connection)
|
rhs, rhs_params = super().process_rhs(compiler, connection)
|
||||||
if connection.vendor == 'oracle':
|
if connection.vendor == 'oracle':
|
||||||
func = []
|
func = []
|
||||||
|
sql = "%s(JSON_OBJECT('value' VALUE %%s FORMAT JSON), '$.value')"
|
||||||
for value in rhs_params:
|
for value in rhs_params:
|
||||||
value = json.loads(value)
|
value = json.loads(value)
|
||||||
function = 'JSON_QUERY' if isinstance(value, (list, dict)) else 'JSON_VALUE'
|
if isinstance(value, (list, dict)):
|
||||||
func.append("%s('%s', '$.value')" % (
|
func.append(sql % 'JSON_QUERY')
|
||||||
function,
|
else:
|
||||||
json.dumps({'value': value}),
|
func.append(sql % 'JSON_VALUE')
|
||||||
))
|
|
||||||
rhs = rhs % tuple(func)
|
rhs = rhs % tuple(func)
|
||||||
rhs_params = []
|
|
||||||
elif connection.vendor == 'sqlite':
|
elif connection.vendor == 'sqlite':
|
||||||
func = ["JSON_EXTRACT(%s, '$')" if value != 'null' else '%s' for value in rhs_params]
|
func = ["JSON_EXTRACT(%s, '$')" if value != 'null' else '%s' for value in rhs_params]
|
||||||
rhs = rhs % tuple(func)
|
rhs = rhs % tuple(func)
|
||||||
|
|
Loading…
Reference in New Issue