mirror of
https://github.com/sqlalchemy/sqlalchemy.git
synced 2026-05-29 20:14:55 -04:00
Merge remote-tracking branch 'origin/pr/164' into pr164
This commit is contained in:
@@ -1170,17 +1170,18 @@ 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.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):
|
||||
@@ -1215,7 +1216,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,
|
||||
|
||||
@@ -2811,7 +2811,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.
|
||||
@@ -2920,8 +2920,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
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -907,6 +907,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):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user