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