You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

489 lines
15KB

  1. # orm/dynamic.py
  2. # Copyright (C) 2005-2021 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 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. self._from_obj = (prop.mapper.selectable, prop.secondary)
  251. self._where_criteria = (
  252. prop._with_parent(instance, alias_secondary=False),
  253. )
  254. if self.attr.order_by:
  255. self._order_by_clauses = self.attr.order_by
  256. def session(self):
  257. sess = object_session(self.instance)
  258. if (
  259. sess is not None
  260. and self.autoflush
  261. and sess.autoflush
  262. and self.instance in sess
  263. ):
  264. sess.flush()
  265. if not orm_util.has_identity(self.instance):
  266. return None
  267. else:
  268. return sess
  269. session = property(session, lambda s, x: None)
  270. def _iter(self):
  271. sess = self.session
  272. if sess is None:
  273. state = attributes.instance_state(self.instance)
  274. if state.detached:
  275. util.warn(
  276. "Instance %s is detached, dynamic relationship cannot "
  277. "return a correct result. This warning will become "
  278. "a DetachedInstanceError in a future release."
  279. % (orm_util.state_str(state))
  280. )
  281. return result.IteratorResult(
  282. result.SimpleResultMetaData([self.attr.class_.__name__]),
  283. self.attr._get_collection_history(
  284. attributes.instance_state(self.instance),
  285. attributes.PASSIVE_NO_INITIALIZE,
  286. ).added_items,
  287. _source_supports_scalars=True,
  288. ).scalars()
  289. else:
  290. return self._generate(sess)._iter()
  291. def __getitem__(self, index):
  292. sess = self.session
  293. if sess is None:
  294. return self.attr._get_collection_history(
  295. attributes.instance_state(self.instance),
  296. attributes.PASSIVE_NO_INITIALIZE,
  297. ).indexed(index)
  298. else:
  299. return self._generate(sess).__getitem__(index)
  300. def count(self):
  301. sess = self.session
  302. if sess is None:
  303. return len(
  304. self.attr._get_collection_history(
  305. attributes.instance_state(self.instance),
  306. attributes.PASSIVE_NO_INITIALIZE,
  307. ).added_items
  308. )
  309. else:
  310. return self._generate(sess).count()
  311. def _generate(self, sess=None):
  312. # note we're returning an entirely new Query class instance
  313. # here without any assignment capabilities; the class of this
  314. # query is determined by the session.
  315. instance = self.instance
  316. if sess is None:
  317. sess = object_session(instance)
  318. if sess is None:
  319. raise orm_exc.DetachedInstanceError(
  320. "Parent instance %s is not bound to a Session, and no "
  321. "contextual session is established; lazy load operation "
  322. "of attribute '%s' cannot proceed"
  323. % (orm_util.instance_str(instance), self.attr.key)
  324. )
  325. if self.query_class:
  326. query = self.query_class(self.attr.target_mapper, session=sess)
  327. else:
  328. query = sess.query(self.attr.target_mapper)
  329. query._where_criteria = self._where_criteria
  330. query._from_obj = self._from_obj
  331. query._order_by_clauses = self._order_by_clauses
  332. return query
  333. def extend(self, iterator):
  334. for item in iterator:
  335. self.attr.append(
  336. attributes.instance_state(self.instance),
  337. attributes.instance_dict(self.instance),
  338. item,
  339. None,
  340. )
  341. def append(self, item):
  342. self.attr.append(
  343. attributes.instance_state(self.instance),
  344. attributes.instance_dict(self.instance),
  345. item,
  346. None,
  347. )
  348. def remove(self, item):
  349. self.attr.remove(
  350. attributes.instance_state(self.instance),
  351. attributes.instance_dict(self.instance),
  352. item,
  353. None,
  354. )
  355. class AppenderQuery(AppenderMixin, Query):
  356. """A dynamic query that supports basic collection storage operations."""
  357. def mixin_user_query(cls):
  358. """Return a new class with AppenderQuery functionality layered over."""
  359. name = "Appender" + cls.__name__
  360. return type(name, (AppenderMixin, cls), {"query_class": cls})
  361. class CollectionHistory(object):
  362. """Overrides AttributeHistory to receive append/remove events directly."""
  363. def __init__(self, attr, state, apply_to=None):
  364. if apply_to:
  365. coll = AppenderQuery(attr, state).autoflush(False)
  366. self.unchanged_items = util.OrderedIdentitySet(coll)
  367. self.added_items = apply_to.added_items
  368. self.deleted_items = apply_to.deleted_items
  369. self._reconcile_collection = True
  370. else:
  371. self.deleted_items = util.OrderedIdentitySet()
  372. self.added_items = util.OrderedIdentitySet()
  373. self.unchanged_items = util.OrderedIdentitySet()
  374. self._reconcile_collection = False
  375. @property
  376. def added_plus_unchanged(self):
  377. return list(self.added_items.union(self.unchanged_items))
  378. @property
  379. def all_items(self):
  380. return list(
  381. self.added_items.union(self.unchanged_items).union(
  382. self.deleted_items
  383. )
  384. )
  385. def as_history(self):
  386. if self._reconcile_collection:
  387. added = self.added_items.difference(self.unchanged_items)
  388. deleted = self.deleted_items.intersection(self.unchanged_items)
  389. unchanged = self.unchanged_items.difference(deleted)
  390. else:
  391. added, unchanged, deleted = (
  392. self.added_items,
  393. self.unchanged_items,
  394. self.deleted_items,
  395. )
  396. return attributes.History(list(added), list(unchanged), list(deleted))
  397. def indexed(self, index):
  398. return list(self.added_items)[index]
  399. def add_added(self, value):
  400. self.added_items.add(value)
  401. def add_removed(self, value):
  402. if value in self.added_items:
  403. self.added_items.remove(value)
  404. else:
  405. self.deleted_items.add(value)