From 2f38f7b8f9bd65905dc6bec4f3e2a30b5da8e338 Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Mon, 12 Dec 2022 09:08:46 +0100 Subject: [PATCH] Refs #33308 -- Added psycopg_any.sql.quote() hook. --- django/contrib/gis/db/backends/postgis/adapter.py | 11 ++--------- django/db/backends/postgresql/psycopg_any.py | 13 ++++++++++++- django/db/backends/postgresql/schema.py | 8 ++------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/django/contrib/gis/db/backends/postgis/adapter.py b/django/contrib/gis/db/backends/postgis/adapter.py index 19d3a60492..9161e25f16 100644 --- a/django/contrib/gis/db/backends/postgis/adapter.py +++ b/django/contrib/gis/db/backends/postgis/adapter.py @@ -1,11 +1,11 @@ """ This object provides quoting for GEOS geometries into PostgreSQL/PostGIS. """ -from psycopg2 import extensions from psycopg2.extensions import ISQLQuote from django.contrib.gis.db.backends.postgis.pgraster import to_pgraster from django.contrib.gis.geos import GEOSGeometry +from django.db.backends.postgresql.psycopg_any import sql class PostGISAdapter: @@ -47,13 +47,6 @@ class PostGISAdapter: def _fix_polygon(cls, poly): return poly - def _quote(self, value): - adapted = extensions.adapt(value) - if hasattr(adapted, "encoding"): - adapted.encoding = "utf8" - # getquoted() returns a quoted bytestring of the adapted value. - return adapted.getquoted().decode() - def getquoted(self): """ Return a properly quoted string for use in PostgreSQL/PostGIS. @@ -62,7 +55,7 @@ class PostGISAdapter: # Psycopg will figure out whether to use E'\\000' or '\000'. return b"%s(%s)" % ( b"ST_GeogFromWKB" if self.geography else b"ST_GeomFromEWKB", - self._quote(self.ewkb).encode(), + sql.quote(self.ewkb).encode(), ) else: # For rasters, add explicit type cast to WKB string. diff --git a/django/db/backends/postgresql/psycopg_any.py b/django/db/backends/postgresql/psycopg_any.py index 6dcd68f5eb..8e0d170867 100644 --- a/django/db/backends/postgresql/psycopg_any.py +++ b/django/db/backends/postgresql/psycopg_any.py @@ -1,6 +1,17 @@ -from psycopg2 import errors, extensions # NOQA +from psycopg2 import errors, extensions, sql # NOQA from psycopg2.extras import DateRange, DateTimeRange, DateTimeTZRange, Inet # NOQA from psycopg2.extras import Json as Jsonb # NOQA from psycopg2.extras import NumericRange, Range # NOQA RANGE_TYPES = (DateRange, DateTimeRange, DateTimeTZRange, NumericRange) + + +def _quote(value, connection=None): + adapted = extensions.adapt(value) + if hasattr(adapted, "encoding"): + adapted.encoding = "utf8" + # getquoted() returns a quoted bytestring of the adapted value. + return adapted.getquoted().decode() + + +sql.quote = _quote diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py index 0c2043db35..cc0da85817 100644 --- a/django/db/backends/postgresql/schema.py +++ b/django/db/backends/postgresql/schema.py @@ -1,6 +1,6 @@ from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.backends.ddl_references import IndexColumns -from django.db.backends.postgresql.psycopg_any import extensions +from django.db.backends.postgresql.psycopg_any import sql from django.db.backends.utils import strip_quotes @@ -51,11 +51,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): def quote_value(self, value): if isinstance(value, str): value = value.replace("%", "%%") - adapted = extensions.adapt(value) - if hasattr(adapted, "encoding"): - adapted.encoding = "utf8" - # getquoted() returns a quoted bytestring of the adapted value. - return adapted.getquoted().decode() + return sql.quote(value, self.connection.connection) def _field_indexes_sql(self, model, field): output = super()._field_indexes_sql(model, field)