]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- a "collection-holding" InstrumentedAttribute is now identified
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 22 Aug 2007 18:21:25 +0000 (18:21 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 22 Aug 2007 18:21:25 +0000 (18:21 +0000)
by the presence of a "get_collection" method.
- added "get_collection" to DynamicCollectionAttribute so its
treated as a collection.

CHANGES
lib/sqlalchemy/orm/attributes.py
lib/sqlalchemy/orm/dynamic.py

diff --git a/CHANGES b/CHANGES
index 732499d9098c6591dc26f8b11f03aba5ee7c5455..1ced053e7dcfa46b358cab8b60a5afe344f3d059 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -54,6 +54,8 @@ CHANGES
 
 - Connection.begin() no longer accepts nested=True, this logic is now
   all in begin_nested().
+
+- fixes to new "dynamic" relation loader involving cascades
     
 0.4.0beta3
 ----------
index 23af66242f1f0700f6702e324abd6adb315bac6a..74e9eb333f45769f1eac04112ea727977c316baa 100644 (file)
@@ -417,7 +417,7 @@ class InstrumentedCollectionAttribute(InstrumentedAttribute):
 
         obj._state['modified'] = True
 
-        collection = self._get_collection(obj)
+        collection = self.get_collection(obj)
         collection.clear_with_event()
         del obj.__dict__[self.key]
 
@@ -431,13 +431,13 @@ class InstrumentedCollectionAttribute(InstrumentedAttribute):
     def append(self, obj, value, initiator):
         if initiator is self:
             return
-        collection = self._get_collection(obj)
+        collection = self.get_collection(obj)
         collection.append_with_event(value, initiator)
 
     def remove(self, obj, value, initiator):
         if initiator is self:
             return
-        collection = self._get_collection(obj)
+        collection = self.get_collection(obj)
         collection.remove_with_event(value, initiator)
 
     def set(self, obj, value, initiator):
@@ -459,7 +459,7 @@ class InstrumentedCollectionAttribute(InstrumentedAttribute):
             trig()
 
         old = self.get(obj)
-        old_collection = self._get_collection(obj, old)
+        old_collection = self.get_collection(obj, old)
 
         new_collection, user_data = self._build_collection(obj)
         self._load_collection(obj, value or [], emit_events=True,
@@ -497,7 +497,7 @@ class InstrumentedCollectionAttribute(InstrumentedAttribute):
         return collection, user_data
 
     def _load_collection(self, obj, values, emit_events=True, collection=None):
-        collection = collection or self._get_collection(obj)
+        collection = collection or self.get_collection(obj)
         if values is None:
             return
         elif emit_events:
@@ -507,7 +507,7 @@ class InstrumentedCollectionAttribute(InstrumentedAttribute):
             for item in values:
                 collection.append_without_event(item)
             
-    def _get_collection(self, obj, user_data=None):
+    def get_collection(self, obj, user_data=None):
         if user_data is None:
             user_data = self.get(obj)
         try:
@@ -557,10 +557,10 @@ class CommittedState(object):
     def rollback(self, manager, obj):
         for attr in manager.managed_attributes(obj.__class__):
             if attr.key in self.data:
-                if not isinstance(attr, InstrumentedCollectionAttribute):
+                if not hasattr(attr, 'get_collection'):
                     obj.__dict__[attr.key] = self.data[attr.key]
                 else:
-                    collection = attr._get_collection(obj)
+                    collection = attr.get_collection(obj)
                     collection.clear_without_event()
                     for item in self.data[attr.key]:
                         collection.append_without_event(item)
@@ -589,14 +589,14 @@ class AttributeHistory(object):
         else:
             original = None
 
-        if isinstance(attr, InstrumentedCollectionAttribute):
+        if hasattr(attr, 'get_collection'):
             self._current = current
             s = util.Set(original or [])
             self._added_items = []
             self._unchanged_items = []
             self._deleted_items = []
             if current:
-                collection = attr._get_collection(obj, current)
+                collection = attr.get_collection(obj, current)
                 for a in collection:
                     if a in s:
                         self._unchanged_items.append(a)
@@ -730,8 +730,8 @@ class AttributeManager(object):
         x = attr.get(obj, passive=passive)
         if x is PASSIVE_NORESULT:
             return []
-        elif isinstance(attr, InstrumentedCollectionAttribute):
-            return list(attr._get_collection(obj, x))
+        elif hasattr(attr, 'get_collection'):
+            return list(attr.get_collection(obj, x))
         elif isinstance(x, list):
             return x
         else:
@@ -853,7 +853,6 @@ class AttributeManager(object):
 
     def init_collection(self, instance, key):
         """Initialize a collection attribute and return the collection adapter."""
-
         attr = self.get_attribute(instance, key)
         user_data = attr.initialize(instance)
-        return attr._get_collection(instance, user_data)
+        return attr.get_collection(instance, user_data)
index fe3628df2d8dc427df6514f2dc50dfad1acbe651..1d4b5f6c939ac70f9cc2567e7e44813ca8df07bd 100644 (file)
@@ -21,7 +21,10 @@ class DynamicCollectionAttribute(attributes.InstrumentedAttribute):
         # we have our own AttributeHistory therefore dont need CommittedState
         # instead, we reset the history stored on the attribute
         obj.__dict__[self.key] = CollectionHistory(self, obj)
-    
+
+    def get_collection(self, obj, user_data=None):
+        return self.get_history(obj)._added_items
+        
     def set(self, obj, value, initiator):
         if initiator is self:
             return