App Engine Python SDK version 1.8.0
[gae.git] / python / lib / django-1.5 / django / db / backends / oracle / compiler.py
blob24030cdffc3de76f9156bf27fb233029d523cef1
1 from django.db.models.sql import compiler
2 # The izip_longest was renamed to zip_longest in py3
3 try:
4 from itertools import zip_longest
5 except ImportError:
6 from itertools import izip_longest as zip_longest
9 class SQLCompiler(compiler.SQLCompiler):
10 def resolve_columns(self, row, fields=()):
11 # If this query has limit/offset information, then we expect the
12 # first column to be an extra "_RN" column that we need to throw
13 # away.
14 if self.query.high_mark is not None or self.query.low_mark:
15 rn_offset = 1
16 else:
17 rn_offset = 0
18 index_start = rn_offset + len(self.query.extra_select)
19 values = [self.query.convert_values(v, None, connection=self.connection)
20 for v in row[rn_offset:index_start]]
21 for value, field in zip_longest(row[index_start:], fields):
22 values.append(self.query.convert_values(value, field, connection=self.connection))
23 return tuple(values)
25 def as_sql(self, with_limits=True, with_col_aliases=False):
26 """
27 Creates the SQL for this query. Returns the SQL string and list
28 of parameters. This is overriden from the original Query class
29 to handle the additional SQL Oracle requires to emulate LIMIT
30 and OFFSET.
32 If 'with_limits' is False, any limit/offset information is not
33 included in the query.
34 """
35 if with_limits and self.query.low_mark == self.query.high_mark:
36 return '', ()
38 # The `do_offset` flag indicates whether we need to construct
39 # the SQL needed to use limit/offset with Oracle.
40 do_offset = with_limits and (self.query.high_mark is not None
41 or self.query.low_mark)
42 if not do_offset:
43 sql, params = super(SQLCompiler, self).as_sql(with_limits=False,
44 with_col_aliases=with_col_aliases)
45 else:
46 sql, params = super(SQLCompiler, self).as_sql(with_limits=False,
47 with_col_aliases=True)
49 # Wrap the base query in an outer SELECT * with boundaries on
50 # the "_RN" column. This is the canonical way to emulate LIMIT
51 # and OFFSET on Oracle.
52 high_where = ''
53 if self.query.high_mark is not None:
54 high_where = 'WHERE ROWNUM <= %d' % (self.query.high_mark,)
55 sql = 'SELECT * FROM (SELECT ROWNUM AS "_RN", "_SUB".* FROM (%s) "_SUB" %s) WHERE "_RN" > %d' % (sql, high_where, self.query.low_mark)
57 return sql, params
60 class SQLInsertCompiler(compiler.SQLInsertCompiler, SQLCompiler):
61 pass
63 class SQLDeleteCompiler(compiler.SQLDeleteCompiler, SQLCompiler):
64 pass
66 class SQLUpdateCompiler(compiler.SQLUpdateCompiler, SQLCompiler):
67 pass
69 class SQLAggregateCompiler(compiler.SQLAggregateCompiler, SQLCompiler):
70 pass
72 class SQLDateCompiler(compiler.SQLDateCompiler, SQLCompiler):
73 pass