You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

introspection.py 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from MySQLdb.constants import FIELD_TYPE
  2. from django.contrib.gis.gdal import OGRGeomType
  3. from django.db.backends.mysql.introspection import DatabaseIntrospection
  4. class MySQLIntrospection(DatabaseIntrospection):
  5. # Updating the data_types_reverse dictionary with the appropriate
  6. # type for Geometry fields.
  7. data_types_reverse = DatabaseIntrospection.data_types_reverse.copy()
  8. data_types_reverse[FIELD_TYPE.GEOMETRY] = 'GeometryField'
  9. def get_geometry_type(self, table_name, geo_col):
  10. with self.connection.cursor() as cursor:
  11. # In order to get the specific geometry type of the field,
  12. # we introspect on the table definition using `DESCRIBE`.
  13. cursor.execute('DESCRIBE %s' %
  14. self.connection.ops.quote_name(table_name))
  15. # Increment over description info until we get to the geometry
  16. # column.
  17. for column, typ, null, key, default, extra in cursor.fetchall():
  18. if column == geo_col:
  19. # Using OGRGeomType to convert from OGC name to Django field.
  20. # MySQL does not support 3D or SRIDs, so the field params
  21. # are empty.
  22. field_type = OGRGeomType(typ).django
  23. field_params = {}
  24. break
  25. return field_type, field_params
  26. def supports_spatial_index(self, cursor, table_name):
  27. # Supported with MyISAM/Aria, or InnoDB on MySQL 5.7.5+/MariaDB 10.2.2+
  28. storage_engine = self.get_storage_engine(cursor, table_name)
  29. if storage_engine == 'InnoDB':
  30. return self.connection.mysql_version >= (
  31. (10, 2, 2) if self.connection.mysql_is_mariadb else (5, 7, 5)
  32. )
  33. return storage_engine in ('MyISAM', 'Aria')