base.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. # orm/base.py
  2. # Copyright (C) 2005-2022 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. """Constants and rudimental functions used throughout the ORM.
  8. """
  9. import operator
  10. from . import exc
  11. from .. import exc as sa_exc
  12. from .. import inspection
  13. from .. import util
  14. PASSIVE_NO_RESULT = util.symbol(
  15. "PASSIVE_NO_RESULT",
  16. """Symbol returned by a loader callable or other attribute/history
  17. retrieval operation when a value could not be determined, based
  18. on loader callable flags.
  19. """,
  20. )
  21. PASSIVE_CLASS_MISMATCH = util.symbol(
  22. "PASSIVE_CLASS_MISMATCH",
  23. """Symbol indicating that an object is locally present for a given
  24. primary key identity but it is not of the requested class. The
  25. return value is therefore None and no SQL should be emitted.""",
  26. )
  27. ATTR_WAS_SET = util.symbol(
  28. "ATTR_WAS_SET",
  29. """Symbol returned by a loader callable to indicate the
  30. retrieved value, or values, were assigned to their attributes
  31. on the target object.
  32. """,
  33. )
  34. ATTR_EMPTY = util.symbol(
  35. "ATTR_EMPTY",
  36. """Symbol used internally to indicate an attribute had no callable.""",
  37. )
  38. NO_VALUE = util.symbol(
  39. "NO_VALUE",
  40. """Symbol which may be placed as the 'previous' value of an attribute,
  41. indicating no value was loaded for an attribute when it was modified,
  42. and flags indicated we were not to load it.
  43. """,
  44. )
  45. NEVER_SET = NO_VALUE
  46. """
  47. Synonymous with NO_VALUE
  48. .. versionchanged:: 1.4 NEVER_SET was merged with NO_VALUE
  49. """
  50. NO_CHANGE = util.symbol(
  51. "NO_CHANGE",
  52. """No callables or SQL should be emitted on attribute access
  53. and no state should change
  54. """,
  55. canonical=0,
  56. )
  57. CALLABLES_OK = util.symbol(
  58. "CALLABLES_OK",
  59. """Loader callables can be fired off if a value
  60. is not present.
  61. """,
  62. canonical=1,
  63. )
  64. SQL_OK = util.symbol(
  65. "SQL_OK",
  66. """Loader callables can emit SQL at least on scalar value attributes.""",
  67. canonical=2,
  68. )
  69. RELATED_OBJECT_OK = util.symbol(
  70. "RELATED_OBJECT_OK",
  71. """Callables can use SQL to load related objects as well
  72. as scalar value attributes.
  73. """,
  74. canonical=4,
  75. )
  76. INIT_OK = util.symbol(
  77. "INIT_OK",
  78. """Attributes should be initialized with a blank
  79. value (None or an empty collection) upon get, if no other
  80. value can be obtained.
  81. """,
  82. canonical=8,
  83. )
  84. NON_PERSISTENT_OK = util.symbol(
  85. "NON_PERSISTENT_OK",
  86. """Callables can be emitted if the parent is not persistent.""",
  87. canonical=16,
  88. )
  89. LOAD_AGAINST_COMMITTED = util.symbol(
  90. "LOAD_AGAINST_COMMITTED",
  91. """Callables should use committed values as primary/foreign keys during a
  92. load.
  93. """,
  94. canonical=32,
  95. )
  96. NO_AUTOFLUSH = util.symbol(
  97. "NO_AUTOFLUSH",
  98. """Loader callables should disable autoflush.""",
  99. canonical=64,
  100. )
  101. NO_RAISE = util.symbol(
  102. "NO_RAISE",
  103. """Loader callables should not raise any assertions""",
  104. canonical=128,
  105. )
  106. DEFERRED_HISTORY_LOAD = util.symbol(
  107. "DEFERRED_HISTORY_LOAD",
  108. """indicates special load of the previous value of an attribute""",
  109. canonical=256,
  110. )
  111. # pre-packaged sets of flags used as inputs
  112. PASSIVE_OFF = util.symbol(
  113. "PASSIVE_OFF",
  114. "Callables can be emitted in all cases.",
  115. canonical=(
  116. RELATED_OBJECT_OK | NON_PERSISTENT_OK | INIT_OK | CALLABLES_OK | SQL_OK
  117. ),
  118. )
  119. PASSIVE_RETURN_NO_VALUE = util.symbol(
  120. "PASSIVE_RETURN_NO_VALUE",
  121. """PASSIVE_OFF ^ INIT_OK""",
  122. canonical=PASSIVE_OFF ^ INIT_OK,
  123. )
  124. PASSIVE_NO_INITIALIZE = util.symbol(
  125. "PASSIVE_NO_INITIALIZE",
  126. "PASSIVE_RETURN_NO_VALUE ^ CALLABLES_OK",
  127. canonical=PASSIVE_RETURN_NO_VALUE ^ CALLABLES_OK,
  128. )
  129. PASSIVE_NO_FETCH = util.symbol(
  130. "PASSIVE_NO_FETCH", "PASSIVE_OFF ^ SQL_OK", canonical=PASSIVE_OFF ^ SQL_OK
  131. )
  132. PASSIVE_NO_FETCH_RELATED = util.symbol(
  133. "PASSIVE_NO_FETCH_RELATED",
  134. "PASSIVE_OFF ^ RELATED_OBJECT_OK",
  135. canonical=PASSIVE_OFF ^ RELATED_OBJECT_OK,
  136. )
  137. PASSIVE_ONLY_PERSISTENT = util.symbol(
  138. "PASSIVE_ONLY_PERSISTENT",
  139. "PASSIVE_OFF ^ NON_PERSISTENT_OK",
  140. canonical=PASSIVE_OFF ^ NON_PERSISTENT_OK,
  141. )
  142. DEFAULT_MANAGER_ATTR = "_sa_class_manager"
  143. DEFAULT_STATE_ATTR = "_sa_instance_state"
  144. EXT_CONTINUE = util.symbol("EXT_CONTINUE")
  145. EXT_STOP = util.symbol("EXT_STOP")
  146. EXT_SKIP = util.symbol("EXT_SKIP")
  147. ONETOMANY = util.symbol(
  148. "ONETOMANY",
  149. """Indicates the one-to-many direction for a :func:`_orm.relationship`.
  150. This symbol is typically used by the internals but may be exposed within
  151. certain API features.
  152. """,
  153. )
  154. MANYTOONE = util.symbol(
  155. "MANYTOONE",
  156. """Indicates the many-to-one direction for a :func:`_orm.relationship`.
  157. This symbol is typically used by the internals but may be exposed within
  158. certain API features.
  159. """,
  160. )
  161. MANYTOMANY = util.symbol(
  162. "MANYTOMANY",
  163. """Indicates the many-to-many direction for a :func:`_orm.relationship`.
  164. This symbol is typically used by the internals but may be exposed within
  165. certain API features.
  166. """,
  167. )
  168. NOT_EXTENSION = util.symbol(
  169. "NOT_EXTENSION",
  170. """Symbol indicating an :class:`InspectionAttr` that's
  171. not part of sqlalchemy.ext.
  172. Is assigned to the :attr:`.InspectionAttr.extension_type`
  173. attribute.
  174. """,
  175. )
  176. _never_set = frozenset([NEVER_SET])
  177. _none_set = frozenset([None, NEVER_SET, PASSIVE_NO_RESULT])
  178. _SET_DEFERRED_EXPIRED = util.symbol("SET_DEFERRED_EXPIRED")
  179. _DEFER_FOR_STATE = util.symbol("DEFER_FOR_STATE")
  180. _RAISE_FOR_STATE = util.symbol("RAISE_FOR_STATE")
  181. def _assertions(*assertions):
  182. @util.decorator
  183. def generate(fn, *args, **kw):
  184. self = args[0]
  185. for assertion in assertions:
  186. assertion(self, fn.__name__)
  187. fn(self, *args[1:], **kw)
  188. return generate
  189. # these can be replaced by sqlalchemy.ext.instrumentation
  190. # if augmented class instrumentation is enabled.
  191. def manager_of_class(cls):
  192. return cls.__dict__.get(DEFAULT_MANAGER_ATTR, None)
  193. instance_state = operator.attrgetter(DEFAULT_STATE_ATTR)
  194. instance_dict = operator.attrgetter("__dict__")
  195. def instance_str(instance):
  196. """Return a string describing an instance."""
  197. return state_str(instance_state(instance))
  198. def state_str(state):
  199. """Return a string describing an instance via its InstanceState."""
  200. if state is None:
  201. return "None"
  202. else:
  203. return "<%s at 0x%x>" % (state.class_.__name__, id(state.obj()))
  204. def state_class_str(state):
  205. """Return a string describing an instance's class via its
  206. InstanceState.
  207. """
  208. if state is None:
  209. return "None"
  210. else:
  211. return "<%s>" % (state.class_.__name__,)
  212. def attribute_str(instance, attribute):
  213. return instance_str(instance) + "." + attribute
  214. def state_attribute_str(state, attribute):
  215. return state_str(state) + "." + attribute
  216. def object_mapper(instance):
  217. """Given an object, return the primary Mapper associated with the object
  218. instance.
  219. Raises :class:`sqlalchemy.orm.exc.UnmappedInstanceError`
  220. if no mapping is configured.
  221. This function is available via the inspection system as::
  222. inspect(instance).mapper
  223. Using the inspection system will raise
  224. :class:`sqlalchemy.exc.NoInspectionAvailable` if the instance is
  225. not part of a mapping.
  226. """
  227. return object_state(instance).mapper
  228. def object_state(instance):
  229. """Given an object, return the :class:`.InstanceState`
  230. associated with the object.
  231. Raises :class:`sqlalchemy.orm.exc.UnmappedInstanceError`
  232. if no mapping is configured.
  233. Equivalent functionality is available via the :func:`_sa.inspect`
  234. function as::
  235. inspect(instance)
  236. Using the inspection system will raise
  237. :class:`sqlalchemy.exc.NoInspectionAvailable` if the instance is
  238. not part of a mapping.
  239. """
  240. state = _inspect_mapped_object(instance)
  241. if state is None:
  242. raise exc.UnmappedInstanceError(instance)
  243. else:
  244. return state
  245. @inspection._inspects(object)
  246. def _inspect_mapped_object(instance):
  247. try:
  248. return instance_state(instance)
  249. except (exc.UnmappedClassError,) + exc.NO_STATE:
  250. return None
  251. def _class_to_mapper(class_or_mapper):
  252. insp = inspection.inspect(class_or_mapper, False)
  253. if insp is not None:
  254. return insp.mapper
  255. else:
  256. raise exc.UnmappedClassError(class_or_mapper)
  257. def _mapper_or_none(entity):
  258. """Return the :class:`_orm.Mapper` for the given class or None if the
  259. class is not mapped.
  260. """
  261. insp = inspection.inspect(entity, False)
  262. if insp is not None:
  263. return insp.mapper
  264. else:
  265. return None
  266. def _is_mapped_class(entity):
  267. """Return True if the given object is a mapped class,
  268. :class:`_orm.Mapper`, or :class:`.AliasedClass`.
  269. """
  270. insp = inspection.inspect(entity, False)
  271. return (
  272. insp is not None
  273. and not insp.is_clause_element
  274. and (insp.is_mapper or insp.is_aliased_class)
  275. )
  276. def _orm_columns(entity):
  277. insp = inspection.inspect(entity, False)
  278. if hasattr(insp, "selectable") and hasattr(insp.selectable, "c"):
  279. return [c for c in insp.selectable.c]
  280. else:
  281. return [entity]
  282. def _is_aliased_class(entity):
  283. insp = inspection.inspect(entity, False)
  284. return insp is not None and getattr(insp, "is_aliased_class", False)
  285. def _entity_descriptor(entity, key):
  286. """Return a class attribute given an entity and string name.
  287. May return :class:`.InstrumentedAttribute` or user-defined
  288. attribute.
  289. """
  290. insp = inspection.inspect(entity)
  291. if insp.is_selectable:
  292. description = entity
  293. entity = insp.c
  294. elif insp.is_aliased_class:
  295. entity = insp.entity
  296. description = entity
  297. elif hasattr(insp, "mapper"):
  298. description = entity = insp.mapper.class_
  299. else:
  300. description = entity
  301. try:
  302. return getattr(entity, key)
  303. except AttributeError as err:
  304. util.raise_(
  305. sa_exc.InvalidRequestError(
  306. "Entity '%s' has no property '%s'" % (description, key)
  307. ),
  308. replace_context=err,
  309. )
  310. _state_mapper = util.dottedgetter("manager.mapper")
  311. @inspection._inspects(type)
  312. def _inspect_mapped_class(class_, configure=False):
  313. try:
  314. class_manager = manager_of_class(class_)
  315. if not class_manager.is_mapped:
  316. return None
  317. mapper = class_manager.mapper
  318. except exc.NO_STATE:
  319. return None
  320. else:
  321. if configure:
  322. mapper._check_configure()
  323. return mapper
  324. def class_mapper(class_, configure=True):
  325. """Given a class, return the primary :class:`_orm.Mapper` associated
  326. with the key.
  327. Raises :exc:`.UnmappedClassError` if no mapping is configured
  328. on the given class, or :exc:`.ArgumentError` if a non-class
  329. object is passed.
  330. Equivalent functionality is available via the :func:`_sa.inspect`
  331. function as::
  332. inspect(some_mapped_class)
  333. Using the inspection system will raise
  334. :class:`sqlalchemy.exc.NoInspectionAvailable` if the class is not mapped.
  335. """
  336. mapper = _inspect_mapped_class(class_, configure=configure)
  337. if mapper is None:
  338. if not isinstance(class_, type):
  339. raise sa_exc.ArgumentError(
  340. "Class object expected, got '%r'." % (class_,)
  341. )
  342. raise exc.UnmappedClassError(class_)
  343. else:
  344. return mapper
  345. class InspectionAttr(object):
  346. """A base class applied to all ORM objects that can be returned
  347. by the :func:`_sa.inspect` function.
  348. The attributes defined here allow the usage of simple boolean
  349. checks to test basic facts about the object returned.
  350. While the boolean checks here are basically the same as using
  351. the Python isinstance() function, the flags here can be used without
  352. the need to import all of these classes, and also such that
  353. the SQLAlchemy class system can change while leaving the flags
  354. here intact for forwards-compatibility.
  355. """
  356. __slots__ = ()
  357. is_selectable = False
  358. """Return True if this object is an instance of
  359. :class:`_expression.Selectable`."""
  360. is_aliased_class = False
  361. """True if this object is an instance of :class:`.AliasedClass`."""
  362. is_instance = False
  363. """True if this object is an instance of :class:`.InstanceState`."""
  364. is_mapper = False
  365. """True if this object is an instance of :class:`_orm.Mapper`."""
  366. is_bundle = False
  367. """True if this object is an instance of :class:`.Bundle`."""
  368. is_property = False
  369. """True if this object is an instance of :class:`.MapperProperty`."""
  370. is_attribute = False
  371. """True if this object is a Python :term:`descriptor`.
  372. This can refer to one of many types. Usually a
  373. :class:`.QueryableAttribute` which handles attributes events on behalf
  374. of a :class:`.MapperProperty`. But can also be an extension type
  375. such as :class:`.AssociationProxy` or :class:`.hybrid_property`.
  376. The :attr:`.InspectionAttr.extension_type` will refer to a constant
  377. identifying the specific subtype.
  378. .. seealso::
  379. :attr:`_orm.Mapper.all_orm_descriptors`
  380. """
  381. _is_internal_proxy = False
  382. """True if this object is an internal proxy object.
  383. .. versionadded:: 1.2.12
  384. """
  385. is_clause_element = False
  386. """True if this object is an instance of
  387. :class:`_expression.ClauseElement`."""
  388. extension_type = NOT_EXTENSION
  389. """The extension type, if any.
  390. Defaults to :data:`.interfaces.NOT_EXTENSION`
  391. .. seealso::
  392. :data:`.HYBRID_METHOD`
  393. :data:`.HYBRID_PROPERTY`
  394. :data:`.ASSOCIATION_PROXY`
  395. """
  396. class InspectionAttrInfo(InspectionAttr):
  397. """Adds the ``.info`` attribute to :class:`.InspectionAttr`.
  398. The rationale for :class:`.InspectionAttr` vs. :class:`.InspectionAttrInfo`
  399. is that the former is compatible as a mixin for classes that specify
  400. ``__slots__``; this is essentially an implementation artifact.
  401. """
  402. @util.memoized_property
  403. def info(self):
  404. """Info dictionary associated with the object, allowing user-defined
  405. data to be associated with this :class:`.InspectionAttr`.
  406. The dictionary is generated when first accessed. Alternatively,
  407. it can be specified as a constructor argument to the
  408. :func:`.column_property`, :func:`_orm.relationship`, or
  409. :func:`.composite`
  410. functions.
  411. .. versionchanged:: 1.0.0 :attr:`.MapperProperty.info` is also
  412. available on extension types via the
  413. :attr:`.InspectionAttrInfo.info` attribute, so that it can apply
  414. to a wider variety of ORM and extension constructs.
  415. .. seealso::
  416. :attr:`.QueryableAttribute.info`
  417. :attr:`.SchemaItem.info`
  418. """
  419. return {}
  420. class _MappedAttribute(object):
  421. """Mixin for attributes which should be replaced by mapper-assigned
  422. attributes.
  423. """
  424. __slots__ = ()