2017-04-07 20:08:07 +08:00
|
|
|
from django.db import NotSupportedError
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.db.models.sql import compiler
|
|
|
|
|
|
|
|
|
|
|
|
class SQLCompiler(compiler.SQLCompiler):
|
2016-10-28 23:20:23 +08:00
|
|
|
def as_sql(self, with_limits=True, with_col_aliases=False):
|
2009-12-22 23:18:51 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Create the SQL for this query. Return the SQL string and list
|
2013-07-28 09:45:25 +08:00
|
|
|
of parameters. This is overridden from the original Query class
|
2009-12-22 23:18:51 +08:00
|
|
|
to handle the additional SQL Oracle requires to emulate LIMIT
|
|
|
|
and OFFSET.
|
|
|
|
|
|
|
|
If 'with_limits' is False, any limit/offset information is not
|
|
|
|
included in the query.
|
|
|
|
"""
|
|
|
|
# The `do_offset` flag indicates whether we need to construct
|
|
|
|
# the SQL needed to use limit/offset with Oracle.
|
2016-04-04 08:37:32 +08:00
|
|
|
do_offset = with_limits and (self.query.high_mark is not None or self.query.low_mark)
|
2009-12-22 23:18:51 +08:00
|
|
|
if not do_offset:
|
2017-01-21 21:13:44 +08:00
|
|
|
sql, params = super().as_sql(with_limits=False, with_col_aliases=with_col_aliases)
|
2017-04-07 20:08:07 +08:00
|
|
|
elif not self.connection.features.supports_select_for_update_with_limit and self.query.select_for_update:
|
|
|
|
raise NotSupportedError(
|
2017-04-11 00:49:27 +08:00
|
|
|
'LIMIT/OFFSET is not supported with select_for_update on this '
|
2017-04-07 20:08:07 +08:00
|
|
|
'database backend.'
|
|
|
|
)
|
2009-12-22 23:18:51 +08:00
|
|
|
else:
|
2017-01-21 21:13:44 +08:00
|
|
|
sql, params = super().as_sql(with_limits=False, with_col_aliases=True)
|
2009-12-22 23:18:51 +08:00
|
|
|
# Wrap the base query in an outer SELECT * with boundaries on
|
|
|
|
# the "_RN" column. This is the canonical way to emulate LIMIT
|
|
|
|
# and OFFSET on Oracle.
|
|
|
|
high_where = ''
|
|
|
|
if self.query.high_mark is not None:
|
|
|
|
high_where = 'WHERE ROWNUM <= %d' % (self.query.high_mark,)
|
2016-04-20 14:56:51 +08:00
|
|
|
|
|
|
|
if self.query.low_mark:
|
|
|
|
sql = (
|
|
|
|
'SELECT * FROM (SELECT "_SUB".*, ROWNUM AS "_RN" FROM (%s) '
|
|
|
|
'"_SUB" %s) WHERE "_RN" > %d' % (sql, high_where, self.query.low_mark)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# Simplify the query to support subqueries if there's no offset.
|
|
|
|
sql = (
|
|
|
|
'SELECT * FROM (SELECT "_SUB".* FROM (%s) "_SUB" %s)' % (sql, high_where)
|
|
|
|
)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
return sql, params
|
|
|
|
|
|
|
|
|
|
|
|
class SQLInsertCompiler(compiler.SQLInsertCompiler, SQLCompiler):
|
|
|
|
pass
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class SQLDeleteCompiler(compiler.SQLDeleteCompiler, SQLCompiler):
|
|
|
|
pass
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class SQLUpdateCompiler(compiler.SQLUpdateCompiler, SQLCompiler):
|
|
|
|
pass
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class SQLAggregateCompiler(compiler.SQLAggregateCompiler, SQLCompiler):
|
|
|
|
pass
|