From: Mike Bayer Date: Fri, 16 Sep 2005 06:05:40 +0000 (+0000) Subject: (no commit message) X-Git-Tag: rel_0_1_0~722 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=535943011eec88d72d84abd7fa954476b96deb7a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git --- diff --git a/lib/sqlalchemy/mapper.py b/lib/sqlalchemy/mapper.py index b7d2d96e98..b5598b9553 100644 --- a/lib/sqlalchemy/mapper.py +++ b/lib/sqlalchemy/mapper.py @@ -164,8 +164,8 @@ class Mapper(object): self._instance(row, imap, result) # store new stuff in the identity map - for key, value in imap.iteritems(): - objectstore.put(key, value) + for value in imap.values(): + objectstore.uow().register_clean(value) return result @@ -190,10 +190,13 @@ class Mapper(object): return None def put(self, instance): - key = objectstore.get_id_key(tuple([self._getattrbycolumn(instance, column) for column in self.primary_keys[self.selectable]]), self.class_, self.table) + key = self.identity_key(instance) objectstore.put(key, instance, self.scope) return key + def identity_key(self, instance): + return objectstore.get_id_key(tuple([self._getattrbycolumn(instance, column) for column in self.primary_keys[self.selectable]]), self.class_, self.table) + def compile(self, whereclause = None, **options): """works like select, except returns the SQL statement object without compiling or executing it""" @@ -319,16 +322,19 @@ class Mapper(object): # been exposed to being modified by the application. identitykey = self._identity_key(row) if objectstore.has_key(identitykey): + instance = objectstore.get(identitykey) if result is not None: - result.append_nohistory(objectstore.get(identitykey)) - else: - return instance + result.append_nohistory(instance) + + return instance # look in result-local identitymap for it. exists = imap.has_key(identitykey) if not exists: instance = self.class_() instance._mapper = self.hashkey + instance._instance_key = identitykey + imap[identitykey] = instance isnew = True else: @@ -547,7 +553,7 @@ class PropertyLoader(MapperProperty): for child in childlist.added_items(): associationrow = {} self.primaryjoin.accept_visitor(setter) - uow.register_saved(childlist) + uow.register_saved_object(childlist) # TODO: deleted items elif self.foreignkey.table == self.parent.table: for child in deplist: @@ -555,7 +561,7 @@ class PropertyLoader(MapperProperty): for obj in childlist.added_items(): associationrow = {} self.primaryjoin.accept_visitor(setter) - uow.register_saved(childlist) + uow.register_saved_object(childlist) # TODO: deleted items else: raise " no foreign key ?" diff --git a/lib/sqlalchemy/objectstore.py b/lib/sqlalchemy/objectstore.py index 8b64091c07..7a7634f251 100644 --- a/lib/sqlalchemy/objectstore.py +++ b/lib/sqlalchemy/objectstore.py @@ -103,17 +103,16 @@ class UOWListElement(util.HistoryArraySet): def __init__(self, obj, items = None): util.HistoryArraySet.__init__(self, items) self.obj = weakref.ref(obj) - self.list = UOWListElement def _setrecord(self, item): res = util.HistoryArraySet._setrecord(self, item) if res: - uow().modified_lists.append(self.list) + uow().modified_lists.append(self) return res def _delrecord(self, item): res = util.HistoryArraySet._delrecord(self, item) if res: - uow().modified_lists.append(self.list) + uow().modified_lists.append(self) return res class UnitOfWork(object): @@ -197,6 +196,7 @@ class UnitOfWork(object): except KeyError: pass put(obj._instance_key, obj, scope=scope) + # TODO: get lists off the object and make sure theyre clean too ? def register_new(self, obj): self.new.append(obj) @@ -233,9 +233,10 @@ class UnitOfWork(object): for item in self.modified_lists: obj = item.obj() self.commit_context.append_task(obj) + print "obj: " + repr(id(obj)) + obj.__class__.__name__ for task in self.commit_context.tasks.values(): - task.mapper.register_dependencies(util.HashSet(task.objects + task.lists), self) + task.mapper.register_dependencies(task.objects, self) mapperlist = self.commit_context.tasks.values() def compare(a, b): @@ -264,7 +265,10 @@ class UnitOfWork(object): self.register_clean(obj) for obj in self.commit_context.saved_lists: - del self.modified_lists[obj] + try: + del self.modified_lists[obj] + except KeyError: + pass self.commit_context = None # TODO: deleted stuff @@ -284,7 +288,6 @@ class UnitOfWork(object): task = self.commit_context.get_task_by_mapper(mapper) if processor is not None: task.dependencies.append((processor, stuff_to_process)) - class UOWTransaction(object): def __init__(self): @@ -308,8 +311,7 @@ class UOWTransaction(object): class UOWTask(object): def __init__(self, mapper): self.mapper = mapper - self.objects = [] - self.lists = [] + self.objects = util.HashSet() self.dependencies = [] uow = util.ScopedRegistry(lambda: UnitOfWork(), "thread") \ No newline at end of file diff --git a/test/mapper.py b/test/mapper.py index 59967673ec..46cc211a89 100644 --- a/test/mapper.py +++ b/test/mapper.py @@ -310,6 +310,8 @@ class SaveTest(AssertMixin): objectstore.uow().register_new(u) + objectstore.uow().commit(u) + print "OK" objectstore.uow().commit() # assert the first one retreives the same from the identity map