mirror of
https://github.com/django/django.git
synced 2025-01-18 06:12:23 +00:00
Used JSON_OBJECT database function on PostgreSQL 16+.
This commit is contained in:
parent
9d52e0720f
commit
81ccf92f15
@ -159,7 +159,22 @@ class JSONObject(Func):
|
|||||||
)
|
)
|
||||||
return super().as_sql(compiler, connection, **extra_context)
|
return super().as_sql(compiler, connection, **extra_context)
|
||||||
|
|
||||||
|
def as_native(self, compiler, connection, *, returning, **extra_context):
|
||||||
|
class ArgJoiner:
|
||||||
|
def join(self, 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=f"%(function)s(%(expressions)s RETURNING {returning})",
|
||||||
|
**extra_context,
|
||||||
|
)
|
||||||
|
|
||||||
def as_postgresql(self, compiler, connection, **extra_context):
|
def as_postgresql(self, compiler, connection, **extra_context):
|
||||||
|
if not connection.features.is_postgresql_16:
|
||||||
copy = self.copy()
|
copy = self.copy()
|
||||||
copy.set_source_expressions(
|
copy.set_source_expressions(
|
||||||
[
|
[
|
||||||
@ -173,20 +188,10 @@ class JSONObject(Func):
|
|||||||
function="JSONB_BUILD_OBJECT",
|
function="JSONB_BUILD_OBJECT",
|
||||||
**extra_context,
|
**extra_context,
|
||||||
)
|
)
|
||||||
|
return self.as_native(compiler, connection, returning="JSONB", **extra_context)
|
||||||
|
|
||||||
def as_oracle(self, compiler, connection, **extra_context):
|
def as_oracle(self, compiler, connection, **extra_context):
|
||||||
class ArgJoiner:
|
return self.as_native(compiler, connection, returning="CLOB", **extra_context)
|
||||||
def join(self, args):
|
|
||||||
args = [" VALUE ".join(arg) for arg in zip(args[::2], args[1::2])]
|
|
||||||
return ", ".join(args)
|
|
||||||
|
|
||||||
return self.as_sql(
|
|
||||||
compiler,
|
|
||||||
connection,
|
|
||||||
arg_joiner=ArgJoiner(),
|
|
||||||
template="%(function)s(%(expressions)s RETURNING CLOB)",
|
|
||||||
**extra_context,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class Least(Func):
|
class Least(Func):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user