web/lib/django/contrib/gis/db/backend/oracle/field.py
changeset 0 0d40e90630ef
equal deleted inserted replaced
-1:000000000000 0:0d40e90630ef
       
     1 from django.db import connection
       
     2 from django.db.backends.util import truncate_name
       
     3 from django.db.models.fields import Field # Django base Field class
       
     4 from django.contrib.gis.db.backend.util import gqn
       
     5 from django.contrib.gis.db.backend.oracle.query import TRANSFORM
       
     6 
       
     7 # Quotename & geographic quotename, respectively.
       
     8 qn = connection.ops.quote_name
       
     9 
       
    10 class OracleSpatialField(Field):
       
    11     """
       
    12     The backend-specific geographic field for Oracle Spatial.
       
    13     """
       
    14 
       
    15     empty_strings_allowed = False
       
    16 
       
    17     def __init__(self, extent=(-180.0, -90.0, 180.0, 90.0), tolerance=0.05, **kwargs):
       
    18         """
       
    19         Oracle Spatial backend needs to have the extent -- for projected coordinate
       
    20         systems _you must define the extent manually_, since the coordinates are
       
    21         for geodetic systems.  The `tolerance` keyword specifies the tolerance
       
    22         for error (in meters), and defaults to 0.05 (5 centimeters).
       
    23         """
       
    24         # Oracle Spatial specific keyword arguments.
       
    25         self._extent = extent
       
    26         self._tolerance = tolerance
       
    27         # Calling the Django field initialization.
       
    28         super(OracleSpatialField, self).__init__(**kwargs)
       
    29 
       
    30     def _add_geom(self, style, db_table):
       
    31         """
       
    32         Adds this geometry column into the Oracle USER_SDO_GEOM_METADATA
       
    33         table.
       
    34         """
       
    35         # Checking the dimensions.
       
    36         # TODO: Add support for 3D geometries.
       
    37         if self.dim != 2:
       
    38             raise Exception('3D geometries not yet supported on Oracle Spatial backend.')
       
    39 
       
    40         # Constructing the SQL that will be used to insert information about
       
    41         # the geometry column into the USER_GSDO_GEOM_METADATA table.
       
    42         meta_sql = (style.SQL_KEYWORD('INSERT INTO ') +
       
    43                     style.SQL_TABLE('USER_SDO_GEOM_METADATA') +
       
    44                     ' (%s, %s, %s, %s)\n  ' % tuple(map(qn, ['TABLE_NAME', 'COLUMN_NAME', 'DIMINFO', 'SRID'])) +
       
    45                     style.SQL_KEYWORD(' VALUES ') + '(\n    ' +
       
    46                     style.SQL_TABLE(gqn(db_table)) + ',\n    ' +
       
    47                     style.SQL_FIELD(gqn(self.column)) + ',\n    ' +
       
    48                     style.SQL_KEYWORD("MDSYS.SDO_DIM_ARRAY") + '(\n      ' +
       
    49                     style.SQL_KEYWORD("MDSYS.SDO_DIM_ELEMENT") +
       
    50                     ("('LONG', %s, %s, %s),\n      " % (self._extent[0], self._extent[2], self._tolerance)) +
       
    51                     style.SQL_KEYWORD("MDSYS.SDO_DIM_ELEMENT") +
       
    52                     ("('LAT', %s, %s, %s)\n    ),\n" % (self._extent[1], self._extent[3], self._tolerance)) +
       
    53                     '    %s\n  );' % self.srid)
       
    54         return meta_sql
       
    55 
       
    56     def _geom_index(self, style, db_table):
       
    57         "Creates an Oracle Geometry index (R-tree) for this geometry field."
       
    58 
       
    59         # Getting the index name, Oracle doesn't allow object
       
    60         # names > 30 characters.
       
    61         idx_name = truncate_name('%s_%s_id' % (db_table, self.column), 30)
       
    62 
       
    63         sql = (style.SQL_KEYWORD('CREATE INDEX ') +
       
    64                style.SQL_TABLE(qn(idx_name)) +
       
    65                style.SQL_KEYWORD(' ON ') +
       
    66                style.SQL_TABLE(qn(db_table)) + '(' +
       
    67                style.SQL_FIELD(qn(self.column)) + ') ' +
       
    68                style.SQL_KEYWORD('INDEXTYPE IS ') +
       
    69                style.SQL_TABLE('MDSYS.SPATIAL_INDEX') + ';')
       
    70         return sql
       
    71 
       
    72     def post_create_sql(self, style, db_table):
       
    73         """
       
    74         Returns SQL that will be executed after the model has been
       
    75         created.
       
    76         """
       
    77         # Getting the meta geometry information.
       
    78         post_sql = self._add_geom(style, db_table)
       
    79 
       
    80         # Getting the geometric index for this Geometry column.
       
    81         if self.spatial_index:
       
    82             return (post_sql, self._geom_index(style, db_table))
       
    83         else:
       
    84             return (post_sql,)
       
    85 
       
    86     def db_type(self):
       
    87         "The Oracle geometric data type is MDSYS.SDO_GEOMETRY."
       
    88         return 'MDSYS.SDO_GEOMETRY'
       
    89 
       
    90     def get_placeholder(self, value):
       
    91         """
       
    92         Provides a proper substitution value for Geometries that are not in the
       
    93         SRID of the field.  Specifically, this routine will substitute in the
       
    94         SDO_CS.TRANSFORM() function call.
       
    95         """
       
    96         if value is None:
       
    97             return 'NULL'
       
    98         elif value.srid != self.srid:
       
    99             # Adding Transform() to the SQL placeholder.
       
   100             return '%s(SDO_GEOMETRY(%%s, %s), %s)' % (TRANSFORM, value.srid, self.srid)
       
   101         else:
       
   102             return 'SDO_GEOMETRY(%%s, %s)' % self.srid