dynamic.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. # orm/dynamic.py
  2. # Copyright (C) 2005-2024 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: https://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 attributes
  12. from . import exc as orm_exc
  13. from . import interfaces
  14. from . import object_mapper
  15. from . import object_session
  16. from . import relationships
  17. from . import strategies
  18. from . import util as orm_util
  19. from .query import Query
  20. from .. import exc
  21. from .. import log
  22. from .. import util
  23. from ..engine import result
  24. @log.class_logger
  25. @relationships.RelationshipProperty.strategy_for(lazy="dynamic")
  26. class DynaLoader(strategies.AbstractRelationshipLoader):
  27. def init_class_attribute(self, mapper):
  28. self.is_class_level = True
  29. if not self.uselist:
  30. raise exc.InvalidRequestError(
  31. "On relationship %s, 'dynamic' loaders cannot be used with "
  32. "many-to-one/one-to-one relationships and/or "
  33. "uselist=False." % self.parent_property
  34. )
  35. elif self.parent_property.direction not in (
  36. interfaces.ONETOMANY,
  37. interfaces.MANYTOMANY,
  38. ):
  39. util.warn(
  40. "On relationship %s, 'dynamic' loaders cannot be used with "
  41. "many-to-one/one-to-one relationships and/or "
  42. "uselist=False. This warning will be an exception in a "
  43. "future release." % self.parent_property
  44. )
  45. strategies._register_attribute(
  46. self.parent_property,
  47. mapper,
  48. useobject=True,
  49. impl_class=DynamicAttributeImpl,
  50. target_mapper=self.parent_property.mapper,
  51. order_by=self.parent_property.order_by,
  52. query_class=self.parent_property.query_class,
  53. )
  54. class DynamicAttributeImpl(attributes.AttributeImpl):
  55. uses_objects = True
  56. default_accepts_scalar_loader = False
  57. supports_population = False
  58. collection = False
  59. dynamic = True
  60. order_by = ()
  61. def __init__(
  62. self,
  63. class_,
  64. key,
  65. typecallable,
  66. dispatch,
  67. target_mapper,
  68. order_by,
  69. query_class=None,
  70. **kw
  71. ):
  72. super(DynamicAttributeImpl, self).__init__(
  73. class_, key, typecallable, dispatch, **kw
  74. )
  75. self.target_mapper = target_mapper
  76. if order_by:
  77. self.order_by = tuple(order_by)
  78. if not query_class:
  79. self.query_class = AppenderQuery
  80. elif AppenderMixin in query_class.mro():
  81. self.query_class = query_class
  82. else:
  83. self.query_class = mixin_user_query(query_class)
  84. def get(self, state, dict_, passive=attributes.PASSIVE_OFF):
  85. if not passive & attributes.SQL_OK:
  86. return self._get_collection_history(
  87. state, attributes.PASSIVE_NO_INITIALIZE
  88. ).added_items
  89. else:
  90. return self.query_class(self, state)
  91. def get_collection(
  92. self,
  93. state,
  94. dict_,
  95. user_data=None,
  96. passive=attributes.PASSIVE_NO_INITIALIZE,
  97. ):
  98. if not passive & attributes.SQL_OK:
  99. data = self._get_collection_history(state, passive).added_items
  100. else:
  101. history = self._get_collection_history(state, passive)
  102. data = history.added_plus_unchanged
  103. return DynamicCollectionAdapter(data)
  104. @util.memoized_property
  105. def _append_token(self):
  106. return attributes.Event(self, attributes.OP_APPEND)
  107. @util.memoized_property
  108. def _remove_token(self):
  109. return attributes.Event(self, attributes.OP_REMOVE)
  110. def fire_append_event(
  111. self, state, dict_, value, initiator, collection_history=None
  112. ):
  113. if collection_history is None:
  114. collection_history = self._modified_event(state, dict_)
  115. collection_history.add_added(value)
  116. for fn in self.dispatch.append:
  117. value = fn(state, value, initiator or self._append_token)
  118. if self.trackparent and value is not None:
  119. self.sethasparent(attributes.instance_state(value), state, True)
  120. def fire_remove_event(
  121. self, state, dict_, value, initiator, collection_history=None
  122. ):
  123. if collection_history is None:
  124. collection_history = self._modified_event(state, dict_)
  125. collection_history.add_removed(value)
  126. if self.trackparent and value is not None:
  127. self.sethasparent(attributes.instance_state(value), state, False)
  128. for fn in self.dispatch.remove:
  129. fn(state, value, initiator or self._remove_token)
  130. def _modified_event(self, state, dict_):
  131. if self.key not in state.committed_state:
  132. state.committed_state[self.key] = CollectionHistory(self, state)
  133. state._modified_event(dict_, self, attributes.NEVER_SET)
  134. # this is a hack to allow the fixtures.ComparableEntity fixture
  135. # to work
  136. dict_[self.key] = True
  137. return state.committed_state[self.key]
  138. def set(
  139. self,
  140. state,
  141. dict_,
  142. value,
  143. initiator=None,
  144. passive=attributes.PASSIVE_OFF,
  145. check_old=None,
  146. pop=False,
  147. _adapt=True,
  148. ):
  149. if initiator and initiator.parent_token is self.parent_token:
  150. return
  151. if pop and value is None:
  152. return
  153. iterable = value
  154. new_values = list(iterable)
  155. if state.has_identity:
  156. old_collection = util.IdentitySet(self.get(state, dict_))
  157. collection_history = self._modified_event(state, dict_)
  158. if not state.has_identity:
  159. old_collection = collection_history.added_items
  160. else:
  161. old_collection = old_collection.union(
  162. collection_history.added_items
  163. )
  164. idset = util.IdentitySet
  165. constants = old_collection.intersection(new_values)
  166. additions = idset(new_values).difference(constants)
  167. removals = old_collection.difference(constants)
  168. for member in new_values:
  169. if member in additions:
  170. self.fire_append_event(
  171. state,
  172. dict_,
  173. member,
  174. None,
  175. collection_history=collection_history,
  176. )
  177. for member in removals:
  178. self.fire_remove_event(
  179. state,
  180. dict_,
  181. member,
  182. None,
  183. collection_history=collection_history,
  184. )
  185. def delete(self, *args, **kwargs):
  186. raise NotImplementedError()
  187. def set_committed_value(self, state, dict_, value):
  188. raise NotImplementedError(
  189. "Dynamic attributes don't support " "collection population."
  190. )
  191. def get_history(self, state, dict_, passive=attributes.PASSIVE_OFF):
  192. c = self._get_collection_history(state, passive)
  193. return c.as_history()
  194. def get_all_pending(
  195. self, state, dict_, passive=attributes.PASSIVE_NO_INITIALIZE
  196. ):
  197. c = self._get_collection_history(state, passive)
  198. return [(attributes.instance_state(x), x) for x in c.all_items]
  199. def _get_collection_history(self, state, passive=attributes.PASSIVE_OFF):
  200. if self.key in state.committed_state:
  201. c = state.committed_state[self.key]
  202. else:
  203. c = CollectionHistory(self, state)
  204. if state.has_identity and (passive & attributes.INIT_OK):
  205. return CollectionHistory(self, state, apply_to=c)
  206. else:
  207. return c
  208. def append(
  209. self, state, dict_, value, initiator, passive=attributes.PASSIVE_OFF
  210. ):
  211. if initiator is not self:
  212. self.fire_append_event(state, dict_, value, initiator)
  213. def remove(
  214. self, state, dict_, value, initiator, passive=attributes.PASSIVE_OFF
  215. ):
  216. if initiator is not self:
  217. self.fire_remove_event(state, dict_, value, initiator)
  218. def pop(
  219. self, state, dict_, value, initiator, passive=attributes.PASSIVE_OFF
  220. ):
  221. self.remove(state, dict_, value, initiator, passive=passive)
  222. class DynamicCollectionAdapter(object):
  223. """simplified CollectionAdapter for internal API consistency"""
  224. def __init__(self, data):
  225. self.data = data
  226. def __iter__(self):
  227. return iter(self.data)
  228. def _reset_empty(self):
  229. pass
  230. def __len__(self):
  231. return len(self.data)
  232. def __bool__(self):
  233. return True
  234. __nonzero__ = __bool__
  235. class AppenderMixin(object):
  236. query_class = None
  237. def __init__(self, attr, state):
  238. super(AppenderMixin, self).__init__(attr.target_mapper, None)
  239. self.instance = instance = state.obj()
  240. self.attr = attr
  241. mapper = object_mapper(instance)
  242. prop = mapper._props[self.attr.key]
  243. if prop.secondary is not None:
  244. # this is a hack right now. The Query only knows how to
  245. # make subsequent joins() without a given left-hand side
  246. # from self._from_obj[0]. We need to ensure prop.secondary
  247. # is in the FROM. So we purposely put the mapper selectable
  248. # in _from_obj[0] to ensure a user-defined join() later on
  249. # doesn't fail, and secondary is then in _from_obj[1].
  250. # note also, we are using the official ORM-annotated selectable
  251. # from __clause_element__(), see #7868
  252. self._from_obj = (prop.mapper.__clause_element__(), prop.secondary)
  253. self._where_criteria = (
  254. prop._with_parent(instance, alias_secondary=False),
  255. )
  256. if self.attr.order_by:
  257. self._order_by_clauses = self.attr.order_by
  258. def session(self):
  259. sess = object_session(self.instance)
  260. if (
  261. sess is not None
  262. and self.autoflush
  263. and sess.autoflush
  264. and self.instance in sess
  265. ):
  266. sess.flush()
  267. if not orm_util.has_identity(self.instance):
  268. return None
  269. else:
  270. return sess
  271. session = property(session, lambda s, x: None)
  272. def _iter(self):
  273. sess = self.session
  274. if sess is None:
  275. state = attributes.instance_state(self.instance)
  276. if state.detached:
  277. util.warn(
  278. "Instance %s is detached, dynamic relationship cannot "
  279. "return a correct result. This warning will become "
  280. "a DetachedInstanceError in a future release."
  281. % (orm_util.state_str(state))
  282. )
  283. return result.IteratorResult(
  284. result.SimpleResultMetaData([self.attr.class_.__name__]),
  285. iter(
  286. self.attr._get_collection_history(
  287. attributes.instance_state(self.instance),
  288. attributes.PASSIVE_NO_INITIALIZE,
  289. ).added_items
  290. ),
  291. _source_supports_scalars=True,
  292. ).scalars()
  293. else:
  294. return self._generate(sess)._iter()
  295. def __getitem__(self, index):
  296. sess = self.session
  297. if sess is None:
  298. return self.attr._get_collection_history(
  299. attributes.instance_state(self.instance),
  300. attributes.PASSIVE_NO_INITIALIZE,
  301. ).indexed(index)
  302. else:
  303. return self._generate(sess).__getitem__(index)
  304. def count(self):
  305. sess = self.session
  306. if sess is None:
  307. return len(
  308. self.attr._get_collection_history(
  309. attributes.instance_state(self.instance),
  310. attributes.PASSIVE_NO_INITIALIZE,
  311. ).added_items
  312. )
  313. else:
  314. return self._generate(sess).count()
  315. def _generate(self, sess=None):
  316. # note we're returning an entirely new Query class instance
  317. # here without any assignment capabilities; the class of this
  318. # query is determined by the session.
  319. instance = self.instance
  320. if sess is None:
  321. sess = object_session(instance)
  322. if sess is None:
  323. raise orm_exc.DetachedInstanceError(
  324. "Parent instance %s is not bound to a Session, and no "
  325. "contextual session is established; lazy load operation "
  326. "of attribute '%s' cannot proceed"
  327. % (orm_util.instance_str(instance), self.attr.key)
  328. )
  329. if self.query_class:
  330. query = self.query_class(self.attr.target_mapper, session=sess)
  331. else:
  332. query = sess.query(self.attr.target_mapper)
  333. query._where_criteria = self._where_criteria
  334. query._from_obj = self._from_obj
  335. query._order_by_clauses = self._order_by_clauses
  336. return query
  337. def extend(self, iterator):
  338. for item in iterator:
  339. self.attr.append(
  340. attributes.instance_state(self.instance),
  341. attributes.instance_dict(self.instance),
  342. item,
  343. None,
  344. )
  345. def append(self, item):
  346. self.attr.append(
  347. attributes.instance_state(self.instance),
  348. attributes.instance_dict(self.instance),
  349. item,
  350. None,
  351. )
  352. def remove(self, item):
  353. self.attr.remove(
  354. attributes.instance_state(self.instance),
  355. attributes.instance_dict(self.instance),
  356. item,
  357. None,
  358. )
  359. class AppenderQuery(AppenderMixin, Query):
  360. """A dynamic query that supports basic collection storage operations."""
  361. def mixin_user_query(cls):
  362. """Return a new class with AppenderQuery functionality layered over."""
  363. name = "Appender" + cls.__name__
  364. return type(name, (AppenderMixin, cls), {"query_class": cls})
  365. class CollectionHistory(object):
  366. """Overrides AttributeHistory to receive append/remove events directly."""
  367. def __init__(self, attr, state, apply_to=None):
  368. if apply_to:
  369. coll = AppenderQuery(attr, state).autoflush(False)
  370. self.unchanged_items = util.OrderedIdentitySet(coll)
  371. self.added_items = apply_to.added_items
  372. self.deleted_items = apply_to.deleted_items
  373. self._reconcile_collection = True
  374. else:
  375. self.deleted_items = util.OrderedIdentitySet()
  376. self.added_items = util.OrderedIdentitySet()
  377. self.unchanged_items = util.OrderedIdentitySet()
  378. self._reconcile_collection = False
  379. @property
  380. def added_plus_unchanged(self):
  381. return list(self.added_items.union(self.unchanged_items))
  382. @property
  383. def all_items(self):
  384. return list(
  385. self.added_items.union(self.unchanged_items).union(
  386. self.deleted_items
  387. )
  388. )
  389. def as_history(self):
  390. if self._reconcile_collection:
  391. added = self.added_items.difference(self.unchanged_items)
  392. deleted = self.deleted_items.intersection(self.unchanged_items)
  393. unchanged = self.unchanged_items.difference(deleted)
  394. else:
  395. added, unchanged, deleted = (
  396. self.added_items,
  397. self.unchanged_items,
  398. self.deleted_items,
  399. )
  400. return attributes.History(list(added), list(unchanged), list(deleted))
  401. def indexed(self, index):
  402. return list(self.added_items)[index]
  403. def add_added(self, value):
  404. self.added_items.add(value)
  405. def add_removed(self, value):
  406. if value in self.added_items:
  407. self.added_items.remove(value)
  408. else:
  409. self.deleted_items.add(value)