From f2d0ae93f859e31d9591f4b4b35890b320e9129c Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Fri, 1 Jan 2010 10:36:22 +0000 Subject: [PATCH] Fixed problem in which SpatiaLite library would not be loaded for the connection under certain circumstances, e.g., when using the geographic admin. Thanks, jtiai, for the bug report. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12037 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../gis/db/backends/spatialite/base.py | 29 ++++++++++--------- .../gis/db/backends/spatialite/creation.py | 3 +- .../gis/db/backends/spatialite/operations.py | 10 ++----- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/django/contrib/gis/db/backends/spatialite/base.py b/django/contrib/gis/db/backends/spatialite/base.py index 0b6332dd0c..9dde4ec0b2 100644 --- a/django/contrib/gis/db/backends/spatialite/base.py +++ b/django/contrib/gis/db/backends/spatialite/base.py @@ -9,7 +9,6 @@ from django.contrib.gis.db.backends.spatialite.client import SpatiaLiteClient from django.contrib.gis.db.backends.spatialite.creation import SpatiaLiteCreation from django.contrib.gis.db.backends.spatialite.operations import SpatiaLiteOperations - class DatabaseWrapper(SqliteDatabaseWrapper): def __init__(self, *args, **kwargs): # Before we get too far, make sure pysqlite 2.5+ is installed. @@ -36,6 +35,7 @@ class DatabaseWrapper(SqliteDatabaseWrapper): def _cursor(self): if self.connection is None: + ## The following is the same as in django.db.backends.sqlite3.base ## settings_dict = self.settings_dict if not settings_dict['NAME']: from django.core.exceptions import ImproperlyConfigured @@ -50,7 +50,11 @@ class DatabaseWrapper(SqliteDatabaseWrapper): self.connection.create_function("django_extract", 2, _sqlite_extract) self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) self.connection.create_function("regexp", 2, _sqlite_regexp) + connection_created.send(sender=self.__class__) + ## From here on, customized for GeoDjango ## + + # Enabling extension loading on the SQLite connection. try: self.connection.enable_load_extension(True) except AttributeError: @@ -59,15 +63,14 @@ class DatabaseWrapper(SqliteDatabaseWrapper): 'the loading of extensions to use SpatiaLite.' ) - connection_created.send(sender=self.__class__) - return self.connection.cursor(factory=SQLiteCursorWrapper) - - def load_spatialite(self): - """ - Loads the SpatiaLite library. - """ - try: - self._cursor().execute("SELECT load_extension(%s)", (self.spatialite_lib,)) - except Exception, msg: - raise ImproperlyConfigured('Unable to load the SpatiaLite extension ' - '"%s" because: %s' % (self.spatialite_lib, msg)) + # Loading the SpatiaLite library extension on the connection, and returning + # the created cursor. + cur = self.connection.cursor(factory=SQLiteCursorWrapper) + try: + cur.execute("SELECT load_extension(%s)", (self.spatialite_lib,)) + except Exception, msg: + raise ImproperlyConfigured('Unable to load the SpatiaLite library extension ' + '"%s" because: %s' % (self.spatialite_lib, msg)) + return cur + else: + return self.connection.cursor(factory=SQLiteCursorWrapper) diff --git a/django/contrib/gis/db/backends/spatialite/creation.py b/django/contrib/gis/db/backends/spatialite/creation.py index 1bec8360df..cbe4a29585 100644 --- a/django/contrib/gis/db/backends/spatialite/creation.py +++ b/django/contrib/gis/db/backends/spatialite/creation.py @@ -24,8 +24,7 @@ class SpatiaLiteCreation(DatabaseCreation): self.connection.settings_dict["NAME"] = test_database_name can_rollback = self._rollback_works() self.connection.settings_dict["SUPPORTS_TRANSACTIONS"] = can_rollback - # Need to load the SpatiaLite library and initializatin SQL before running `syncdb`. - self.connection.load_spatialite() + # Need to load the SpatiaLite initialization SQL before running `syncdb`. self.load_spatialite_sql() call_command('syncdb', verbosity=verbosity, interactive=False, database=self.connection.alias) diff --git a/django/contrib/gis/db/backends/spatialite/operations.py b/django/contrib/gis/db/backends/spatialite/operations.py index f49cf77f3a..444a212c13 100644 --- a/django/contrib/gis/db/backends/spatialite/operations.py +++ b/django/contrib/gis/db/backends/spatialite/operations.py @@ -51,8 +51,7 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations): name = 'spatialite' spatialite = True version_regex = re.compile(r'^(?P\d)\.(?P\d)\.(?P\d+)') - valid_aggregates = dict([(k, None) for k in - ('Extent', 'Union')]) + valid_aggregates = dict([(k, None) for k in ('Extent', 'Union')]) Adapter = SpatiaLiteAdapter @@ -112,10 +111,7 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations): super(DatabaseOperations, self).__init__() self.connection = connection - # Load the spatialite library (must be done before getting the - # SpatiaLite version). - self.connection.load_spatialite() - + # Determine the version of the SpatiaLite library. try: vtup = self.spatialite_version_tuple() version = vtup[1:] @@ -269,7 +265,7 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations): """ Returns the SpatiaLite-specific SQL for the given lookup value [a tuple of (alias, column, db_type)], lookup type, lookup - value, and the model field. + value, the model field, and the quoting function. """ alias, col, db_type = lvalue