PageRenderTime 97ms CodeModel.GetById 86ms app.highlight 4ms RepoModel.GetById 5ms app.codeStats 0ms

/django/contrib/gis/db/backends/postgis/creation.py

https://code.google.com/p/mango-py/
Python | 60 lines | 46 code | 8 blank | 6 comment | 6 complexity | 957c7e5ebd6bac2b4e6984cf6b785a6f MD5 | raw file
 1from django.conf import settings
 2from django.db.backends.postgresql.creation import DatabaseCreation
 3
 4class PostGISCreation(DatabaseCreation):
 5    geom_index_type = 'GIST'
 6    geom_index_opts = 'GIST_GEOMETRY_OPS'
 7
 8    def sql_indexes_for_field(self, model, f, style):
 9        "Return any spatial index creation SQL for the field."
10        from django.contrib.gis.db.models.fields import GeometryField
11
12        output = super(PostGISCreation, self).sql_indexes_for_field(model, f, style)
13
14        if isinstance(f, GeometryField):
15            gqn = self.connection.ops.geo_quote_name
16            qn = self.connection.ops.quote_name
17            db_table = model._meta.db_table
18
19            if f.geography:
20                # Geogrophy columns are created normally.
21                pass
22            else:
23                # Geometry columns are created by `AddGeometryColumn`
24                # stored procedure.
25                output.append(style.SQL_KEYWORD('SELECT ') +
26                              style.SQL_TABLE('AddGeometryColumn') + '(' +
27                              style.SQL_TABLE(gqn(db_table)) + ', ' +
28                              style.SQL_FIELD(gqn(f.column)) + ', ' +
29                              style.SQL_FIELD(str(f.srid)) + ', ' +
30                              style.SQL_COLTYPE(gqn(f.geom_type)) + ', ' +
31                              style.SQL_KEYWORD(str(f.dim)) + ');')
32
33                if not f.null:
34                    # Add a NOT NULL constraint to the field
35                    output.append(style.SQL_KEYWORD('ALTER TABLE ') +
36                                  style.SQL_TABLE(qn(db_table)) +
37                                  style.SQL_KEYWORD(' ALTER ') +
38                                  style.SQL_FIELD(qn(f.column)) +
39                                  style.SQL_KEYWORD(' SET NOT NULL') + ';')
40
41
42            if f.spatial_index:
43                # Spatial indexes created the same way for both Geometry and
44                # Geography columns
45                if f.geography:
46                    index_opts = ''
47                else:
48                    index_opts = ' ' + style.SQL_KEYWORD(self.geom_index_opts)
49                output.append(style.SQL_KEYWORD('CREATE INDEX ') +
50                              style.SQL_TABLE(qn('%s_%s_id' % (db_table, f.column))) +
51                              style.SQL_KEYWORD(' ON ') +
52                              style.SQL_TABLE(qn(db_table)) +
53                              style.SQL_KEYWORD(' USING ') +
54                              style.SQL_COLTYPE(self.geom_index_type) + ' ( ' +
55                              style.SQL_FIELD(qn(f.column)) + index_opts + ' );')
56        return output
57
58    def sql_table_creation_suffix(self):
59        qn = self.connection.ops.quote_name
60        return ' TEMPLATE %s' % qn(getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis'))