Ticket #3283: empty_in_clause.patch
File empty_in_clause.patch, 3.2 KB (added by , 18 years ago) |
---|
-
query.py
25 25 # Larger values are slightly faster at the expense of more storage space. 26 26 GET_ITERATOR_CHUNK_SIZE = 100 27 27 28 class EmptyInClause(Exception): 29 pass 30 28 31 #################### 29 32 # HELPER FUNCTIONS # 30 33 #################### … … 168 171 extra_select = self._select.items() 169 172 170 173 cursor = connection.cursor() 171 select, sql, params = self._get_sql_clause() 174 175 try: 176 select, sql, params = self._get_sql_clause() 177 except EmptyInClause: 178 raise StopIteration 179 172 180 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 173 181 fill_cache = self._select_related 174 182 index_end = len(self.model._meta.fields) … … 192 200 counter._offset = None 193 201 counter._limit = None 194 202 counter._select_related = False 195 select, sql, params = counter._get_sql_clause() 203 204 try: 205 select, sql, params = counter._get_sql_clause() 206 except EmptyInClause: 207 return 0 208 196 209 cursor = connection.cursor() 197 210 if self._distinct: 198 211 id_col = "%s.%s" % (backend.quote_name(self.model._meta.db_table), … … 523 536 field_names = [f.attname for f in self.model._meta.fields] 524 537 525 538 cursor = connection.cursor() 526 select, sql, params = self._get_sql_clause() 539 540 try: 541 select, sql, params = self._get_sql_clause() 542 except EmptyInClause: 543 raise StopIteration 544 527 545 select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns] 528 546 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 529 547 while 1: … … 545 563 if self._field.null: 546 564 self._where.append('%s.%s IS NOT NULL' % \ 547 565 (backend.quote_name(self.model._meta.db_table), backend.quote_name(self._field.column))) 548 select, sql, params = self._get_sql_clause() 566 567 try: 568 select, sql, params = self._get_sql_clause() 569 except EmptyInClause: 570 raise StopIteration 571 549 572 sql = 'SELECT %s %s GROUP BY 1 ORDER BY 1 %s' % \ 550 573 (backend.get_date_trunc_sql(self._kind, '%s.%s' % (backend.quote_name(self.model._meta.db_table), 551 574 backend.quote_name(self._field.column))), sql, self._order) … … 645 687 if in_string: 646 688 return '%s%s IN (%s)' % (table_prefix, field_name, in_string) 647 689 else: 648 # Most backends do not accept an empty string inside the IN 649 # expression, i.e. cannot do "WHERE ... IN ()". Since there are 650 # also some backends that do not accept "WHERE false", we instead 651 # use an expression that always evaluates to False. 652 return '0=1' 690 raise EmptyInClause 653 691 elif lookup_type == 'range': 654 692 return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name) 655 693 elif lookup_type in ('year', 'month', 'day'):