summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/dynamic.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2007-12-08 18:58:03 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2007-12-08 18:58:03 +0000
commit8693d4b2876e9239cf48bbc42a7ffaa11c01b506 (patch)
tree6438a7f4a319c4e1f25376fb004de6fb73bfd7c0 /lib/sqlalchemy/orm/dynamic.py
parent78bb82a44b7f382c6cfeab0cfc7f932e68c4de86 (diff)
downloadsqlalchemy-8693d4b2876e9239cf48bbc42a7ffaa11c01b506.tar.gz
- flush() refactor merged from uow_nontree branch r3871-r3885
- topological.py cleaned up, presents three public facing functions which return list/tuple based structures, without exposing any internals. only the third function returns the "hierarchical" structure. when results include "cycles" or "child" items, 2- or 3- tuples are used to represent results. - unitofwork uses InstanceState almost exclusively now. new and deleted lists are now dicts which ref the actual object to provide a strong ref for the duration that they're in those lists. IdentitySet is only used for the public facing versions of "new" and "deleted". - unitofwork topological sort no longer uses the "hierarchical" version of the sort for the base sort, only for the "per-object" secondary sort where it still helps to group non-dependent operations together and provides expected insert order. the default sort deals with UOWTasks in a straight list and is greatly simplified. Tests all pass but need to see if svilen's stuff still works, one block of code in _sort_cyclical_dependencies() seems to not be needed anywhere but i definitely put it there for a reason at some point; if not hopefully we can derive more test coverage from that. - the UOWEventHandler is only applied to object-storing attributes, not scalar (i.e. column-based) ones. cuts out a ton of overhead when setting non-object based attributes. - InstanceState also used throughout the flush process, i.e. dependency.py, mapper.save_obj()/delete_obj(), sync.execute() all expect InstanceState objects in most cases now. - mapper/property cascade_iterator() takes InstanceState as its argument, but still returns lists of object instances so that they are not dereferenced. - a few tricks needed when dealing with InstanceState, i.e. when loading a list of items that are possibly fresh from the DB, you *have* to get the actual objects into a strong-referencing datastructure else they fall out of scope immediately. dependency.py caches lists of dependent objects which it loads now (i.e. history collections). - AttributeHistory is gone, replaced by a function that returns a 3-tuple of added, unchanged, deleted. these collections still reference the object instances directly for the strong-referencing reasons mentiontioned, but it uses less IdentitySet logic to generate.
Diffstat (limited to 'lib/sqlalchemy/orm/dynamic.py')
-rw-r--r--lib/sqlalchemy/orm/dynamic.py56
1 files changed, 24 insertions, 32 deletions
diff --git a/lib/sqlalchemy/orm/dynamic.py b/lib/sqlalchemy/orm/dynamic.py
index e55703c42..af1be28bc 100644
--- a/lib/sqlalchemy/orm/dynamic.py
+++ b/lib/sqlalchemy/orm/dynamic.py
@@ -13,7 +13,7 @@ class DynamicAttributeImpl(attributes.AttributeImpl):
def get(self, state, passive=False):
if passive:
- return self.get_history(state, passive=True).added_items()
+ return self._get_collection(state, passive=True).added_items
else:
return AppenderQuery(self, state)
@@ -23,7 +23,7 @@ class DynamicAttributeImpl(attributes.AttributeImpl):
state.dict[self.key] = CollectionHistory(self, state)
def get_collection(self, state, user_data=None):
- return self.get_history(state, passive=True)._added_items
+ return self._get_collection(state, passive=True).added_items
def set(self, state, value, initiator):
if initiator is self:
@@ -38,6 +38,10 @@ class DynamicAttributeImpl(attributes.AttributeImpl):
raise NotImplementedError()
def get_history(self, state, passive=False):
+ c = self._get_collection(state, passive)
+ return (c.added_items, c.unchanged_items, c.deleted_items)
+
+ def _get_collection(self, state, passive=False):
try:
c = state.dict[self.key]
except KeyError:
@@ -47,15 +51,15 @@ class DynamicAttributeImpl(attributes.AttributeImpl):
return CollectionHistory(self, state, apply_to=c)
else:
return c
-
+
def append(self, state, value, initiator, passive=False):
if initiator is not self:
- self.get_history(state, passive=True)._added_items.append(value)
+ self._get_collection(state, passive=True).added_items.append(value)
self.fire_append_event(state, value, initiator)
def remove(self, state, value, initiator, passive=False):
if initiator is not self:
- self.get_history(state, passive=True)._deleted_items.append(value)
+ self._get_collection(state, passive=True).deleted_items.append(value)
self.fire_remove_event(state, value, initiator)
@@ -82,21 +86,21 @@ class AppenderQuery(Query):
def __iter__(self):
sess = self.__session()
if sess is None:
- return iter(self.attr.get_history(self.state, passive=True)._added_items)
+ return iter(self.attr._get_collection(self.state, passive=True).added_items)
else:
return iter(self._clone(sess))
def __getitem__(self, index):
sess = self.__session()
if sess is None:
- return self.attr.get_history(self.state, passive=True)._added_items.__getitem__(index)
+ return self.attr._get_collection(self.state, passive=True).added_items.__getitem__(index)
else:
return self._clone(sess).__getitem__(index)
def count(self):
sess = self.__session()
if sess is None:
- return len(self.attr.get_history(self.state, passive=True)._added_items)
+ return len(self.attr._get_collection(self.state, passive=True).added_items)
else:
return self._clone(sess).count()
@@ -121,7 +125,7 @@ class AppenderQuery(Query):
oldlist = list(self)
else:
oldlist = []
- self.attr.get_history(self.state, passive=True).replace(oldlist, collection)
+ self.attr._get_collection(self.state, passive=True).replace(oldlist, collection)
return oldlist
def append(self, item):
@@ -131,35 +135,23 @@ class AppenderQuery(Query):
self.attr.remove(self.state, item, None)
-class CollectionHistory(attributes.AttributeHistory):
+class CollectionHistory(object):
"""Overrides AttributeHistory to receive append/remove events directly."""
def __init__(self, attr, state, apply_to=None):
if apply_to:
- deleted = util.IdentitySet(apply_to._deleted_items)
- added = apply_to._added_items
+ deleted = util.IdentitySet(apply_to.deleted_items)
+ added = apply_to.added_items
coll = AppenderQuery(attr, state).autoflush(False)
- self._unchanged_items = [o for o in util.IdentitySet(coll) if o not in deleted]
- self._added_items = apply_to._added_items
- self._deleted_items = apply_to._deleted_items
+ self.unchanged_items = [o for o in util.IdentitySet(coll) if o not in deleted]
+ self.added_items = apply_to.added_items
+ self.deleted_items = apply_to.deleted_items
else:
- self._deleted_items = []
- self._added_items = []
- self._unchanged_items = []
+ self.deleted_items = []
+ self.added_items = []
+ self.unchanged_items = []
def replace(self, olditems, newitems):
- self._added_items = newitems
- self._deleted_items = olditems
+ self.added_items = newitems
+ self.deleted_items = olditems
- def is_modified(self):
- return len(self._deleted_items) > 0 or len(self._added_items) > 0
-
- def added_items(self):
- return self._added_items
-
- def unchanged_items(self):
- return self._unchanged_items
-
- def deleted_items(self):
- return self._deleted_items
-