def for_update_clause(self, select):
if select.for_update == 'nowait':
+ if select.for_update_of is not None:
+ return " FOR UPDATE OF " + select.for_update_of + " NOWAIT"
return " FOR UPDATE NOWAIT"
elif select.for_update == 'read':
return " FOR SHARE"
_criterion = None
_yield_per = None
_lockmode = None
+ _lockmode_of = None
_order_by = False
_group_by = False
_having = None
self._execution_options = self._execution_options.union(kwargs)
@_generative()
- def with_lockmode(self, mode):
+ def with_lockmode(self, mode, of=None):
"""Return a new Query object with the specified locking mode.
:param mode: a string representing the desired locking mode. A
.. versionadded:: 0.7.7
``FOR SHARE`` and ``FOR SHARE NOWAIT`` (PostgreSQL).
+ :param of: a table descriptor representing the optional OF
+ part of the clause. This passes ``for_update_of=table'``
+ which translates to ``FOR UPDATE OF table [NOWAIT]``.
"""
self._lockmode = mode
+ self._lockmode_of = of
@_generative()
def params(self, *args, **kwargs):
except KeyError:
raise sa_exc.ArgumentError(
"Unknown lockmode %r" % self._lockmode)
+ if self._lockmode_of is not None:
+ context.for_update_of = self._lockmode_of
+
for entity in self._entities:
entity.setup_context(self, context)
statement = sql.select(
[inner] + context.secondary_columns,
for_update=context.for_update,
+ for_update_of=context.for_update_of,
use_labels=context.labels)
from_clause = inner
from_obj=context.froms,
use_labels=context.labels,
for_update=context.for_update,
+ for_update_of=context.for_update_of,
order_by=context.order_by,
**self._select_args
)
adapter = None
froms = ()
for_update = False
+ for_update_of = None
def __init__(self, query):
def __init__(self,
use_labels=False,
for_update=False,
+ for_update_of=None,
limit=None,
offset=None,
order_by=None,
autocommit=None):
self.use_labels = use_labels
self.for_update = for_update
+ self.for_update_of = for_update_of
if autocommit is not None:
util.warn_deprecated('autocommit on select() is '
'deprecated. Use .execution_options(a'
dialect=postgresql.dialect()
)
+ def test_postgres_update_of(self):
+ User = self.classes.User
+ sess = Session()
+ self.assert_compile(sess.query(User.id).with_lockmode('update', of='users'),
+ "SELECT users.id AS users_id FROM users FOR UPDATE OF users",
+ dialect=postgresql.dialect()
+ )
+
def test_postgres_update_nowait(self):
User = self.classes.User
sess = Session()
dialect=postgresql.dialect()
)
+ def test_postgres_update_nowait_of(self):
+ User = self.classes.User
+ sess = Session()
+ self.assert_compile(sess.query(User.id).with_lockmode('update_nowait', of='users'),
+ "SELECT users.id AS users_id FROM users FOR UPDATE OF users NOWAIT",
+ dialect=postgresql.dialect()
+ )
+
def test_oracle_update(self):
User = self.classes.User
sess = Session()