operations.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from django.db.backends.mysql.base import DatabaseOperations
  2. from django.contrib.gis.db.backends.adapter import WKTAdapter
  3. from django.contrib.gis.db.backends.base import BaseSpatialOperations
  4. class MySQLOperations(DatabaseOperations, BaseSpatialOperations):
  5. compiler_module = 'django.contrib.gis.db.backends.mysql.compiler'
  6. mysql = True
  7. name = 'mysql'
  8. select = 'AsText(%s)'
  9. from_wkb = 'GeomFromWKB'
  10. from_text = 'GeomFromText'
  11. Adapter = WKTAdapter
  12. Adaptor = Adapter # Backwards-compatibility alias.
  13. geometry_functions = {
  14. 'bbcontains': 'MBRContains', # For consistency w/PostGIS API
  15. 'bboverlaps': 'MBROverlaps', # .. ..
  16. 'contained': 'MBRWithin', # .. ..
  17. 'contains': 'MBRContains',
  18. 'disjoint': 'MBRDisjoint',
  19. 'equals': 'MBREqual',
  20. 'exact': 'MBREqual',
  21. 'intersects': 'MBRIntersects',
  22. 'overlaps': 'MBROverlaps',
  23. 'same_as': 'MBREqual',
  24. 'touches': 'MBRTouches',
  25. 'within': 'MBRWithin',
  26. }
  27. gis_terms = set(geometry_functions) | set(['isnull'])
  28. def geo_db_type(self, f):
  29. return f.geom_type
  30. def get_geom_placeholder(self, value, srid):
  31. """
  32. The placeholder here has to include MySQL's WKT constructor. Because
  33. MySQL does not support spatial transformations, there is no need to
  34. modify the placeholder based on the contents of the given value.
  35. """
  36. if hasattr(value, 'expression'):
  37. placeholder = self.get_expression_column(value)
  38. else:
  39. placeholder = '%s(%%s)' % self.from_text
  40. return placeholder
  41. def spatial_lookup_sql(self, lvalue, lookup_type, value, field, qn):
  42. geo_col, db_type = lvalue
  43. lookup_info = self.geometry_functions.get(lookup_type, False)
  44. if lookup_info:
  45. sql = "%s(%s, %s)" % (lookup_info, geo_col,
  46. self.get_geom_placeholder(value, field.srid))
  47. return sql, []
  48. # TODO: Is this really necessary? MySQL can't handle NULL geometries
  49. # in its spatial indexes anyways.
  50. if lookup_type == 'isnull':
  51. return "%s IS %sNULL" % (geo_col, ('' if value else 'NOT ')), []
  52. raise TypeError("Got invalid lookup_type: %s" % repr(lookup_type))