Skip to content

Commit 8c6da62

Browse files
committed
add support for select_for_update
1 parent 3f93238 commit 8c6da62

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

sql_server/pyodbc/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
5252
can_return_id_from_insert = True
5353
can_use_chunked_reads = False
5454
has_bulk_insert = True
55+
has_select_for_update = _DJANGO_VERSION >= 14
56+
has_select_for_update_nowait = _DJANGO_VERSION >= 14
5557
ignores_nulls_in_unique_constraints = False
5658
supports_1000_query_parameters = False
5759
supports_microsecond_precision = True

sql_server/pyodbc/compiler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ def as_sql(self, with_limits=True, with_col_aliases=False):
9292
result.extend(from_)
9393
params.extend(f_params)
9494

95+
if self.query.select_for_update and self.connection.features.has_select_for_update:
96+
# If we've been asked for a NOWAIT query but the backend does not support it,
97+
# raise a DatabaseError otherwise we could get an unexpected deadlock.
98+
nowait = self.query.select_for_update_nowait
99+
result.append(self.connection.ops.for_update_sql(nowait=nowait))
100+
95101
if where:
96102
result.append('WHERE %s' % where)
97103
params.extend(w_params)

sql_server/pyodbc/operations.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ def field_cast_sql(self, db_type):
144144
"""
145145
return '%s'
146146

147+
def for_update_sql(self, nowait=False):
148+
"""
149+
Returns the FOR UPDATE SQL clause to lock rows for an update operation.
150+
"""
151+
if nowait:
152+
return 'WITH (NOWAIT, ROWLOCK, UPDLOCK)'
153+
else:
154+
return 'WITH (ROWLOCK, UPDLOCK)'
155+
147156
def fulltext_search_sql(self, field_name):
148157
"""
149158
Returns the SQL WHERE clause to use in order to perform a full-text

0 commit comments

Comments
 (0)