Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compiler.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from django.db import NotSupportedError
  2. from django.db.models.sql import compiler
  3. class SQLCompiler(compiler.SQLCompiler):
  4. def as_sql(self, with_limits=True, with_col_aliases=False):
  5. """
  6. Create the SQL for this query. Return the SQL string and list of
  7. parameters. This is overridden from the original Query class to handle
  8. the restriction in Oracle 12.1 and emulate LIMIT and OFFSET with
  9. a subquery.
  10. If 'with_limits' is False, any limit/offset information is not included
  11. in the query.
  12. """
  13. # Whether the query must be constructed using limit/offset.
  14. do_offset = with_limits and (self.query.high_mark is not None or self.query.low_mark)
  15. if not do_offset:
  16. sql, params = super().as_sql(with_limits=False, with_col_aliases=with_col_aliases)
  17. elif not self.connection.features.supports_select_for_update_with_limit and self.query.select_for_update:
  18. raise NotSupportedError(
  19. 'LIMIT/OFFSET is not supported with select_for_update on this '
  20. 'database backend.'
  21. )
  22. else:
  23. sql, params = super().as_sql(with_limits=False, with_col_aliases=True)
  24. # Wrap the base query in an outer SELECT * with boundaries on
  25. # the "_RN" column. This is the canonical way to emulate LIMIT
  26. # and OFFSET on Oracle.
  27. high_where = ''
  28. if self.query.high_mark is not None:
  29. high_where = 'WHERE ROWNUM <= %d' % (self.query.high_mark,)
  30. if self.query.low_mark:
  31. sql = (
  32. 'SELECT * FROM (SELECT "_SUB".*, ROWNUM AS "_RN" FROM (%s) '
  33. '"_SUB" %s) WHERE "_RN" > %d' % (sql, high_where, self.query.low_mark)
  34. )
  35. else:
  36. # Simplify the query to support subqueries if there's no offset.
  37. sql = (
  38. 'SELECT * FROM (SELECT "_SUB".* FROM (%s) "_SUB" %s)' % (sql, high_where)
  39. )
  40. return sql, params
  41. class SQLInsertCompiler(compiler.SQLInsertCompiler, SQLCompiler):
  42. pass
  43. class SQLDeleteCompiler(compiler.SQLDeleteCompiler, SQLCompiler):
  44. pass
  45. class SQLUpdateCompiler(compiler.SQLUpdateCompiler, SQLCompiler):
  46. pass
  47. class SQLAggregateCompiler(compiler.SQLAggregateCompiler, SQLCompiler):
  48. pass