Ticket #10888: orcl_null_v2.diff

File orcl_null_v2.diff, 5.7 KB (added by jbronn, 15 years ago)
  • django/contrib/gis/db/models/sql/subqueries.py

     
     1from django.contrib.gis.db.backend import SpatialBackend
     2from django.db.models.query import insert_query
     3
     4if SpatialBackend.oracle:
     5    from django.db import connection
     6    from django.db.models.sql.subqueries import InsertQuery
     7
     8    class OracleGeoInsertQuery(InsertQuery):
     9        def insert_values(self, insert_values, raw_values=False):
     10            """
     11            This routine is overloaded from InsertQuery so that no parameter is
     12            passed into cx_Oracle for NULL geometries.  The reason is that
     13            cx_Oracle has no way to bind Oracle object values (like
     14            MDSYS.SDO_GEOMETRY).
     15            """
     16            placeholders, values = [], []
     17            for field, val in insert_values:
     18                if hasattr(field, 'get_placeholder'):
     19                    ph = field.get_placeholder(val)
     20                else:
     21                    ph = '%s'
     22
     23                placeholders.append(ph)
     24                self.columns.append(field.column)
     25
     26                # If 'NULL' for the placeholder, omit appending None
     27                # to the values list (which is used for db params).
     28                if not ph == 'NULL':
     29                    values.append(val)
     30            if raw_values:
     31                self.values.extend(values)
     32            else:
     33                self.params += tuple(values)
     34                self.values.extend(placeholders)
     35
     36    def insert_query(model, values, return_id=False, raw_values=False):
     37        query = OracleGeoInsertQuery(model, connection)
     38        query.insert_values(values, raw_values)
     39        return query.execute_sql(return_id)
  • django/contrib/gis/db/models/manager.py

     
    11from django.db.models.manager import Manager
    22from django.contrib.gis.db.models.query import GeoQuerySet
     3from django.contrib.gis.db.models.sql.subqueries import insert_query
    34
    45class GeoManager(Manager):
    56    "Overrides Manager to return Geographic QuerySets."
     
    8687
    8788    def unionagg(self, *args, **kwargs):
    8889        return self.get_query_set().unionagg(*args, **kwargs)
     90
     91    def _insert(self, values, **kwargs):
     92        return insert_query(self.model, values, **kwargs)
  • django/contrib/gis/db/backend/oracle/field.py

     
    9494        SDO_CS.TRANSFORM() function call.
    9595        """
    9696        if value is None:
    97             return '%s'
     97            return 'NULL'
    9898        elif value.srid != self.srid:
    9999            # Adding Transform() to the SQL placeholder.
    100100            return '%s(SDO_GEOMETRY(%%s, %s), %s)' % (TRANSFORM, value.srid, self.srid)
  • django/contrib/gis/tests/geoapp/tests.py

     
    3030            data_dir = os.path.join(os.path.dirname(__file__), 'sql')
    3131            def get_file(wkt_file):
    3232                return os.path.join(data_dir, wkt_file)
    33 
     33            State(name='Puerto Rico', poly=None).save()
    3434            State(name='Colorado', poly=fromfile(get_file('co.wkt'))).save()
    3535            State(name='Kansas', poly=fromfile(get_file('ks.wkt'))).save()
    3636            Country(name='Texas', mpoly=fromfile(get_file('tx.wkt'))).save()
     
    3939        # Ensuring that data was loaded from initial SQL.
    4040        self.assertEqual(2, Country.objects.count())
    4141        self.assertEqual(8, City.objects.count())
     42        self.assertEqual(3, State.objects.count())
    4243
    43         # Only PostGIS can handle NULL geometries
    44         if SpatialBackend.postgis or SpatialBackend.spatialite:
    45             n_state = 3
    46         else:
    47             n_state = 2
    48         self.assertEqual(n_state, State.objects.count())
    49 
    5044    def test02_proxy(self):
    5145        "Testing Lazy-Geometry support (using the GeometryProxy)."
    5246        if DISABLE: return
     
    369363            m1.save()
    370364            self.assertEqual(-1, m1.geom.srid)
    371365
    372     # Oracle does not support NULL geometries in its spatial index for
    373     # some routines (e.g., SDO_GEOM.RELATE).
    374     @no_oracle
    375     @no_spatialite
    376366    def test12_null_geometries(self):
    377367        "Testing NULL geometry support, and the `isnull` lookup type."
    378368        if DISABLE: return
     
    391381        self.assertEqual(True, 'Kansas' in state_names)
    392382
    393383        # Saving another commonwealth w/a NULL geometry.
    394         if not SpatialBackend.oracle:
    395             # TODO: Fix saving w/NULL geometry on Oracle.
    396             State(name='Northern Mariana Islands', poly=None).save()
     384        nmi = State.objects.create(name='Northern Mariana Islands', poly=None)
     385        self.assertEqual(nmi.poly, None)
    397386
     387        # Assigning a geomery and saving -- then UPDATE back to NULL.
     388        nmi.poly = 'POLYGON((0 0,1 0,1 1,1 0,0 0))'
     389        nmi.save()
     390        State.objects.filter(name='Northern Mariana Islands').update(poly=None)
     391        self.assertEqual(None, State.objects.get(name='Northern Mariana Islands').poly)
     392
    398393    @no_oracle # No specific `left` or `right` operators in Oracle.
    399394    @no_spatialite # No `left` or `right` operators in SpatiaLite.
    400395    def test13_left_right(self):
Back to Top