Ticket #7258: connection_in_sqlqueries.diff
File connection_in_sqlqueries.diff, 8.2 KB (added by , 17 years ago) |
---|
-
where.py
4 4 import datetime 5 5 6 6 from django.utils import tree 7 from django.db import connection8 7 from django.db.models.fields import Field 9 8 from django.db.models.query_utils import QueryWrapper 10 9 from datastructures import EmptyResultSet, FullResultSet 11 10 11 from copy import deepcopy 12 12 13 # Connection types 13 14 AND = 'AND' 14 15 OR = 'OR' … … 26 27 """ 27 28 default = AND 28 29 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 29 44 def as_sql(self, node=None, qn=None): 30 45 """ 31 46 Returns the SQL version of the where clause and the value to be … … 38 53 if node is None: 39 54 node = self 40 55 if not qn: 41 qn = connection.ops.quote_name56 qn = self.connection.ops.quote_name 42 57 if not node.children: 43 58 return None, [] 44 59 result = [] … … 99 114 lhs = '%s.%s' % (qn(table_alias), qn(name)) 100 115 else: 101 116 lhs = qn(name) 117 102 118 db_type = field and field.db_type() or None 103 field_sql = connection.ops.field_cast_sql(db_type) % lhs104 119 field_sql = self.connection.ops.field_cast_sql(db_type) % lhs 120 105 121 if isinstance(value, datetime.datetime): 106 cast_sql = connection.ops.datetime_cast_sql()122 cast_sql = self.connection.ops.datetime_cast_sql() 107 123 else: 108 124 cast_sql = '%s' 109 125 … … 116 132 else: 117 133 extra = '' 118 134 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), 121 137 extra) 122 138 return (format % (field_sql, 123 connection.operators[lookup_type] % cast_sql), params)139 self.connection.operators[lookup_type] % cast_sql), params) 124 140 125 141 if lookup_type == 'in': 126 142 if not value: … … 132 148 elif lookup_type in ('range', 'year'): 133 149 return ('%s BETWEEN %%s and %%s' % field_sql, params) 134 150 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, 136 152 field_sql), params) 137 153 elif lookup_type == 'isnull': 138 154 return ('%s IS %sNULL' % (field_sql, (not value and 'NOT ' or '')), 139 155 params) 140 156 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) 142 158 elif lookup_type in ('regex', 'iregex'): 143 return connection.ops.regex_lookup(lookup_type) % (field_sql, cast_sql), params159 return self.connection.ops.regex_lookup(lookup_type) % (field_sql, cast_sql), params 144 160 145 161 raise TypeError('Invalid lookup_type: %r' % lookup_type) 146 162 -
query.py
60 60 # SQL-related attributes 61 61 self.select = [] 62 62 self.tables = [] # Aliases in the order they are created. 63 self.where = where( )63 self.where = where(self.connection) 64 64 self.where_class = where 65 65 self.group_by = [] 66 66 self.having = [] … … 215 215 obj.related_select_cols = [] 216 216 obj.related_select_fields = [] 217 217 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), 219 219 distinct=False) 220 220 obj.select = [] 221 221 obj.extra_select = {} … … 346 346 self.where.add(EverythingNode(), AND) 347 347 elif self.where: 348 348 # rhs has an empty where clause. 349 w = self.where_class( )349 w = self.where_class(self.connection) 350 350 w.add(EverythingNode(), AND) 351 351 else: 352 w = self.where_class( )352 w = self.where_class(self.connection) 353 353 self.where.add(w, connector) 354 354 355 355 # Selection columns and extra extensions are those provided by 'rhs'. -
subqueries.py
47 47 for related in cls._meta.get_all_related_many_to_many_objects(): 48 48 if not isinstance(related.field, generic.GenericRelation): 49 49 for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): 50 where = self.where_class( )50 where = self.where_class(self.connection) 51 51 where.add((None, related.field.m2m_reverse_name(), 52 52 related.field, 'in', 53 53 pk_list[offset : offset+GET_ITERATOR_CHUNK_SIZE]), … … 55 55 self.do_query(related.field.m2m_db_table(), where) 56 56 57 57 for f in cls._meta.many_to_many: 58 w1 = self.where_class( )58 w1 = self.where_class(self.connection) 59 59 if isinstance(f, generic.GenericRelation): 60 60 from django.contrib.contenttypes.models import ContentType 61 61 field = f.rel.to._meta.get_field(f.content_type_field_name) 62 62 w1.add((None, field.column, field, 'exact', 63 63 ContentType.objects.get_for_model(cls).id), AND) 64 64 for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): 65 where = self.where_class( )65 where = self.where_class(self.connection) 66 66 where.add((None, f.m2m_column_name(), f, 'in', 67 67 pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]), 68 68 AND) … … 79 79 lot of values in pk_list. 80 80 """ 81 81 for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): 82 where = self.where_class( )82 where = self.where_class(self.connection) 83 83 field = self.model._meta.pk 84 84 where.add((None, field.column, field, 'in', 85 85 pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]), AND) … … 178 178 179 179 # Now we adjust the current query: reset the where clause and get rid 180 180 # 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) 182 182 if self.related_updates or must_pre_select: 183 183 # Either we're using the idents in multiple update queries (so 184 184 # don't want them to change), or the db backend doesn't support … … 202 202 This is used by the QuerySet.delete_objects() method. 203 203 """ 204 204 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) 206 206 f = self.model._meta.pk 207 207 self.where.add((None, f.column, f, 'in', 208 208 pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]),