Changeset 7104 for django/branches/gis/django/contrib/gis/db/backend/oracle
- Timestamp:
- 02/10/08 20:25:01 (1 year ago)
- Files:
-
- django/branches/gis/django/contrib/gis/db/backend/oracle/adaptor.py (modified) (2 diffs)
- django/branches/gis/django/contrib/gis/db/backend/oracle/field.py (modified) (3 diffs)
- django/branches/gis/django/contrib/gis/db/backend/oracle/__init__.py (modified) (1 diff)
- django/branches/gis/django/contrib/gis/db/backend/oracle/query.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/db/backend/oracle/adaptor.py
r6524 r7104 2 2 This object provides the database adaptor for Oracle geometries. 3 3 """ 4 from cx_Oracle import CLOB5 6 4 class OracleSpatialAdaptor(object): 7 5 def __init__(self, geom): … … 12 10 "WKT is used for the substitution value of the geometry." 13 11 return self.wkt 14 15 def oracle_type(self):16 """17 The parameter type is a CLOB because no string (VARCHAR2) greater18 than 4000 characters will be accepted through the Oracle database19 API and/or SQL*Plus.20 """21 return CLOBdjango/branches/gis/django/contrib/gis/db/backend/oracle/field.py
r6919 r7104 1 import re2 from types import StringType, UnicodeType3 1 from django.db import connection 4 2 from django.db.backends.util import truncate_name 5 3 from django.db.models.fields import Field # Django base Field class 6 from django.contrib.gis.geos import GEOSGeometry 7 from django.contrib.gis.db.backend.util import GeoFieldSQL 8 from django.contrib.gis.db.backend.oracle.adaptor import OracleSpatialAdaptor 9 from django.contrib.gis.db.backend.oracle.query import ORACLE_SPATIAL_TERMS, DISTANCE_FUNCTIONS, TRANSFORM 4 from django.contrib.gis.db.backend.util import gqn 5 from django.contrib.gis.db.backend.oracle.query import TRANSFORM 10 6 11 7 # Quotename & geographic quotename, respectively. 12 8 qn = connection.ops.quote_name 13 def gqn(value):14 if isinstance(value, UnicodeType): value = value.encode('ascii')15 return "'%s'" % value16 9 17 10 class OracleSpatialField(Field): … … 96 89 return 'MDSYS.SDO_GEOMETRY' 97 90 98 def get_db_prep_lookup(self, lookup_type, value):99 """100 Returns field's value prepared for database lookup, accepts WKT and101 GEOS Geometries for the value.102 """103 if lookup_type in ORACLE_SPATIAL_TERMS:104 # special case for isnull lookup105 if lookup_type == 'isnull': return GeoFieldSQL([], [])106 107 # Get the geometry with SRID; defaults SRID to that108 # of the field if it is None109 geom = self.get_geometry(value)110 111 # The adaptor will be used by psycopg2 for quoting the WKT.112 adapt = OracleSpatialAdaptor(geom)113 114 if geom.srid != self._srid:115 # Adding the necessary string substitutions and parameters116 # to perform a geometry transformation.117 where = ['%s(SDO_GEOMETRY(%%s, %s), %%s)' % (TRANSFORM, geom.srid)]118 params = [adapt, self._srid]119 else:120 where = ['SDO_GEOMETRY(%%s, %s)' % geom.srid]121 params = [adapt]122 123 if isinstance(value, tuple):124 if lookup_type in DISTANCE_FUNCTIONS or lookup_type == 'dwithin':125 # Getting the distance parameter in the units of the field126 where += [self.get_distance(value[1])]127 elif lookup_type == 'relate':128 # No extra where parameters for SDO_RELATE queries.129 pass130 else:131 where += map(gqn, value[1:])132 return GeoFieldSQL(where, params)133 else:134 raise TypeError("Field has invalid lookup: %s" % lookup_type)135 136 def get_db_prep_save(self, value):137 "Prepares the value for saving in the database."138 if not bool(value):139 # Return an empty string for NULL -- but this doesn't work yet.140 return ''141 if isinstance(value, GEOSGeometry):142 return OracleSpatialAdaptor(value)143 else:144 raise TypeError('Geometry Proxy should only return GEOSGeometry objects.')145 146 91 def get_placeholder(self, value): 147 92 """ … … 150 95 SDO_CS.TRANSFORM() function call. 151 96 """ 152 if isinstance(value, GEOSGeometry) and value.srid != self._srid: 97 if value is None: 98 return '%s' 99 elif value.srid != self._srid: 153 100 # Adding Transform() to the SQL placeholder. 154 101 return '%s(SDO_GEOMETRY(%%s, %s), %s)' % (TRANSFORM, value.srid, self._srid) 155 elif value is None:156 return '%s'157 102 else: 158 103 return 'SDO_GEOMETRY(%%s, %s)' % self._srid django/branches/gis/django/contrib/gis/db/backend/oracle/__init__.py
r6886 r7104 1 """2 The Oracle spatial database backend module.3 4 Please note that WKT support is broken on the XE version, and thus5 this backend will not work on such platforms. Specifically, XE lacks6 support for an internal JVM, and Java libraries are required to use7 the WKT constructors.8 """9 from django.contrib.gis.db.backend.oracle.creation import create_spatial_db10 from django.contrib.gis.db.backend.oracle.field import OracleSpatialField, gqn11 from django.contrib.gis.db.backend.oracle.query import \12 get_geo_where_clause, ORACLE_SPATIAL_TERMS, \13 ASGML, DISTANCE, GEOM_SELECT, TRANSFORM, UNION14 django/branches/gis/django/contrib/gis/db/backend/oracle/query.py
r6919 r7104 1 1 """ 2 This module contains the spatial lookup types, and the get_geo_where_clause()2 This module contains the spatial lookup types, and the `get_geo_where_clause` 3 3 routine for Oracle Spatial. 4 5 Please note that WKT support is broken on the XE version, and thus 6 this backend will not work on such platforms. Specifically, XE lacks 7 support for an internal JVM, and Java libraries are required to use 8 the WKT constructors. 4 9 """ 5 10 import re … … 26 31 class SDOOperation(SpatialFunction): 27 32 "Base class for SDO* Oracle operations." 28 def __init__(self, func, end_subst=") %s '%s'"): 29 super(SDOOperation, self).__init__(func, end_subst=end_subst, operator='=', result='TRUE') 33 def __init__(self, func, **kwargs): 34 kwargs.setdefault('operator', '=') 35 kwargs.setdefault('result', 'TRUE') 36 kwargs.setdefault('end_subst', ") %s '%s'") 37 super(SDOOperation, self).__init__(func, **kwargs) 30 38 31 39 class SDODistance(SpatialFunction): … … 56 64 57 65 # Valid distance types and substitutions 58 dtypes = (Decimal, Distance, float, int )66 dtypes = (Decimal, Distance, float, int, long) 59 67 DISTANCE_FUNCTIONS = { 60 68 'distance_gt' : (SDODistance('>'), dtypes), … … 62 70 'distance_lt' : (SDODistance('<'), dtypes), 63 71 'distance_lte' : (SDODistance('<='), dtypes), 72 'dwithin' : (SDOOperation('SDO_WITHIN_DISTANCE', 73 beg_subst="%s(%s, %%s, 'distance=%%s'"), dtypes), 64 74 } 65 75 … … 69 79 'covers' : SDOOperation('SDO_COVERS'), 70 80 'disjoint' : SDOGeomRelate('DISJOINT'), 71 'dwithin' : (SDOOperation('SDO_WITHIN_DISTANCE', end_subst=", %%s, 'distance=%%s') %s '%s'"), dtypes),72 81 'intersects' : SDOOperation('SDO_OVERLAPBDYINTERSECT'), # TODO: Is this really the same as ST_Intersects()? 73 82 'equals' : SDOOperation('SDO_EQUAL'), … … 90 99 91 100 #### The `get_geo_where_clause` function for Oracle #### 92 def get_geo_where_clause(lookup_type, table_prefix, field _name, value):101 def get_geo_where_clause(lookup_type, table_prefix, field, value): 93 102 "Returns the SQL WHERE clause for use in Oracle spatial SQL construction." 94 103 # Getting the quoted table name as `geo_col`. 95 geo_col = '%s.%s' % (qn(table_prefix), qn(field _name))104 geo_col = '%s.%s' % (qn(table_prefix), qn(field.column)) 96 105 97 106 # See if a Oracle Geometry function matches the lookup type next … … 123 132 return sdo_op.as_sql(geo_col) 124 133 else: 125 # Lookup info is a SDOOperation instance, whos `as_sql` method returns134 # Lookup info is a SDOOperation instance, whose `as_sql` method returns 126 135 # the SQL necessary for the geometry function call. For example: 127 136 # SDO_CONTAINS("geoapp_country"."poly", SDO_GEOMTRY('POINT(5 23)', 4326)) = 'TRUE'
