dynamic.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. # orm/dynamic.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """Dynamic collection API.
  8. Dynamic collections act like Query() objects for read operations and support
  9. basic add/delete mutation.
  10. """
  11. from .. import log, util, exc
  12. from ..sql import operators
  13. from . import (
  14. attributes, object_session, util as orm_util, strategies,
  15. object_mapper, exc as orm_exc, properties
  16. )
  17. from .query import Query
  18. @log.class_logger
  19. @properties.RelationshipProperty.strategy_for(lazy="dynamic")
  20. class DynaLoader(strategies.AbstractRelationshipLoader):
  21. def init_class_attribute(self, mapper):
  22. self.is_class_level = True
  23. if not self.uselist:
  24. raise exc.InvalidRequestError(
  25. "On relationship %s, 'dynamic' loaders cannot be used with "
  26. "many-to-one/one-to-one relationships and/or "
  27. "uselist=False." % self.parent_property)
  28. strategies._register_attribute(
  29. self.parent_property,
  30. mapper,
  31. useobject=True,
  32. impl_class=DynamicAttributeImpl,
  33. target_mapper=self.parent_property.mapper,
  34. order_by=self.parent_property.order_by,
  35. query_class=self.parent_property.query_class,
  36. )
  37. class DynamicAttributeImpl(attributes.AttributeImpl):
  38. uses_objects = True
  39. accepts_scalar_loader = False
  40. supports_population = False
  41. collection = False
  42. def __init__(self, class_, key, typecallable,
  43. dispatch,
  44. target_mapper, order_by, query_class=None, **kw):
  45. super(DynamicAttributeImpl, self).\
  46. __init__(class_, key, typecallable, dispatch, **kw)
  47. self.target_mapper = target_mapper
  48. self.order_by = order_by
  49. if not query_class:
  50. self.query_class = AppenderQuery
  51. elif AppenderMixin in query_class.mro():
  52. self.query_class = query_class
  53. else:
  54. self.query_class = mixin_user_query(query_class)
  55. def get(self, state, dict_, passive=attributes.PASSIVE_OFF):
  56. if not passive & attributes.SQL_OK:
  57. return self._get_collection_history(
  58. state, attributes.PASSIVE_NO_INITIALIZE).added_items
  59. else:
  60. return self.query_class(self, state)
  61. def get_collection(self, state, dict_, user_data=None,
  62. passive=attributes.PASSIVE_NO_INITIALIZE):
  63. if not passive & attributes.SQL_OK:
  64. return self._get_collection_history(state,
  65. passive).added_items
  66. else:
  67. history = self._get_collection_history(state, passive)
  68. return history.added_plus_unchanged
  69. @util.memoized_property
  70. def _append_token(self):
  71. return attributes.Event(self, attributes.OP_APPEND)
  72. @util.memoized_property
  73. def _remove_token(self):
  74. return attributes.Event(self, attributes.OP_REMOVE)
  75. def fire_append_event(self, state, dict_, value, initiator,
  76. collection_history=None):
  77. if collection_history is None:
  78. collection_history = self._modified_event(state, dict_)
  79. collection_history.add_added(value)
  80. for fn in self.dispatch.append:
  81. value = fn(state, value, initiator or self._append_token)
  82. if self.trackparent and value is not None:
  83. self.sethasparent(attributes.instance_state(value), state, True)
  84. def fire_remove_event(self, state, dict_, value, initiator,
  85. collection_history=None):
  86. if collection_history is None:
  87. collection_history = self._modified_event(state, dict_)
  88. collection_history.add_removed(value)
  89. if self.trackparent and value is not None:
  90. self.sethasparent(attributes.instance_state(value), state, False)
  91. for fn in self.dispatch.remove:
  92. fn(state, value, initiator or self._remove_token)
  93. def _modified_event(self, state, dict_):
  94. if self.key not in state.committed_state:
  95. state.committed_state[self.key] = CollectionHistory(self, state)
  96. state._modified_event(dict_,
  97. self,
  98. attributes.NEVER_SET)
  99. # this is a hack to allow the fixtures.ComparableEntity fixture
  100. # to work
  101. dict_[self.key] = True
  102. return state.committed_state[self.key]
  103. def set(self, state, dict_, value, initiator=None,
  104. passive=attributes.PASSIVE_OFF,
  105. check_old=None, pop=False, _adapt=True):
  106. if initiator and initiator.parent_token is self.parent_token:
  107. return
  108. if pop and value is None:
  109. return
  110. iterable = value
  111. new_values = list(iterable)
  112. if state.has_identity:
  113. old_collection = util.IdentitySet(self.get(state, dict_))
  114. collection_history = self._modified_event(state, dict_)
  115. if not state.has_identity:
  116. old_collection = collection_history.added_items
  117. else:
  118. old_collection = old_collection.union(
  119. collection_history.added_items)
  120. idset = util.IdentitySet
  121. constants = old_collection.intersection(new_values)
  122. additions = idset(new_values).difference(constants)
  123. removals = old_collection.difference(constants)
  124. for member in new_values:
  125. if member in additions:
  126. self.fire_append_event(state, dict_, member, None,
  127. collection_history=collection_history)
  128. for member in removals:
  129. self.fire_remove_event(state, dict_, member, None,
  130. collection_history=collection_history)
  131. def delete(self, *args, **kwargs):
  132. raise NotImplementedError()
  133. def set_committed_value(self, state, dict_, value):
  134. raise NotImplementedError("Dynamic attributes don't support "
  135. "collection population.")
  136. def get_history(self, state, dict_, passive=attributes.PASSIVE_OFF):
  137. c = self._get_collection_history(state, passive)
  138. return c.as_history()
  139. def get_all_pending(self, state, dict_,
  140. passive=attributes.PASSIVE_NO_INITIALIZE):
  141. c = self._get_collection_history(
  142. state, passive)
  143. return [
  144. (attributes.instance_state(x), x)
  145. for x in
  146. c.all_items
  147. ]
  148. def _get_collection_history(self, state, passive=attributes.PASSIVE_OFF):
  149. if self.key in state.committed_state:
  150. c = state.committed_state[self.key]
  151. else:
  152. c = CollectionHistory(self, state)
  153. if state.has_identity and (passive & attributes.INIT_OK):
  154. return CollectionHistory(self, state, apply_to=c)
  155. else:
  156. return c
  157. def append(self, state, dict_, value, initiator,
  158. passive=attributes.PASSIVE_OFF):
  159. if initiator is not self:
  160. self.fire_append_event(state, dict_, value, initiator)
  161. def remove(self, state, dict_, value, initiator,
  162. passive=attributes.PASSIVE_OFF):
  163. if initiator is not self:
  164. self.fire_remove_event(state, dict_, value, initiator)
  165. def pop(self, state, dict_, value, initiator,
  166. passive=attributes.PASSIVE_OFF):
  167. self.remove(state, dict_, value, initiator, passive=passive)
  168. class AppenderMixin(object):
  169. query_class = None
  170. def __init__(self, attr, state):
  171. super(AppenderMixin, self).__init__(attr.target_mapper, None)
  172. self.instance = instance = state.obj()
  173. self.attr = attr
  174. mapper = object_mapper(instance)
  175. prop = mapper._props[self.attr.key]
  176. self._criterion = prop._with_parent(
  177. instance,
  178. alias_secondary=False)
  179. if self.attr.order_by:
  180. self._order_by = self.attr.order_by
  181. def session(self):
  182. sess = object_session(self.instance)
  183. if sess is not None and self.autoflush and sess.autoflush \
  184. and self.instance in sess:
  185. sess.flush()
  186. if not orm_util.has_identity(self.instance):
  187. return None
  188. else:
  189. return sess
  190. session = property(session, lambda s, x: None)
  191. def __iter__(self):
  192. sess = self.session
  193. if sess is None:
  194. return iter(self.attr._get_collection_history(
  195. attributes.instance_state(self.instance),
  196. attributes.PASSIVE_NO_INITIALIZE).added_items)
  197. else:
  198. return iter(self._clone(sess))
  199. def __getitem__(self, index):
  200. sess = self.session
  201. if sess is None:
  202. return self.attr._get_collection_history(
  203. attributes.instance_state(self.instance),
  204. attributes.PASSIVE_NO_INITIALIZE).indexed(index)
  205. else:
  206. return self._clone(sess).__getitem__(index)
  207. def count(self):
  208. sess = self.session
  209. if sess is None:
  210. return len(self.attr._get_collection_history(
  211. attributes.instance_state(self.instance),
  212. attributes.PASSIVE_NO_INITIALIZE).added_items)
  213. else:
  214. return self._clone(sess).count()
  215. def _clone(self, sess=None):
  216. # note we're returning an entirely new Query class instance
  217. # here without any assignment capabilities; the class of this
  218. # query is determined by the session.
  219. instance = self.instance
  220. if sess is None:
  221. sess = object_session(instance)
  222. if sess is None:
  223. raise orm_exc.DetachedInstanceError(
  224. "Parent instance %s is not bound to a Session, and no "
  225. "contextual session is established; lazy load operation "
  226. "of attribute '%s' cannot proceed" % (
  227. orm_util.instance_str(instance), self.attr.key))
  228. if self.query_class:
  229. query = self.query_class(self.attr.target_mapper, session=sess)
  230. else:
  231. query = sess.query(self.attr.target_mapper)
  232. query._criterion = self._criterion
  233. query._order_by = self._order_by
  234. return query
  235. def extend(self, iterator):
  236. for item in iterator:
  237. self.attr.append(
  238. attributes.instance_state(self.instance),
  239. attributes.instance_dict(self.instance), item, None)
  240. def append(self, item):
  241. self.attr.append(
  242. attributes.instance_state(self.instance),
  243. attributes.instance_dict(self.instance), item, None)
  244. def remove(self, item):
  245. self.attr.remove(
  246. attributes.instance_state(self.instance),
  247. attributes.instance_dict(self.instance), item, None)
  248. class AppenderQuery(AppenderMixin, Query):
  249. """A dynamic query that supports basic collection storage operations."""
  250. def mixin_user_query(cls):
  251. """Return a new class with AppenderQuery functionality layered over."""
  252. name = 'Appender' + cls.__name__
  253. return type(name, (AppenderMixin, cls), {'query_class': cls})
  254. class CollectionHistory(object):
  255. """Overrides AttributeHistory to receive append/remove events directly."""
  256. def __init__(self, attr, state, apply_to=None):
  257. if apply_to:
  258. coll = AppenderQuery(attr, state).autoflush(False)
  259. self.unchanged_items = util.OrderedIdentitySet(coll)
  260. self.added_items = apply_to.added_items
  261. self.deleted_items = apply_to.deleted_items
  262. self._reconcile_collection = True
  263. else:
  264. self.deleted_items = util.OrderedIdentitySet()
  265. self.added_items = util.OrderedIdentitySet()
  266. self.unchanged_items = util.OrderedIdentitySet()
  267. self._reconcile_collection = False
  268. @property
  269. def added_plus_unchanged(self):
  270. return list(self.added_items.union(self.unchanged_items))
  271. @property
  272. def all_items(self):
  273. return list(self.added_items.union(
  274. self.unchanged_items).union(self.deleted_items))
  275. def as_history(self):
  276. if self._reconcile_collection:
  277. added = self.added_items.difference(self.unchanged_items)
  278. deleted = self.deleted_items.intersection(self.unchanged_items)
  279. unchanged = self.unchanged_items.difference(deleted)
  280. else:
  281. added, unchanged, deleted = self.added_items,\
  282. self.unchanged_items,\
  283. self.deleted_items
  284. return attributes.History(
  285. list(added),
  286. list(unchanged),
  287. list(deleted),
  288. )
  289. def indexed(self, index):
  290. return list(self.added_items)[index]
  291. def add_added(self, value):
  292. self.added_items.add(value)
  293. def add_removed(self, value):
  294. if value in self.added_items:
  295. self.added_items.remove(value)
  296. else:
  297. self.deleted_items.add(value)