diff options
| author | Mikhail Denisenko <denisenkom@gmail.com> | 2015-11-15 23:14:55 -0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-09-09 11:57:38 -0400 |
| commit | bae64dd0f13ba247448197ecf83cdc7a80691bb4 (patch) | |
| tree | 17faa3160a7cef9bb7c8388b5a7dbd617369e6ac /django/db/models/sql | |
| parent | ef021412d5e7eb78c89f0b7cf2ec01bcb959a837 (diff) | |
Fixed #27062 -- Eased implementing select_for_update() on MSSQL.
Diffstat (limited to 'django/db/models/sql')
| -rw-r--r-- | django/db/models/sql/compiler.py | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index 5eaac1fc74..7c40fbeb38 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -402,6 +402,25 @@ class SQLCompiler(object): result.extend(from_) params.extend(f_params) + for_update_part = None + if self.query.select_for_update and self.connection.features.has_select_for_update: + if self.connection.get_autocommit(): + raise TransactionManagementError("select_for_update cannot be used outside of a transaction.") + + nowait = self.query.select_for_update_nowait + skip_locked = self.query.select_for_update_skip_locked + # If it's a NOWAIT/SKIP LOCKED query but the backend doesn't + # support it, raise a DatabaseError to prevent a possible + # deadlock. + if nowait and not self.connection.features.has_select_for_update_nowait: + raise DatabaseError('NOWAIT is not supported on this database backend.') + elif skip_locked and not self.connection.features.has_select_for_update_skip_locked: + raise DatabaseError('SKIP LOCKED is not supported on this database backend.') + for_update_part = self.connection.ops.for_update_sql(nowait=nowait, skip_locked=skip_locked) + + if for_update_part and self.connection.features.for_update_after_from: + result.append(for_update_part) + if where: result.append('WHERE %s' % where) params.extend(w_params) @@ -439,22 +458,8 @@ class SQLCompiler(object): result.append('LIMIT %d' % val) result.append('OFFSET %d' % self.query.low_mark) - if self.query.select_for_update and self.connection.features.has_select_for_update: - if self.connection.get_autocommit(): - raise TransactionManagementError( - "select_for_update cannot be used outside of a transaction." - ) - - nowait = self.query.select_for_update_nowait - skip_locked = self.query.select_for_update_skip_locked - # If we've been asked for a NOWAIT/SKIP LOCKED query but the - # backend does not support it, raise a DatabaseError otherwise - # we could get an unexpected deadlock. - if nowait and not self.connection.features.has_select_for_update_nowait: - raise DatabaseError('NOWAIT is not supported on this database backend.') - elif skip_locked and not self.connection.features.has_select_for_update_skip_locked: - raise DatabaseError('SKIP LOCKED is not supported on this database backend.') - result.append(self.connection.ops.for_update_sql(nowait=nowait, skip_locked=skip_locked)) + if for_update_part and not self.connection.features.for_update_after_from: + result.append(for_update_part) return ' '.join(result), tuple(params) finally: |
