Ticket #6422: query.py.patch
File query.py.patch, 3.9 KB (added by , 17 years ago) |
---|
-
query.py
old new 186 186 extra_select = self._select.items() 187 187 188 188 cursor = connection.cursor() 189 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 190 189 select, sql, params = self._get_sql_clause() 190 if self._distinct == False: 191 distinct = "" 192 elif self._distinct == True: 193 distinct = "DISTINCT " 194 else: 195 # CHECKME: probably need some qouting around the %s (or use %r ???) 196 distinct = "DISTINCT ON (%s) " % ( 197 # CHECKME: the type check would not be necessary if the 198 # distinct method would ensure to store a tuple in 199 # self._distinct in any case. 200 type(self._distinct) == type('') and self._distinct 201 or ','.join(self._distinct)) 202 cursor.execute("SELECT " + distinct + ",".join(select) + sql, params) 191 203 fill_cache = self._select_related 192 204 fields = self.model._meta.fields 193 205 index_end = len(fields) … … 236 248 237 249 cursor = connection.cursor() 238 250 if self._distinct: 251 # CHECKME: may have to change this for DISTINCT ON queries, i.e. 252 # when self._distinct is not 'True' but a string or tuple 239 253 id_col = "%s.%s" % (connection.ops.quote_name(self.model._meta.db_table), 240 254 connection.ops.quote_name(self.model._meta.pk.column)) 241 255 cursor.execute("SELECT COUNT(DISTINCT(%s))" % id_col + sql, params) … … 421 435 "Cannot reorder a query once a slice has been taken." 422 436 return self._clone(_order_by=field_names) 423 437 424 def distinct(self, true_or_false=True ):438 def distinct(self, true_or_false=True, *fields): 425 439 "Returns a new QuerySet instance with '_distinct' modified." 426 return self._clone(_distinct=true_or_false) 440 if fields: 441 # CHECKME: if fields is a one-tuple a simple string will be stored 442 # in self._distinct in the cloned object. This leads to more 443 # complex precessing when the value is used so it would be 444 # beneficial to make sure to always store a tuple here. 445 return self._clone(_distinct=fields) 446 else: 447 return self._clone(_distinct=true_or_false) 427 448 428 449 def extra(self, select=None, where=None, params=None, tables=None): 429 450 assert self._limit is None and self._offset is None, \ … … 462 483 assert self._limit is None and self._offset is None \ 463 484 and other._limit is None and other._offset is None, \ 464 485 "Cannot combine queries once a slice has been taken." 486 # CHECKME: for DISTINCT ON queries this will always return false 487 # unless both field sets are identical. I don't know if this is 488 # correct, if so the error message may have to be adapted. 465 489 assert self._distinct == other._distinct, \ 466 490 "Cannot combine a unique query with a non-unique query" 467 491 # use 'other's order by … … 617 641 field_names.extend([f[0] for f in extra_select]) 618 642 619 643 cursor = connection.cursor() 620 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 644 if self._distinct == False: 645 distinct = "" 646 elif self._distinct == True: 647 distinct = "DISTINCT " 648 else: 649 # CHECKME: see above (same as in the QuerySet class) 650 distinct = "DISTINCT ON (%s) " % ( 651 type(self._distinct) == type('') and self._distinct 652 or ','.join(self._distinct)) 653 cursor.execute("SELECT " + distinct + ",".join(select) + sql, params) 621 654 622 655 has_resolve_columns = hasattr(self, 'resolve_columns') 623 656 while 1: