summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2023-12-31 08:07:19 +0000
committerGitHub <noreply@github.com>2023-12-31 09:07:19 +0100
commit81ccf92f154c6d9eac3e30bac0aa67574d0ace15 (patch)
treeef9cf0d95abad7950a9caf64b93c31bee831d2d7
parent9d52e0720f79ac3cff3d9888a97ac227884a621e (diff)
Used JSON_OBJECT database function on PostgreSQL 16+.
-rw-r--r--django/db/models/functions/comparison.py43
1 files changed, 24 insertions, 19 deletions
diff --git a/django/db/models/functions/comparison.py b/django/db/models/functions/comparison.py
index eec2ab752d..ae41f1da95 100644
--- a/django/db/models/functions/comparison.py
+++ b/django/db/models/functions/comparison.py
@@ -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):
"""