diff options
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r-- | lib/sqlalchemy/orm/attributes.py | 4 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/dynamic.py | 39 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/strategies.py | 14 |
3 files changed, 44 insertions, 13 deletions
diff --git a/lib/sqlalchemy/orm/attributes.py b/lib/sqlalchemy/orm/attributes.py index ab31736ed..f91ff51f2 100644 --- a/lib/sqlalchemy/orm/attributes.py +++ b/lib/sqlalchemy/orm/attributes.py @@ -427,6 +427,7 @@ class ScalarAttributeImpl(AttributeImpl): accepts_scalar_loader = True uses_objects = False + supports_population = True def delete(self, state, dict_): @@ -481,6 +482,7 @@ class MutableScalarAttributeImpl(ScalarAttributeImpl): """ uses_objects = False + supports_population = True def __init__(self, class_, key, callable_, class_manager, copy_function=None, @@ -547,6 +549,7 @@ class ScalarObjectAttributeImpl(ScalarAttributeImpl): accepts_scalar_loader = False uses_objects = True + supports_population = True def __init__(self, class_, key, callable_, trackparent=False, extension=None, copy_function=None, @@ -637,6 +640,7 @@ class CollectionAttributeImpl(AttributeImpl): """ accepts_scalar_loader = False uses_objects = True + supports_population = True def __init__(self, class_, key, callable_, typecallable=None, trackparent=False, extension=None, diff --git a/lib/sqlalchemy/orm/dynamic.py b/lib/sqlalchemy/orm/dynamic.py index d0aa2c20b..d55838011 100644 --- a/lib/sqlalchemy/orm/dynamic.py +++ b/lib/sqlalchemy/orm/dynamic.py @@ -43,10 +43,12 @@ log.class_logger(DynaLoader) class DynamicAttributeImpl(attributes.AttributeImpl): uses_objects = True accepts_scalar_loader = False - + supports_population = False + def __init__(self, class_, key, typecallable, target_mapper, order_by, query_class=None, **kwargs): - super(DynamicAttributeImpl, self).__init__(class_, key, typecallable, **kwargs) + super(DynamicAttributeImpl, self).\ + __init__(class_, key, typecallable, **kwargs) self.target_mapper = target_mapper self.order_by = order_by if not query_class: @@ -58,15 +60,18 @@ class DynamicAttributeImpl(attributes.AttributeImpl): def get(self, state, dict_, passive=False): if passive: - return self._get_collection_history(state, passive=True).added_items + return self._get_collection_history(state, + passive=True).added_items else: return self.query_class(self, state) def get_collection(self, state, dict_, user_data=None, passive=True): if passive: - return self._get_collection_history(state, passive=passive).added_items + return self._get_collection_history(state, + passive=passive).added_items else: - history = self._get_collection_history(state, passive=passive) + history = self._get_collection_history(state, + passive=passive) return history.added_items + history.unchanged_items def fire_append_event(self, state, dict_, value, initiator): @@ -105,30 +110,36 @@ class DynamicAttributeImpl(attributes.AttributeImpl): dict_[self.key] = True return state.committed_state[self.key] - def set(self, state, dict_, value, initiator, passive=attributes.PASSIVE_OFF): + def set(self, state, dict_, value, initiator, + passive=attributes.PASSIVE_OFF): if initiator is self: return self._set_iterable(state, dict_, value) def _set_iterable(self, state, dict_, iterable, adapter=None): - collection_history = self._modified_event(state, dict_) new_values = list(iterable) - if state.has_identity: old_collection = list(self.get(state, dict_)) else: old_collection = [] - - collections.bulk_replace(new_values, DynCollectionAdapter(self, state, old_collection), DynCollectionAdapter(self, state, new_values)) + collections.bulk_replace(new_values, DynCollectionAdapter(self, + state, old_collection), + DynCollectionAdapter(self, state, + new_values)) def delete(self, *args, **kwargs): raise NotImplementedError() + def set_committed_value(self, state, dict_, value): + raise NotImplementedError("Dynamic attributes don't support " + "collection population.") + def get_history(self, state, dict_, passive=False): c = self._get_collection_history(state, passive) - return attributes.History(c.added_items, c.unchanged_items, c.deleted_items) + return attributes.History(c.added_items, c.unchanged_items, + c.deleted_items) def _get_collection_history(self, state, passive=False): if self.key in state.committed_state: @@ -193,7 +204,8 @@ class AppenderMixin(object): def __session(self): sess = object_session(self.instance) - if sess is not None and self.autoflush and sess.autoflush and self.instance in sess: + if sess is not None and self.autoflush and sess.autoflush \ + and self.instance in sess: sess.flush() if not has_identity(self.instance): return None @@ -283,7 +295,8 @@ class CollectionHistory(object): 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.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: diff --git a/lib/sqlalchemy/orm/strategies.py b/lib/sqlalchemy/orm/strategies.py index 62602ff37..1c4571aed 100644 --- a/lib/sqlalchemy/orm/strategies.py +++ b/lib/sqlalchemy/orm/strategies.py @@ -806,6 +806,12 @@ class SubqueryLoader(AbstractRelationshipLoader): ] def create_row_processor(self, context, path, mapper, row, adapter): + if not self.parent.class_manager[self.key].impl.supports_population: + raise sa_exc.InvalidRequestError( + "'%s' does not support object " + "population - eager loading cannot be applied." % + self) + path = path + (self.key,) path = interfaces._reduce_path(path) @@ -876,6 +882,7 @@ class EagerLoader(AbstractRelationshipLoader): **kwargs): """Add a left outer join to the statement thats being constructed.""" + if not context.query._enable_eagerloads: return @@ -1069,7 +1076,14 @@ class EagerLoader(AbstractRelationshipLoader): return False def create_row_processor(self, context, path, mapper, row, adapter): + if not self.parent.class_manager[self.key].impl.supports_population: + raise sa_exc.InvalidRequestError( + "'%s' does not support object " + "population - eager loading cannot be applied." % + self) + path = path + (self.key,) + eager_adapter = self._create_eager_adapter( context, |