objects present in the constraints collection. Pull request courtesy
Alex Grönholm.
+ .. change::
+ :tags: feature, postgresql, oracle
+ :pullreq: bitbucket:86
+
+ Added new parameter
+ :paramref:`.GenerativeSelect.with_for_update.skip_locked`, which
+ will render the ``SKIP LOCKED`` phrase for a ``FOR UPDATE`` or
+ ``FOR SHARE`` lock on the Postgresql and Oracle backends. Pull
+ request courtesy Jack Zhou.
+
.. change::
:tags: change, orm
:tickets: 3394
Engine URLs of the form ``postgres://`` will still continue to function,
however.
+Support for SKIP LOCKED
+-----------------------
+
+The new parameter :paramref:`.GenerativeSelect.with_for_update.skip_locked`
+in both Core and ORM will generate the "SKIP LOCKED" suffix for a
+"SELECT...FOR UPDATE" or "SELECT.. FOR SHARE" query.
+
+
Dialect Improvements and Changes - MySQL
=============================================
Dialect Improvements and Changes - Oracle
=============================================
+
+Support for SKIP LOCKED
+-----------------------
+
+The new parameter :paramref:`.GenerativeSelect.with_for_update.skip_locked`
+in both Core and ORM will generate the "SKIP LOCKED" suffix for a
+"SELECT...FOR UPDATE" or "SELECT.. FOR SHARE" query.
if select._for_update_arg.nowait:
tmp += " NOWAIT"
+ if select._for_update_arg.skip_locked:
+ tmp += " SKIP LOCKED"
return tmp
if select._for_update_arg.nowait:
tmp += " NOWAIT"
+ if select._for_update_arg.skip_locked:
+ tmp += " SKIP LOCKED"
return tmp
self._for_update_arg = LockmodeArg.parse_legacy_query(mode)
@_generative()
- def with_for_update(self, read=False, nowait=False, of=None):
+ def with_for_update(self, read=False, nowait=False, of=None,
+ skip_locked=False):
"""return a new :class:`.Query` with the specified options for the
``FOR UPDATE`` clause.
full argument and behavioral description.
"""
- self._for_update_arg = LockmodeArg(read=read, nowait=nowait, of=of)
+ self._for_update_arg = LockmodeArg(read=read, nowait=nowait, of=of,
+ skip_locked=skip_locked)
@_generative()
def params(self, *args, **kwargs):
if self.of is not None:
self.of = [clone(col, **kw) for col in self.of]
- def __init__(self, nowait=False, read=False, of=None):
+ def __init__(self, nowait=False, read=False, of=None, skip_locked=False):
"""Represents arguments specified to :meth:`.Select.for_update`.
.. versionadded:: 0.9.0
+
"""
self.nowait = nowait
self.read = read
+ self.skip_locked = skip_locked
if of is not None:
self.of = [_interpret_as_column_or_from(elem)
for elem in util.to_list(of)]
self._for_update_arg = ForUpdateArg.parse_legacy_select(value)
@_generative
- def with_for_update(self, nowait=False, read=False, of=None):
+ def with_for_update(self, nowait=False, read=False, of=None,
+ skip_locked=False):
"""Specify a ``FOR UPDATE`` clause for this :class:`.GenerativeSelect`.
E.g.::
and Oracle. May render as a table or as a column depending on
backend.
+ :param skip_locked: boolean, will render ``FOR UPDATE SKIP LOCKED``
+ on Oracle and Postgresql dialects or ``FOR SHARE SKIP LOCKED`` if
+ ``read=True`` is also specified.
+
+ .. versionadded:: 1.1.0
+
.. versionadded:: 0.9.0
+
"""
- self._for_update_arg = ForUpdateArg(nowait=nowait, read=read, of=of)
+ self._for_update_arg = ForUpdateArg(nowait=nowait, read=read, of=of,
+ skip_locked=skip_locked)
@_generative
def apply_labels(self):
"SELECT mytable.myid, mytable.name, mytable.description "
"FROM mytable WHERE mytable.myid = %(myid_1)s FOR UPDATE NOWAIT")
+ self.assert_compile(
+ table1.select(table1.c.myid == 7).
+ with_for_update(skip_locked=True),
+ "SELECT mytable.myid, mytable.name, mytable.description "
+ "FROM mytable WHERE mytable.myid = %(myid_1)s "
+ "FOR UPDATE SKIP LOCKED")
+
self.assert_compile(
table1.select(table1.c.myid == 7).with_for_update(read=True),
"SELECT mytable.myid, mytable.name, mytable.description "
"SELECT mytable.myid, mytable.name, mytable.description "
"FROM mytable WHERE mytable.myid = %(myid_1)s FOR SHARE NOWAIT")
+ self.assert_compile(
+ table1.select(table1.c.myid == 7).
+ with_for_update(read=True, skip_locked=True),
+ "SELECT mytable.myid, mytable.name, mytable.description "
+ "FROM mytable WHERE mytable.myid = %(myid_1)s "
+ "FOR SHARE SKIP LOCKED")
+
self.assert_compile(
table1.select(table1.c.myid == 7).
with_for_update(of=table1.c.myid),
"FROM mytable WHERE mytable.myid = %(myid_1)s "
"FOR SHARE OF mytable NOWAIT")
+ self.assert_compile(
+ table1.select(table1.c.myid == 7).
+ with_for_update(read=True, skip_locked=True,
+ of=[table1.c.myid, table1.c.name]),
+ "SELECT mytable.myid, mytable.name, mytable.description "
+ "FROM mytable WHERE mytable.myid = %(myid_1)s "
+ "FOR SHARE OF mytable SKIP LOCKED")
+
ta = table1.alias()
self.assert_compile(
ta.select(ta.c.myid == 7).
"FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE OF "
"mytable.myid, mytable.name NOWAIT")
+ self.assert_compile(
+ table1.select(table1.c.myid == 7).
+ with_for_update(skip_locked=True, of=[table1.c.myid, table1.c.name]),
+ "SELECT mytable.myid, mytable.name, mytable.description "
+ "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE OF "
+ "mytable.myid, mytable.name SKIP LOCKED")
+
ta = table1.alias()
self.assert_compile(
ta.select(ta.c.myid == 7).
dialect="postgresql"
)
+ def test_postgres_update_skip_locked(self):
+ User = self.classes.User
+ sess = Session()
+ self.assert_compile(sess.query(User.id).
+ with_for_update(skip_locked=True),
+ "SELECT users.id AS users_id FROM users FOR UPDATE SKIP LOCKED",
+ dialect="postgresql"
+ )
+
def test_oracle_update(self):
User = self.classes.User
dialect="oracle"
)
+ def test_oracle_update_skip_locked(self):
+ User = self.classes.User
+ sess = Session()
+ self.assert_compile(sess.query(User.id)
+ .with_for_update(skip_locked=True),
+ "SELECT users.id AS users_id FROM users FOR UPDATE SKIP LOCKED",
+ dialect="oracle"
+ )
+
def test_mysql_read(self):
User = self.classes.User
sess = Session()