~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to django/db/backends/postgresql_psycopg2/operations.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from __future__ import unicode_literals
2
2
 
 
3
from django.conf import settings
3
4
from django.db.backends import BaseDatabaseOperations
4
5
 
5
6
 
8
9
        super(DatabaseOperations, self).__init__(connection)
9
10
 
10
11
    def date_extract_sql(self, lookup_type, field_name):
11
 
        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
 
12
        # http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
12
13
        if lookup_type == 'week_day':
13
14
            # For consistency across backends, we return Sunday=1, Saturday=7.
14
15
            return "EXTRACT('dow' FROM %s) + 1" % field_name
33
34
        return '(%s)' % conn.join([sql, 'interval \'%s\'' % mods])
34
35
 
35
36
    def date_trunc_sql(self, lookup_type, field_name):
36
 
        # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
 
37
        # http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
37
38
        return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
38
39
 
 
40
    def datetime_extract_sql(self, lookup_type, field_name, tzname):
 
41
        if settings.USE_TZ:
 
42
            field_name = "%s AT TIME ZONE %%s" % field_name
 
43
            params = [tzname]
 
44
        else:
 
45
            params = []
 
46
        # http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
 
47
        if lookup_type == 'week_day':
 
48
            # For consistency across backends, we return Sunday=1, Saturday=7.
 
49
            sql = "EXTRACT('dow' FROM %s) + 1" % field_name
 
50
        else:
 
51
            sql = "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
 
52
        return sql, params
 
53
 
 
54
    def datetime_trunc_sql(self, lookup_type, field_name, tzname):
 
55
        if settings.USE_TZ:
 
56
            field_name = "%s AT TIME ZONE %%s" % field_name
 
57
            params = [tzname]
 
58
        else:
 
59
            params = []
 
60
        # http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
 
61
        sql = "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
 
62
        return sql, params
 
63
 
39
64
    def deferrable_sql(self):
40
65
        return " DEFERRABLE INITIALLY DEFERRED"
41
66
 
44
69
 
45
70
        # Cast text lookups to text to allow things like filter(x__contains=4)
46
71
        if lookup_type in ('iexact', 'contains', 'icontains', 'startswith',
47
 
                           'istartswith', 'endswith', 'iendswith'):
 
72
                           'istartswith', 'endswith', 'iendswith', 'regex', 'iregex'):
48
73
            lookup = "%s::text"
49
74
 
50
75
        # Use UPPER(x) for case-insensitive lookups; it's faster.
53
78
 
54
79
        return lookup
55
80
 
56
 
    def field_cast_sql(self, db_type):
57
 
        if db_type == 'inet':
 
81
    def field_cast_sql(self, db_type, internal_type):
 
82
        if internal_type == "GenericIPAddressField" or internal_type == "IPAddressField":
58
83
            return 'HOST(%s)'
59
84
        return '%s'
60
85
 
76
101
    def set_time_zone_sql(self):
77
102
        return "SET TIME ZONE %s"
78
103
 
79
 
    def sql_flush(self, style, tables, sequences):
 
104
    def sql_flush(self, style, tables, sequences, allow_cascade=False):
80
105
        if tables:
81
106
            # Perform a single SQL 'TRUNCATE x, y, z...;' statement.  It allows
82
107
            # us to truncate tables referenced by a foreign key in any other
83
108
            # table.
84
 
            sql = ['%s %s;' % \
85
 
                (style.SQL_KEYWORD('TRUNCATE'),
86
 
                    style.SQL_FIELD(', '.join([self.quote_name(table) for table in tables]))
87
 
            )]
 
109
            tables_sql = ', '.join(
 
110
                style.SQL_FIELD(self.quote_name(table)) for table in tables)
 
111
            if allow_cascade:
 
112
                sql = ['%s %s %s;' % (
 
113
                    style.SQL_KEYWORD('TRUNCATE'),
 
114
                    tables_sql,
 
115
                    style.SQL_KEYWORD('CASCADE'),
 
116
                )]
 
117
            else:
 
118
                sql = ['%s %s;' % (
 
119
                    style.SQL_KEYWORD('TRUNCATE'),
 
120
                    tables_sql,
 
121
                )]
88
122
            sql.extend(self.sequence_reset_by_name_sql(style, sequences))
89
123
            return sql
90
124
        else:
150
184
                        style.SQL_TABLE(qn(f.m2m_db_table()))))
151
185
        return output
152
186
 
153
 
    def savepoint_create_sql(self, sid):
154
 
        return "SAVEPOINT %s" % sid
155
 
 
156
 
    def savepoint_commit_sql(self, sid):
157
 
        return "RELEASE SAVEPOINT %s" % sid
158
 
 
159
 
    def savepoint_rollback_sql(self, sid):
160
 
        return "ROLLBACK TO SAVEPOINT %s" % sid
161
 
 
162
187
    def prep_for_iexact_query(self, x):
163
188
        return x
164
189
 
165
 
    def check_aggregate_support(self, aggregate):
166
 
        """Check that the backend fully supports the provided aggregate.
167
 
 
168
 
        The implementation of population statistics (STDDEV_POP and VAR_POP)
169
 
        under Postgres 8.2 - 8.2.4 is known to be faulty. Raise
170
 
        NotImplementedError if this is the database in use.
171
 
        """
172
 
        if aggregate.sql_function in ('STDDEV_POP', 'VAR_POP'):
173
 
            pg_version = self.connection.pg_version
174
 
            if pg_version >= 80200 and pg_version <= 80204:
175
 
                raise NotImplementedError('PostgreSQL 8.2 to 8.2.4 is known to have a faulty implementation of %s. Please upgrade your version of PostgreSQL.' % aggregate.sql_function)
176
 
 
177
190
    def max_name_length(self):
178
191
        """
179
192
        Returns the maximum length of an identifier.