summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorJack Zhou <univerio@gmail.com>2016-05-31 10:01:46 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2016-06-02 17:46:16 -0400
commite8f97c9e357ed0793ce11086823f83aa4a8bb4ad (patch)
treee87b5fbd22799b56ab771197fd4658afddd08c91 /lib/sqlalchemy
parenteb28ebb0f8a48ba57f68f21d64479b56bf689d24 (diff)
downloadsqlalchemy-e8f97c9e357ed0793ce11086823f83aa4a8bb4ad.tar.gz
Add SKIP LOCKED support for Postgresql, Oracle
This adds `SELECT ... FOR UPDATE SKIP LOCKED`/ `SELECT ... FOR SHARE SKIP LOCKED` rendering. Change-Id: Id1dc4f1cafc1de23f397a6f73d54ab2c58d5910d Pull-request: https://bitbucket.org/zzzeek/sqlalchemy/pull-requests/86
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/dialects/oracle/base.py2
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py2
-rw-r--r--lib/sqlalchemy/orm/query.py6
-rw-r--r--lib/sqlalchemy/sql/selectable.py17
4 files changed, 22 insertions, 5 deletions
diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py
index 6992670c4..493bf362f 100644
--- a/lib/sqlalchemy/dialects/oracle/base.py
+++ b/lib/sqlalchemy/dialects/oracle/base.py
@@ -814,6 +814,8 @@ class OracleCompiler(compiler.SQLCompiler):
if select._for_update_arg.nowait:
tmp += " NOWAIT"
+ if select._for_update_arg.skip_locked:
+ tmp += " SKIP LOCKED"
return tmp
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py
index 136cb1b28..1bc4409f2 100644
--- a/lib/sqlalchemy/dialects/postgresql/base.py
+++ b/lib/sqlalchemy/dialects/postgresql/base.py
@@ -1185,6 +1185,8 @@ class PGCompiler(compiler.SQLCompiler):
if select._for_update_arg.nowait:
tmp += " NOWAIT"
+ if select._for_update_arg.skip_locked:
+ tmp += " SKIP LOCKED"
return tmp
diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index 34daa707f..7fab33197 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -1397,7 +1397,8 @@ 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,
+ skip_locked=False):
"""return a new :class:`.Query` with the specified options for the
``FOR UPDATE`` clause.
@@ -1425,7 +1426,8 @@ 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,
+ skip_locked=skip_locked)
@_generative()
def params(self, *args, **kwargs):
diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py
index e299f067e..bd1d04e57 100644
--- a/lib/sqlalchemy/sql/selectable.py
+++ b/lib/sqlalchemy/sql/selectable.py
@@ -1723,14 +1723,16 @@ 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, 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)]
@@ -1873,7 +1875,8 @@ 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,
+ skip_locked=False):
"""Specify a ``FOR UPDATE`` clause for this :class:`.GenerativeSelect`.
E.g.::
@@ -1908,10 +1911,18 @@ class GenerativeSelect(SelectBase):
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):