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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import cx_Oracle
  2. from django.db.backends.oracle.introspection import DatabaseIntrospection
  3. class OracleIntrospection(DatabaseIntrospection):
  4. # Associating any OBJECTVAR instances with GeometryField. Of course,
  5. # this won't work right on Oracle objects that aren't MDSYS.SDO_GEOMETRY,
  6. # but it is the only object type supported within Django anyways.
  7. data_types_reverse = DatabaseIntrospection.data_types_reverse.copy()
  8. data_types_reverse[cx_Oracle.OBJECT] = 'GeometryField'
  9. def get_geometry_type(self, table_name, geo_col):
  10. with self.connection.cursor() as cursor:
  11. # Querying USER_SDO_GEOM_METADATA to get the SRID and dimension information.
  12. try:
  13. cursor.execute(
  14. 'SELECT "DIMINFO", "SRID" FROM "USER_SDO_GEOM_METADATA" '
  15. 'WHERE "TABLE_NAME"=%s AND "COLUMN_NAME"=%s',
  16. (table_name.upper(), geo_col.upper())
  17. )
  18. row = cursor.fetchone()
  19. except Exception as exc:
  20. raise Exception(
  21. 'Could not find entry in USER_SDO_GEOM_METADATA '
  22. 'corresponding to "%s"."%s"' % (table_name, geo_col)
  23. ) from exc
  24. # TODO: Research way to find a more specific geometry field type for
  25. # the column's contents.
  26. field_type = 'GeometryField'
  27. # Getting the field parameters.
  28. field_params = {}
  29. dim, srid = row
  30. if srid != 4326:
  31. field_params['srid'] = srid
  32. # Size of object array (SDO_DIM_ARRAY) is number of dimensions.
  33. dim = dim.size()
  34. if dim != 2:
  35. field_params['dim'] = dim
  36. return field_type, field_params