Commits

Mike Bayer committed 8e1b233

- [feature] Added new for_update/with_lockmode()
options for Postgresql: for_update="read"/
with_lockmode("read"),
for_update="read_nowait"/
with_lockmode("read_nowait").
These emit "FOR SHARE" and "FOR SHARE NOWAIT",
respectively. Courtesy Diana Clarke
[ticket:2445]

Comments (0)

Files changed (6)

     directives in statements.  Courtesy
     Diana Clarke [ticket:2443]
 
+- postgresql
+  - [feature] Added new for_update/with_lockmode()
+    options for Postgresql: for_update="read"/
+    with_lockmode("read"),
+    for_update="read_nowait"/
+    with_lockmode("read_nowait").
+    These emit "FOR SHARE" and "FOR SHARE NOWAIT",
+    respectively.  Courtesy Diana Clarke 
+    [ticket:2445]
+
 0.7.6
 =====
 - orm

lib/sqlalchemy/dialects/postgresql/base.py

     def for_update_clause(self, select):
         if select.for_update == 'nowait':
             return " FOR UPDATE NOWAIT"
+        elif select.for_update == 'read':
+            return " FOR SHARE"
+        elif select.for_update == 'read_nowait':
+            return " FOR SHARE NOWAIT"
         else:
             return super(PGCompiler, self).for_update_clause(select)
 

lib/sqlalchemy/orm/query.py

             ``FOR UPDATE`` (standard SQL, supported by most dialects)
 
             ``'update_nowait'`` - passes ``for_update='nowait'``, which
-            translates to ``FOR UPDATE NOWAIT`` (supported by Oracle)
+            translates to ``FOR UPDATE NOWAIT`` (supported by Oracle, 
+            PostgreSQL)
 
             ``'read'`` - passes ``for_update='read'``, which translates to
