Ticket #14298: cursor-close-patch.diff
File cursor-close-patch.diff, 2.8 KB (added by , 14 years ago) |
---|
-
django/db/models/sql/compiler.py
13 13 self.connection = connection 14 14 self.using = using 15 15 self.quote_cache = {} 16 self.iter_cursor_map = {} 16 17 17 18 def pre_sql_setup(self): 18 19 """ … … 677 676 resolve_columns = hasattr(self, 'resolve_columns') 678 677 fields = None 679 678 has_aggregate_select = bool(self.query.aggregate_select) 680 for rows in self.execute_sql(MULTI): 679 cursor_iter = self.execute_sql(MULTI) 680 for rows in cursor_iter: 681 681 for row in rows: 682 682 if resolve_columns: 683 683 if fields is None: … … 707 707 ]) + tuple(row[aggregate_end:]) 708 708 709 709 yield row 710 # iteration over cursor is completed. 711 # it's now save to close the cursor and remove it from the lookup dict 712 if cursor_iter in self.iter_cursor_map: 713 self.iter_cursor_map[cursor_iter].close() 714 del self.iter_cursor_map[cursor_iter] 710 715 711 716 def execute_sql(self, result_type=MULTI): 712 717 """ … … 734 739 cursor = self.connection.cursor() 735 740 cursor.execute(sql, params) 736 741 742 # close cursors for single results. 737 743 if not result_type: 738 744 return cursor 739 745 if result_type == SINGLE: 740 746 if self.query.ordering_aliases: 741 return cursor.fetchone()[:-len(self.query.ordering_aliases)] 742 return cursor.fetchone() 747 temp_result = cursor.fetchone()[:-len(self.query.ordering_aliases)] 748 cursor.close() 749 return temp_result 750 temp_result = cursor.fetchone() 751 cursor.close() 752 return temp_result 743 753 744 754 # The MULTI case. 745 755 if self.query.ordering_aliases: … … 748 758 else: 749 759 result = iter((lambda: cursor.fetchmany(GET_ITERATOR_CHUNK_SIZE)), 750 760 self.connection.features.empty_fetchmany_value) 761 # store iterator only if we use chunks 762 if self.connection.features.can_use_chunked_reads: 763 self.iter_cursor_map[result] = cursor 751 764 if not self.connection.features.can_use_chunked_reads: 752 765 # If we are using non-chunked reads, we return the same data 753 766 # structure as normally, but ensure it is all read into memory 754 767 # before going any further. 755 return list(result) 768 temp_ressult = list(result) 769 cursor.close() 770 return temp_result 756 771 return result 757 772 758 759 773 class SQLInsertCompiler(SQLCompiler): 760 774 def placeholder(self, field, val): 761 775 if field is None: