1
0
mirror of https://github.com/django/django.git synced 2025-06-06 12:09:11 +00:00

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
This commit is contained in:
Justin Bronn 2010-01-01 10:36:22 +00:00
parent dae4972b4d
commit f2d0ae93f8
3 changed files with 20 additions and 22 deletions

View File

@ -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.creation import SpatiaLiteCreation
from django.contrib.gis.db.backends.spatialite.operations import SpatiaLiteOperations from django.contrib.gis.db.backends.spatialite.operations import SpatiaLiteOperations
class DatabaseWrapper(SqliteDatabaseWrapper): class DatabaseWrapper(SqliteDatabaseWrapper):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
# Before we get too far, make sure pysqlite 2.5+ is installed. # Before we get too far, make sure pysqlite 2.5+ is installed.
@ -36,6 +35,7 @@ class DatabaseWrapper(SqliteDatabaseWrapper):
def _cursor(self): def _cursor(self):
if self.connection is None: if self.connection is None:
## The following is the same as in django.db.backends.sqlite3.base ##
settings_dict = self.settings_dict settings_dict = self.settings_dict
if not settings_dict['NAME']: if not settings_dict['NAME']:
from django.core.exceptions import ImproperlyConfigured 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_extract", 2, _sqlite_extract)
self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
self.connection.create_function("regexp", 2, _sqlite_regexp) 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: try:
self.connection.enable_load_extension(True) self.connection.enable_load_extension(True)
except AttributeError: except AttributeError:
@ -59,15 +63,14 @@ class DatabaseWrapper(SqliteDatabaseWrapper):
'the loading of extensions to use SpatiaLite.' 'the loading of extensions to use SpatiaLite.'
) )
connection_created.send(sender=self.__class__) # Loading the SpatiaLite library extension on the connection, and returning
return self.connection.cursor(factory=SQLiteCursorWrapper) # the created cursor.
cur = self.connection.cursor(factory=SQLiteCursorWrapper)
def load_spatialite(self): try:
""" cur.execute("SELECT load_extension(%s)", (self.spatialite_lib,))
Loads the SpatiaLite library. except Exception, msg:
""" raise ImproperlyConfigured('Unable to load the SpatiaLite library extension '
try: '"%s" because: %s' % (self.spatialite_lib, msg))
self._cursor().execute("SELECT load_extension(%s)", (self.spatialite_lib,)) return cur
except Exception, msg: else:
raise ImproperlyConfigured('Unable to load the SpatiaLite extension ' return self.connection.cursor(factory=SQLiteCursorWrapper)
'"%s" because: %s' % (self.spatialite_lib, msg))

View File

@ -24,8 +24,7 @@ class SpatiaLiteCreation(DatabaseCreation):
self.connection.settings_dict["NAME"] = test_database_name self.connection.settings_dict["NAME"] = test_database_name
can_rollback = self._rollback_works() can_rollback = self._rollback_works()
self.connection.settings_dict["SUPPORTS_TRANSACTIONS"] = can_rollback self.connection.settings_dict["SUPPORTS_TRANSACTIONS"] = can_rollback
# Need to load the SpatiaLite library and initializatin SQL before running `syncdb`. # Need to load the SpatiaLite initialization SQL before running `syncdb`.
self.connection.load_spatialite()
self.load_spatialite_sql() self.load_spatialite_sql()
call_command('syncdb', verbosity=verbosity, interactive=False, database=self.connection.alias) call_command('syncdb', verbosity=verbosity, interactive=False, database=self.connection.alias)

View File

@ -51,8 +51,7 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations):
name = 'spatialite' name = 'spatialite'
spatialite = True spatialite = True
version_regex = re.compile(r'^(?P<major>\d)\.(?P<minor1>\d)\.(?P<minor2>\d+)') version_regex = re.compile(r'^(?P<major>\d)\.(?P<minor1>\d)\.(?P<minor2>\d+)')
valid_aggregates = dict([(k, None) for k in valid_aggregates = dict([(k, None) for k in ('Extent', 'Union')])
('Extent', 'Union')])
Adapter = SpatiaLiteAdapter Adapter = SpatiaLiteAdapter
@ -112,10 +111,7 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations):
super(DatabaseOperations, self).__init__() super(DatabaseOperations, self).__init__()
self.connection = connection self.connection = connection
# Load the spatialite library (must be done before getting the # Determine the version of the SpatiaLite library.
# SpatiaLite version).
self.connection.load_spatialite()
try: try:
vtup = self.spatialite_version_tuple() vtup = self.spatialite_version_tuple()
version = vtup[1:] version = vtup[1:]
@ -269,7 +265,7 @@ class SpatiaLiteOperations(DatabaseOperations, BaseSpatialOperations):
""" """
Returns the SpatiaLite-specific SQL for the given lookup value Returns the SpatiaLite-specific SQL for the given lookup value
[a tuple of (alias, column, db_type)], lookup type, lookup [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 alias, col, db_type = lvalue