1
0
mirror of https://github.com/django/django.git synced 2024-12-22 09:05:43 +00:00

Used JSON_OBJECT database function on PostgreSQL 16+.

This commit is contained in:
Nick Pope 2023-12-31 08:07:19 +00:00 committed by GitHub
parent 9d52e0720f
commit 81ccf92f15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -159,35 +159,40 @@ class JSONObject(Func):
)
return super().as_sql(compiler, connection, **extra_context)
def as_postgresql(self, compiler, connection, **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):
def as_native(self, compiler, connection, *, returning, **extra_context):
class ArgJoiner:
def join(self, args):
args = [" VALUE ".join(arg) for arg in zip(args[::2], args[1::2])]
return ", ".join(args)
pairs = zip(args[::2], args[1::2], strict=True)
return ", ".join([" VALUE ".join(pair) for pair in pairs])
return self.as_sql(
compiler,
connection,
arg_joiner=ArgJoiner(),
template="%(function)s(%(expressions)s RETURNING CLOB)",
template=f"%(function)s(%(expressions)s RETURNING {returning})",
**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):
"""