~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/contrib/gis/db/backends/spatialite/creation.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
from django.conf import settings
 
3
from django.core.exceptions import ImproperlyConfigured
 
4
from django.core.management import call_command
 
5
from django.db.backends.sqlite3.creation import DatabaseCreation
 
6
 
 
7
class SpatiaLiteCreation(DatabaseCreation):
 
8
 
 
9
    def create_test_db(self, verbosity=1, autoclobber=False):
 
10
        """
 
11
        Creates a test database, prompting the user for confirmation if the
 
12
        database already exists. Returns the name of the test database created.
 
13
 
 
14
        This method is overloaded to load up the SpatiaLite initialization
 
15
        SQL prior to calling the `syncdb` command.
 
16
        """
 
17
        if verbosity >= 1:
 
18
            print "Creating test database '%s'..." % self.connection.alias
 
19
 
 
20
        test_database_name = self._create_test_db(verbosity, autoclobber)
 
21
 
 
22
        self.connection.close()
 
23
 
 
24
        self.connection.settings_dict["NAME"] = test_database_name
 
25
        can_rollback = self._rollback_works()
 
26
        self.connection.settings_dict["SUPPORTS_TRANSACTIONS"] = can_rollback
 
27
        # Need to load the SpatiaLite initialization SQL before running `syncdb`.
 
28
        self.load_spatialite_sql()
 
29
        call_command('syncdb', verbosity=verbosity, interactive=False, database=self.connection.alias)
 
30
 
 
31
        if settings.CACHE_BACKEND.startswith('db://'):
 
32
            from django.core.cache import parse_backend_uri
 
33
            _, cache_name, _ = parse_backend_uri(settings.CACHE_BACKEND)
 
34
            call_command('createcachetable', cache_name)
 
35
 
 
36
        # Get a cursor (even though we don't need one yet). This has
 
37
        # the side effect of initializing the test database.
 
38
        cursor = self.connection.cursor()
 
39
 
 
40
        return test_database_name
 
41
 
 
42
    def sql_indexes_for_field(self, model, f, style):
 
43
        "Return any spatial index creation SQL for the field."
 
44
        from django.contrib.gis.db.models.fields import GeometryField
 
45
 
 
46
        output = super(SpatiaLiteCreation, self).sql_indexes_for_field(model, f, style)
 
47
 
 
48
        if isinstance(f, GeometryField):
 
49
            gqn = self.connection.ops.geo_quote_name
 
50
            qn = self.connection.ops.quote_name
 
51
            db_table = model._meta.db_table
 
52
 
 
53
            output.append(style.SQL_KEYWORD('SELECT ') +
 
54
                          style.SQL_TABLE('AddGeometryColumn') + '(' +
 
55
                          style.SQL_TABLE(gqn(db_table)) + ', ' +
 
56
                          style.SQL_FIELD(gqn(f.column)) + ', ' +
 
57
                          style.SQL_FIELD(str(f.srid)) + ', ' +
 
58
                          style.SQL_COLTYPE(gqn(f.geom_type)) + ', ' +
 
59
                          style.SQL_KEYWORD(str(f.dim)) + ', ' +
 
60
                          style.SQL_KEYWORD(str(int(not f.null))) +
 
61
                          ');')
 
62
 
 
63
            if f.spatial_index:
 
64
                output.append(style.SQL_KEYWORD('SELECT ') +
 
65
                              style.SQL_TABLE('CreateSpatialIndex') + '(' +
 
66
                              style.SQL_TABLE(gqn(db_table)) + ', ' +
 
67
                              style.SQL_FIELD(gqn(f.column)) + ');')
 
68
 
 
69
        return output
 
70
 
 
71
    def load_spatialite_sql(self):
 
72
        """
 
73
        This routine loads up the SpatiaLite SQL file.
 
74
        """
 
75
        # Getting the location of the SpatiaLite SQL file, and confirming
 
76
        # it exists.
 
77
        spatialite_sql = self.spatialite_init_file()
 
78
        if not os.path.isfile(spatialite_sql):
 
79
            raise ImproperlyConfigured('Could not find the required SpatiaLite initialization '
 
80
                                       'SQL file (necessary for testing): %s' % spatialite_sql)
 
81
 
 
82
        # Opening up the SpatiaLite SQL initialization file and executing
 
83
        # as a script.
 
84
        sql_fh = open(spatialite_sql, 'r')
 
85
        try:
 
86
            cur = self.connection._cursor()
 
87
            cur.executescript(sql_fh.read())
 
88
        finally:
 
89
            sql_fh.close()
 
90
 
 
91
    def spatialite_init_file(self):
 
92
        # SPATIALITE_SQL may be placed in settings to tell GeoDjango
 
93
        # to use a specific path to the SpatiaLite initilization SQL.
 
94
        return getattr(settings, 'SPATIALITE_SQL',
 
95
                       'init_spatialite-%s.%s.sql' %
 
96
                       self.connection.ops.spatial_version[:2])