Refactored gis/spatialite connection initialization

The connection state is now initialized in get_new_connection().
Refs #19274.
This commit is contained in:
Anssi Kääriäinen 2012-11-27 18:33:56 +02:00
parent 64f6e0370a
commit 86644e065f
1 changed files with 20 additions and 26 deletions

View File

@ -36,29 +36,23 @@ class DatabaseWrapper(SQLiteDatabaseWrapper):
self.creation = SpatiaLiteCreation(self) self.creation = SpatiaLiteCreation(self)
self.introspection = SpatiaLiteIntrospection(self) self.introspection = SpatiaLiteIntrospection(self)
def _cursor(self): def get_new_connection(self, conn_params):
if self.connection is None: conn = super(DatabaseWrapper, self).get_new_connection(conn_params)
self._sqlite_create_connection() # Enabling extension loading on the SQLite connection.
try:
## From here on, customized for GeoDjango ## conn.enable_load_extension(True)
except AttributeError:
# Enabling extension loading on the SQLite connection. raise ImproperlyConfigured(
try: 'The pysqlite library does not support C extension loading. '
self.connection.enable_load_extension(True) 'Both SQLite and pysqlite must be configured to allow '
except AttributeError: 'the loading of extensions to use SpatiaLite.')
raise ImproperlyConfigured('The pysqlite library does not support C extension loading. ' # Loading the SpatiaLite library extension on the connection, and returning
'Both SQLite and pysqlite must be configured to allow ' # the created cursor.
'the loading of extensions to use SpatiaLite.' cur = conn.cursor(factory=SQLiteCursorWrapper)
) try:
cur.execute("SELECT load_extension(%s)", (self.spatialite_lib,))
# Loading the SpatiaLite library extension on the connection, and returning except Exception as msg:
# the created cursor. raise ImproperlyConfigured('Unable to load the SpatiaLite library extension '
cur = self.connection.cursor(factory=SQLiteCursorWrapper) '"%s" because: %s' % (self.spatialite_lib, msg))
try: cur.close()
cur.execute("SELECT load_extension(%s)", (self.spatialite_lib,)) return conn
except Exception as 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)