From 66444e0cfb427cc9a0593c62245ea3fac07584e6 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 17 Sep 2005 19:27:48 +0000 Subject: [PATCH] --- lib/sqlalchemy/attributes.py | 42 ++++++++++++++++++++++------------- lib/sqlalchemy/mapper.py | 4 ++-- lib/sqlalchemy/objectstore.py | 12 +++++++--- lib/sqlalchemy/util.py | 2 +- test/attributes.py | 7 +++--- test/objectstore.py | 1 + 6 files changed, 43 insertions(+), 25 deletions(-) diff --git a/lib/sqlalchemy/attributes.py b/lib/sqlalchemy/attributes.py index 1e1cab893c..bf28111df7 100644 --- a/lib/sqlalchemy/attributes.py +++ b/lib/sqlalchemy/attributes.py @@ -33,7 +33,7 @@ class ListElement(util.HistoryArraySet): self.obj = obj self.key = key util.HistoryArraySet.__init__(self, items) - print "listelement init" + obj.__dict__[key] = self.data def list_value_changed(self, obj, key, listval): pass @@ -73,7 +73,7 @@ class PropHistory(object): if self.orig is not PropHistory.NONE: self.obj.__dict__[self.key] = self.orig self.orig = PropHistory.NONE - def clear_history(self): + def commit(self): self.orig = PropHistory.NONE def added_items(self): if self.orig is not PropHistory.NONE: @@ -134,21 +134,31 @@ class AttributeManager(object): def delete_list_attribute(self, obj, key): pass - def rollback(self, obj): - try: - attributes = self.attribute_history[obj] - for hist in attributes.values(): - hist.rollback() - except KeyError: - pass + def rollback(self, obj = None): + if obj is None: + for attr in self.attribute_history.values(): + for hist in attr.values(): + hist.rollback() + else: + try: + attributes = self.attribute_history[obj] + for hist in attributes.values(): + hist.rollback() + except KeyError: + pass - def clear_history(self, obj): - try: - attributes = self.attribute_history[obj] - for hist in attributes.values(): - hist.clear_history() - except KeyError: - pass + def commit(self, obj = None): + if obj is None: + for attr in self.attribute_history.values(): + for hist in attr.values(): + hist.commit() + else: + try: + attributes = self.attribute_history[obj] + for hist in attributes.values(): + hist.commit() + except KeyError: + pass def get_history(self, obj, key): try: diff --git a/lib/sqlalchemy/mapper.py b/lib/sqlalchemy/mapper.py index b7f5fd53e6..98f225403c 100644 --- a/lib/sqlalchemy/mapper.py +++ b/lib/sqlalchemy/mapper.py @@ -155,7 +155,7 @@ class Mapper(object): def init(self): [prop.init(key, self) for key, prop in self.props.iteritems()] - print "well hi!" + # TODO: get some notion of "primary mapper" going so multiple mappers dont collide self.class_._mapper = self.hashkey def instances(self, cursor, db = None): @@ -713,7 +713,7 @@ class EagerLoader(PropertyLoader): result_list = [] setattr(instance, self.key, result_list) result_list = getattr(instance, self.key) - result_list.clear_history() + result_list.commit() else: result_list = getattr(instance, self.key) diff --git a/lib/sqlalchemy/objectstore.py b/lib/sqlalchemy/objectstore.py index d18dca9008..8d46fd6b6b 100644 --- a/lib/sqlalchemy/objectstore.py +++ b/lib/sqlalchemy/objectstore.py @@ -184,7 +184,6 @@ class UnitOfWork(object): else: for obj in [n for n in self.new] + [d for d in self.dirty]: commit_context.append_task(obj) - print "COMMIT append " + obj.__class__.__name__ + " " + repr(id(obj)) for item in self.modified_lists: obj = item.obj commit_context.append_task(obj) @@ -201,16 +200,23 @@ class UnitOfWork(object): except: for e in engines: e.rollback() - if self.parent: - uow.set(self.parent) + if self.parent: + self.rollback() raise for e in engines: e.commit() commit_context.post_exec() + self.attributes.commit() if self.parent: uow.set(self.parent) + + def rollback(self): + if not self.is_begun: + raise "UOW transaction is not begun" + self.attributes.rollback() + uow.set(self.parent) class UOWTransaction(object): def __init__(self, uow): diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index 1575c28582..ae0442c16a 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -175,7 +175,7 @@ class HistoryArraySet(UserList.UserList): del self.records[item] except KeyError: pass - def clear_history(self): + def commit(self): for key in self.records.keys(): value = self.records[key] if value is False: diff --git a/test/attributes.py b/test/attributes.py index 718f302143..7b3c82d7b6 100644 --- a/test/attributes.py +++ b/test/attributes.py @@ -22,7 +22,7 @@ class AttributesTest(PersistTest): print repr(u.__dict__) self.assert_(u.user_id == 7 and u.user_name == 'john' and u.email_address == 'lala@123.com') - manager.clear_history(u) + manager.commit(u) print repr(u.__dict__) self.assert_(u.user_id == 7 and u.user_name == 'john' and u.email_address == 'lala@123.com') @@ -58,7 +58,7 @@ class AttributesTest(PersistTest): print repr(u.__dict__) self.assert_(u.user_id == 7 and u.user_name == 'john' and u.addresses[0].email_address == 'lala@123.com') - manager.clear_history(u) + manager.commit() print repr(u.__dict__) self.assert_(u.user_id == 7 and u.user_name == 'john' and u.addresses[0].email_address == 'lala@123.com') @@ -70,8 +70,9 @@ class AttributesTest(PersistTest): print repr(u.__dict__) self.assert_(u.user_id == 7 and u.user_name == 'heythere' and u.addresses[0].email_address == 'lala@123.com' and u.addresses[1].email_address == 'foo@bar.com') - manager.rollback(u) + manager.rollback() print repr(u.__dict__) + print repr(u.addresses[0].__dict__) self.assert_(u.user_id == 7 and u.user_name == 'john' and u.addresses[0].email_address == 'lala@123.com') if __name__ == "__main__": diff --git a/test/objectstore.py b/test/objectstore.py index 0684e302ee..96839acccd 100644 --- a/test/objectstore.py +++ b/test/objectstore.py @@ -32,6 +32,7 @@ class HistoryTest(AssertMixin): u.addresses.append(Address()) u.addresses[1].email_address = 'there' print repr(u.__dict__) + print repr(u.addresses) m.rollback(u) print repr(u.__dict__) -- 2.47.2