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-09-12 11:27:29 +08:00
|
|
|
DatabaseWrapper as SQLiteDatabaseWrapper,
|
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.
|
2018-06-02 00:26:14 +08:00
|
|
|
self.lib_spatialite_paths = [name for name in [
|
|
|
|
getattr(settings, 'SPATIALITE_LIBRARY_PATH', None),
|
|
|
|
'mod_spatialite.so',
|
|
|
|
'mod_spatialite',
|
|
|
|
find_library('spatialite'),
|
|
|
|
] if name is not None]
|
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.'
|
|
|
|
)
|
2017-09-12 11:27:29 +08:00
|
|
|
# Load the SpatiaLite library extension on the connection.
|
2018-06-02 00:26:14 +08:00
|
|
|
for path in self.lib_spatialite_paths:
|
|
|
|
try:
|
|
|
|
conn.load_extension(path)
|
|
|
|
except Exception:
|
|
|
|
if getattr(settings, 'SPATIALITE_LIBRARY_PATH', None):
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
'Unable to load the SpatiaLite library extension '
|
|
|
|
'as specified in your SPATIALITE_LIBRARY_PATH setting.'
|
|
|
|
)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
else:
|
2017-01-08 03:13:29 +08:00
|
|
|
raise ImproperlyConfigured(
|
2018-06-02 00:26:14 +08:00
|
|
|
'Unable to load the SpatiaLite library extension. '
|
|
|
|
'Library names tried: %s' % ', '.join(self.lib_spatialite_paths)
|
|
|
|
)
|
2012-11-28 00:33:56 +08:00
|
|
|
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() == []:
|
2017-10-01 02:13:18 +08:00
|
|
|
cursor.execute("SELECT InitSpatialMetaData(1)")
|