m +&Gc@sVdZdklZlZlZdklZdklZl Z dk Tdk Z dk Z dk Z deifdYZdeifd YZd eifd YZd efd YZdefdYZdefdYZdefdYZdefdYZdefdYZdeifdYZeZdS(sfthe internals for the Unit Of Work system. includes hooks into the attributes package enabling the routing of change events to Unit Of Work objects, as well as the flush() mechanism which creates a dependency structure that executes change operations. a Unit of Work is essentially a system of maintaining a graph of in-memory objects and their modified state. Objects are maintained as unique against their primary key identity using an "identity map" pattern. The Unit of Work then maintains lists of objects that are new, dirty, or deleted and provides the capability to flush all those changes at once. (sutilsloggings topological(s attributes(s object_mappers class_mapper(t*NtUOWEventHandlercBs5tZdZedZdZdZdZRS(sPan event handler added to all class attributes which handles session operations.cCs||_||_||_dS(N(tkeytselftclass_tcascade(RRRR((t=/home/holguin2/public_html/spyce/sqlalchemy/orm/unitofwork.pyt__init__s  cCst|}|dj om|idj oY|iioL||jo?t|}|i |i }|i i }|i|d|qndS(Nt entity_name(tobject_sessiontobjtsesstNoneRRt save_updatetitemt object_mappertmappertpropsRtpropRtenametsave_or_update(RteventR RRR RR((Rtappends  *  cCsdS(N((RRR R((Rtdelete)sc Cst|}|dj oz|dj oi|idj oY|iioL||jo?t|}|i |i }|i i }|i|d|qndS(NR(R R R R tnewvalueRRR RRRRRRRR( RRR RtoldvalueRR RR((Rtset.s  7  (t__name__t __module__t__doc__R RRRR(((RRs   t UOWPropertycBs2tZdZeedZedddZRS(soverride InstrumentedAttribute to provide an extra AttributeExtension to all managed attributes as well as the 'property' property.c Ksmti|pg}|idt||d|tt |i |||||d|| ||_dS(NiRt extension(tutiltto_listRtinsertRRRRtsuperRRRtmanagertuselistt callable_t typecallabletkwargs( RR$RRR%R&R'RRR(((RR;s+cCst|ii|iS(N(t class_mappertsRRR(R*((RtAstdocs?returns the MapperProperty object associated with this property(RRRR Rtproperty(((RR8s tUOWAttributeManagercBstZdZdZRS(s]override AttributeManager to provide the UOWProperty instance for all InstrumentedAttributes.cKst|||||||S(N(RRRRR%R&R'R((RRRR%R&R'R(((Rt create_propEs(RRRR/(((RR.Cs t UnitOfWorkcBsztZdZedZeiZdZdZ dZ dZ dZ dZ dZd Zed ZRS( smain UOW object which stores lists of dirty/new/deleted objects. provides top-level "flush" functionality as well as the transaction boundaries with the SQLEngine(s) involved in a write operation.cCs]|dj o ||_nti|_ti|_ti|_t i ||_ dS(N( t identity_mapR RtweakreftWeakValueDictionaryR tSettnewtdeletedtloggingtinstance_loggertlogger(RR1((RRLs   cCsyt|do|i|i=ny|ii|Wntj onXy|ii|Wntj onXdS(Nt _instance_key( thasattrR RR1R:R6tremovetKeyErrorR5(RR ((Rt_remove_deletedXscCsft|do|ii|i p!t|d o*||ijotdt|ndS(NR:s<Instance '%s' is not attached or pending within this session( R;R RR1thas_keyR:R5tInvalidRequestErrortrepr(RR ((Rt _validate_objdsHcKsti||||dS(N(tattribute_managertregister_attributeRRR%R((RRRR%R(((RRDiscKsti|||||dS(N(RCt set_callableR RtfuncR%R((RR RRFR%R(((Rtregister_callablelscCsy|ii|Wntj onXt|dp"t|}|i||_ nyt |dWnt j onX||i |i RBRDRGRMRPRQRVRr(((RR0Hs         R[cBstZdZdZeiZeeeedZ dZ dZ edZ dZ dZdZd Zd Zd ZRS( svhandles the details of organizing and executing transaction tasks during a UnitOfWork object's flush() operation.cCsU||_||_ti|_h|_h|_ti ||_ |i |_ dS(N( tuowRR\R R4tmapperst dependenciesttasksR7R8R9RZ(RRtR\((RRs    c Ks|ii|t|}|ii||i|}|o|i ||dSn|i odSn|i||d||dS(sadds an object to this UOWTransaction to be updated in the database. 'isdelete' indicates whether the object is to be deleted or saved (update/inserted). 'listonly', indicates that only this object's dependency relationships should be refreshed/updated to reflect a recent save/upcoming delete operation, but not a full save/delete operation on the object itself, unless an additional save/delete registration is entered for the object.NRW(RRtRBR RRRuRNtget_task_by_mapperttaskt postupdatetappend_postupdatetpost_update_colstcircularRtlistonlyRWR(( RR RWR~RzR|R(RRy((RRis  cCs@t|}|i|}||ijo|i|ndS(N(RR RRRxRyR^R(RR RRy((Rtunregister_objects cCs(t|}|i|}|i|S(N(RR RRRxRyt is_deleted(RR RRy((RRs cCs[y|i|SWnEtj o9|odSnt||}|ii||SnXdS(sXevery individual mapper involved in the transaction has a single corresponding UOWTask object, which stores all the operations involved with that mapper as well as operations dependent on those operations. this method returns or creates the single per-transaction instance of UOWTask that exists for that mapper.N( RRwRR=t dontcreateR tUOWTaskRytregister_dependencies(RRRRy((RRxscCs;|ii}|ii}t|i||fR RM(RRyR((RRqJs csidi}tit|idt}i i dt ||}|S(screates a hierarchical tree of dependent tasks. the root node is returned. when the root node is executed, it also executes its child tasks recursively.cs|djodSni|i}|idj oLg}x*|iD]}|i i|iqGW|i ||_ nx>|i D]3}|}|dj o|ii |qqW|S(N(tnodeR RRxRRytcyclesRwtnRt_sort_circular_dependenciesR}tchildrentchildt sort_hierttt childtasks(RRyRRwRR(RR(RRXs     tallow_all_cyclessDependency sort: N(RRRRutDependencySorterRvRftsortRjRR9tdebugtstrRy(RRuRRyR((RRRRUs $ cCsFti}x3|iiD]"}|ii}|i |qW|S(sreturns a list of UOWTasks whose mappers are not inheriting from the mapper of another UOWTask. i.e., this returns the root UOWTasks for all the inheritance hierarchies represented in this UOWTransaction.N( R R4RuRRwRSRyRRtbaseRN(RRuRyR((RRms (RRRRR7RsRZRlR RiRRRxRRRoRqRR(((RR[s        RcBstZdZedZdZeeedZdZdZ dZ dZ dZ d Z ed Zd Zd Zed ZedZedZedZedZedZedZedZdZdZdZRS(sVrepresents the full list of objects that are to be saved/deleted by a specific Mapper.cCsp|p||i||iiD]%\}}|ii|i|qWnh}x#|iD]} | ||}qW|S(NRR~RWR(tobject_to_original_taskRRtoriginating_taskt nexttasksRR RRRRRt parenttaskRRlR^RWR~RvR?t iteritemsRtdeptaskRRNtbranchtndRRtmake_task_treett2( RRRRRRRRRR(RRvRR(RRs    03 ! RN(6t allobjectsRRyRRRRjteR ttuplesR R4t extradeplistRvRRtdeps_by_targettaskRRRRRRRRRlt taskelementRRRWtget_object_dependenciesRmt childlistR RxRRRt added_itemstunchanged_itemst deleted_itemstoRtwhose_dependent_on_whotwhosdepRRRRRRRtdRNR(RRmRRRRRRRRRRRWRvRR RRyRRRRRRRRRRRR((RRmRRvRRRRs8              ((0 + cCs/ti}dk}|i|||iS(N(tStringIOtbuft uowdumpert UOWDumperRtgetvalue(RRR((RRs  cCsw|idj oM|iiidjo$|iiid|iii}qct|i}nd}dt ||fS(NtMappert/s(none)sUOWTask(%d) Mapper: '%s'( RRR t __class__RRt local_tabletnameRAtid(RR((Rt__repr__s $(RRRR RRRlRR{RRRRoRRRRRR-tpolymorphic_tosave_elementstpolymorphic_todelete_elementsttosave_elementsttodelete_elementsttosave_objectsRRRRR(((RRvs0  (           RcBs}tZdZdZdZdZdZdZeeeZ eeeZ dZ dZ dZ d ZRS( shan element within a UOWTask. corresponds to a single object instance to be saved, deleted, or just part of the transaction as a placeholder for further dependencies (i.e. 'listonly'). 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.cCs1||_t|_g|_t|_h|_dS(N(R RRjt_UOWTaskElement__listonlyRRlt_UOWTaskElement__isdeletet_UOWTaskElement__preprocessed(RR ((RRs     cCs|iS(N(RR(R((Rt _get_listonlyscCs-| o!|iot|_|indS(sBset_listonly is a one-way setter, will only go from True to False.N(tvalueRRRltclear_preprocessed(RR((Rt _set_listonlys cCs|iS(N(RR(R((Rt _get_isdeletescCs+|i|j o||_|indS(N(RRRR(RR((Rt _set_isdeletes cCst|i|