diff --git a/django/contrib/postgres/search.py b/django/contrib/postgres/search.py index 2b57156263..05c8f72f6f 100644 --- a/django/contrib/postgres/search.py +++ b/django/contrib/postgres/search.py @@ -1,5 +1,3 @@ -import psycopg2 - from django.db.models import ( CharField, Expression, @@ -309,14 +307,9 @@ class SearchHeadline(Func): options_sql = "" options_params = [] if self.options: - # getquoted() returns a quoted bytestring of the adapted value. options_params.append( ", ".join( - "%s=%s" - % ( - option, - psycopg2.extensions.adapt(value).getquoted().decode(), - ) + connection.ops.compose_sql(f"{option}=%s", [value]) for option, value in self.options.items() ) ) diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py index e86628ede2..824e0c3e4b 100644 --- a/django/db/backends/postgresql/operations.py +++ b/django/db/backends/postgresql/operations.py @@ -3,7 +3,7 @@ from functools import lru_cache, partial from django.conf import settings 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.models.constants import OnConflict @@ -174,6 +174,9 @@ class DatabaseOperations(BaseDatabaseOperations): return name # Quoting once is enough. return '"%s"' % name + def compose_sql(self, sql, params): + return mogrify(sql, params, self.connection) + def set_time_zone_sql(self): return "SET TIME ZONE %s" diff --git a/django/db/backends/postgresql/psycopg_any.py b/django/db/backends/postgresql/psycopg_any.py index 83e8a9f4d3..e9bb84f313 100644 --- a/django/db/backends/postgresql/psycopg_any.py +++ b/django/db/backends/postgresql/psycopg_any.py @@ -24,3 +24,8 @@ def _quote(value, connection=None): sql.quote = _quote + + +def mogrify(sql, params, connection): + with connection.cursor() as cursor: + return cursor.mogrify(sql, params).decode() diff --git a/tests/inspectdb/tests.py b/tests/inspectdb/tests.py index 66b2eb8260..7944e5f221 100644 --- a/tests/inspectdb/tests.py +++ b/tests/inspectdb/tests.py @@ -563,17 +563,19 @@ class InspectDBTransactionalTests(TransactionTestCase): "CREATE SERVER inspectdb_server FOREIGN DATA WRAPPER file_fdw" ) cursor.execute( - """\ - CREATE FOREIGN TABLE inspectdb_iris_foreign_table ( - petal_length real, - petal_width real, - sepal_length real, - sepal_width real - ) SERVER inspectdb_server OPTIONS ( - filename %s + connection.ops.compose_sql( + """ + CREATE FOREIGN TABLE inspectdb_iris_foreign_table ( + petal_length real, + petal_width real, + sepal_length real, + sepal_width real + ) SERVER inspectdb_server OPTIONS ( + filename %s + ) + """, + [os.devnull], ) - """, - [os.devnull], ) out = StringIO() foreign_table_model = "class InspectdbIrisForeignTable(models.Model):"