summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py2
-rw-r--r--lib/sqlalchemy/orm/query.py21
-rw-r--r--lib/sqlalchemy/sql/selectable.py36
3 files changed, 46 insertions, 13 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py
index 9d019b56e..5407aa4be 100644
--- a/lib/sqlalchemy/dialects/postgresql/base.py
+++ b/lib/sqlalchemy/dialects/postgresql/base.py
@@ -1159,6 +1159,8 @@ class PGCompiler(compiler.SQLCompiler):
if select._for_update_arg.read:
tmp = " FOR SHARE"
+ elif select._for_update_arg.no_key:
+ tmp = " FOR NO KEY UPDATE"
else:
tmp = " FOR UPDATE"
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index 34daa707f..2b1a88582 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -1388,6 +1388,12 @@ class Query(object):
* ``'read'`` - translates to ``LOCK IN SHARE MODE`` (for MySQL),
and ``FOR SHARE`` (for PostgreSQL)
+ * ``'no_key'`` - translates to ``FOR NO KEY UPDATE`` (for PostgreSQL),
+ and ``FOR UPDATE`` on other backends
+
+ * ``'no_key_nowait'`` - translates to ``FOR NO KEY UPDATE NOWAIT`` (for PostgreSQL),
+ and ``FOR UPDATE`` on other backends
+
.. seealso::
:meth:`.Query.with_for_update` - improved API for
@@ -1397,7 +1403,7 @@ class Query(object):
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, no_key=None):
"""return a new :class:`.Query` with the specified options for the
``FOR UPDATE`` clause.
@@ -1411,10 +1417,12 @@ class Query(object):
E.g.::
q = sess.query(User).with_for_update(nowait=True, of=User)
+ q = sess.query(User).with_for_update(nowait=True, no_key=True)
- The above query on a Postgresql backend will render like::
+ The above queries on a Postgresql backend will render like, accordingly::
SELECT users.id AS users_id FROM users FOR UPDATE OF users NOWAIT
+ SELECT users.id AS users_id FROM users FOR NO KEY UPDATE NOWAIT
.. versionadded:: 0.9.0 :meth:`.Query.with_for_update` supersedes
the :meth:`.Query.with_lockmode` method.
@@ -1425,7 +1433,7 @@ class Query(object):
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, no_key=no_key)
@_generative()
def params(self, *args, **kwargs):
@@ -3411,6 +3419,7 @@ class LockmodeArg(ForUpdateArg):
if mode in (None, False):
return None
+ no_key = nowait = read = False
if mode == "read":
read = True
nowait = False
@@ -3419,11 +3428,15 @@ class LockmodeArg(ForUpdateArg):
elif mode == "update_nowait":
nowait = True
read = False
+ elif mode == "no_key":
+ no_key = True
+ elif mode == "no_key_nowait":
+ no_key = nowait = True
else:
raise sa_exc.ArgumentError(
"Unknown with_lockmode argument: %r" % mode)
- return LockmodeArg(read=read, nowait=nowait)
+ return LockmodeArg(read=read, nowait=nowait, no_key=no_key)
class _QueryEntity(object):
diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py
index e299f067e..38c8a1453 100644
--- a/lib/sqlalchemy/sql/selectable.py
+++ b/lib/sqlalchemy/sql/selectable.py
@@ -1673,7 +1673,7 @@ class ForUpdateArg(ClauseElement):
@classmethod
def parse_legacy_select(self, arg):
- """Parse the for_update arugment of :func:`.select`.
+ """Parse the for_update argument of :func:`.select`.
:param mode: Defines the lockmode to use.
@@ -1688,6 +1688,12 @@ class ForUpdateArg(ClauseElement):
``'read'`` - translates to ``LOCK IN SHARE MODE`` (for MySQL),
and ``FOR SHARE`` (for PostgreSQL)
+ ``'no_key'`` - translates to ``FOR NO KEY UPDATE`` (for PostgreSQL),
+ and ``FOR UPDATE`` on other backends
+
+ ``'no_key_nowait'`` - translates to ``FOR NO KEY UPDATE NOWAIT`` (for PostgreSQL),
+ and ``FOR UPDATE`` on other backends
+
``'read_nowait'`` - translates to ``FOR SHARE NOWAIT``
(supported by PostgreSQL). ``FOR SHARE`` and
``FOR SHARE NOWAIT`` (PostgreSQL).
@@ -1696,17 +1702,21 @@ class ForUpdateArg(ClauseElement):
if arg in (None, False):
return None
- nowait = read = False
+ nowait = read = no_key = False
if arg == 'nowait':
nowait = True
elif arg == 'read':
read = True
elif arg == 'read_nowait':
read = nowait = True
+ elif arg == 'no_key':
+ no_key = True
+ elif arg == 'no_key_nowait':
+ no_key = nowait = True
elif arg is not True:
raise exc.ArgumentError("Unknown for_update argument: %r" % arg)
- return ForUpdateArg(read=read, nowait=nowait)
+ return ForUpdateArg(read=read, nowait=nowait, no_key=no_key)
@property
def legacy_for_update_value(self):
@@ -1723,7 +1733,7 @@ class ForUpdateArg(ClauseElement):
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, no_key=None):
"""Represents arguments specified to :meth:`.Select.for_update`.
.. versionadded:: 0.9.0
@@ -1731,12 +1741,16 @@ class ForUpdateArg(ClauseElement):
self.nowait = nowait
self.read = read
+ self.no_key = no_key
if of is not None:
self.of = [_interpret_as_column_or_from(elem)
for elem in util.to_list(of)]
else:
self.of = None
+ if self.read and self.no_key:
+ raise exc.ArgumentError("Bad argument values: 'no_key' and 'read' cannot be True together.")
+
class SelectBase(HasCTE, Executable, FromClause):
"""Base class for SELECT statements.
@@ -1873,19 +1887,23 @@ class GenerativeSelect(SelectBase):
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, no_key=None):
"""Specify a ``FOR UPDATE`` clause for this :class:`.GenerativeSelect`.
E.g.::
- stmt = select([table]).with_for_update(nowait=True)
+ stmt = select([table]).with_for_update(nowait=True, no_key=True)
- On a database like Postgresql or Oracle, the above would render a
+ On a database like Postgresql, the above would render a
statement like::
+ SELECT table.a, table.b FROM table FOR NO KEY UPDATE NOWAIT
+
+ on Oracle::
+
SELECT table.a, table.b FROM table FOR UPDATE NOWAIT
- on other backends, the ``nowait`` option is ignored and instead
+ on other backends, the ``nowait`` and ``no_key`` options is ignored and instead
would produce::
SELECT table.a, table.b FROM table FOR UPDATE
@@ -1911,7 +1929,7 @@ class GenerativeSelect(SelectBase):
.. 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, no_key=no_key)
@_generative
def apply_labels(self):