summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/mapping
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2006-03-26 21:44:22 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2006-03-26 21:44:22 +0000
commitcb3be7803bd2d8071ead35d5f053c90f706a678b (patch)
treec2412d863c4f07b149d41a863b02f1f26dcc0925 /lib/sqlalchemy/mapping
parentc0775bdadc0ad37b7823379c5ab03df98b63da44 (diff)
downloadsqlalchemy-cb3be7803bd2d8071ead35d5f053c90f706a678b.tar.gz
rework to expire() to make it smarter. when you expire(), history is immediately removed as well as explicit from dirty/deleted lists. this also changes uow.rollback_object() to remove from those lists, which is strange that it didnt do that before. anyway the mapper, when selecting and creating instances, asks the uow if this already identity-mapped instance is expired, and if so refreshes it on the fly, saving the need for the re-_get() operation, if some other query happens to touch upon the expired object. unit test added to confirm this.
Diffstat (limited to 'lib/sqlalchemy/mapping')
-rw-r--r--lib/sqlalchemy/mapping/mapper.py8
-rw-r--r--lib/sqlalchemy/mapping/objectstore.py2
-rw-r--r--lib/sqlalchemy/mapping/unitofwork.py20
3 files changed, 25 insertions, 5 deletions
diff --git a/lib/sqlalchemy/mapping/mapper.py b/lib/sqlalchemy/mapping/mapper.py
index 1efdb649a..8ff28cf56 100644
--- a/lib/sqlalchemy/mapping/mapper.py
+++ b/lib/sqlalchemy/mapping/mapper.py
@@ -816,16 +816,16 @@ class Mapper(object):
# including modifying any of its related items lists, as its already
# been exposed to being modified by the application.
identitykey = self._identity_key(row)
- if objectstore.get_session().has_key(identitykey):
- instance = objectstore.get_session()._get(identitykey)
+ sess = objectstore.get_session()
+ if sess.has_key(identitykey):
+ instance = sess._get(identitykey)
isnew = False
- if populate_existing:
+ if populate_existing or sess.is_expired(instance, unexpire=True):
if not imap.has_key(identitykey):
imap[identitykey] = instance
for prop in self.props.values():
prop.execute(instance, row, identitykey, imap, True)
-
if self.extension.append_result(self, row, imap, result, instance, isnew, populate_existing=populate_existing):
if result is not None:
result.append_nohistory(instance)
diff --git a/lib/sqlalchemy/mapping/objectstore.py b/lib/sqlalchemy/mapping/objectstore.py
index d6b476562..7827c1b78 100644
--- a/lib/sqlalchemy/mapping/objectstore.py
+++ b/lib/sqlalchemy/mapping/objectstore.py
@@ -166,7 +166,7 @@ class Session(object):
"""invalidates the data in the given objects and sets them to refresh themselves
the next time they are requested."""
for o in obj:
- global_attributes.trigger_history(o, lambda: refresh(o))
+ self.uow.expire(o)
def expunge(self, *obj):
for o in obj:
diff --git a/lib/sqlalchemy/mapping/unitofwork.py b/lib/sqlalchemy/mapping/unitofwork.py
index fc589f65b..0b392b447 100644
--- a/lib/sqlalchemy/mapping/unitofwork.py
+++ b/lib/sqlalchemy/mapping/unitofwork.py
@@ -100,6 +100,18 @@ class UnitOfWork(object):
self.rollback_object(obj)
object_mapper(obj)._get(obj._instance_key, reload=True)
+ def expire(self, obj):
+ self.rollback_object(obj)
+ def exp():
+ object_mapper(obj)._get(obj._instance_key, reload=True)
+ global_attributes.trigger_history(obj, exp)
+
+ def is_expired(self, obj, unexpire=False):
+ ret = global_attributes.has_trigger(obj)
+ if ret and unexpire:
+ global_attributes.untrigger_history(obj)
+ return ret
+
def has_key(self, key):
"""returns True if the given key is present in this UnitOfWork's identity map."""
return self.identity_map.has_key(key)
@@ -247,6 +259,14 @@ class UnitOfWork(object):
def rollback_object(self, obj):
"""'rolls back' the attributes that have been changed on an object instance."""
self.attributes.rollback(obj)
+ try:
+ del self.dirty[obj]
+ except KeyError:
+ pass
+ try:
+ del self.deleted[obj]
+ except KeyError:
+ pass
class UOWTransaction(object):
"""handles the details of organizing and executing transaction tasks