Author: jbronn
Date: 2010-03-29 14:40:59 -0500 (Mon, 29 Mar 2010)
New Revision: 12882

Modified:
   django/trunk/django/contrib/gis/db/backends/spatialite/operations.py
Log:
Fixed #13070 -- Introduced fallback code to detect SpatiaLite 2.3.0 versions 
(which do not have `spatialite_version` function).


Modified: django/trunk/django/contrib/gis/db/backends/spatialite/operations.py
===================================================================
--- django/trunk/django/contrib/gis/db/backends/spatialite/operations.py        
2010-03-29 18:34:09 UTC (rev 12881)
+++ django/trunk/django/contrib/gis/db/backends/spatialite/operations.py        
2010-03-29 19:40:59 UTC (rev 12882)
@@ -8,6 +8,7 @@
 from django.contrib.gis.measure import Distance
 from django.core.exceptions import ImproperlyConfigured
 from django.db.backends.sqlite3.base import DatabaseOperations
+from django.db.utils import DatabaseError
 
 class SpatiaLiteOperator(SpatialOperation):
     "For SpatiaLite operators (e.g. `&&`, `~`)."
@@ -115,9 +116,9 @@
         try:
             vtup = self.spatialite_version_tuple()
             version = vtup[1:]
-            if version < (2, 3, 1):
+            if version < (2, 3, 0):
                 raise ImproperlyConfigured('GeoDjango only supports SpatiaLite 
versions '
-                                           '2.3.1 and above')
+                                           '2.3.0 and above')
             self.spatial_version = version
         except ImproperlyConfigured:
             raise
@@ -203,12 +204,13 @@
 
     def _get_spatialite_func(self, func):
         """
-        Helper routine for calling PostGIS functions and returning their 
result.
+        Helper routine for calling SpatiaLite functions and returning
+        their result.
         """
         cursor = self.connection._cursor()
         try:
             try:
-                cursor.execute('SELECT %s()' % func)
+                cursor.execute('SELECT %s' % func)
                 row = cursor.fetchone()
             except:
                 # Responsibility of caller to perform error handling.
@@ -219,25 +221,39 @@
 
     def geos_version(self):
         "Returns the version of GEOS used by SpatiaLite as a string."
-        return self._get_spatialite_func('geos_version')
+        return self._get_spatialite_func('geos_version()')
 
     def proj4_version(self):
         "Returns the version of the PROJ.4 library used by SpatiaLite."
-        return self._get_spatialite_func('proj4_version')
+        return self._get_spatialite_func('proj4_version()')
 
     def spatialite_version(self):
         "Returns the SpatiaLite library version as a string."
-        return self._get_spatialite_func('spatialite_version')
+        return self._get_spatialite_func('spatialite_version()')
 
     def spatialite_version_tuple(self):
         """
         Returns the SpatiaLite version as a tuple (version string, major,
         minor, subminor).
         """
-        # Getting the PostGIS version
-        version = self.spatialite_version()
+        # Getting the SpatiaLite version.
+        try:
+            version = self.spatialite_version()
+        except DatabaseError:
+            # The `spatialite_version` function first appeared in version 2.3.1
+            # of SpatiaLite, so doing a fallback test for 2.3.0 (which is
+            # used by popular Debian/Ubuntu packages).
+            version = None
+            try:
+                tmp = self._get_spatialite_func("X(GeomFromText('POINT(1 
1)'))")
+                if tmp == 1.0: version = '2.3.0'
+            except DatabaseError:
+                pass
+            # If no version string defined, then just re-raise the original
+            # exception.
+            if version is None: raise
+
         m = self.version_regex.match(version)
-
         if m:
             major = int(m.group('major'))
             minor1 = int(m.group('minor1'))

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to