Ticket #2705: select_for_update.patch
File select_for_update.patch, 8.3 KB (added by , 18 years ago) |
---|
-
django/db/models/manager.py
99 99 def order_by(self, *args, **kwargs): 100 100 return self.get_query_set().order_by(*args, **kwargs) 101 101 102 def select_for_update(self, *args, **kwargs): 103 return self.get_query_set().select_for_update(*args, **kwargs) 104 102 105 def select_related(self, *args, **kwargs): 103 106 return self.get_query_set().select_related(*args, **kwargs) 104 107 -
django/db/models/query.py
82 82 def __init__(self, model=None): 83 83 self.model = model 84 84 self._filters = Q() 85 self._order_by = None # Ordering, e.g. ('date', '-name'). If None, use model's ordering. 86 self._select_related = False # Whether to fill cache for related objects. 87 self._max_related_depth = 0 # Maximum "depth" for select_related 88 self._distinct = False # Whether the query should use SELECT DISTINCT. 89 self._select = {} # Dictionary of attname -> SQL. 90 self._where = [] # List of extra WHERE clauses to use. 91 self._params = [] # List of params to use for extra WHERE clauses. 92 self._tables = [] # List of extra tables to use. 93 self._offset = None # OFFSET clause. 94 self._limit = None # LIMIT clause. 85 self._order_by = None # Ordering, e.g. ('date', '-name'). If None, use model's ordering. 86 self._select_for_update = False # Whether to select for update. 87 self._select_related = False # Whether to fill cache for related objects. 88 self._max_related_depth = 0 # Maximum "depth" for select_related 89 self._distinct = False # Whether the query should use SELECT DISTINCT. 90 self._select = {} # Dictionary of attname -> SQL. 91 self._where = [] # List of extra WHERE clauses to use. 92 self._params = [] # List of params to use for extra WHERE clauses. 93 self._tables = [] # List of extra tables to use. 94 self._offset = None # OFFSET clause. 95 self._limit = None # LIMIT clause. 95 96 self._result_cache = None 96 97 97 98 ######################## … … 211 212 212 213 counter = self._clone() 213 214 counter._order_by = () 215 counter._select_for_update = False 214 216 counter._select_related = False 215 217 216 218 offset = counter._offset … … 316 318 del_query = self._clone() 317 319 318 320 # disable non-supported fields 321 del_query._select_for_update = False 319 322 del_query._select_related = False 320 323 del_query._order_by = [] 321 324 … … 398 401 else: 399 402 return self._filter_or_exclude(None, **filter_obj) 400 403 404 def select_for_update(self, true_or_false=True): 405 "Returns a new QuerySet instance with '_select_for_update' modified." 406 return self._clone(_select_for_update=true_or_false) 407 401 408 def select_related(self, true_or_false=True, depth=0): 402 409 "Returns a new QuerySet instance with '_select_related' modified." 403 410 return self._clone(_select_related=true_or_false, _max_related_depth=depth) … … 433 440 c.model = self.model 434 441 c._filters = self._filters 435 442 c._order_by = self._order_by 443 c._select_for_update = self._select_for_update 436 444 c._select_related = self._select_related 437 445 c._max_related_depth = self._max_related_depth 438 446 c._distinct = self._distinct … … 549 557 else: 550 558 assert self._offset is None, "'offset' is not allowed without 'limit'" 551 559 560 # FOR UPDATE 561 if self._select_for_update: 562 sql.append("%s" % backend.get_for_update_sql()) 563 552 564 return select, " ".join(sql), params 553 565 554 566 class ValuesQuerySet(QuerySet): -
django/db/backends/ado_mssql/base.py
116 116 if lookup_type=='day': 117 117 return "Convert(datetime, Convert(varchar(12), %s))" % field_name 118 118 119 def get_for_update_sql(): 120 return 'WITH (UPDLOCK, HOLDLOCK)' 121 119 122 def get_limit_offset_sql(limit, offset=None): 120 123 # TODO: This is a guess. Make sure this is correct. 121 124 sql = "LIMIT %s" % limit -
django/db/backends/mysql_old/base.py
166 166 sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str) 167 167 return sql 168 168 169 def get_for_update_sql(): 170 return 'FOR UPDATE' 171 169 172 def get_limit_offset_sql(limit, offset=None): 170 173 sql = "LIMIT " 171 174 if offset and offset != 0: -
django/db/backends/postgresql/base.py
137 137 # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC 138 138 return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) 139 139 140 def get_for_update_sql(): 141 return 'FOR UPDATE' 142 140 143 def get_limit_offset_sql(limit, offset=None): 141 144 sql = "LIMIT %s" % limit 142 145 if offset and offset != 0: -
django/db/backends/sqlite3/base.py
131 131 # sqlite doesn't support DATE_TRUNC, so we fake it as above. 132 132 return 'django_date_trunc("%s", %s)' % (lookup_type.lower(), field_name) 133 133 134 def get_for_update_sql(): 135 # sqlite does not support FOR UPDATE 136 return '' 137 134 138 def get_limit_offset_sql(limit, offset=None): 135 139 sql = "LIMIT %s" % limit 136 140 if offset and offset != 0: -
django/db/backends/mysql/base.py
164 164 sql = "CAST(DATE_FORMAT(%s, '%s') AS DATETIME)" % (field_name, format_str) 165 165 return sql 166 166 167 def get_for_update_sql(): 168 return 'FOR UPDATE' 169 167 170 def get_limit_offset_sql(limit, offset=None): 168 171 sql = "LIMIT " 169 172 if offset and offset != 0: -
django/db/backends/oracle/base.py
101 101 def get_date_trunc_sql(lookup_type, field_name): 102 102 return "EXTRACT(%s FROM TRUNC(%s))" % (lookup_type, field_name) 103 103 104 def get_for_update_sql(): 105 return 'FOR UPDATE' 106 104 107 def get_limit_offset_sql(limit, offset=None): 105 108 # Limits and offset are too complicated to be handled here. 106 109 # Instead, they are handled in django/db/query.py. -
django/db/backends/postgresql_psycopg2/base.py
97 97 # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC 98 98 return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name) 99 99 100 def get_for_update_sql(): 101 return 'FOR UPDATE' 102 100 103 def get_limit_offset_sql(limit, offset=None): 101 104 sql = "LIMIT %s" % limit 102 105 if offset and offset != 0: -
django/db/backends/dummy/base.py
34 34 get_last_insert_id = complain 35 35 get_date_extract_sql = complain 36 36 get_date_trunc_sql = complain 37 get_for_update_sql = complain 37 38 get_limit_offset_sql = complain 38 39 get_random_function_sql = complain 39 40 get_deferrable_sql = complain