Ticket #2705: select_for_update_r7188.patch

File select_for_update_r7188.patch, 4.7 KB (added by KBS, 7 years ago)

updated patch that works against r7188 of trunk

  • django/db/models/manager.py

     
    9595    def order_by(self, *args, **kwargs):
    9696        return self.get_query_set().order_by(*args, **kwargs)
    9797
     98    def select_for_update(self, *args, **kwargs):
     99        return self.get_query_set().select_for_update(*args, **kwargs)
     100       
    98101    def select_related(self, *args, **kwargs):
    99102        return self.get_query_set().select_related(*args, **kwargs)
    100103
  • django/db/models/query.py

     
    8888    def __init__(self, model=None):
    8989        self.model = model
    9090        self._filters = Q()
    91         self._order_by = None        # Ordering, e.g. ('date', '-name'). If None, use model's ordering.
    92         self._select_related = False # Whether to fill cache for related objects.
    93         self._max_related_depth = 0  # Maximum "depth" for select_related
    94         self._distinct = False       # Whether the query should use SELECT DISTINCT.
    95         self._select = {}            # Dictionary of attname -> SQL.
    96         self._where = []             # List of extra WHERE clauses to use.
    97         self._params = []            # List of params to use for extra WHERE clauses.
    98         self._tables = []            # List of extra tables to use.
    99         self._offset = None          # OFFSET clause.
    100         self._limit = None           # LIMIT clause.
     91        self._order_by = None           # Ordering, e.g. ('date', '-name'). If None, use model's ordering.
     92        self._select_for_update = False # Whether to select for update.
     93        self._select_related = False    # Whether to fill cache for related objects.
     94        self._max_related_depth = 0     # Maximum "depth" for select_related
     95        self._distinct = False          # Whether the query should use SELECT DISTINCT.
     96        self._select = {}               # Dictionary of attname -> SQL.
     97        self._where = []                # List of extra WHERE clauses to use.
     98        self._params = []               # List of params to use for extra WHERE clauses.
     99        self._tables = []               # List of extra tables to use.
     100        self._offset = None             # OFFSET clause.
     101        self._limit = None              # LIMIT clause.
    101102        self._result_cache = None
    102103
    103104    ########################
     
    222223
    223224        counter = self._clone()
    224225        counter._order_by = ()
     226        counter._select_for_update = False
    225227        counter._select_related = False
    226228
    227229        offset = counter._offset
     
    329331        del_query = self._clone()
    330332
    331333        # disable non-supported fields
     334        del_query._select_for_update = False
    332335        del_query._select_related = False
    333336        del_query._order_by = []
    334337
     
    411414        else:
    412415            return self._filter_or_exclude(None, **filter_obj)
    413416
     417    def select_for_update(self, true_or_false=True):
     418        "Returns a new QuerySet instance with '_select_for_update' modified."
     419        return self._clone(_select_for_update=true_or_false)
     420   
    414421    def select_related(self, true_or_false=True, depth=0):
    415422        "Returns a new QuerySet instance with '_select_related' modified."
    416423        return self._clone(_select_related=true_or_false, _max_related_depth=depth)
     
    446453        c.model = self.model
    447454        c._filters = self._filters
    448455        c._order_by = self._order_by
     456        c._select_for_update = self._select_for_update
    449457        c._select_related = self._select_related
    450458        c._max_related_depth = self._max_related_depth
    451459        c._distinct = self._distinct
     
    563571        else:
    564572            assert self._offset is None, "'offset' is not allowed without 'limit'"
    565573
     574        # FOR UPDATE
     575        if self._select_for_update:
     576            sql.append("%s" % connection.ops.get_for_update_sql())
     577       
    566578        return select, " ".join(sql), params
    567579
    568580# Use the backend's QuerySet class if it defines one. Otherwise, use _QuerySet.
  • django/db/backends/__init__.py

     
    159159        """
    160160        return cursor.lastrowid
    161161
     162    def get_for_update_sql(self):
     163        """
     164        Return FOR UPDATE SQL clause to lock row for update
     165        """
     166        return 'FOR UPDATE'
     167
    162168    def limit_offset_sql(self, limit, offset=None):
    163169        """
    164170        Returns a LIMIT/OFFSET SQL clause, given a limit and optional offset.
Back to Top