base.py 15 KB

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