[sqlalchemy] Get list of items to be flushed in session extension

2010-05-04 Thread chris e
I'm trying to provide functionality in a session extension  for an
class to provide a 'before_flush' method that allows the class to make
changes to the session, and add additional items. To do this I need to
get the list of instances to be flushed to the database, and the order
in which sqlalchemy would commit the changes to the database. I then
reverse the order of this list so that items that the instances are
processed in the reverse order of the database commits. I used to do
this using some of the internal task functionality of UOW(see below),
but that is no longer available in 0.6.0. Any suggestions?

   # from UOW
while True:
ret = False
for task in uow.tasks.values():
for up in list(task.dependencies):
if up.preexecute(uow):
ret = True
if not ret:
break

# HACK we are using a hidden method of UOW here
# run our tasks in reverse order this will
# cause child flushes to be called before
# parent ones
tasks = uow._sort_dependencies()
tasks.reverse()
reprocess = False
for task in tasks :
for element in task.elements :
obj_instance = element.state.obj()
if hasattr(obj_instance, 'before_flush')
and \
 
callable(obj_instance.before_flush) and \
not obj_instance in
self.before_items_processed :
reprocess = \
obj_instance.before_flush() or
reprocess and True or False
 
self.before_items_processed.append(obj_instance)

if reprocess :
self._before_flush_inner(session, instances_in)

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Get list of items to be flushed in session extension

2010-05-04 Thread Michael Bayer

On May 4, 2010, at 5:23 PM, chris e wrote:

 I'm trying to provide functionality in a session extension  for an
 class to provide a 'before_flush' method that allows the class to make
 changes to the session, and add additional items. To do this I need to
 get the list of instances to be flushed to the database, and the order
 in which sqlalchemy would commit the changes to the database. I then
 reverse the order of this list so that items that the instances are
 processed in the reverse order of the database commits. I used to do
 this using some of the internal task functionality of UOW(see below),
 but that is no longer available in 0.6.0. Any suggestions?

getting the order is pretty controversial.what elements of the order 
are significant to you and why isn't this something you are tracking yourself ? 
 wiring business logic onto the details of persistence doesn't seem like a good 
idea.   Or are your flush rules related to SQL -level dependencies, in which 
case why not let the flush handle it, or at least use a MapperExtension so that 
your hooks are invoked within the order of flush  ?

anyway, the order is available in a similar way as before if you peek into 
what UOWTransaction.execute() is calling, namely _generate_actions().It 
would be necessary for you to call this separately yourself which is fairly 
wasteful from a performance standpoint.   it returns a structure that is 
significantly simpler than the old one but you'll still have to poke around 
unitofwork.py to get a feel for it, since this isn't any kind of documented 
public API (you obviously figured out the previous one, this one is simpler).

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.