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

« back to all changes in this revision

Viewing changes to django/db/backends/oracle/query.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2008-11-15 19:15:33 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20081115191533-xbt1ut2xf4fvwtvc
Tags: 1.0.1-0ubuntu1
* New upstream release:
  - Bug fixes.

* The tests/ sub-directory appaers to have been dropped upstream, so pull
  our patch to workaround the tests and modify the rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
        pass
26
26
 
27
27
    class OracleQuery(QueryClass):
 
28
        def __reduce__(self):
 
29
            """
 
30
            Enable pickling for this class (normal pickling handling doesn't
 
31
            work as Python can only pickle module-level classes by default).
 
32
            """
 
33
            if hasattr(QueryClass, '__getstate__'):
 
34
                assert hasattr(QueryClass, '__setstate__')
 
35
                data = self.__getstate__()
 
36
            else:
 
37
                data = self.__dict__
 
38
            return (unpickle_query_class, (QueryClass,), data)
 
39
 
28
40
        def resolve_columns(self, row, fields=()):
29
 
            index_start = len(self.extra_select.keys())
30
 
            values = [self.convert_values(v, None) for v in row[:index_start]]
 
41
            # If this query has limit/offset information, then we expect the
 
42
            # first column to be an extra "_RN" column that we need to throw
 
43
            # away.
 
44
            if self.high_mark is not None or self.low_mark:
 
45
                rn_offset = 1
 
46
            else:
 
47
                rn_offset = 0
 
48
            index_start = rn_offset + len(self.extra_select.keys())
 
49
            values = [self.convert_values(v, None)
 
50
                      for v in row[rn_offset:index_start]]
31
51
            for value, field in map(None, row[index_start:], fields):
32
52
                values.append(self.convert_values(value, field))
33
53
            return values
97
117
                sql, params = super(OracleQuery, self).as_sql(with_limits=False,
98
118
                        with_col_aliases=with_col_aliases)
99
119
            else:
100
 
                # `get_columns` needs to be called before `get_ordering` to
101
 
                # populate `_select_alias`.
102
 
                self.pre_sql_setup()
103
 
                self.get_columns()
104
 
                ordering = self.get_ordering()
105
 
 
106
 
                # Oracle's ROW_NUMBER() function requires an ORDER BY clause.
107
 
                if ordering:
108
 
                    rn_orderby = ', '.join(ordering)
109
 
                else:
110
 
                    # Create a default ORDER BY since none was specified.
111
 
                    qn = self.quote_name_unless_alias
112
 
                    opts = self.model._meta
113
 
                    rn_orderby = '%s.%s' % (qn(opts.db_table),
114
 
                        qn(opts.fields[0].db_column or opts.fields[0].column))
115
 
 
116
 
                # Ensure the base query SELECTs our special "_RN" column
117
 
                self.extra_select['_RN'] = ('ROW_NUMBER() OVER (ORDER BY %s)'
118
 
                                            % rn_orderby, '')
119
120
                sql, params = super(OracleQuery, self).as_sql(with_limits=False,
120
121
                                                        with_col_aliases=True)
121
122
 
122
123
                # Wrap the base query in an outer SELECT * with boundaries on
123
124
                # the "_RN" column.  This is the canonical way to emulate LIMIT
124
125
                # and OFFSET on Oracle.
125
 
                sql = 'SELECT * FROM (%s) WHERE "_RN" > %d' % (sql, self.low_mark)
 
126
                high_where = ''
126
127
                if self.high_mark is not None:
127
 
                    sql = '%s AND "_RN" <= %d' % (sql, self.high_mark)
 
128
                    high_where = 'WHERE ROWNUM <= %d' % (self.high_mark,)
 
129
                sql = 'SELECT * FROM (SELECT ROWNUM AS "_RN", "_SUB".* FROM (%s) "_SUB" %s) WHERE "_RN" > %d' % (sql, high_where, self.low_mark)
128
130
 
129
131
            return sql, params
130
132
 
131
 
        def set_limits(self, low=None, high=None):
132
 
            super(OracleQuery, self).set_limits(low, high)
133
 
            # We need to select the row number for the LIMIT/OFFSET sql.
134
 
            # A placeholder is added to extra_select now, because as_sql is
135
 
            # too late to be modifying extra_select.  However, the actual sql
136
 
            # depends on the ordering, so that is generated in as_sql.
137
 
            self.extra_select['_RN'] = ('1', '')
138
 
 
139
 
        def clear_limits(self):
140
 
            super(OracleQuery, self).clear_limits()
141
 
            if '_RN' in self.extra_select:
142
 
                del self.extra_select['_RN']
143
 
 
144
133
    _classes[QueryClass] = OracleQuery
145
134
    return OracleQuery
 
135
 
 
136
def unpickle_query_class(QueryClass):
 
137
    """
 
138
    Utility function, called by Python's unpickling machinery, that handles
 
139
    unpickling of Oracle Query subclasses.
 
140
    """
 
141
    # XXX: Would be nice to not have any dependency on cx_Oracle here. Since
 
142
    # modules can't be pickled, we need a way to know to load the right module.
 
143
    import cx_Oracle
 
144
 
 
145
    klass = query_class(QueryClass, cx_Oracle)
 
146
    return klass.__new__(klass)
 
147
unpickle_query_class.__safe_for_unpickling__ = True
 
148