Debian Codegraph @

django/contrib/gis/db/backends/mysql/introspection.py

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