mirror of
https://github.com/django/django.git
synced 2025-06-02 18:19:11 +00:00
Refs #33308 -- Added DatabaseOperations.compose_sql() on PostgreSQL.
This commit is contained in:
parent
1d90c9b113
commit
db7bb3b64e
@ -1,5 +1,3 @@
|
|||||||
import psycopg2
|
|
||||||
|
|
||||||
from django.db.models import (
|
from django.db.models import (
|
||||||
CharField,
|
CharField,
|
||||||
Expression,
|
Expression,
|
||||||
@ -309,14 +307,9 @@ class SearchHeadline(Func):
|
|||||||
options_sql = ""
|
options_sql = ""
|
||||||
options_params = []
|
options_params = []
|
||||||
if self.options:
|
if self.options:
|
||||||
# getquoted() returns a quoted bytestring of the adapted value.
|
|
||||||
options_params.append(
|
options_params.append(
|
||||||
", ".join(
|
", ".join(
|
||||||
"%s=%s"
|
connection.ops.compose_sql(f"{option}=%s", [value])
|
||||||
% (
|
|
||||||
option,
|
|
||||||
psycopg2.extensions.adapt(value).getquoted().decode(),
|
|
||||||
)
|
|
||||||
for option, value in self.options.items()
|
for option, value in self.options.items()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -3,7 +3,7 @@ from functools import lru_cache, partial
|
|||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db.backends.base.operations import BaseDatabaseOperations
|
from django.db.backends.base.operations import BaseDatabaseOperations
|
||||||
from django.db.backends.postgresql.psycopg_any import Inet, Jsonb
|
from django.db.backends.postgresql.psycopg_any import Inet, Jsonb, mogrify
|
||||||
from django.db.backends.utils import split_tzname_delta
|
from django.db.backends.utils import split_tzname_delta
|
||||||
from django.db.models.constants import OnConflict
|
from django.db.models.constants import OnConflict
|
||||||
|
|
||||||
@ -174,6 +174,9 @@ class DatabaseOperations(BaseDatabaseOperations):
|
|||||||
return name # Quoting once is enough.
|
return name # Quoting once is enough.
|
||||||
return '"%s"' % name
|
return '"%s"' % name
|
||||||
|
|
||||||
|
def compose_sql(self, sql, params):
|
||||||
|
return mogrify(sql, params, self.connection)
|
||||||
|
|
||||||
def set_time_zone_sql(self):
|
def set_time_zone_sql(self):
|
||||||
return "SET TIME ZONE %s"
|
return "SET TIME ZONE %s"
|
||||||
|
|
||||||
|
@ -24,3 +24,8 @@ def _quote(value, connection=None):
|
|||||||
|
|
||||||
|
|
||||||
sql.quote = _quote
|
sql.quote = _quote
|
||||||
|
|
||||||
|
|
||||||
|
def mogrify(sql, params, connection):
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
return cursor.mogrify(sql, params).decode()
|
||||||
|
@ -563,17 +563,19 @@ class InspectDBTransactionalTests(TransactionTestCase):
|
|||||||
"CREATE SERVER inspectdb_server FOREIGN DATA WRAPPER file_fdw"
|
"CREATE SERVER inspectdb_server FOREIGN DATA WRAPPER file_fdw"
|
||||||
)
|
)
|
||||||
cursor.execute(
|
cursor.execute(
|
||||||
"""\
|
connection.ops.compose_sql(
|
||||||
CREATE FOREIGN TABLE inspectdb_iris_foreign_table (
|
"""
|
||||||
petal_length real,
|
CREATE FOREIGN TABLE inspectdb_iris_foreign_table (
|
||||||
petal_width real,
|
petal_length real,
|
||||||
sepal_length real,
|
petal_width real,
|
||||||
sepal_width real
|
sepal_length real,
|
||||||
) SERVER inspectdb_server OPTIONS (
|
sepal_width real
|
||||||
filename %s
|
) SERVER inspectdb_server OPTIONS (
|
||||||
|
filename %s
|
||||||
|
)
|
||||||
|
""",
|
||||||
|
[os.devnull],
|
||||||
)
|
)
|
||||||
""",
|
|
||||||
[os.devnull],
|
|
||||||
)
|
)
|
||||||
out = StringIO()
|
out = StringIO()
|
||||||
foreign_table_model = "class InspectdbIrisForeignTable(models.Model):"
|
foreign_table_model = "class InspectdbIrisForeignTable(models.Model):"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user