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.

1024 lines
33KB

  1. # orm/state.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. """Defines instrumentation of instances.
  8. This module is usually not directly visible to user applications, but
  9. defines a large part of the ORM's interactivity.
  10. """
  11. import weakref
  12. from . import base
  13. from . import exc as orm_exc
  14. from . import interfaces
  15. from .base import ATTR_WAS_SET
  16. from .base import INIT_OK
  17. from .base import NEVER_SET
  18. from .base import NO_VALUE
  19. from .base import PASSIVE_NO_INITIALIZE
  20. from .base import PASSIVE_NO_RESULT
  21. from .base import PASSIVE_OFF
  22. from .base import SQL_OK
  23. from .path_registry import PathRegistry
  24. from .. import exc as sa_exc
  25. from .. import inspection
  26. from .. import util
  27. # late-populated by session.py
  28. _sessions = None
  29. # optionally late-provided by sqlalchemy.ext.asyncio.session
  30. _async_provider = None
  31. @inspection._self_inspects
  32. class InstanceState(interfaces.InspectionAttrInfo):
  33. """tracks state information at the instance level.
  34. The :class:`.InstanceState` is a key object used by the
  35. SQLAlchemy ORM in order to track the state of an object;
  36. it is created the moment an object is instantiated, typically
  37. as a result of :term:`instrumentation` which SQLAlchemy applies
  38. to the ``__init__()`` method of the class.
  39. :class:`.InstanceState` is also a semi-public object,
  40. available for runtime inspection as to the state of a
  41. mapped instance, including information such as its current
  42. status within a particular :class:`.Session` and details
  43. about data on individual attributes. The public API
  44. in order to acquire a :class:`.InstanceState` object
  45. is to use the :func:`_sa.inspect` system::
  46. >>> from sqlalchemy import inspect
  47. >>> insp = inspect(some_mapped_object)
  48. .. seealso::
  49. :ref:`core_inspection_toplevel`
  50. """
  51. session_id = None
  52. key = None
  53. runid = None
  54. load_options = util.EMPTY_SET
  55. load_path = PathRegistry.root
  56. insert_order = None
  57. _strong_obj = None
  58. modified = False
  59. expired = False
  60. _deleted = False
  61. _load_pending = False
  62. _orphaned_outside_of_session = False
  63. is_instance = True
  64. identity_token = None
  65. _last_known_values = ()
  66. callables = ()
  67. """A namespace where a per-state loader callable can be associated.
  68. In SQLAlchemy 1.0, this is only used for lazy loaders / deferred
  69. loaders that were set up via query option.
  70. Previously, callables was used also to indicate expired attributes
  71. by storing a link to the InstanceState itself in this dictionary.
  72. This role is now handled by the expired_attributes set.
  73. """
  74. def __init__(self, obj, manager):
  75. self.class_ = obj.__class__
  76. self.manager = manager
  77. self.obj = weakref.ref(obj, self._cleanup)
  78. self.committed_state = {}
  79. self.expired_attributes = set()
  80. expired_attributes = None
  81. """The set of keys which are 'expired' to be loaded by
  82. the manager's deferred scalar loader, assuming no pending
  83. changes.
  84. see also the ``unmodified`` collection which is intersected
  85. against this set when a refresh operation occurs."""
  86. @util.memoized_property
  87. def attrs(self):
  88. """Return a namespace representing each attribute on
  89. the mapped object, including its current value
  90. and history.
  91. The returned object is an instance of :class:`.AttributeState`.
  92. This object allows inspection of the current data
  93. within an attribute as well as attribute history
  94. since the last flush.
  95. """
  96. return util.ImmutableProperties(
  97. dict((key, AttributeState(self, key)) for key in self.manager)
  98. )
  99. @property
  100. def transient(self):
  101. """Return ``True`` if the object is :term:`transient`.
  102. .. seealso::
  103. :ref:`session_object_states`
  104. """
  105. return self.key is None and not self._attached
  106. @property
  107. def pending(self):
  108. """Return ``True`` if the object is :term:`pending`.
  109. .. seealso::
  110. :ref:`session_object_states`
  111. """
  112. return self.key is None and self._attached
  113. @property
  114. def deleted(self):
  115. """Return ``True`` if the object is :term:`deleted`.
  116. An object that is in the deleted state is guaranteed to
  117. not be within the :attr:`.Session.identity_map` of its parent
  118. :class:`.Session`; however if the session's transaction is rolled
  119. back, the object will be restored to the persistent state and
  120. the identity map.
  121. .. note::
  122. The :attr:`.InstanceState.deleted` attribute refers to a specific
  123. state of the object that occurs between the "persistent" and
  124. "detached" states; once the object is :term:`detached`, the
  125. :attr:`.InstanceState.deleted` attribute **no longer returns
  126. True**; in order to detect that a state was deleted, regardless
  127. of whether or not the object is associated with a
  128. :class:`.Session`, use the :attr:`.InstanceState.was_deleted`
  129. accessor.
  130. .. versionadded: 1.1
  131. .. seealso::
  132. :ref:`session_object_states`
  133. """
  134. return self.key is not None and self._attached and self._deleted
  135. @property
  136. def was_deleted(self):
  137. """Return True if this object is or was previously in the
  138. "deleted" state and has not been reverted to persistent.
  139. This flag returns True once the object was deleted in flush.
  140. When the object is expunged from the session either explicitly
  141. or via transaction commit and enters the "detached" state,
  142. this flag will continue to report True.
  143. .. versionadded:: 1.1 - added a local method form of
  144. :func:`.orm.util.was_deleted`.
  145. .. seealso::
  146. :attr:`.InstanceState.deleted` - refers to the "deleted" state
  147. :func:`.orm.util.was_deleted` - standalone function
  148. :ref:`session_object_states`
  149. """
  150. return self._deleted
  151. @property
  152. def persistent(self):
  153. """Return ``True`` if the object is :term:`persistent`.
  154. An object that is in the persistent state is guaranteed to
  155. be within the :attr:`.Session.identity_map` of its parent
  156. :class:`.Session`.
  157. .. versionchanged:: 1.1 The :attr:`.InstanceState.persistent`
  158. accessor no longer returns True for an object that was
  159. "deleted" within a flush; use the :attr:`.InstanceState.deleted`
  160. accessor to detect this state. This allows the "persistent"
  161. state to guarantee membership in the identity map.
  162. .. seealso::
  163. :ref:`session_object_states`
  164. """
  165. return self.key is not None and self._attached and not self._deleted
  166. @property
  167. def detached(self):
  168. """Return ``True`` if the object is :term:`detached`.
  169. .. seealso::
  170. :ref:`session_object_states`
  171. """
  172. return self.key is not None and not self._attached
  173. @property
  174. @util.preload_module("sqlalchemy.orm.session")
  175. def _attached(self):
  176. return (
  177. self.session_id is not None
  178. and self.session_id in util.preloaded.orm_session._sessions
  179. )
  180. def _track_last_known_value(self, key):
  181. """Track the last known value of a particular key after expiration
  182. operations.
  183. .. versionadded:: 1.3
  184. """
  185. if key not in self._last_known_values:
  186. self._last_known_values = dict(self._last_known_values)
  187. self._last_known_values[key] = NO_VALUE
  188. @property
  189. def session(self):
  190. """Return the owning :class:`.Session` for this instance,
  191. or ``None`` if none available.
  192. Note that the result here can in some cases be *different*
  193. from that of ``obj in session``; an object that's been deleted
  194. will report as not ``in session``, however if the transaction is
  195. still in progress, this attribute will still refer to that session.
  196. Only when the transaction is completed does the object become
  197. fully detached under normal circumstances.
  198. .. seealso::
  199. :attr:`_orm.InstanceState.async_session`
  200. """
  201. if self.session_id:
  202. try:
  203. return _sessions[self.session_id]
  204. except KeyError:
  205. pass
  206. return None
  207. @property
  208. def async_session(self):
  209. """Return the owning :class:`_asyncio.AsyncSession` for this instance,
  210. or ``None`` if none available.
  211. This attribute is only non-None when the :mod:`sqlalchemy.ext.asyncio`
  212. API is in use for this ORM object. The returned
  213. :class:`_asyncio.AsyncSession` object will be a proxy for the
  214. :class:`_orm.Session` object that would be returned from the
  215. :attr:`_orm.InstanceState.session` attribute for this
  216. :class:`_orm.InstanceState`.
  217. .. versionadded:: 1.4.18
  218. .. seealso::
  219. :ref:`asyncio_toplevel`
  220. """
  221. if _async_provider is None:
  222. return None
  223. sess = self.session
  224. if sess is not None:
  225. return _async_provider(sess)
  226. else:
  227. return None
  228. @property
  229. def object(self):
  230. """Return the mapped object represented by this
  231. :class:`.InstanceState`."""
  232. return self.obj()
  233. @property
  234. def identity(self):
  235. """Return the mapped identity of the mapped object.
  236. This is the primary key identity as persisted by the ORM
  237. which can always be passed directly to
  238. :meth:`_query.Query.get`.
  239. Returns ``None`` if the object has no primary key identity.
  240. .. note::
  241. An object which is :term:`transient` or :term:`pending`
  242. does **not** have a mapped identity until it is flushed,
  243. even if its attributes include primary key values.
  244. """
  245. if self.key is None:
  246. return None
  247. else:
  248. return self.key[1]
  249. @property
  250. def identity_key(self):
  251. """Return the identity key for the mapped object.
  252. This is the key used to locate the object within
  253. the :attr:`.Session.identity_map` mapping. It contains
  254. the identity as returned by :attr:`.identity` within it.
  255. """
  256. # TODO: just change .key to .identity_key across
  257. # the board ? probably
  258. return self.key
  259. @util.memoized_property
  260. def parents(self):
  261. return {}
  262. @util.memoized_property
  263. def _pending_mutations(self):
  264. return {}
  265. @util.memoized_property
  266. def _empty_collections(self):
  267. return {}
  268. @util.memoized_property
  269. def mapper(self):
  270. """Return the :class:`_orm.Mapper` used for this mapped object."""
  271. return self.manager.mapper
  272. @property
  273. def has_identity(self):
  274. """Return ``True`` if this object has an identity key.
  275. This should always have the same value as the
  276. expression ``state.persistent`` or ``state.detached``.
  277. """
  278. return bool(self.key)
  279. @classmethod
  280. def _detach_states(self, states, session, to_transient=False):
  281. persistent_to_detached = (
  282. session.dispatch.persistent_to_detached or None
  283. )
  284. deleted_to_detached = session.dispatch.deleted_to_detached or None
  285. pending_to_transient = session.dispatch.pending_to_transient or None
  286. persistent_to_transient = (
  287. session.dispatch.persistent_to_transient or None
  288. )
  289. for state in states:
  290. deleted = state._deleted
  291. pending = state.key is None
  292. persistent = not pending and not deleted
  293. state.session_id = None
  294. if to_transient and state.key:
  295. del state.key
  296. if persistent:
  297. if to_transient:
  298. if persistent_to_transient is not None:
  299. persistent_to_transient(session, state)
  300. elif persistent_to_detached is not None:
  301. persistent_to_detached(session, state)
  302. elif deleted and deleted_to_detached is not None:
  303. deleted_to_detached(session, state)
  304. elif pending and pending_to_transient is not None:
  305. pending_to_transient(session, state)
  306. state._strong_obj = None
  307. def _detach(self, session=None):
  308. if session:
  309. InstanceState._detach_states([self], session)
  310. else:
  311. self.session_id = self._strong_obj = None
  312. def _dispose(self):
  313. self._detach()
  314. del self.obj
  315. def _cleanup(self, ref):
  316. """Weakref callback cleanup.
  317. This callable cleans out the state when it is being garbage
  318. collected.
  319. this _cleanup **assumes** that there are no strong refs to us!
  320. Will not work otherwise!
  321. """
  322. # Python builtins become undefined during interpreter shutdown.
  323. # Guard against exceptions during this phase, as the method cannot
  324. # proceed in any case if builtins have been undefined.
  325. if dict is None:
  326. return
  327. instance_dict = self._instance_dict()
  328. if instance_dict is not None:
  329. instance_dict._fast_discard(self)
  330. del self._instance_dict
  331. # we can't possibly be in instance_dict._modified
  332. # b.c. this is weakref cleanup only, that set
  333. # is strong referencing!
  334. # assert self not in instance_dict._modified
  335. self.session_id = self._strong_obj = None
  336. del self.obj
  337. def obj(self):
  338. return None
  339. @property
  340. def dict(self):
  341. """Return the instance dict used by the object.
  342. Under normal circumstances, this is always synonymous
  343. with the ``__dict__`` attribute of the mapped object,
  344. unless an alternative instrumentation system has been
  345. configured.
  346. In the case that the actual object has been garbage
  347. collected, this accessor returns a blank dictionary.
  348. """
  349. o = self.obj()
  350. if o is not None:
  351. return base.instance_dict(o)
  352. else:
  353. return {}
  354. def _initialize_instance(*mixed, **kwargs):
  355. self, instance, args = mixed[0], mixed[1], mixed[2:] # noqa
  356. manager = self.manager
  357. manager.dispatch.init(self, args, kwargs)
  358. try:
  359. return manager.original_init(*mixed[1:], **kwargs)
  360. except:
  361. with util.safe_reraise():
  362. manager.dispatch.init_failure(self, args, kwargs)
  363. def get_history(self, key, passive):
  364. return self.manager[key].impl.get_history(self, self.dict, passive)
  365. def get_impl(self, key):
  366. return self.manager[key].impl
  367. def _get_pending_mutation(self, key):
  368. if key not in self._pending_mutations:
  369. self._pending_mutations[key] = PendingCollection()
  370. return self._pending_mutations[key]
  371. def __getstate__(self):
  372. state_dict = {"instance": self.obj()}
  373. state_dict.update(
  374. (k, self.__dict__[k])
  375. for k in (
  376. "committed_state",
  377. "_pending_mutations",
  378. "modified",
  379. "expired",
  380. "callables",
  381. "key",
  382. "parents",
  383. "load_options",
  384. "class_",
  385. "expired_attributes",
  386. "info",
  387. )
  388. if k in self.__dict__
  389. )
  390. if self.load_path:
  391. state_dict["load_path"] = self.load_path.serialize()
  392. state_dict["manager"] = self.manager._serialize(self, state_dict)
  393. return state_dict
  394. def __setstate__(self, state_dict):
  395. inst = state_dict["instance"]
  396. if inst is not None:
  397. self.obj = weakref.ref(inst, self._cleanup)
  398. self.class_ = inst.__class__
  399. else:
  400. # None being possible here generally new as of 0.7.4
  401. # due to storage of state in "parents". "class_"
  402. # also new.
  403. self.obj = None
  404. self.class_ = state_dict["class_"]
  405. self.committed_state = state_dict.get("committed_state", {})
  406. self._pending_mutations = state_dict.get("_pending_mutations", {})
  407. self.parents = state_dict.get("parents", {})
  408. self.modified = state_dict.get("modified", False)
  409. self.expired = state_dict.get("expired", False)
  410. if "info" in state_dict:
  411. self.info.update(state_dict["info"])
  412. if "callables" in state_dict:
  413. self.callables = state_dict["callables"]
  414. try:
  415. self.expired_attributes = state_dict["expired_attributes"]
  416. except KeyError:
  417. self.expired_attributes = set()
  418. # 0.9 and earlier compat
  419. for k in list(self.callables):
  420. if self.callables[k] is self:
  421. self.expired_attributes.add(k)
  422. del self.callables[k]
  423. else:
  424. if "expired_attributes" in state_dict:
  425. self.expired_attributes = state_dict["expired_attributes"]
  426. else:
  427. self.expired_attributes = set()
  428. self.__dict__.update(
  429. [
  430. (k, state_dict[k])
  431. for k in ("key", "load_options")
  432. if k in state_dict
  433. ]
  434. )
  435. if self.key:
  436. try:
  437. self.identity_token = self.key[2]
  438. except IndexError:
  439. # 1.1 and earlier compat before identity_token
  440. assert len(self.key) == 2
  441. self.key = self.key + (None,)
  442. self.identity_token = None
  443. if "load_path" in state_dict:
  444. self.load_path = PathRegistry.deserialize(state_dict["load_path"])
  445. state_dict["manager"](self, inst, state_dict)
  446. def _reset(self, dict_, key):
  447. """Remove the given attribute and any
  448. callables associated with it."""
  449. old = dict_.pop(key, None)
  450. if old is not None and self.manager[key].impl.collection:
  451. self.manager[key].impl._invalidate_collection(old)
  452. self.expired_attributes.discard(key)
  453. if self.callables:
  454. self.callables.pop(key, None)
  455. def _copy_callables(self, from_):
  456. if "callables" in from_.__dict__:
  457. self.callables = dict(from_.callables)
  458. @classmethod
  459. def _instance_level_callable_processor(cls, manager, fn, key):
  460. impl = manager[key].impl
  461. if impl.collection:
  462. def _set_callable(state, dict_, row):
  463. if "callables" not in state.__dict__:
  464. state.callables = {}
  465. old = dict_.pop(key, None)
  466. if old is not None:
  467. impl._invalidate_collection(old)
  468. state.callables[key] = fn
  469. else:
  470. def _set_callable(state, dict_, row):
  471. if "callables" not in state.__dict__:
  472. state.callables = {}
  473. state.callables[key] = fn
  474. return _set_callable
  475. def _expire(self, dict_, modified_set):
  476. self.expired = True
  477. if self.modified:
  478. modified_set.discard(self)
  479. self.committed_state.clear()
  480. self.modified = False
  481. self._strong_obj = None
  482. if "_pending_mutations" in self.__dict__:
  483. del self.__dict__["_pending_mutations"]
  484. if "parents" in self.__dict__:
  485. del self.__dict__["parents"]
  486. self.expired_attributes.update(
  487. [impl.key for impl in self.manager._loader_impls]
  488. )
  489. if self.callables:
  490. # the per state loader callables we can remove here are
  491. # LoadDeferredColumns, which undefers a column at the instance
  492. # level that is mapped with deferred, and LoadLazyAttribute,
  493. # which lazy loads a relationship at the instance level that
  494. # is mapped with "noload" or perhaps "immediateload".
  495. # Before 1.4, only column-based
  496. # attributes could be considered to be "expired", so here they
  497. # were the only ones "unexpired", which means to make them deferred
  498. # again. For the moment, as of 1.4 we also apply the same
  499. # treatment relationships now, that is, an instance level lazy
  500. # loader is reset in the same way as a column loader.
  501. for k in self.expired_attributes.intersection(self.callables):
  502. del self.callables[k]
  503. for k in self.manager._collection_impl_keys.intersection(dict_):
  504. collection = dict_.pop(k)
  505. collection._sa_adapter.invalidated = True
  506. if self._last_known_values:
  507. self._last_known_values.update(
  508. (k, dict_[k]) for k in self._last_known_values if k in dict_
  509. )
  510. for key in self.manager._all_key_set.intersection(dict_):
  511. del dict_[key]
  512. self.manager.dispatch.expire(self, None)
  513. def _expire_attributes(self, dict_, attribute_names, no_loader=False):
  514. pending = self.__dict__.get("_pending_mutations", None)
  515. callables = self.callables
  516. for key in attribute_names:
  517. impl = self.manager[key].impl
  518. if impl.accepts_scalar_loader:
  519. if no_loader and (impl.callable_ or key in callables):
  520. continue
  521. self.expired_attributes.add(key)
  522. if callables and key in callables:
  523. del callables[key]
  524. old = dict_.pop(key, NO_VALUE)
  525. if impl.collection and old is not NO_VALUE:
  526. impl._invalidate_collection(old)
  527. if (
  528. self._last_known_values
  529. and key in self._last_known_values
  530. and old is not NO_VALUE
  531. ):
  532. self._last_known_values[key] = old
  533. self.committed_state.pop(key, None)
  534. if pending:
  535. pending.pop(key, None)
  536. self.manager.dispatch.expire(self, attribute_names)
  537. def _load_expired(self, state, passive):
  538. """__call__ allows the InstanceState to act as a deferred
  539. callable for loading expired attributes, which is also
  540. serializable (picklable).
  541. """
  542. if not passive & SQL_OK:
  543. return PASSIVE_NO_RESULT
  544. toload = self.expired_attributes.intersection(self.unmodified)
  545. toload = toload.difference(
  546. attr
  547. for attr in toload
  548. if not self.manager[attr].impl.load_on_unexpire
  549. )
  550. self.manager.expired_attribute_loader(self, toload, passive)
  551. # if the loader failed, or this
  552. # instance state didn't have an identity,
  553. # the attributes still might be in the callables
  554. # dict. ensure they are removed.
  555. self.expired_attributes.clear()
  556. return ATTR_WAS_SET
  557. @property
  558. def unmodified(self):
  559. """Return the set of keys which have no uncommitted changes"""
  560. return set(self.manager).difference(self.committed_state)
  561. def unmodified_intersection(self, keys):
  562. """Return self.unmodified.intersection(keys)."""
  563. return (
  564. set(keys)
  565. .intersection(self.manager)
  566. .difference(self.committed_state)
  567. )
  568. @property
  569. def unloaded(self):
  570. """Return the set of keys which do not have a loaded value.
  571. This includes expired attributes and any other attribute that
  572. was never populated or modified.
  573. """
  574. return (
  575. set(self.manager)
  576. .difference(self.committed_state)
  577. .difference(self.dict)
  578. )
  579. @property
  580. def unloaded_expirable(self):
  581. """Return the set of keys which do not have a loaded value.
  582. This includes expired attributes and any other attribute that
  583. was never populated or modified.
  584. """
  585. return self.unloaded
  586. @property
  587. def _unloaded_non_object(self):
  588. return self.unloaded.intersection(
  589. attr
  590. for attr in self.manager
  591. if self.manager[attr].impl.accepts_scalar_loader
  592. )
  593. def _instance_dict(self):
  594. return None
  595. def _modified_event(
  596. self, dict_, attr, previous, collection=False, is_userland=False
  597. ):
  598. if attr:
  599. if not attr.send_modified_events:
  600. return
  601. if is_userland and attr.key not in dict_:
  602. raise sa_exc.InvalidRequestError(
  603. "Can't flag attribute '%s' modified; it's not present in "
  604. "the object state" % attr.key
  605. )
  606. if attr.key not in self.committed_state or is_userland:
  607. if collection:
  608. if previous is NEVER_SET:
  609. if attr.key in dict_:
  610. previous = dict_[attr.key]
  611. if previous not in (None, NO_VALUE, NEVER_SET):
  612. previous = attr.copy(previous)
  613. self.committed_state[attr.key] = previous
  614. if attr.key in self._last_known_values:
  615. self._last_known_values[attr.key] = NO_VALUE
  616. # assert self._strong_obj is None or self.modified
  617. if (self.session_id and self._strong_obj is None) or not self.modified:
  618. self.modified = True
  619. instance_dict = self._instance_dict()
  620. if instance_dict:
  621. has_modified = bool(instance_dict._modified)
  622. instance_dict._modified.add(self)
  623. else:
  624. has_modified = False
  625. # only create _strong_obj link if attached
  626. # to a session
  627. inst = self.obj()
  628. if self.session_id:
  629. self._strong_obj = inst
  630. # if identity map already had modified objects,
  631. # assume autobegin already occurred, else check
  632. # for autobegin
  633. if not has_modified:
  634. # inline of autobegin, to ensure session transaction
  635. # snapshot is established
  636. try:
  637. session = _sessions[self.session_id]
  638. except KeyError:
  639. pass
  640. else:
  641. if session._transaction is None:
  642. session._autobegin()
  643. if inst is None and attr:
  644. raise orm_exc.ObjectDereferencedError(
  645. "Can't emit change event for attribute '%s' - "
  646. "parent object of type %s has been garbage "
  647. "collected."
  648. % (self.manager[attr.key], base.state_class_str(self))
  649. )
  650. def _commit(self, dict_, keys):
  651. """Commit attributes.
  652. This is used by a partial-attribute load operation to mark committed
  653. those attributes which were refreshed from the database.
  654. Attributes marked as "expired" can potentially remain "expired" after
  655. this step if a value was not populated in state.dict.
  656. """
  657. for key in keys:
  658. self.committed_state.pop(key, None)
  659. self.expired = False
  660. self.expired_attributes.difference_update(
  661. set(keys).intersection(dict_)
  662. )
  663. # the per-keys commit removes object-level callables,
  664. # while that of commit_all does not. it's not clear
  665. # if this behavior has a clear rationale, however tests do
  666. # ensure this is what it does.
  667. if self.callables:
  668. for key in (
  669. set(self.callables).intersection(keys).intersection(dict_)
  670. ):
  671. del self.callables[key]
  672. def _commit_all(self, dict_, instance_dict=None):
  673. """commit all attributes unconditionally.
  674. This is used after a flush() or a full load/refresh
  675. to remove all pending state from the instance.
  676. - all attributes are marked as "committed"
  677. - the "strong dirty reference" is removed
  678. - the "modified" flag is set to False
  679. - any "expired" markers for scalar attributes loaded are removed.
  680. - lazy load callables for objects / collections *stay*
  681. Attributes marked as "expired" can potentially remain
  682. "expired" after this step if a value was not populated in state.dict.
  683. """
  684. self._commit_all_states([(self, dict_)], instance_dict)
  685. @classmethod
  686. def _commit_all_states(self, iter_, instance_dict=None):
  687. """Mass / highly inlined version of commit_all()."""
  688. for state, dict_ in iter_:
  689. state_dict = state.__dict__
  690. state.committed_state.clear()
  691. if "_pending_mutations" in state_dict:
  692. del state_dict["_pending_mutations"]
  693. state.expired_attributes.difference_update(dict_)
  694. if instance_dict and state.modified:
  695. instance_dict._modified.discard(state)
  696. state.modified = state.expired = False
  697. state._strong_obj = None
  698. class AttributeState(object):
  699. """Provide an inspection interface corresponding
  700. to a particular attribute on a particular mapped object.
  701. The :class:`.AttributeState` object is accessed
  702. via the :attr:`.InstanceState.attrs` collection
  703. of a particular :class:`.InstanceState`::
  704. from sqlalchemy import inspect
  705. insp = inspect(some_mapped_object)
  706. attr_state = insp.attrs.some_attribute
  707. """
  708. def __init__(self, state, key):
  709. self.state = state
  710. self.key = key
  711. @property
  712. def loaded_value(self):
  713. """The current value of this attribute as loaded from the database.
  714. If the value has not been loaded, or is otherwise not present
  715. in the object's dictionary, returns NO_VALUE.
  716. """
  717. return self.state.dict.get(self.key, NO_VALUE)
  718. @property
  719. def value(self):
  720. """Return the value of this attribute.
  721. This operation is equivalent to accessing the object's
  722. attribute directly or via ``getattr()``, and will fire
  723. off any pending loader callables if needed.
  724. """
  725. return self.state.manager[self.key].__get__(
  726. self.state.obj(), self.state.class_
  727. )
  728. @property
  729. def history(self):
  730. """Return the current **pre-flush** change history for
  731. this attribute, via the :class:`.History` interface.
  732. This method will **not** emit loader callables if the value of the
  733. attribute is unloaded.
  734. .. note::
  735. The attribute history system tracks changes on a **per flush
  736. basis**. Each time the :class:`.Session` is flushed, the history
  737. of each attribute is reset to empty. The :class:`.Session` by
  738. default autoflushes each time a :class:`_query.Query` is invoked.
  739. For
  740. options on how to control this, see :ref:`session_flushing`.
  741. .. seealso::
  742. :meth:`.AttributeState.load_history` - retrieve history
  743. using loader callables if the value is not locally present.
  744. :func:`.attributes.get_history` - underlying function
  745. """
  746. return self.state.get_history(self.key, PASSIVE_NO_INITIALIZE)
  747. def load_history(self):
  748. """Return the current **pre-flush** change history for
  749. this attribute, via the :class:`.History` interface.
  750. This method **will** emit loader callables if the value of the
  751. attribute is unloaded.
  752. .. note::
  753. The attribute history system tracks changes on a **per flush
  754. basis**. Each time the :class:`.Session` is flushed, the history
  755. of each attribute is reset to empty. The :class:`.Session` by
  756. default autoflushes each time a :class:`_query.Query` is invoked.
  757. For
  758. options on how to control this, see :ref:`session_flushing`.
  759. .. seealso::
  760. :attr:`.AttributeState.history`
  761. :func:`.attributes.get_history` - underlying function
  762. .. versionadded:: 0.9.0
  763. """
  764. return self.state.get_history(self.key, PASSIVE_OFF ^ INIT_OK)
  765. class PendingCollection(object):
  766. """A writable placeholder for an unloaded collection.
  767. Stores items appended to and removed from a collection that has not yet
  768. been loaded. When the collection is loaded, the changes stored in
  769. PendingCollection are applied to it to produce the final result.
  770. """
  771. def __init__(self):
  772. self.deleted_items = util.IdentitySet()
  773. self.added_items = util.OrderedIdentitySet()
  774. def append(self, value):
  775. if value in self.deleted_items:
  776. self.deleted_items.remove(value)
  777. else:
  778. self.added_items.add(value)
  779. def remove(self, value):
  780. if value in self.added_items:
  781. self.added_items.remove(value)
  782. else:
  783. self.deleted_items.add(value)