Ticket #6261: query_wherenode.diff
File query_wherenode.diff, 5.0 KB (added by , 17 years ago) |
---|
-
branches/queryset-refactor/django/db/models/sql/query.py
78 78 79 79 alias_prefix = 'T' 80 80 81 def __init__(self, model, connection ):81 def __init__(self, model, connection, where=WhereNode): 82 82 self.model = model 83 83 self.connection = connection 84 84 self.alias_map = {} # Maps alias to table name … … 91 91 # SQL-related attributes 92 92 self.select = [] 93 93 self.tables = [] # Aliases in the order they are created. 94 self.where = WhereNode() 94 self.where = where() 95 self.where_class = where 95 96 self.group_by = [] 96 97 self.having = [] 97 98 self.order_by = [] … … 156 157 obj.select = self.select[:] 157 158 obj.tables = self.tables[:] 158 159 obj.where = copy.deepcopy(self.where) 160 obj.where_class = self.where_class 159 161 obj.group_by = self.group_by[:] 160 162 obj.having = self.having[:] 161 163 obj.order_by = self.order_by[:] … … 192 194 obj.clear_limits() 193 195 obj.select_related = False 194 196 if obj.distinct and len(obj.select) > 1: 195 obj = self.clone(CountQuery, _query=obj, where= WhereNode(),197 obj = self.clone(CountQuery, _query=obj, where=self.where_class(), 196 198 distinct=False) 197 199 obj.select = [] 198 200 obj.extra_select = SortedDict() … … 210 212 211 213 return number 212 214 215 def get_terms(self): 216 """ 217 Returns all valid Query lookup types. 218 """ 219 return QUERY_TERMS 220 213 221 def as_sql(self, with_limits=True): 214 222 """ 215 223 Creates the SQL for this query. Returns the SQL string and list of … … 320 328 elif self.where: 321 329 # rhs has an empty where clause. Make it match everything (see 322 330 # above for reasoning). 323 w = WhereNode()331 w = self.where_class() 324 332 alias = self.join((None, self.model._meta.db_table, None, None)) 325 333 pk = self.model._meta.pk 326 334 w.add([alias, pk.column, pk, 'isnull', False], AND) 327 335 else: 328 w = WhereNode()336 w = self.where_class() 329 337 self.where.add(w, connector) 330 338 331 339 # Selection columns and extra extensions are those provided by 'rhs'. … … 705 713 raise TypeError("Cannot parse keyword query %r" % arg) 706 714 707 715 # Work out the lookup type and remove it from 'parts', if necessary. 708 if len(parts) == 1 or parts[-1] not in QUERY_TERMS:716 if len(parts) == 1 or parts[-1] not in self.get_terms(): 709 717 lookup_type = 'exact' 710 718 else: 711 719 lookup_type = parts.pop() … … 1110 1118 for related in cls._meta.get_all_related_many_to_many_objects(): 1111 1119 if not isinstance(related.field, generic.GenericRelation): 1112 1120 for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): 1113 where = WhereNode()1121 where = self.where_class() 1114 1122 where.add((None, related.field.m2m_reverse_name(), 1115 1123 related.field, 'in', 1116 1124 pk_list[offset : offset+GET_ITERATOR_CHUNK_SIZE]), … … 1118 1126 self.do_query(related.field.m2m_db_table(), where) 1119 1127 1120 1128 for f in cls._meta.many_to_many: 1121 w1 = WhereNode()1129 w1 = self.where_class() 1122 1130 if isinstance(f, generic.GenericRelation): 1123 1131 from django.contrib.contenttypes.models import ContentType 1124 1132 field = f.rel.to._meta.get_field(f.content_type_field_name) 1125 1133 w1.add((None, field.column, field, 'exact', 1126 1134 ContentType.objects.get_for_model(cls).id), AND) 1127 1135 for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): 1128 where = WhereNode()1136 where = self.where_class() 1129 1137 where.add((None, f.m2m_column_name(), f, 'in', 1130 1138 pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]), 1131 1139 AND) … … 1142 1150 lot of values in pk_list. 1143 1151 """ 1144 1152 for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): 1145 where = WhereNode()1153 where = self.where_class() 1146 1154 field = self.model._meta.pk 1147 1155 where.add((None, field.column, field, 'in', 1148 1156 pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]), AND) … … 1186 1194 This is used by the QuerySet.delete_objects() method. 1187 1195 """ 1188 1196 for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): 1189 where = WhereNode()1197 where = self.where_class() 1190 1198 f = self.model._meta.pk 1191 1199 where.add((None, f.column, f, 'in', 1192 1200 pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]),