base.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. # orm/base.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. """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. PASSIVE_MERGE = util.symbol(
  143. "PASSIVE_OFF | NO_RAISE",
  144. "Symbol used specifically for session.merge() and similar cases",
  145. canonical=PASSIVE_OFF | NO_RAISE,
  146. )
  147. DEFAULT_MANAGER_ATTR = "_sa_class_manager"
  148. DEFAULT_STATE_ATTR = "_sa_instance_state"
  149. EXT_CONTINUE = util.symbol("EXT_CONTINUE")
  150. EXT_STOP = util.symbol("EXT_STOP")
  151. EXT_SKIP = util.symbol("EXT_SKIP")
  152. ONETOMANY = util.symbol(
  153. "ONETOMANY",
  154. """Indicates the one-to-many direction for a :func:`_orm.relationship`.
  155. This symbol is typically used by the internals but may be exposed within
  156. certain API features.
  157. """,
  158. )
  159. MANYTOONE = util.symbol(
  160. "MANYTOONE",
  161. """Indicates the many-to-one direction for a :func:`_orm.relationship`.
  162. This symbol is typically used by the internals but may be exposed within
  163. certain API features.
  164. """,
  165. )
  166. MANYTOMANY = util.symbol(
  167. "MANYTOMANY",
  168. """Indicates the many-to-many direction for a :func:`_orm.relationship`.
  169. This symbol is typically used by the internals but may be exposed within
  170. certain API features.
  171. """,
  172. )
  173. NOT_EXTENSION = util.symbol(
  174. "NOT_EXTENSION",
  175. """Symbol indicating an :class:`InspectionAttr` that's
  176. not part of sqlalchemy.ext.
  177. Is assigned to the :attr:`.InspectionAttr.extension_type`
  178. attribute.
  179. """,
  180. )
  181. _never_set = frozenset([NEVER_SET])
  182. _none_set = frozenset([None, NEVER_SET, PASSIVE_NO_RESULT])
  183. _SET_DEFERRED_EXPIRED = util.symbol("SET_DEFERRED_EXPIRED")
  184. _DEFER_FOR_STATE = util.symbol("DEFER_FOR_STATE")
  185. _RAISE_FOR_STATE = util.symbol("RAISE_FOR_STATE")
  186. def _assertions(*assertions):
  187. @util.decorator
  188. def generate(fn, *args, **kw):
  189. self = args[0]
  190. for assertion in assertions:
  191. assertion(self, fn.__name__)
  192. fn(self, *args[1:], **kw)
  193. return generate
  194. # these can be replaced by sqlalchemy.ext.instrumentation
  195. # if augmented class instrumentation is enabled.
  196. def manager_of_class(cls):
  197. return cls.__dict__.get(DEFAULT_MANAGER_ATTR, None)
  198. instance_state = operator.attrgetter(DEFAULT_STATE_ATTR)
  199. instance_dict = operator.attrgetter("__dict__")
  200. def instance_str(instance):
  201. """Return a string describing an instance."""
  202. return state_str(instance_state(instance))
  203. def state_str(state):
  204. """Return a string describing an instance via its InstanceState."""
  205. if state is None:
  206. return "None"
  207. else:
  208. return "<%s at 0x%x>" % (state.class_.__name__, id(state.obj()))
  209. def state_class_str(state):
  210. """Return a string describing an instance's class via its
  211. InstanceState.
  212. """
  213. if state is None:
  214. return "None"
  215. else:
  216. return "<%s>" % (state.class_.__name__,)
  217. def attribute_str(instance, attribute):
  218. return instance_str(instance) + "." + attribute
  219. def state_attribute_str(state, attribute):
  220. return state_str(state) + "." + attribute
  221. def object_mapper(instance):
  222. """Given an object, return the primary Mapper associated with the object
  223. instance.
  224. Raises :class:`sqlalchemy.orm.exc.UnmappedInstanceError`
  225. if no mapping is configured.
  226. This function is available via the inspection system as::
  227. inspect(instance).mapper
  228. Using the inspection system will raise
  229. :class:`sqlalchemy.exc.NoInspectionAvailable` if the instance is
  230. not part of a mapping.
  231. """
  232. return object_state(instance).mapper
  233. def object_state(instance):
  234. """Given an object, return the :class:`.InstanceState`
  235. associated with the object.
  236. Raises :class:`sqlalchemy.orm.exc.UnmappedInstanceError`
  237. if no mapping is configured.
  238. Equivalent functionality is available via the :func:`_sa.inspect`
  239. function as::
  240. inspect(instance)
  241. Using the inspection system will raise
  242. :class:`sqlalchemy.exc.NoInspectionAvailable` if the instance is
  243. not part of a mapping.
  244. """
  245. state = _inspect_mapped_object(instance)
  246. if state is None:
  247. raise exc.UnmappedInstanceError(instance)
  248. else:
  249. return state
  250. @inspection._inspects(object)
  251. def _inspect_mapped_object(instance):
  252. try:
  253. return instance_state(instance)
  254. except (exc.UnmappedClassError,) + exc.NO_STATE:
  255. return None
  256. def _class_to_mapper(class_or_mapper):
  257. insp = inspection.inspect(class_or_mapper, False)
  258. if insp is not None:
  259. return insp.mapper
  260. else:
  261. raise exc.UnmappedClassError(class_or_mapper)
  262. def _mapper_or_none(entity):
  263. """Return the :class:`_orm.Mapper` for the given class or None if the
  264. class is not mapped.
  265. """
  266. insp = inspection.inspect(entity, False)
  267. if insp is not None:
  268. return insp.mapper
  269. else:
  270. return None
  271. def _is_mapped_class(entity):
  272. """Return True if the given object is a mapped class,
  273. :class:`_orm.Mapper`, or :class:`.AliasedClass`.
  274. """
  275. insp = inspection.inspect(entity, False)
  276. return (
  277. insp is not None
  278. and not insp.is_clause_element
  279. and (insp.is_mapper or insp.is_aliased_class)
  280. )
  281. def _orm_columns(entity):
  282. insp = inspection.inspect(entity, False)
  283. if hasattr(insp, "selectable") and hasattr(insp.selectable, "c"):
  284. return [c for c in insp.selectable.c]
  285. else:
  286. return [entity]
  287. def _is_aliased_class(entity):
  288. insp = inspection.inspect(entity, False)
  289. return insp is not None and getattr(insp, "is_aliased_class", False)
  290. def _entity_descriptor(entity, key):
  291. """Return a class attribute given an entity and string name.
  292. May return :class:`.InstrumentedAttribute` or user-defined
  293. attribute.
  294. """
  295. insp = inspection.inspect(entity)
  296. if insp.is_selectable:
  297. description = entity
  298. entity = insp.c
  299. elif insp.is_aliased_class:
  300. entity = insp.entity
  301. description = entity
  302. elif hasattr(insp, "mapper"):
  303. description = entity = insp.mapper.class_
  304. else:
  305. description = entity
  306. try:
  307. return getattr(entity, key)
  308. except AttributeError as err:
  309. util.raise_(
  310. sa_exc.InvalidRequestError(
  311. "Entity '%s' has no property '%s'" % (description, key)
  312. ),
  313. replace_context=err,
  314. )
  315. _state_mapper = util.dottedgetter("manager.mapper")
  316. @inspection._inspects(type)
  317. def _inspect_mapped_class(class_, configure=False):
  318. try:
  319. class_manager = manager_of_class(class_)
  320. if not class_manager.is_mapped:
  321. return None
  322. mapper = class_manager.mapper
  323. except exc.NO_STATE:
  324. return None
  325. else:
  326. if configure:
  327. mapper._check_configure()
  328. return mapper
  329. def class_mapper(class_, configure=True):
  330. """Given a class, return the primary :class:`_orm.Mapper` associated
  331. with the key.
  332. Raises :exc:`.UnmappedClassError` if no mapping is configured
  333. on the given class, or :exc:`.ArgumentError` if a non-class
  334. object is passed.
  335. Equivalent functionality is available via the :func:`_sa.inspect`
  336. function as::
  337. inspect(some_mapped_class)
  338. Using the inspection system will raise
  339. :class:`sqlalchemy.exc.NoInspectionAvailable` if the class is not mapped.
  340. """
  341. mapper = _inspect_mapped_class(class_, configure=configure)
  342. if mapper is None:
  343. if not isinstance(class_, type):
  344. raise sa_exc.ArgumentError(
  345. "Class object expected, got '%r'." % (class_,)
  346. )
  347. raise exc.UnmappedClassError(class_)
  348. else:
  349. return mapper
  350. class InspectionAttr(object):
  351. """A base class applied to all ORM objects that can be returned
  352. by the :func:`_sa.inspect` function.
  353. The attributes defined here allow the usage of simple boolean
  354. checks to test basic facts about the object returned.
  355. While the boolean checks here are basically the same as using
  356. the Python isinstance() function, the flags here can be used without
  357. the need to import all of these classes, and also such that
  358. the SQLAlchemy class system can change while leaving the flags
  359. here intact for forwards-compatibility.
  360. """
  361. __slots__ = ()
  362. is_selectable = False
  363. """Return True if this object is an instance of
  364. :class:`_expression.Selectable`."""
  365. is_aliased_class = False
  366. """True if this object is an instance of :class:`.AliasedClass`."""
  367. is_instance = False
  368. """True if this object is an instance of :class:`.InstanceState`."""
  369. is_mapper = False
  370. """True if this object is an instance of :class:`_orm.Mapper`."""
  371. is_bundle = False
  372. """True if this object is an instance of :class:`.Bundle`."""
  373. is_property = False
  374. """True if this object is an instance of :class:`.MapperProperty`."""
  375. is_attribute = False
  376. """True if this object is a Python :term:`descriptor`.
  377. This can refer to one of many types. Usually a
  378. :class:`.QueryableAttribute` which handles attributes events on behalf
  379. of a :class:`.MapperProperty`. But can also be an extension type
  380. such as :class:`.AssociationProxy` or :class:`.hybrid_property`.
  381. The :attr:`.InspectionAttr.extension_type` will refer to a constant
  382. identifying the specific subtype.
  383. .. seealso::
  384. :attr:`_orm.Mapper.all_orm_descriptors`
  385. """
  386. _is_internal_proxy = False
  387. """True if this object is an internal proxy object.
  388. .. versionadded:: 1.2.12
  389. """
  390. is_clause_element = False
  391. """True if this object is an instance of
  392. :class:`_expression.ClauseElement`."""
  393. extension_type = NOT_EXTENSION
  394. """The extension type, if any.
  395. Defaults to :data:`.interfaces.NOT_EXTENSION`
  396. .. seealso::
  397. :data:`.HYBRID_METHOD`
  398. :data:`.HYBRID_PROPERTY`
  399. :data:`.ASSOCIATION_PROXY`
  400. """
  401. class InspectionAttrInfo(InspectionAttr):
  402. """Adds the ``.info`` attribute to :class:`.InspectionAttr`.
  403. The rationale for :class:`.InspectionAttr` vs. :class:`.InspectionAttrInfo`
  404. is that the former is compatible as a mixin for classes that specify
  405. ``__slots__``; this is essentially an implementation artifact.
  406. """
  407. @util.memoized_property
  408. def info(self):
  409. """Info dictionary associated with the object, allowing user-defined
  410. data to be associated with this :class:`.InspectionAttr`.
  411. The dictionary is generated when first accessed. Alternatively,
  412. it can be specified as a constructor argument to the
  413. :func:`.column_property`, :func:`_orm.relationship`, or
  414. :func:`.composite`
  415. functions.
  416. .. versionchanged:: 1.0.0 :attr:`.MapperProperty.info` is also
  417. available on extension types via the
  418. :attr:`.InspectionAttrInfo.info` attribute, so that it can apply
  419. to a wider variety of ORM and extension constructs.
  420. .. seealso::
  421. :attr:`.QueryableAttribute.info`
  422. :attr:`.SchemaItem.info`
  423. """
  424. return {}
  425. class _MappedAttribute(object):
  426. """Mixin for attributes which should be replaced by mapper-assigned
  427. attributes.
  428. """
  429. __slots__ = ()