Ticket #11003: with-hints-13402.diff

File with-hints-13402.diff, 3.0 KB (added by Simon Litchfield, 14 years ago)

Adds queryset.with_hints() support for MySQL

  • django/db/models/sql/compiler.py

     
    441441                continue
    442442            alias_str = (alias != name and ' %s' % alias or '')
    443443            if join_type and not first:
    444                 result.append('%s %s%s ON (%s.%s = %s.%s)'
     444                part ='%s %s%s ON (%s.%s = %s.%s)' \
    445445                        % (join_type, qn(name), alias_str, qn(lhs),
    446                            qn2(lhs_col), qn(alias), qn2(col)))
     446                           qn2(lhs_col), qn(alias), qn2(col))
    447447            else:
    448448                connector = not first and ', ' or ''
    449                 result.append('%s%s%s' % (connector, qn(name), alias_str))
     449                part = '%s%s%s' % (connector, qn(name), alias_str)
     450            for model, hint in self.query.hints.items():
     451                if model._meta.db_table == name:
     452                    part += ' USE INDEX (%s)' % ', '.join(hint)
     453            result.append(part)
    450454            first = False
    451455        for t in self.query.extra_tables:
    452456            alias, unused = self.query.table_alias(t)
  • django/db/models/sql/query.py

     
    133133        self.distinct = False
    134134        self.select_related = False
    135135        self.related_select_cols = []
     136        self.hints = {}
    136137
    137138        # SQL aggregate-related attributes
    138139        self.aggregates = SortedDict() # Maps alias -> SQL aggregate function
     
    284285            obj._extra_select_cache = self._extra_select_cache.copy()
    285286        obj.extra_tables = self.extra_tables
    286287        obj.extra_order_by = self.extra_order_by
     288        obj.hints = self.hints
    287289        obj.deferred_loading = deepcopy(self.deferred_loading, memo=memo)
    288290        if self.filter_is_sticky and self.used_aliases:
    289291            obj.used_aliases = self.used_aliases.copy()
     
    16411643        if order_by:
    16421644            self.extra_order_by = order_by
    16431645
     1646    def add_hint(self, model, hint):
     1647        add_to_dict(self.hints, model, hint)
     1648           
    16441649    def clear_deferred_loading(self):
    16451650        """
    16461651        Remove any fields from the deferred loading set.
  • django/db/models/query.py

     
    708708        clone._db = alias
    709709        return clone
    710710
     711    def with_hints(self, *args, **kwargs):
     712        clone = self._clone()
     713        for hint in args:
     714            clone.query.add_hint(self.model, hint)
     715        for model, hint in kwargs.items():
     716            clone.query.add_hint(model, hint)
     717        return clone
     718       
    711719    ###################################
    712720    # PUBLIC INTROSPECTION ATTRIBUTES #
    713721    ###################################
Back to Top