1 from django
.db
.models
.sql
import compiler
2 # The izip_longest was renamed to zip_longest in py3
4 from itertools
import zip_longest
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
14 if self
.query
.high_mark
is not None or self
.query
.low_mark
:
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
))
25 def as_sql(self
, with_limits
=True, with_col_aliases
=False):
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
32 If 'with_limits' is False, any limit/offset information is not
33 included in the query.
35 if with_limits
and self
.query
.low_mark
== self
.query
.high_mark
:
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
)
43 sql
, params
= super(SQLCompiler
, self
).as_sql(with_limits
=False,
44 with_col_aliases
=with_col_aliases
)
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.
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
)
60 class SQLInsertCompiler(compiler
.SQLInsertCompiler
, SQLCompiler
):
63 class SQLDeleteCompiler(compiler
.SQLDeleteCompiler
, SQLCompiler
):
66 class SQLUpdateCompiler(compiler
.SQLUpdateCompiler
, SQLCompiler
):
69 class SQLAggregateCompiler(compiler
.SQLAggregateCompiler
, SQLCompiler
):
72 class SQLDateCompiler(compiler
.SQLDateCompiler
, SQLCompiler
):