From 3365a4f78ed54fc0c242c01a3f58e87b1518d68d Mon Sep 17 00:00:00 2001 From: Amir Sadoughi Date: Fri, 20 Mar 2015 00:18:09 -0500 Subject: [PATCH] Allow kwargs to be passed through update() This is useful to be able to pass in mysql_limit=1 from using the ORM. --- lib/sqlalchemy/orm/persistence.py | 10 ++++++---- lib/sqlalchemy/orm/query.py | 5 +++-- test/orm/test_update_delete.py | 14 +++++++++++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/sqlalchemy/orm/persistence.py b/lib/sqlalchemy/orm/persistence.py index c5df63dfda..3b85d717ed 100644 --- a/lib/sqlalchemy/orm/persistence.py +++ b/lib/sqlalchemy/orm/persistence.py @@ -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, diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 36180e8d5b..fb2749fa1f 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -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 diff --git a/test/orm/test_update_delete.py b/test/orm/test_update_delete.py index a3ad37e605..6447f83c3a 100644 --- a/test/orm/test_update_delete.py +++ b/test/orm/test_update_delete.py @@ -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): -- 2.47.3