Ticket #7258: connection_in_sqlqueries.diff

File connection_in_sqlqueries.diff, 8.2 KB (added by Koen Biermans <koen.biermans@…>, 17 years ago)

patch to pass connection from Query into WhereNode

  • where.py

     
    44import datetime
    55
    66from django.utils import tree
    7 from django.db import connection
    87from django.db.models.fields import Field
    98from django.db.models.query_utils import QueryWrapper
    109from datastructures import EmptyResultSet, FullResultSet
    1110
     11from copy import deepcopy
     12
    1213# Connection types
    1314AND = 'AND'
    1415OR = 'OR'
     
    2627    """
    2728    default = AND
    2829
     30    def __init__(self, connection, *args, **kwargs):
     31        self.connection = connection
     32        super(WhereNode, self).__init__(*args, **kwargs)
     33
     34    def __deepcopy__(self, memo):
     35        x = WhereNode.__new__(WhereNode)
     36        memo[id(self)] = x
     37        for n, v in self.__dict__.iteritems():
     38            if n != 'connection':
     39                setattr(x, n, deepcopy(v, memo))
     40            else:
     41                setattr(x, n, self.connection)
     42        return x
     43
    2944    def as_sql(self, node=None, qn=None):
    3045        """
    3146        Returns the SQL version of the where clause and the value to be
     
    3853        if node is None:
    3954            node = self
    4055        if not qn:
    41             qn = connection.ops.quote_name
     56            qn = self.connection.ops.quote_name
    4257        if not node.children:
    4358            return None, []
    4459        result = []
     
    99114            lhs = '%s.%s' % (qn(table_alias), qn(name))
    100115        else:
    101116            lhs = qn(name)
     117
    102118        db_type = field and field.db_type() or None
    103         field_sql = connection.ops.field_cast_sql(db_type) % lhs
    104 
     119        field_sql = self.connection.ops.field_cast_sql(db_type) % lhs
     120       
    105121        if isinstance(value, datetime.datetime):
    106             cast_sql = connection.ops.datetime_cast_sql()
     122            cast_sql = self.connection.ops.datetime_cast_sql()
    107123        else:
    108124            cast_sql = '%s'
    109125
     
    116132        else:
    117133            extra = ''
    118134
    119         if lookup_type in connection.operators:
    120             format = "%s %%s %s" % (connection.ops.lookup_cast(lookup_type),
     135        if lookup_type in self.connection.operators:
     136            format = "%s %%s %s" % (self.connection.ops.lookup_cast(lookup_type),
    121137                    extra)
    122138            return (format % (field_sql,
    123                     connection.operators[lookup_type] % cast_sql), params)
     139                    self.connection.operators[lookup_type] % cast_sql), params)
    124140
    125141        if lookup_type == 'in':
    126142            if not value:
     
    132148        elif lookup_type in ('range', 'year'):
    133149            return ('%s BETWEEN %%s and %%s' % field_sql, params)
    134150        elif lookup_type in ('month', 'day'):
    135             return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type,
     151            return ('%s = %%s' % self.connection.ops.date_extract_sql(lookup_type,
    136152                    field_sql), params)
    137153        elif lookup_type == 'isnull':
    138154            return ('%s IS %sNULL' % (field_sql, (not value and 'NOT ' or '')),
    139155                    params)
    140156        elif lookup_type == 'search':
    141             return (connection.ops.fulltext_search_sql(field_sql), params)
     157            return (self.connection.ops.fulltext_search_sql(field_sql), params)
    142158        elif lookup_type in ('regex', 'iregex'):
    143             return connection.ops.regex_lookup(lookup_type) % (field_sql, cast_sql), params
     159            return self.connection.ops.regex_lookup(lookup_type) % (field_sql, cast_sql), params
    144160
    145161        raise TypeError('Invalid lookup_type: %r' % lookup_type)
    146162
  • query.py

     
    6060        # SQL-related attributes
    6161        self.select = []
    6262        self.tables = []    # Aliases in the order they are created.
    63         self.where = where()
     63        self.where = where(self.connection)
    6464        self.where_class = where
    6565        self.group_by = []
    6666        self.having = []
     
    215215        obj.related_select_cols = []
    216216        obj.related_select_fields = []
    217217        if obj.distinct and len(obj.select) > 1:
    218             obj = self.clone(CountQuery, _query=obj, where=self.where_class(),
     218            obj = self.clone(CountQuery, _query=obj, where=self.where_class(self.connection),
    219219                    distinct=False)
    220220            obj.select = []
    221221            obj.extra_select = {}
     
    346346                self.where.add(EverythingNode(), AND)
    347347        elif self.where:
    348348            # rhs has an empty where clause.
    349             w = self.where_class()
     349            w = self.where_class(self.connection)
    350350            w.add(EverythingNode(), AND)
    351351        else:
    352             w = self.where_class()
     352            w = self.where_class(self.connection)
    353353        self.where.add(w, connector)
    354354
    355355        # Selection columns and extra extensions are those provided by 'rhs'.
  • subqueries.py

     
    4747        for related in cls._meta.get_all_related_many_to_many_objects():
    4848            if not isinstance(related.field, generic.GenericRelation):
    4949                for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
    50                     where = self.where_class()
     50                    where = self.where_class(self.connection)
    5151                    where.add((None, related.field.m2m_reverse_name(),
    5252                            related.field, 'in',
    5353                            pk_list[offset : offset+GET_ITERATOR_CHUNK_SIZE]),
     
    5555                    self.do_query(related.field.m2m_db_table(), where)
    5656
    5757        for f in cls._meta.many_to_many:
    58             w1 = self.where_class()
     58            w1 = self.where_class(self.connection)
    5959            if isinstance(f, generic.GenericRelation):
    6060                from django.contrib.contenttypes.models import ContentType
    6161                field = f.rel.to._meta.get_field(f.content_type_field_name)
    6262                w1.add((None, field.column, field, 'exact',
    6363                        ContentType.objects.get_for_model(cls).id), AND)
    6464            for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
    65                 where = self.where_class()
     65                where = self.where_class(self.connection)
    6666                where.add((None, f.m2m_column_name(), f, 'in',
    6767                        pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]),
    6868                        AND)
     
    7979        lot of values in pk_list.
    8080        """
    8181        for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
    82             where = self.where_class()
     82            where = self.where_class(self.connection)
    8383            field = self.model._meta.pk
    8484            where.add((None, field.column, field, 'in',
    8585                    pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]), AND)
     
    178178
    179179        # Now we adjust the current query: reset the where clause and get rid
    180180        # of all the tables we don't need (since they're in the sub-select).
    181         self.where = self.where_class()
     181        self.where = self.where_class(self.connection)
    182182        if self.related_updates or must_pre_select:
    183183            # Either we're using the idents in multiple update queries (so
    184184            # don't want them to change), or the db backend doesn't support
     
    202202        This is used by the QuerySet.delete_objects() method.
    203203        """
    204204        for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
    205             self.where = self.where_class()
     205            self.where = self.where_class(self.connection)
    206206            f = self.model._meta.pk
    207207            self.where.add((None, f.column, f, 'in',
    208208                    pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]),
Back to Top