Ticket #2705: for_update_7477.patch

File for_update_7477.patch, 4.1 KB (added by sakyamuni, 7 years ago)

patch against 7477

  • django/db/models/sql/query.py

     
    6767        self.order_by = []
    6868        self.low_mark, self.high_mark = 0, None  # Used for offset/limit
    6969        self.distinct = False
     70        self.select_for_update = False
    7071        self.select_related = False
    7172        self.related_select_cols = []
    7273
     
    155156        obj.order_by = self.order_by[:]
    156157        obj.low_mark, obj.high_mark = self.low_mark, self.high_mark
    157158        obj.distinct = self.distinct
     159        obj.select_for_update = self.select_for_update
    158160        obj.select_related = self.select_related
    159161        obj.related_select_cols = []
    160162        obj.max_depth = self.max_depth
     
    193195        obj = self.clone()
    194196        obj.clear_ordering(True)
    195197        obj.clear_limits()
     198        obj.select_for_update = False
    196199        obj.select_related = False
    197200        obj.related_select_cols = []
    198201        obj.related_select_fields = []
     
    272275                        result.append('LIMIT %d' % val)
    273276                result.append('OFFSET %d' % self.low_mark)
    274277
     278        if self.select_for_update:
     279            result.append("%s" % self.connection.ops.for_update_sql())
     280
    275281        params.extend(self.extra_params)
    276282        return ' '.join(result), tuple(params)
    277283
  • django/db/models/manager.py

     
    108108    def order_by(self, *args, **kwargs):
    109109        return self.get_query_set().order_by(*args, **kwargs)
    110110
     111    def select_for_update(self, *args, **kwargs):
     112        return self.get_query_set().select_for_update(*args, **kwargs)
     113       
    111114    def select_related(self, *args, **kwargs):
    112115        return self.get_query_set().select_related(*args, **kwargs)
    113116
  • django/db/models/query.py

     
    256256        del_query = self._clone()
    257257
    258258        # Disable non-supported fields.
     259        del_query.query.select_for_update = False
    259260        del_query.query.select_related = False
    260261        del_query.query.clear_ordering()
    261262
     
    390391        else:
    391392            return self._filter_or_exclude(None, **filter_obj)
    392393
     394    def select_for_update(self):
     395        """
     396        Returns a new QuerySet instance that will select objects with a
     397        FOR UPDATE lock.
     398        """
     399        obj = self._clone()
     400        obj.query.select_for_update = True
     401        return obj
     402
    393403    def select_related(self, *fields, **kwargs):
    394404        """
    395405        Returns a new QuerySet instance that will select related objects. If
  • django/db/backends/sqlite3/base.py

     
    5252        # function django_date_trunc that's registered in connect().
    5353        return 'django_date_trunc("%s", %s)' % (lookup_type.lower(), field_name)
    5454
     55    def for_update_sql():
     56        # sqlite does not support FOR UPDATE
     57        return ''
     58
    5559    def drop_foreignkey_sql(self):
    5660        return ""
    5761
     
    171175        return bool(re.search(re_pattern, re_string))
    172176    except:
    173177        return False
     178
  • django/db/backends/__init__.py

     
    159159        """
    160160        return cursor.lastrowid
    161161
     162    def 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