mirror of
https://github.com/django/django.git
synced 2024-11-24 20:07:01 +01:00
Fixed #35778 -- Used JSON_OBJECT database function on PostgreSQL 16+ with server-side bindings.
This commit is contained in:
parent
18b3a9dd39
commit
78c9a27031
@ -162,7 +162,8 @@ class JSONObject(Func):
|
|||||||
|
|
||||||
def join(self, args):
|
def join(self, args):
|
||||||
pairs = zip(args[::2], args[1::2], strict=True)
|
pairs = zip(args[::2], args[1::2], strict=True)
|
||||||
return ", ".join([" VALUE ".join(pair) for pair in pairs])
|
# Wrap 'key' in parentheses in case of postgres cast :: syntax.
|
||||||
|
return ", ".join([f"({key}) VALUE {value}" for key, value in pairs])
|
||||||
|
|
||||||
def as_native(self, compiler, connection, *, returning, **extra_context):
|
def as_native(self, compiler, connection, *, returning, **extra_context):
|
||||||
return self.as_sql(
|
return self.as_sql(
|
||||||
@ -174,10 +175,9 @@ class JSONObject(Func):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def as_postgresql(self, compiler, connection, **extra_context):
|
def as_postgresql(self, compiler, connection, **extra_context):
|
||||||
if (
|
# Casting keys to text is only required when using JSONB_BUILD_OBJECT
|
||||||
not connection.features.is_postgresql_16
|
# or when using JSON_OBJECT on PostgreSQL 16+ with server-side bindings.
|
||||||
or connection.features.uses_server_side_binding
|
# This is done in all cases for consistency.
|
||||||
):
|
|
||||||
copy = self.copy()
|
copy = self.copy()
|
||||||
copy.set_source_expressions(
|
copy.set_source_expressions(
|
||||||
[
|
[
|
||||||
@ -185,13 +185,18 @@ class JSONObject(Func):
|
|||||||
for index, expression in enumerate(copy.get_source_expressions())
|
for index, expression in enumerate(copy.get_source_expressions())
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if connection.features.is_postgresql_16:
|
||||||
|
return copy.as_native(
|
||||||
|
compiler, connection, returning="JSONB", **extra_context
|
||||||
|
)
|
||||||
|
|
||||||
return super(JSONObject, copy).as_sql(
|
return super(JSONObject, copy).as_sql(
|
||||||
compiler,
|
compiler,
|
||||||
connection,
|
connection,
|
||||||
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):
|
||||||
return self.as_native(compiler, connection, returning="CLOB", **extra_context)
|
return self.as_native(compiler, connection, returning="CLOB", **extra_context)
|
||||||
|
Loading…
Reference in New Issue
Block a user