Skip to content

Commit 85fa7d8

Browse files
committed
Fixed #13070 -- Introduced fallback code to detect SpatiaLite 2.3.0 versions (which do not have spatialite_version function).
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12882 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 85e1368 commit 85fa7d8

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

django/contrib/gis/db/backends/spatialite/operations.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from django.contrib.gis.measure import Distance
99
from django.core.exceptions import ImproperlyConfigured
1010
from django.db.backends.sqlite3.base import DatabaseOperations
11+
from django.db.utils import DatabaseError
1112

1213
class SpatiaLiteOperator(SpatialOperation):
1314
"For SpatiaLite operators (e.g. `&&`, `~`)."
@@ -115,9 +116,9 @@ def __init__(self, connection):
115116
try:
116117
vtup = self.spatialite_version_tuple()
117118
version = vtup[1:]
118-
if version < (2, 3, 1):
119+
if version < (2, 3, 0):
119120
raise ImproperlyConfigured('GeoDjango only supports SpatiaLite versions '
120-
'2.3.1 and above')
121+
'2.3.0 and above')
121122
self.spatial_version = version
122123
except ImproperlyConfigured:
123124
raise
@@ -203,12 +204,13 @@ def transform_value(value, srid):
203204

204205
def _get_spatialite_func(self, func):
205206
"""
206-
Helper routine for calling PostGIS functions and returning their result.
207+
Helper routine for calling SpatiaLite functions and returning
208+
their result.
207209
"""
208210
cursor = self.connection._cursor()
209211
try:
210212
try:
211-
cursor.execute('SELECT %s()' % func)
213+
cursor.execute('SELECT %s' % func)
212214
row = cursor.fetchone()
213215
except:
214216
# Responsibility of caller to perform error handling.
@@ -219,25 +221,39 @@ def _get_spatialite_func(self, func):
219221

220222
def geos_version(self):
221223
"Returns the version of GEOS used by SpatiaLite as a string."
222-
return self._get_spatialite_func('geos_version')
224+
return self._get_spatialite_func('geos_version()')
223225

224226
def proj4_version(self):
225227
"Returns the version of the PROJ.4 library used by SpatiaLite."
226-
return self._get_spatialite_func('proj4_version')
228+
return self._get_spatialite_func('proj4_version()')
227229

228230
def spatialite_version(self):
229231
"Returns the SpatiaLite library version as a string."
230-
return self._get_spatialite_func('spatialite_version')
232+
return self._get_spatialite_func('spatialite_version()')
231233

232234
def spatialite_version_tuple(self):
233235
"""
234236
Returns the SpatiaLite version as a tuple (version string, major,
235237
minor, subminor).
236238
"""
237-
# Getting the PostGIS version
238-
version = self.spatialite_version()
239-
m = self.version_regex.match(version)
239+
# Getting the SpatiaLite version.
240+
try:
241+
version = self.spatialite_version()
242+
except DatabaseError:
243+
# The `spatialite_version` function first appeared in version 2.3.1
244+
# of SpatiaLite, so doing a fallback test for 2.3.0 (which is
245+
# used by popular Debian/Ubuntu packages).
246+
version = None
247+
try:
248+
tmp = self._get_spatialite_func("X(GeomFromText('POINT(1 1)'))")
249+
if tmp == 1.0: version = '2.3.0'
250+
except DatabaseError:
251+
pass
252+
# If no version string defined, then just re-raise the original
253+
# exception.
254+
if version is None: raise
240255

256+
m = self.version_regex.match(version)
241257
if m:
242258
major = int(m.group('major'))
243259
minor1 = int(m.group('minor1'))

0 commit comments

Comments
 (0)