]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Allow kwargs to be passed through update()
authorAmir Sadoughi <amir.sadoughi@gmail.com>
Fri, 20 Mar 2015 05:18:09 +0000 (00:18 -0500)
committerAmir Sadoughi <amir.sadoughi@gmail.com>
Fri, 20 Mar 2015 22:04:29 +0000 (17:04 -0500)
This is useful to be able to pass in mysql_limit=1 from using the
ORM.

lib/sqlalchemy/orm/persistence.py
lib/sqlalchemy/orm/query.py
test/orm/test_update_delete.py

index c5df63dfda077c6ac66f8251a7b3c8ab59724fce..3b85d717ed8ee1f86ca8ee50b1fcc510e647317a 100644 (file)
@@ -1132,18 +1132,19 @@ class BulkFetch(BulkUD):
 class BulkUpdate(BulkUD):
     """BulkUD which handles UPDATEs."""
 
-    def __init__(self, query, values):
+    def __init__(self, query, values, update_kwargs):
         super(BulkUpdate, self).__init__(query)
         self.query._no_select_modifiers("update")
         self.values = values
+        self.update_kwargs = update_kwargs
 
     @classmethod
-    def factory(cls, query, synchronize_session, values):
+    def factory(cls, query, synchronize_session, values, update_kwargs):
         return BulkUD._factory({
             "evaluate": BulkUpdateEvaluate,
             "fetch": BulkUpdateFetch,
             False: BulkUpdate
-        }, synchronize_session, query, values)
+        }, synchronize_session, query, values, update_kwargs)
 
     def _resolve_string_to_expr(self, key):
         if self.mapper and isinstance(key, util.string_types):
@@ -1178,7 +1179,8 @@ class BulkUpdate(BulkUD):
             for k, v in self.values.items()
         )
         update_stmt = sql.update(self.primary_table,
-                                 self.context.whereclause, values)
+                                 self.context.whereclause, values,
+                                 **self.update_kwargs)
 
         self.result = self.query.session.execute(
             update_stmt, params=self.query._params,
index 36180e8d5b5a48ca496068202b37f8f02892f1b6..fb2749fa1f085553ab41778c51d7f9e4298854a6 100644 (file)
@@ -2827,7 +2827,7 @@ class Query(object):
         delete_op.exec_()
         return delete_op.rowcount
 
-    def update(self, values, synchronize_session='evaluate'):
+    def update(self, values, synchronize_session='evaluate', update_args=None):
         """Perform a bulk update query.
 
         Updates rows matched by this query in the database.
@@ -2936,8 +2936,9 @@ class Query(object):
 
         """
 
+        update_args = update_args or {}
         update_op = persistence.BulkUpdate.factory(
-            self, synchronize_session, values)
+            self, synchronize_session, values, update_args)
         update_op.exec_()
         return update_op.rowcount
 
index a3ad37e60573d01807da161d5bada1b2a1ce04c3..6447f83c3a8a506d0ca4b5cd1fe1c78d5a5e1871 100644 (file)
@@ -3,7 +3,7 @@ from sqlalchemy.testing import fixtures
 from sqlalchemy import Integer, String, ForeignKey, or_, exc, \
     select, func, Boolean, case, text, column
 from sqlalchemy.orm import mapper, relationship, backref, Session, \
-    joinedload, synonym
+    joinedload, synonym, query
 from sqlalchemy import testing
 
 from sqlalchemy.testing.schema import Table, Column
@@ -885,6 +885,18 @@ class ExpressionUpdateTest(fixtures.MappedTest):
         eq_(d1.cnt, 2)
         sess.close()
 
+    def test_update_args(self):
+        Data = self.classes.Data
+        session = testing.mock.Mock(wraps=Session())
+        update_args = {"mysql_limit": 1}
+        query.Query(Data, session).update({Data.cnt: Data.cnt + 1},
+                                          update_args=update_args)
+        eq_(session.execute.call_count, 1)
+        args, kwargs = session.execute.call_args
+        eq_(len(args), 1)
+        update_stmt = args[0]
+        eq_(update_stmt.dialect_kwargs, update_args)
+
 
 class InheritTest(fixtures.DeclarativeMappedTest):