[Sqlalchemy-commits] [1502] sqlalchemy/branches/schema/lib/sqlalchemy/orm/unitofwork.py: dependency
Brought to you by:
zzzeek
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:':';} #msg dl, #msg dt, #msg ul, #msg li { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[1502] sqlalchemy/branches/schema/lib/sqlalchemy/orm/unitofwork.py: dependency processing is more complete, checks carefully for individual objects that may have</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>1502</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2006-05-24 17:46:59 -0500 (Wed, 24 May 2006)</dd> </dl> <h3>Log Message</h3> <pre>dependency processing is more complete, checks carefully for individual objects that may have moved from "saved" to "deleted"</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemybranchesschemalibsqlalchemyormunitofworkpy">sqlalchemy/branches/schema/lib/sqlalchemy/orm/unitofwork.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemybranchesschemalibsqlalchemyormunitofworkpy"></a> <div class="modfile"><h4>Modified: sqlalchemy/branches/schema/lib/sqlalchemy/orm/unitofwork.py (1501 => 1502)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/branches/schema/lib/sqlalchemy/orm/unitofwork.py 2006-05-24 21:57:05 UTC (rev 1501) +++ sqlalchemy/branches/schema/lib/sqlalchemy/orm/unitofwork.py 2006-05-24 22:46:59 UTC (rev 1502) </span><span class="lines">@@ -356,15 +356,11 @@ </span><span class="cx"> self._mark_modified() </span><span class="cx"> </span><span class="cx"> def execute(self, echo=False): </span><del>- # tell all related mappers to set up dependency processors - #for task in self.tasks.values(): - # task.mapper.register_dependencies(self) - </del><span class="cx"> # pre-execute dependency processors. this process may </span><del>- # result in new tasks and/or dependency processors being added, </del><ins>+ # result in new tasks, objects and/or dependency processors being added, </ins><span class="cx"> # particularly with 'delete-orphan' cascade rules. </span><del>- # keep running through the full list of tasks until no more - # new objects get processed. </del><ins>+ # keep running through the full list of tasks until all + # objects have been processed. </ins><span class="cx"> while True: </span><span class="cx"> ret = False </span><span class="cx"> for task in self.tasks.values(): </span><span class="lines">@@ -437,13 +433,41 @@ </span><span class="cx"> """an element within a UOWTask. corresponds to a single object instance </span><span class="cx"> to be saved, deleted, or just part of the transaction as a placeholder for </span><span class="cx"> further dependencies (i.e. 'listonly'). </span><del>- in the case of self-referential mappers, may also store a "childtask", which is a - UOWTask containing objects dependent on this element's object instance.""" </del><ins>+ in the case of self-referential mappers, may also store a list of childtasks, + further UOWTasks containing objects dependent on this element's object instance.""" </ins><span class="cx"> def __init__(self, obj): </span><span class="cx"> self.obj = obj </span><del>- self.listonly = True </del><ins>+ self.__listonly = True </ins><span class="cx"> self.childtasks = [] </span><del>- self.isdelete = False </del><ins>+ self.__isdelete = False + self.__preprocessed = {} + def _get_listonly(self): + return self.__listonly + def _set_listonly(self, value): + """set_listonly is a one-way setter, will only go from True to False.""" + if not value and self.__listonly: + self.__listonly = False + self.clear_preprocessed() + def _get_isdelete(self): + return self.__isdelete + def _set_isdelete(self, value): + if self.__isdelete is not value: + self.__isdelete = value + self.clear_preprocessed() + listonly = property(_get_listonly, _set_listonly) + isdelete = property(_get_isdelete, _set_isdelete) + + def mark_preprocessed(self, processor): + """marks this element as "preprocessed" by a particular UOWDependencyProcessor. preprocessing is the step + which sweeps through all the relationships on all the objects in the flush transaction and adds other objects + which are also affected, In some cases it can switch an object from "tosave" to "todelete". changes to the state + of this UOWTaskElement will reset all "preprocessed" flags, causing it to be preprocessed again. When all UOWTaskElements + have been fully preprocessed by all UOWDependencyProcessors, then the topological sort can be done.""" + self.__preprocessed[processor] = True + def is_preprocessed(self, processor): + return self.__preprocessed.get(processor, False) + def clear_preprocessed(self): + self.__preprocessed.clear() </ins><span class="cx"> def __repr__(self): </span><span class="cx"> return "UOWTaskElement/%d: %s/%d %s" % (id(self), self.obj.__class__.__name__, id(self.obj), (self.listonly and 'listonly' or (self.isdelete and 'delete' or 'save')) ) </span><span class="cx"> </span><span class="lines">@@ -454,27 +478,25 @@ </span><span class="cx"> def __init__(self, processor, targettask): </span><span class="cx"> self.processor = processor </span><span class="cx"> self.targettask = targettask </span><del>- self.preprocessed = Set() - </del><ins>+ </ins><span class="cx"> def preexecute(self, trans): </span><ins>+ """traverses all objects handled by this dependency processor and locates additional objects which should be + part of the transaction, such as those affected deletes, orphans to be deleted, etc. Returns True if any + objects were preprocessed, or False if no objects were preprocessed.""" + def getobj(elem): + elem.mark_preprocessed(self) + return elem.obj + </ins><span class="cx"> ret = False </span><del>- elements = [elem.obj for elem in self.targettask.tosave_elements if elem.obj is not None and elem.obj not in self.preprocessed] </del><ins>+ elements = [getobj(elem) for elem in self.targettask.tosave_elements if elem.obj is not None and not elem.is_preprocessed(self)] </ins><span class="cx"> if len(elements): </span><span class="cx"> ret = True </span><del>- todo = [] - for e in elements: - self.preprocessed.add(e) - todo.append(e) - self.processor.preprocess_dependencies(self.targettask, todo, trans, delete=False) </del><ins>+ self.processor.preprocess_dependencies(self.targettask, elements, trans, delete=False) </ins><span class="cx"> </span><del>- elements = [elem.obj for elem in self.targettask.todelete_elements if elem.obj is not None and elem.obj not in self.preprocessed] </del><ins>+ elements = [getobj(elem) for elem in self.targettask.todelete_elements if elem.obj is not None and not elem.is_preprocessed(self)] </ins><span class="cx"> if len(elements): </span><span class="cx"> ret = True </span><del>- todo = [] - for e in elements: - self.preprocessed.add(e) - todo.append(e) - self.processor.preprocess_dependencies(self.targettask, todo, trans, delete=True) </del><ins>+ self.processor.preprocess_dependencies(self.targettask, elements, trans, delete=True) </ins><span class="cx"> return ret </span><span class="cx"> </span><span class="cx"> def execute(self, trans, delete): </span><span class="lines">@@ -527,6 +549,8 @@ </span><span class="cx"> rec.childtasks.append(childtask) </span><span class="cx"> if isdelete: </span><span class="cx"> rec.isdelete = True </span><ins>+ #if not childtask: + # rec.preprocessed = False </ins><span class="cx"> return retval </span><span class="cx"> </span><span class="cx"> def append_postupdate(self, obj): </span></span></pre> </div> </div> </body> </html> |