From 84003a8d402c5d7539cf2d53f4061cde62d04413 Mon Sep 17 00:00:00 2001 From: Martijn Faassen Date: Thu, 6 Nov 2008 06:12:11 +0000 Subject: add two new hooks for bulk operations to SessionExtension: * after_bulk_delete * after_bulk_update --- lib/sqlalchemy/orm/interfaces.py | 20 ++++++++++++++++++++ lib/sqlalchemy/orm/query.py | 6 ++++++ 2 files changed, 26 insertions(+) (limited to 'lib') diff --git a/lib/sqlalchemy/orm/interfaces.py b/lib/sqlalchemy/orm/interfaces.py index bd934ce13..609ed0ca1 100644 --- a/lib/sqlalchemy/orm/interfaces.py +++ b/lib/sqlalchemy/orm/interfaces.py @@ -307,6 +307,26 @@ class SessionExtension(object): This is called after an add, delete or merge. """ + def after_bulk_update(self, session, query, query_context, result): + """Execute after a bulk update operation to the session. + + This is called after a session.query(...).update() + + `query` is the query object that this update operation was called on. + `query_context` was the query context object. + `result` is the result object returned from the bulk operation. + """ + + def after_bulk_delete(self, session, query, query_context, result): + """Execute after a bulk delete operation to the session. + + This is called after a session.query(...).delete() + + `query` is the query object that this delete operation was called on. + `query_context` was the query context object. + `result` is the result object returned from the bulk operation. + """ + class MapperProperty(object): """Manage the relationship of a ``Mapper`` to a single class attribute, as well as that attribute as it appears on individual diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index ec378d9c6..aa30f1517 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -1348,6 +1348,9 @@ class Query(object): if identity_key in session.identity_map: session._remove_newly_deleted(attributes.instance_state(session.identity_map[identity_key])) + for ext in session.extensions: + ext.after_bulk_delete(session, self, context, result) + return result.rowcount def update(self, values, synchronize_session='expire'): @@ -1442,6 +1445,9 @@ class Query(object): if identity_key in session.identity_map: session.expire(session.identity_map[identity_key], values.keys()) + for ext in session.extensions: + ext.after_bulk_update(session, self, context, result) + return result.rowcount def _compile_context(self, labels=True): -- cgit v1.2.1