Ticket #16778: postgis-adapter.patch

File postgis-adapter.patch, 1.4 KB (added by piro, 13 years ago)
  • django/contrib/gis/db/backends/postgis/adapter.py

    old new  
    33"""
    44
    55from psycopg2 import Binary
    6 from psycopg2.extensions import ISQLQuote
     6from psycopg2.extensions import ISQLQuote, adapt
    77
    88class PostGISAdapter(object):
    99    def __init__(self, geom):
     
    1212        # the adaptor) and the SRID from the geometry.
    1313        self.ewkb = str(geom.ewkb)
    1414        self.srid = geom.srid
     15        self._adapter = Binary(self.ewkb)
    1516
    1617    def __conform__(self, proto):
    1718        # Does the given protocol conform to what Psycopg2 expects?
     
    2627    def __str__(self):
    2728        return self.getquoted()
    2829
     30    def prepare(self, conn):
     31        # Pass the connection to the adapter: this allows escaping the binary
     32        # in the style required by the server's standard_conforming_string setting.
     33        self._adapter.prepare(conn)
     34
    2935    def getquoted(self):
    3036        "Returns a properly quoted string for use in PostgreSQL/PostGIS."
    31         # Want to use WKB, so wrap with psycopg2 Binary() to quote properly.
    32         return 'ST_GeomFromEWKB(E%s)' % Binary(self.ewkb)
     37        # psycopg will figure out whether to use E'\\000' or '\000'
     38        return 'ST_GeomFromEWKB(%s)' % adapt(self._adapter)
    3339
    3440    def prepare_database_save(self, unused):
    3541        return self
Back to Top