MCPcopy
hub / github.com/django/django / as_sql

Method as_sql

django/db/models/sql/compiler.py:756–981  ·  view source on GitHub ↗

Create the SQL for this query. Return the SQL string and list of parameters. If 'with_limits' is False, any limit/offset information is not included in the query.

(self, with_limits=True, with_col_aliases=False)

Source from the content-addressed store, hash-verified

754 return result, params
755
756 def as_sql(self, with_limits=True, with_col_aliases=False):
757 """
758 Create the SQL for this query. Return the SQL string and list of
759 parameters.
760
761 If 'with_limits' is False, any limit/offset information is not included
762 in the query.
763 """
764 refcounts_before = self.query.alias_refcount.copy()
765 try:
766 combinator = self.query.combinator
767 extra_select, order_by, group_by = self.pre_sql_setup(
768 with_col_aliases=with_col_aliases or bool(combinator),
769 )
770 for_update_part = None
771 # Is a LIMIT/OFFSET clause needed?
772 with_limit_offset = with_limits and self.query.is_sliced
773 combinator = self.query.combinator
774 features = self.connection.features
775 if combinator:
776 if not getattr(features, "supports_select_{}".format(combinator)):
777 raise NotSupportedError(
778 "{} is not supported on this database backend.".format(
779 combinator
780 )
781 )
782 result, params = self.get_combinator_sql(
783 combinator, self.query.combinator_all
784 )
785 elif self.qualify:
786 result, params = self.get_qualify_sql()
787 order_by = None
788 else:
789 distinct_fields, distinct_params = self.get_distinct()
790 # This must come after 'select', 'ordering', and 'distinct'
791 # (see docstring of get_from_clause() for details).
792 from_, f_params = self.get_from_clause()
793 try:
794 where, w_params = (
795 self.compile(self.where) if self.where is not None else ("", [])
796 )
797 except EmptyResultSet:
798 if self.elide_empty:
799 raise
800 # Use a predicate that's always False.
801 where, w_params = "0 = 1", []
802 except FullResultSet:
803 where, w_params = "", []
804 try:
805 having, h_params = (
806 self.compile(self.having)
807 if self.having is not None
808 else ("", [])
809 )
810 except FullResultSet:
811 having, h_params = "", []
812 result = ["SELECT"]
813 params = []

Callers 1

execute_sqlMethod · 0.95

Calls 15

pre_sql_setupMethod · 0.95
get_combinator_sqlMethod · 0.95
get_qualify_sqlMethod · 0.95
get_distinctMethod · 0.95
get_from_clauseMethod · 0.95
compileMethod · 0.95
NotSupportedErrorClass · 0.90
extendMethod · 0.80
get_autocommitMethod · 0.80
for_update_sqlMethod · 0.80

Tested by

no test coverage detected