-            ``LOCK IN SHARE MODE`` (supported by MySQL).
+            ``LOCK IN SHARE MODE`` (for MySQL), and ``FOR SHARE`` (for 
+            PostgreSQL)
+
+            ``'read_nowait'`` - passes ``for_update='read_nowait'``, which 
+            translates to ``FOR SHARE NOWAIT`` (supported by PostgreSQL).
+            
+            New in 0.7.7: ``FOR SHARE`` and ``FOR SHARE NOWAIT`` (PostgreSQL)
         """
 
         self._lockmode = mode
         if self._lockmode:
             try:
                 for_update = {'read': 'read',
+                              'read_nowait': 'read_nowait',
                               'update': True,
                               'update_nowait': 'nowait',
                               None: False}[self._lockmode]

lib/sqlalchemy/sql/expression.py

 
     :param for_update=False:
       when ``True``, applies ``FOR UPDATE`` to the end of the
-      resulting statement.  Certain database dialects also support
-      alternate values for this parameter, for example mysql
-      supports "read" which translates to ``LOCK IN SHARE MODE``,
-      and oracle supports "nowait" which translates to ``FOR UPDATE
-      NOWAIT``.
+      resulting statement.  
+      
+      Certain database dialects also support
+      alternate values for this parameter:
+      
+      * With the MySQL dialect, the value ``"read"`` translates to 
+        ``LOCK IN SHARE MODE``.
+      * With the Oracle and Postgresql dialects, the value ``"nowait"``
+        translates to ``FOR UPDATE NOWAIT``.
+      * With the Postgresql dialect, the values "read" and ``"read_nowait"``
+        translate to ``FOR SHARE`` and ``FOR SHARE NOWAIT``, respectively
+        (new in 0.7.7).
 
     :param group_by:
       a list of :class:`.ClauseElement` objects which will comprise the

test/orm/test_lockmode.py

+from sqlalchemy.engine import default
+from sqlalchemy.databases import *
+from sqlalchemy.orm import mapper
+from sqlalchemy.orm import Session
+from test.lib import AssertsCompiledSQL
+from test.lib.testing import assert_raises_message
+from test.orm import _fixtures
+
+
+class LockModeTest(_fixtures.FixtureTest, AssertsCompiledSQL):
+    run_inserts = None
+
+    @classmethod
+    def setup_mappers(cls):
+        User, users = cls.classes.User, cls.tables.users
+        mapper(User, users)
+
+    def test_default_update(self):
+        User = self.classes.User
+        sess = Session()
+        self.assert_compile(sess.query(User.id).with_lockmode('update'),
+            "SELECT users.id AS users_id FROM users FOR UPDATE",
+            dialect=default.DefaultDialect()
+        )
+
+    def test_not_supported_by_dialect_should_just_use_update(self):
+        User = self.classes.User
+        sess = Session()
+        self.assert_compile(sess.query(User.id).with_lockmode('read'),
+            "SELECT users.id AS users_id FROM users FOR UPDATE",
+            dialect=default.DefaultDialect()
+        )
+
+    def test_none_lock_mode(self):
+        User = self.classes.User
+        sess = Session()
+        self.assert_compile(sess.query(User.id).with_lockmode(None),
+            "SELECT users.id AS users_id FROM users",
+            dialect=default.DefaultDialect()
+        )
+
+    def test_unknown_lock_mode(self):
+        User = self.classes.User
+        sess = Session()
+        assert_raises_message(
+            Exception, "Unknown lockmode 'unknown_mode'", 
+            self.assert_compile, 
+            sess.query(User.id).with_lockmode('unknown_mode'), None,
+            dialect=default.DefaultDialect()
+        )
+
+    def test_postgres_read(self):
+        User = self.classes.User
+        sess = Session()
+        self.assert_compile(sess.query(User.id).with_lockmode('read'),
+            "SELECT users.id AS users_id FROM users FOR SHARE",
+            dialect=postgresql.dialect()
+        )
+
+    def test_postgres_read_nowait(self):
+        User = self.classes.User
+        sess = Session()
+        self.assert_compile(sess.query(User.id).with_lockmode('read_nowait'),
+            "SELECT users.id AS users_id FROM users FOR SHARE NOWAIT",
+            dialect=postgresql.dialect()
+        )
+
+    def test_postgres_update(self):
+        User = self.classes.User
+        sess = Session()
+        self.assert_compile(sess.query(User.id).with_lockmode('update'),
+            "SELECT users.id AS users_id FROM users FOR UPDATE",
+            dialect=postgresql.dialect()
+        )
+
+    def test_postgres_update_nowait(self):
+        User = self.classes.User
+        sess = Session()
+        self.assert_compile(sess.query(User.id).with_lockmode('update_nowait'),
+            "SELECT users.id AS users_id FROM users FOR UPDATE NOWAIT",
+            dialect=postgresql.dialect()
+        )
+
+    def test_oracle_update(self):
+        User = self.classes.User
+        sess = Session()
+        self.assert_compile(sess.query(User.id).with_lockmode('update'),
+            "SELECT users.id AS users_id FROM users FOR UPDATE",
+            dialect=oracle.dialect()
+        )
+
+    def test_oracle_update_nowait(self):
+        User = self.classes.User
+        sess = Session()
+        self.assert_compile(sess.query(User.id).with_lockmode('update_nowait'),
+            "SELECT users.id AS users_id FROM users FOR UPDATE NOWAIT",
+            dialect=oracle.dialect()
+        )
+
+    def test_mysql_read(self):
+        User = self.classes.User
+        sess = Session()
+        self.assert_compile(sess.query(User.id).with_lockmode('read'),
+            "SELECT users.id AS users_id FROM users LOCK IN SHARE MODE",
+            dialect=mysql.dialect()
+        )
+
+    def test_mysql_update(self):
+        User = self.classes.User
+        sess = Session()
+        self.assert_compile(sess.query(User.id).with_lockmode('update'),
+            "SELECT users.id AS users_id FROM users FOR UPDATE",
+            dialect=mysql.dialect()
+        )

test/sql/test_compiler.py

 
     def test_for_update(self):
         self.assert_compile(
-                    table1.select(table1.c.myid==7, for_update=True), 
-                    "SELECT mytable.myid, mytable.name, mytable.description "
-                    "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE")
+            table1.select(table1.c.myid==7, for_update=True),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE")
 
         self.assert_compile(
-                    table1.select(table1.c.myid==7, for_update="nowait"), 
-                    "SELECT mytable.myid, mytable.name, mytable.description "
-                    "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE")
-
+            table1.select(table1.c.myid==7, for_update=False),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = :myid_1")
+
+        # not supported by dialect, should just use update
         self.assert_compile(
-                    table1.select(table1.c.myid==7, for_update="nowait"), 
-                    "SELECT mytable.myid, mytable.name, mytable.description "
-                    "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE NOWAIT", 
-                    dialect=oracle.dialect())
-
+            table1.select(table1.c.myid==7, for_update='nowait'),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE")
+
+        # unknown lock mode
         self.assert_compile(
-                    table1.select(table1.c.myid==7, for_update="read"), 
-                    "SELECT mytable.myid, mytable.name, mytable.description "
-                    "FROM mytable WHERE mytable.myid = %s LOCK IN SHARE MODE", 
-                    dialect=mysql.dialect())
+            table1.select(table1.c.myid==7, for_update='unknown_mode'),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE")
+
+        # ----- mysql
 
         self.assert_compile(
-                    table1.select(table1.c.myid==7, for_update=True), 
-                    "SELECT mytable.myid, mytable.name, mytable.description "
-                    "FROM mytable WHERE mytable.myid = %s FOR UPDATE", 
-                    dialect=mysql.dialect())
+            table1.select(table1.c.myid==7, for_update=True),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = %s FOR UPDATE",
+            dialect=mysql.dialect())
 
         self.assert_compile(
-                    table1.select(table1.c.myid==7, for_update=True), 
-                    "SELECT mytable.myid, mytable.name, mytable.description "
-                    "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE", 
-                    dialect=oracle.dialect())
+            table1.select(table1.c.myid==7, for_update="read"),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = %s LOCK IN SHARE MODE",
+            dialect=mysql.dialect())
+
+        # ----- oracle
+
+        self.assert_compile(
+            table1.select(table1.c.myid==7, for_update=True),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE",
+            dialect=oracle.dialect())
+
+        self.assert_compile(
+            table1.select(table1.c.myid==7, for_update="nowait"),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = :myid_1 FOR UPDATE NOWAIT",
+            dialect=oracle.dialect())
+
+        # ----- postgresql
+
+        self.assert_compile(
+            table1.select(table1.c.myid==7, for_update=True),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = %(myid_1)s FOR UPDATE",
+            dialect=postgresql.dialect())
+
+        self.assert_compile(
+            table1.select(table1.c.myid==7, for_update="nowait"),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = %(myid_1)s FOR UPDATE NOWAIT",
+            dialect=postgresql.dialect())
+
+        self.assert_compile(
+            table1.select(table1.c.myid==7, for_update="read"),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = %(myid_1)s FOR SHARE",
+            dialect=postgresql.dialect())
+        
+        self.assert_compile(
+            table1.select(table1.c.myid==7, for_update="read_nowait"),
+            "SELECT mytable.myid, mytable.name, mytable.description "
+            "FROM mytable WHERE mytable.myid = %(myid_1)s FOR SHARE NOWAIT",
+            dialect=postgresql.dialect())
 
     def test_alias(self):
         # test the alias for a table1.  column names stay the same, table name "changes" to "foo".
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.