1. Michael Bayer
  2. sqlalchemy
  3. Issues

Issues

Issue #2193 resolved

add adapt() + custom args use case to user defined types

Michael Bayer
repo owner created an issue

geoalchemy type:

class GeometryBase(TypeEngine):
   """Base Geometry column type for all spatial databases.

   Converts bind/result values to/from a generic Persistent value.
   This is used as a base class and overridden into dialect specific
   Persistent values.
   """

   name = 'GEOMETRY'

   def __init__(self, dimension=2, srid=4326, spatial_index=True, **kwargs):
       self.dimension = dimension
       self.srid = srid
       self.spatial_index = spatial_index
       self.kwargs = kwargs
       super(GeometryBase, self).__init__()

   def bind_processor(self, dialect):
       def process(value):
           if value is not None:
               if isinstance(value, SpatialElement):
                   if isinstance(value.desc, SpatialElement):
                       return value.desc.desc
                   return value.desc
               else:
                   return value
           else:
               return value
       return process

   def result_processor(self, dialect, coltype=None):
       def process(value):
           if value is not None:
               return PersistentSpatialElement(value)
           else:
               return value
       return process

   def _compiler_dispatch(self, *args):
       """Required for the Cast() operator when used for the compilation
       of DBSpatialElement"""
       return self.name

   def adapt(self, cls, **kwargs):
       return cls(dimension=self.dimension, srid=self.srid,
                  spatial_index=self.spatial_index,
                  **self.kwargs)

also figure out what that _compiler_dispatch is about.

Comments (5)

  1. Anonymous

    Before commit, to be closer with what we do in GeoAlchemy, we could make init call its superclass:

        super(MyType, self).__init__()
    
  2. Log in to comment