PageRenderTime 32ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/db/backends/spatialite/base.py

https://code.google.com/p/mango-py/
Python | 79 lines | 60 code | 7 blank | 12 comment | 9 complexity | 12184437bed945ee9203338bf510e46a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from ctypes.util import find_library
  2. from django.conf import settings
  3. from django.core.exceptions import ImproperlyConfigured
  4. from django.db.backends.sqlite3.base import (
  5. _sqlite_extract, _sqlite_date_trunc, _sqlite_regexp, _sqlite_format_dtdelta,
  6. connection_created, Database, DatabaseWrapper as SQLiteDatabaseWrapper,
  7. SQLiteCursorWrapper)
  8. from django.contrib.gis.db.backends.spatialite.client import SpatiaLiteClient
  9. from django.contrib.gis.db.backends.spatialite.creation import SpatiaLiteCreation
  10. from django.contrib.gis.db.backends.spatialite.introspection import SpatiaLiteIntrospection
  11. from django.contrib.gis.db.backends.spatialite.operations import SpatiaLiteOperations
  12. class DatabaseWrapper(SQLiteDatabaseWrapper):
  13. def __init__(self, *args, **kwargs):
  14. # Before we get too far, make sure pysqlite 2.5+ is installed.
  15. if Database.version_info < (2, 5, 0):
  16. raise ImproperlyConfigured('Only versions of pysqlite 2.5+ are '
  17. 'compatible with SpatiaLite and GeoDjango.')
  18. # Trying to find the location of the SpatiaLite library.
  19. # Here we are figuring out the path to the SpatiaLite library
  20. # (`libspatialite`). If it's not in the system library path (e.g., it
  21. # cannot be found by `ctypes.util.find_library`), then it may be set
  22. # manually in the settings via the `SPATIALITE_LIBRARY_PATH` setting.
  23. self.spatialite_lib = getattr(settings, 'SPATIALITE_LIBRARY_PATH',
  24. find_library('spatialite'))
  25. if not self.spatialite_lib:
  26. raise ImproperlyConfigured('Unable to locate the SpatiaLite library. '
  27. 'Make sure it is in your library path, or set '
  28. 'SPATIALITE_LIBRARY_PATH in your settings.'
  29. )
  30. super(DatabaseWrapper, self).__init__(*args, **kwargs)
  31. self.ops = SpatiaLiteOperations(self)
  32. self.client = SpatiaLiteClient(self)
  33. self.creation = SpatiaLiteCreation(self)
  34. self.introspection = SpatiaLiteIntrospection(self)
  35. def _cursor(self):
  36. if self.connection is None:
  37. ## The following is the same as in django.db.backends.sqlite3.base ##
  38. settings_dict = self.settings_dict
  39. if not settings_dict['NAME']:
  40. raise ImproperlyConfigured("Please fill out the database NAME in the settings module before using the database.")
  41. kwargs = {
  42. 'database': settings_dict['NAME'],
  43. 'detect_types': Database.PARSE_DECLTYPES | Database.PARSE_COLNAMES,
  44. }
  45. kwargs.update(settings_dict['OPTIONS'])
  46. self.connection = Database.connect(**kwargs)
  47. # Register extract, date_trunc, and regexp functions.
  48. self.connection.create_function("django_extract", 2, _sqlite_extract)
  49. self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
  50. self.connection.create_function("regexp", 2, _sqlite_regexp)
  51. self.connection.create_function("django_format_dtdelta", 5, _sqlite_format_dtdelta)
  52. connection_created.send(sender=self.__class__, connection=self)
  53. ## From here on, customized for GeoDjango ##
  54. # Enabling extension loading on the SQLite connection.
  55. try:
  56. self.connection.enable_load_extension(True)
  57. except AttributeError:
  58. raise ImproperlyConfigured('The pysqlite library does not support C extension loading. '
  59. 'Both SQLite and pysqlite must be configured to allow '
  60. 'the loading of extensions to use SpatiaLite.'
  61. )
  62. # Loading the SpatiaLite library extension on the connection, and returning
  63. # the created cursor.
  64. cur = self.connection.cursor(factory=SQLiteCursorWrapper)
  65. try:
  66. cur.execute("SELECT load_extension(%s)", (self.spatialite_lib,))
  67. except Exception, msg:
  68. raise ImproperlyConfigured('Unable to load the SpatiaLite library extension '
  69. '"%s" because: %s' % (self.spatialite_lib, msg))
  70. return cur
  71. else:
  72. return self.connection.cursor(factory=SQLiteCursorWrapper)