27
27
class OracleQuery(QueryClass):
30
Enable pickling for this class (normal pickling handling doesn't
31
work as Python can only pickle module-level classes by default).
33
if hasattr(QueryClass, '__getstate__'):
34
assert hasattr(QueryClass, '__setstate__')
35
data = self.__getstate__()
38
return (unpickle_query_class, (QueryClass,), data)
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
44
if self.high_mark is not None or self.low_mark:
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))
97
117
sql, params = super(OracleQuery, self).as_sql(with_limits=False,
98
118
with_col_aliases=with_col_aliases)
100
# `get_columns` needs to be called before `get_ordering` to
101
# populate `_select_alias`.
104
ordering = self.get_ordering()
106
# Oracle's ROW_NUMBER() function requires an ORDER BY clause.
108
rn_orderby = ', '.join(ordering)
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))
116
# Ensure the base query SELECTs our special "_RN" column
117
self.extra_select['_RN'] = ('ROW_NUMBER() OVER (ORDER BY %s)'
119
120
sql, params = super(OracleQuery, self).as_sql(with_limits=False,
120
121
with_col_aliases=True)
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
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)
129
131
return sql, params
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', '')
139
def clear_limits(self):
140
super(OracleQuery, self).clear_limits()
141
if '_RN' in self.extra_select:
142
del self.extra_select['_RN']
144
133
_classes[QueryClass] = OracleQuery
145
134
return OracleQuery
136
def unpickle_query_class(QueryClass):
138
Utility function, called by Python's unpickling machinery, that handles
139
unpickling of Oracle Query subclasses.
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.
145
klass = query_class(QueryClass, cx_Oracle)
146
return klass.__new__(klass)
147
unpickle_query_class.__safe_for_unpickling__ = True