Skip to content

Commit

Permalink
Refs #33308 -- Added psycopg_any.sql.quote() hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo13 authored and felixxm committed Dec 12, 2022
1 parent 2ebfbd8 commit 2f38f7b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
11 changes: 2 additions & 9 deletions django/contrib/gis/db/backends/postgis/adapter.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -47,13 +47,6 @@ def __str__(self):
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.
Expand All @@ -62,7 +55,7 @@ def getquoted(self):
# 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.
Expand Down
13 changes: 12 additions & 1 deletion django/db/backends/postgresql/psycopg_any.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 2 additions & 6 deletions django/db/backends/postgresql/schema.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2f38f7b

Please sign in to comment.