Ticket #2705: for_update_8031.2.patch
File for_update_8031.2.patch, 7.9 KB (added by , 16 years ago) |
---|
-
django/db/models/sql/query.py
70 70 self.order_by = [] 71 71 self.low_mark, self.high_mark = 0, None # Used for offset/limit 72 72 self.distinct = False 73 self.select_for_update = False 74 self.select_for_update_nowait = False 73 75 self.select_related = False 74 76 self.related_select_cols = [] 75 77 … … 177 179 obj.order_by = self.order_by[:] 178 180 obj.low_mark, obj.high_mark = self.low_mark, self.high_mark 179 181 obj.distinct = self.distinct 182 obj.select_for_update = self.select_for_update 183 obj.select_for_update_nowait = self.select_for_update_nowait 180 184 obj.select_related = self.select_related 181 185 obj.related_select_cols = [] 182 186 obj.max_depth = self.max_depth … … 215 219 obj = self.clone() 216 220 obj.clear_ordering(True) 217 221 obj.clear_limits() 222 obj.select_for_update = False 218 223 obj.select_related = False 219 224 obj.related_select_cols = [] 220 225 obj.related_select_fields = [] … … 293 298 result.append('LIMIT %d' % val) 294 299 result.append('OFFSET %d' % self.low_mark) 295 300 301 if self.select_for_update: 302 result.append("%s" % self.connection.ops.for_update_sql(nowait=self.select_for_update_nowait)) 303 296 304 params.extend(self.extra_params) 297 305 return ' '.join(result), tuple(params) 298 306 -
django/db/models/manager.py
108 108 def order_by(self, *args, **kwargs): 109 109 return self.get_query_set().order_by(*args, **kwargs) 110 110 111 def select_for_update(self, *args, **kwargs): 112 return self.get_query_set().select_for_update(*args, **kwargs) 113 111 114 def select_related(self, *args, **kwargs): 112 115 return self.get_query_set().select_related(*args, **kwargs) 113 116 -
django/db/models/query.py
372 372 del_query = self._clone() 373 373 374 374 # Disable non-supported fields. 375 del_query.query.select_for_update = False 375 376 del_query.query.select_related = False 376 377 del_query.query.clear_ordering() 377 378 … … 514 515 else: 515 516 return self._filter_or_exclude(None, **filter_obj) 516 517 518 def select_for_update(self, **kwargs): 519 """ 520 Returns a new QuerySet instance that will select objects with a 521 FOR UPDATE lock. 522 """ 523 # Default to false for nowait 524 nowait = kwargs.pop('nowait', False) 525 obj = self._clone() 526 obj.query.select_for_update = True 527 obj.query.select_for_update_nowait = nowait 528 return obj 529 517 530 def select_related(self, *fields, **kwargs): 518 531 """ 519 532 Returns a new QuerySet instance that will select related objects. -
django/db/backends/sqlite3/base.py
57 57 # function django_date_trunc that's registered in connect(). 58 58 return 'django_date_trunc("%s", %s)' % (lookup_type.lower(), field_name) 59 59 60 def for_update_sql(self): 61 # sqlite does not support FOR UPDATE 62 return '' 63 60 64 def drop_foreignkey_sql(self): 61 65 return "" 62 66 … … 179 183 return bool(re.search(re_pattern, re_string)) 180 184 except: 181 185 return False 186 -
django/db/backends/mysql/base.py
89 89 def fulltext_search_sql(self, field_name): 90 90 return 'MATCH (%s) AGAINST (%%s IN BOOLEAN MODE)' % field_name 91 91 92 def for_update_sql(self, nowait=False): 93 """ 94 Return FOR UPDATE SQL clause to lock row for update 95 Currently mysql ignores NOWAIT 96 """ 97 if nowait: 98 raise NotImplementedError("NOWAIT option for SELECT ... FOR UPDATE not implemented with mysql"); 99 BaseDatabaseWrapper.for_update_sql(self,nowait=False) 100 101 92 102 def no_limit_value(self): 93 103 # 2**64 - 1, as recommended by the MySQL documentation 94 104 return 18446744073709551615L -
django/db/backends/__init__.py
123 123 """ 124 124 return '%s' 125 125 126 def for_update_sql(self, nowait=False): 127 """ 128 Return FOR UPDATE SQL clause to lock row for update 129 """ 130 if nowait: 131 nowaitstr = ' NOWAIT' 132 else: 133 nowaitstr = '' 134 return 'FOR UPDATE' + nowaitstr 135 126 136 def fulltext_search_sql(self, field_name): 127 137 """ 128 138 Returns the SQL WHERE clause to use in order to perform a full-text -
tests/regressiontests/queries/models.py
726 726 >>> Item.objects.exclude(~Q(tags__name='t1', name='one'), name='two') 727 727 [<Item: four>, <Item: one>, <Item: three>] 728 728 729 Bug #2075 730 Added FOR UPDATE functionality 731 >>> from django.db import transaction 732 >>> @transaction.commit_manually 733 >>> def test_for_update(): 734 >>> t = Tag(name='for update test') 735 >>> t.save() 736 >>> transaction.commit() 737 >>> tfound = Tag.objects.select_for_update().get(pk=t.id) 738 >>> tfound.name = 'for update test 2' 739 >>> tfound.save() 740 >>> transaction.commit() 741 >>> test_for_update() 742 729 743 Bug #7095 730 744 Updates that are filtered on the model being updated are somewhat tricky to get 731 745 in MySQL. This exercises that case. -
docs/db-api.txt
1071 1071 ``extra()`` is new. Previously, you could attempt to pass parameters for 1072 1072 ``select`` in the ``params`` argument, but it worked very unreliably. 1073 1073 1074 select_for_update 1075 ~~~~~~~~~~~~~~~~~ 1076 1077 **New in Django development version:** 1078 1079 Lock rows returned from a query. Most databases allow you to exclusively 1080 lock rows with ``select_for_update()``. To do this call the method 1081 ``select_for_update()`` to lock all retrieved objects:: 1082 1083 entry = Entry.objects.select_for_update().get(pk=1) 1084 ... 1085 entry.save() 1086 1087 All objects returned using ``select_for_update()`` will be locked with an 1088 exclusive lock which will remain locked until the transaction has finished. 1089 In the case of using middleware, the locks will be released when the view 1090 returns and the transaction is committed or rolled back. SQLite does not 1091 support an exclusive lock so this is simply ignored for SQLite. Other 1092 databases issue a ``FOR UPDATE``. 1093 1094 If you would not like to wait for the lock then set the parameter nowait to 1095 True. In this case, it will grab the lock if it isn't already locked. If 1096 it is locked then it will throw an exception. This is not supported 1097 with mysql. Doing this with mysql will raise a ``NotImplemented`` exception. 1098 1099 Note that all rows returned are locked. If you retrieve multiple objects, 1100 all objects will be locked until the transaction is committed. Another 1101 process which does a ``select_for_update()`` on the same rows will wait until 1102 the transaction which has the locks is finished. 1103 1074 1104 QuerySet methods that do not return QuerySets 1075 1105 --------------------------------------------- 1076 1106