summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/unitofwork.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-12-18 12:14:23 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2010-12-18 12:14:23 -0500
commit182fc1a7c6fcb39593fed982b3e40de59712ec99 (patch)
tree355cd8834878f7155eb0ea7c97b27b275f560c2a /lib/sqlalchemy/orm/unitofwork.py
parent2e7a54d6fbfe9260887a0eb848e296e3b5e59c47 (diff)
downloadsqlalchemy-182fc1a7c6fcb39593fed982b3e40de59712ec99.tar.gz
- some good inlinings to the whole cascade_iterator() thing.
cascade_iterator() should probably not yield the "instance" at all and only deal in states. 30-40K methods taken off the orm2010 test.
Diffstat (limited to 'lib/sqlalchemy/orm/unitofwork.py')
-rw-r--r--lib/sqlalchemy/orm/unitofwork.py36
1 files changed, 22 insertions, 14 deletions
diff --git a/lib/sqlalchemy/orm/unitofwork.py b/lib/sqlalchemy/orm/unitofwork.py
index 1e1eda4a3..17b73e5f9 100644
--- a/lib/sqlalchemy/orm/unitofwork.py
+++ b/lib/sqlalchemy/orm/unitofwork.py
@@ -28,7 +28,9 @@ class UOWEventHandler(interfaces.AttributeExtension):
def __init__(self, key):
self.key = key
-
+
+ # TODO: migrate these to unwrapped events
+
def append(self, state, item, initiator):
# process "save_update" cascade rules for when
# an instance is appended to the list of another instance
@@ -36,10 +38,11 @@ class UOWEventHandler(interfaces.AttributeExtension):
sess = session._state_session(state)
if sess:
prop = _state_mapper(state)._props[self.key]
+ item_state = attributes.instance_state(item)
if prop.cascade.save_update and \
(prop.cascade_backrefs or self.key == initiator.key) and \
- item not in sess:
- sess.add(item)
+ not sess._contains_state(item_state):
+ sess._save_or_update_state(item_state)
return item
def remove(self, state, item, initiator):
@@ -47,9 +50,10 @@ class UOWEventHandler(interfaces.AttributeExtension):
if sess:
prop = _state_mapper(state)._props[self.key]
# expunge pending orphans
+ item_state = attributes.instance_state(item)
if prop.cascade.delete_orphan and \
- item in sess.new and \
- prop.mapper._is_orphan(attributes.instance_state(item)):
+ item_state in sess._new and \
+ prop.mapper._is_orphan(item_state):
sess.expunge(item)
def set(self, state, newvalue, oldvalue, initiator):
@@ -61,15 +65,19 @@ class UOWEventHandler(interfaces.AttributeExtension):
sess = session._state_session(state)
if sess:
prop = _state_mapper(state)._props[self.key]
- if newvalue is not None and \
- prop.cascade.save_update and \
- (prop.cascade_backrefs or self.key == initiator.key) and \
- newvalue not in sess:
- sess.add(newvalue)
- if prop.cascade.delete_orphan and \
- oldvalue in sess.new and \
- prop.mapper._is_orphan(attributes.instance_state(oldvalue)):
- sess.expunge(oldvalue)
+ if newvalue is not None:
+ newvalue_state = attributes.instance_state(newvalue)
+ if prop.cascade.save_update and \
+ (prop.cascade_backrefs or self.key == initiator.key) and \
+ not sess._contains_state(newvalue_state):
+ sess._save_or_update_state(newvalue_state)
+
+ if oldvalue is not None and prop.cascade.delete_orphan:
+ oldvalue_state = attributes.instance_state(oldvalue)
+
+ if oldvalue_state in sess._new and \
+ prop.mapper._is_orphan(oldvalue_state):
+ sess.expunge(oldvalue)
return newvalue