1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #33308 -- Added psycopg_any.sql.quote() hook.

This commit is contained in:
Florian Apolloner 2022-12-12 09:08:46 +01:00 committed by Mariusz Felisiak
parent 2ebfbd894e
commit 2f38f7b8f9
3 changed files with 16 additions and 16 deletions

View File

@ -1,11 +1,11 @@
""" """
This object provides quoting for GEOS geometries into PostgreSQL/PostGIS. This object provides quoting for GEOS geometries into PostgreSQL/PostGIS.
""" """
from psycopg2 import extensions
from psycopg2.extensions import ISQLQuote from psycopg2.extensions import ISQLQuote
from django.contrib.gis.db.backends.postgis.pgraster import to_pgraster from django.contrib.gis.db.backends.postgis.pgraster import to_pgraster
from django.contrib.gis.geos import GEOSGeometry from django.contrib.gis.geos import GEOSGeometry
from django.db.backends.postgresql.psycopg_any import sql
class PostGISAdapter: class PostGISAdapter:
@ -47,13 +47,6 @@ class PostGISAdapter:
def _fix_polygon(cls, poly): def _fix_polygon(cls, poly):
return 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): def getquoted(self):
""" """
Return a properly quoted string for use in PostgreSQL/PostGIS. 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'. # Psycopg will figure out whether to use E'\\000' or '\000'.
return b"%s(%s)" % ( return b"%s(%s)" % (
b"ST_GeogFromWKB" if self.geography else b"ST_GeomFromEWKB", b"ST_GeogFromWKB" if self.geography else b"ST_GeomFromEWKB",
self._quote(self.ewkb).encode(), sql.quote(self.ewkb).encode(),
) )
else: else:
# For rasters, add explicit type cast to WKB string. # For rasters, add explicit type cast to WKB string.

View File

@ -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 DateRange, DateTimeRange, DateTimeTZRange, Inet # NOQA
from psycopg2.extras import Json as Jsonb # NOQA from psycopg2.extras import Json as Jsonb # NOQA
from psycopg2.extras import NumericRange, Range # NOQA from psycopg2.extras import NumericRange, Range # NOQA
RANGE_TYPES = (DateRange, DateTimeRange, DateTimeTZRange, NumericRange) 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

View File

@ -1,6 +1,6 @@
from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.backends.ddl_references import IndexColumns 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 from django.db.backends.utils import strip_quotes
@ -51,11 +51,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
def quote_value(self, value): def quote_value(self, value):
if isinstance(value, str): if isinstance(value, str):
value = value.replace("%", "%%") value = value.replace("%", "%%")
adapted = extensions.adapt(value) return sql.quote(value, self.connection.connection)
if hasattr(adapted, "encoding"):
adapted.encoding = "utf8"
# getquoted() returns a quoted bytestring of the adapted value.
return adapted.getquoted().decode()
def _field_indexes_sql(self, model, field): def _field_indexes_sql(self, model, field):
output = super()._field_indexes_sql(model, field) output = super()._field_indexes_sql(model, field)