mirror of
https://github.com/django/django.git
synced 2025-01-03 15:06:09 +00:00
Used JSON_OBJECT database function on PostgreSQL 16+.
This commit is contained in:
parent
9d52e0720f
commit
81ccf92f15
@ -159,35 +159,40 @@ class JSONObject(Func):
|
|||||||
)
|
)
|
||||||
return super().as_sql(compiler, connection, **extra_context)
|
return super().as_sql(compiler, connection, **extra_context)
|
||||||
|
|
||||||
def as_postgresql(self, compiler, connection, **extra_context):
|
def as_native(self, compiler, connection, *, returning, **extra_context):
|
||||||
copy = self.copy()
|
|
||||||
copy.set_source_expressions(
|
|
||||||
[
|
|
||||||
Cast(expression, TextField()) if index % 2 == 0 else expression
|
|
||||||
for index, expression in enumerate(copy.get_source_expressions())
|
|
||||||
]
|
|
||||||
)
|
|
||||||
return super(JSONObject, copy).as_sql(
|
|
||||||
compiler,
|
|
||||||
connection,
|
|
||||||
function="JSONB_BUILD_OBJECT",
|
|
||||||
**extra_context,
|
|
||||||
)
|
|
||||||
|
|
||||||
def as_oracle(self, compiler, connection, **extra_context):
|
|
||||||
class ArgJoiner:
|
class ArgJoiner:
|
||||||
def join(self, args):
|
def join(self, args):
|
||||||
args = [" VALUE ".join(arg) for arg in zip(args[::2], args[1::2])]
|
pairs = zip(args[::2], args[1::2], strict=True)
|
||||||
return ", ".join(args)
|
return ", ".join([" VALUE ".join(pair) for pair in pairs])
|
||||||
|
|
||||||
return self.as_sql(
|
return self.as_sql(
|
||||||
compiler,
|
compiler,
|
||||||
connection,
|
connection,
|
||||||
arg_joiner=ArgJoiner(),
|
arg_joiner=ArgJoiner(),
|
||||||
template="%(function)s(%(expressions)s RETURNING CLOB)",
|
template=f"%(function)s(%(expressions)s RETURNING {returning})",
|
||||||
**extra_context,
|
**extra_context,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def as_postgresql(self, compiler, connection, **extra_context):
|
||||||
|
if not connection.features.is_postgresql_16:
|
||||||
|
copy = self.copy()
|
||||||
|
copy.set_source_expressions(
|
||||||
|
[
|
||||||
|
Cast(expression, TextField()) if index % 2 == 0 else expression
|
||||||
|
for index, expression in enumerate(copy.get_source_expressions())
|
||||||
|
]
|
||||||
|
)
|
||||||
|
return super(JSONObject, copy).as_sql(
|
||||||
|
compiler,
|
||||||
|
connection,
|
||||||
|
function="JSONB_BUILD_OBJECT",
|
||||||
|
**extra_context,
|
||||||
|
)
|
||||||
|
return self.as_native(compiler, connection, returning="JSONB", **extra_context)
|
||||||
|
|
||||||
|
def as_oracle(self, compiler, connection, **extra_context):
|
||||||
|
return self.as_native(compiler, connection, returning="CLOB", **extra_context)
|
||||||
|
|
||||||
|
|
||||||
class Least(Func):
|
class Least(Func):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user