| 21 | self.field_replace_dict = field_replace_dict |
| 22 | |
| 23 | def get_query_str(self, with_limits=True, with_table_name=False, with_col_aliases=False): |
| 24 | refcounts_before = self.query.alias_refcount.copy() |
| 25 | try: |
| 26 | combinator = self.query.combinator |
| 27 | extra_select, order_by, group_by = self.pre_sql_setup( |
| 28 | with_col_aliases=with_col_aliases or bool(combinator), |
| 29 | ) |
| 30 | for_update_part = None |
| 31 | # Is a LIMIT/OFFSET clause needed? |
| 32 | with_limit_offset = with_limits and self.query.is_sliced |
| 33 | combinator = self.query.combinator |
| 34 | features = self.connection.features |
| 35 | if combinator: |
| 36 | if not getattr(features, "supports_select_{}".format(combinator)): |
| 37 | raise NotSupportedError( |
| 38 | "{} is not supported on this database backend.".format( |
| 39 | combinator |
| 40 | ) |
| 41 | ) |
| 42 | result, params = self.get_combinator_sql( |
| 43 | combinator, self.query.combinator_all |
| 44 | ) |
| 45 | elif self.qualify: |
| 46 | result, params = self.get_qualify_sql() |
| 47 | order_by = None |
| 48 | else: |
| 49 | distinct_fields, distinct_params = self.get_distinct() |
| 50 | # This must come after 'select', 'ordering', and 'distinct' |
| 51 | # (see docstring of get_from_clause() for details). |
| 52 | from_, f_params = self.get_from_clause() |
| 53 | try: |
| 54 | where, w_params = ( |
| 55 | self.compile(self.where) if self.where is not None else ("", []) |
| 56 | ) |
| 57 | except EmptyResultSet: |
| 58 | if self.elide_empty: |
| 59 | raise |
| 60 | # Use a predicate that's always False. |
| 61 | where, w_params = "0 = 1", [] |
| 62 | except FullResultSet: |
| 63 | where, w_params = "", [] |
| 64 | try: |
| 65 | having, h_params = ( |
| 66 | self.compile(self.having) |
| 67 | if self.having is not None |
| 68 | else ("", []) |
| 69 | ) |
| 70 | except FullResultSet: |
| 71 | having, h_params = "", [] |
| 72 | result = [] |
| 73 | params = [] |
| 74 | |
| 75 | if self.query.distinct: |
| 76 | distinct_result, distinct_params = self.connection.ops.distinct_sql( |
| 77 | distinct_fields, |
| 78 | distinct_params, |
| 79 | ) |
| 80 | result += distinct_result |