]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
add two new hooks for bulk operations to SessionExtension:
authorMartijn Faassen <faassen@startifact.com>
Thu, 6 Nov 2008 06:12:11 +0000 (06:12 +0000)
committerMartijn Faassen <faassen@startifact.com>
Thu, 6 Nov 2008 06:12:11 +0000 (06:12 +0000)
* after_bulk_delete

* after_bulk_update

CHANGES
lib/sqlalchemy/orm/interfaces.py
lib/sqlalchemy/orm/query.py
test/orm/session.py

diff --git a/CHANGES b/CHANGES
index a2121429864710ab4cba3481f76f4d18cbfe4310..3bf18cfebae46399c4853bac5e55e890040aec6b 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -7,6 +7,12 @@ CHANGES
 0.5.0rc3
 ========
 - orm
+
+    - Added two new hooks to SessionExtension: after_bulk_delete and
+      after_bulk_update. after_bulk_delete is called after a bulk delete()
+      operation on a query. after_bulk_update is called after a bulk update()
+      operation on a query.
+
     - "not equals" comparisons of simple many-to-one relation to an
       instance will not drop into an EXISTS clause and will compare
       foreign key columns instead.
index bd934ce13b33f6734e6d37643209a298c511d3b8..609ed0ca11fbdc2f034fc55c68ece9e273c87954 100644 (file)
@@ -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
index ec378d9c6d1fdc6d895d9ed243b4b7cfbc36b73a..aa30f151744bb9ce4043071b72757cf37322199d 100644 (file)
@@ -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):
index 58c4888a761aca2be1aa329c254e61139e9a7745..f32736b32ed984613d9fe1a42b683855f828667a 100644 (file)
@@ -898,6 +898,10 @@ class SessionTest(_fixtures.FixtureTest):
                 log.append('after_begin')
             def after_attach(self, session, instance):
                 log.append('after_attach')
+            def after_bulk_update(self, session, query, query_context, result):
+                log.append('after_bulk_update')
+            def after_bulk_delete(self, session, query, query_context, result):
+                log.append('after_bulk_delete')
 
         sess = create_session(extension = MyExt())
         u = User(name='u1')
@@ -920,6 +924,14 @@ class SessionTest(_fixtures.FixtureTest):
         log = []
         sess.commit()
         assert log == ['before_commit', 'after_commit']
+
+        log = []
+        sess.query(User).delete()
+        assert log == ['after_begin', 'after_bulk_delete']
+
+        log = []
+        sess.query(User).update({'name': 'foo'})
+        assert log == ['after_bulk_update']
         
         log = []
         sess = create_session(autocommit=False, extension=MyExt(), bind=testing.db)