Commits

Anonymous committed 2b84065

add two new hooks for bulk operations to SessionExtension:

* after_bulk_delete

* after_bulk_update

Comments (0)

Files changed (4)

 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.

lib/sqlalchemy/orm/interfaces.py

         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

lib/sqlalchemy/orm/query.py

                 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'):
                 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):

test/orm/session.py

                 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')
         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)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.