2009-12-22 23:18:51 +08:00
|
|
|
from ctypes.util import find_library
|
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.conf import settings
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.sqlite3.base import (
|
2017-01-21 07:21:15 +08:00
|
|
|
DatabaseWrapper as SQLiteDatabaseWrapper, SQLiteCursorWrapper,
|
2015-01-13 04:20:40 +08:00
|
|
|
)
|
2014-08-18 01:06:25 +08:00
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
from .client import SpatiaLiteClient
|
|
|
|
from .features import DatabaseFeatures
|
|
|
|
from .introspection import SpatiaLiteIntrospection
|
|
|
|
from .operations import SpatiaLiteOperations
|
|
|
|
from .schema import SpatialiteSchemaEditor
|
2014-10-31 01:52:42 +08:00
|
|
|
|
2014-08-18 01:06:25 +08:00
|
|
|
|
2011-09-10 06:34:23 +08:00
|
|
|
class DatabaseWrapper(SQLiteDatabaseWrapper):
|
2014-09-26 01:59:03 +08:00
|
|
|
SchemaEditorClass = SpatialiteSchemaEditor
|
2016-09-09 04:33:36 +08:00
|
|
|
# Classes instantiated in __init__().
|
|
|
|
client_class = SpatiaLiteClient
|
|
|
|
features_class = DatabaseFeatures
|
|
|
|
introspection_class = SpatiaLiteIntrospection
|
|
|
|
ops_class = SpatiaLiteOperations
|
2014-09-26 01:59:03 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# Trying to find the location of the SpatiaLite library.
|
|
|
|
# Here we are figuring out the path to the SpatiaLite library
|
|
|
|
# (`libspatialite`). If it's not in the system library path (e.g., it
|
|
|
|
# cannot be found by `ctypes.util.find_library`), then it may be set
|
|
|
|
# manually in the settings via the `SPATIALITE_LIBRARY_PATH` setting.
|
|
|
|
self.spatialite_lib = getattr(settings, 'SPATIALITE_LIBRARY_PATH',
|
|
|
|
find_library('spatialite'))
|
|
|
|
if not self.spatialite_lib:
|
|
|
|
raise ImproperlyConfigured('Unable to locate the SpatiaLite library. '
|
|
|
|
'Make sure it is in your library path, or set '
|
|
|
|
'SPATIALITE_LIBRARY_PATH in your settings.'
|
|
|
|
)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2012-11-28 00:33:56 +08:00
|
|
|
def get_new_connection(self, conn_params):
|
2017-01-21 21:13:44 +08:00
|
|
|
conn = super().get_new_connection(conn_params)
|
2012-11-28 00:33:56 +08:00
|
|
|
# Enabling extension loading on the SQLite connection.
|
|
|
|
try:
|
|
|
|
conn.enable_load_extension(True)
|
|
|
|
except AttributeError:
|
|
|
|
raise ImproperlyConfigured(
|
2017-01-21 07:21:15 +08:00
|
|
|
'SpatiaLite requires SQLite to be configured to allow '
|
|
|
|
'extension loading.'
|
|
|
|
)
|
2012-11-28 00:33:56 +08:00
|
|
|
# Loading the SpatiaLite library extension on the connection, and returning
|
|
|
|
# the created cursor.
|
|
|
|
cur = conn.cursor(factory=SQLiteCursorWrapper)
|
|
|
|
try:
|
|
|
|
cur.execute("SELECT load_extension(%s)", (self.spatialite_lib,))
|
2017-01-08 03:13:29 +08:00
|
|
|
except Exception as exc:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Unable to load the SpatiaLite library extension "%s"' % self.spatialite_lib
|
|
|
|
) from exc
|
2012-11-28 00:33:56 +08:00
|
|
|
cur.close()
|
|
|
|
return conn
|
2014-12-06 18:05:54 +08:00
|
|
|
|
|
|
|
def prepare_database(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().prepare_database()
|
2014-12-06 18:05:54 +08:00
|
|
|
# Check if spatial metadata have been initialized in the database
|
|
|
|
with self.cursor() as cursor:
|
|
|
|
cursor.execute("PRAGMA table_info(geometry_columns);")
|
|
|
|
if cursor.fetchall() == []:
|
|
|
|
arg = "1" if self.features.supports_initspatialmetadata_in_one_transaction else ""
|
|
|
|
cursor.execute("SELECT InitSpatialMetaData(%s)" % arg)
|