properties.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. # orm/properties.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. """MapperProperty implementations.
  8. This is a private module which defines the behavior of individual ORM-
  9. mapped attributes.
  10. """
  11. from __future__ import absolute_import
  12. from . import attributes
  13. from .descriptor_props import CompositeProperty
  14. from .descriptor_props import ConcreteInheritedProperty
  15. from .descriptor_props import SynonymProperty
  16. from .interfaces import PropComparator
  17. from .interfaces import StrategizedProperty
  18. from .relationships import RelationshipProperty
  19. from .util import _orm_full_deannotate
  20. from .. import log
  21. from .. import util
  22. from ..sql import coercions
  23. from ..sql import roles
  24. __all__ = [
  25. "ColumnProperty",
  26. "CompositeProperty",
  27. "ConcreteInheritedProperty",
  28. "RelationshipProperty",
  29. "SynonymProperty",
  30. ]
  31. @log.class_logger
  32. class ColumnProperty(StrategizedProperty):
  33. """Describes an object attribute that corresponds to a table column.
  34. Public constructor is the :func:`_orm.column_property` function.
  35. """
  36. strategy_wildcard_key = "column"
  37. inherit_cache = True
  38. _links_to_entity = False
  39. __slots__ = (
  40. "_orig_columns",
  41. "columns",
  42. "group",
  43. "deferred",
  44. "instrument",
  45. "comparator_factory",
  46. "descriptor",
  47. "active_history",
  48. "expire_on_flush",
  49. "info",
  50. "doc",
  51. "strategy_key",
  52. "_creation_order",
  53. "_is_polymorphic_discriminator",
  54. "_mapped_by_synonym",
  55. "_deferred_column_loader",
  56. "_raise_column_loader",
  57. "_renders_in_subqueries",
  58. "raiseload",
  59. )
  60. def __init__(self, *columns, **kwargs):
  61. r"""Provide a column-level property for use with a mapping.
  62. Column-based properties can normally be applied to the mapper's
  63. ``properties`` dictionary using the :class:`_schema.Column`
  64. element directly.
  65. Use this function when the given column is not directly present within
  66. the mapper's selectable; examples include SQL expressions, functions,
  67. and scalar SELECT queries.
  68. The :func:`_orm.column_property` function returns an instance of
  69. :class:`.ColumnProperty`.
  70. Columns that aren't present in the mapper's selectable won't be
  71. persisted by the mapper and are effectively "read-only" attributes.
  72. :param \*cols:
  73. list of Column objects to be mapped.
  74. :param active_history=False:
  75. When ``True``, indicates that the "previous" value for a
  76. scalar attribute should be loaded when replaced, if not
  77. already loaded. Normally, history tracking logic for
  78. simple non-primary-key scalar values only needs to be
  79. aware of the "new" value in order to perform a flush. This
  80. flag is available for applications that make use of
  81. :func:`.attributes.get_history` or :meth:`.Session.is_modified`
  82. which also need to know
  83. the "previous" value of the attribute.
  84. :param comparator_factory: a class which extends
  85. :class:`.ColumnProperty.Comparator` which provides custom SQL
  86. clause generation for comparison operations.
  87. :param group:
  88. a group name for this property when marked as deferred.
  89. :param deferred:
  90. when True, the column property is "deferred", meaning that
  91. it does not load immediately, and is instead loaded when the
  92. attribute is first accessed on an instance. See also
  93. :func:`~sqlalchemy.orm.deferred`.
  94. :param doc:
  95. optional string that will be applied as the doc on the
  96. class-bound descriptor.
  97. :param expire_on_flush=True:
  98. Disable expiry on flush. A column_property() which refers
  99. to a SQL expression (and not a single table-bound column)
  100. is considered to be a "read only" property; populating it
  101. has no effect on the state of data, and it can only return
  102. database state. For this reason a column_property()'s value
  103. is expired whenever the parent object is involved in a
  104. flush, that is, has any kind of "dirty" state within a flush.
  105. Setting this parameter to ``False`` will have the effect of
  106. leaving any existing value present after the flush proceeds.
  107. Note however that the :class:`.Session` with default expiration
  108. settings still expires
  109. all attributes after a :meth:`.Session.commit` call, however.
  110. :param info: Optional data dictionary which will be populated into the
  111. :attr:`.MapperProperty.info` attribute of this object.
  112. :param raiseload: if True, indicates the column should raise an error
  113. when undeferred, rather than loading the value. This can be
  114. altered at query time by using the :func:`.deferred` option with
  115. raiseload=False.
  116. .. versionadded:: 1.4
  117. .. seealso::
  118. :ref:`deferred_raiseload`
  119. .. seealso::
  120. :ref:`column_property_options` - to map columns while including
  121. mapping options
  122. :ref:`mapper_column_property_sql_expressions` - to map SQL
  123. expressions
  124. """
  125. super(ColumnProperty, self).__init__()
  126. self._orig_columns = [
  127. coercions.expect(roles.LabeledColumnExprRole, c) for c in columns
  128. ]
  129. self.columns = [
  130. coercions.expect(
  131. roles.LabeledColumnExprRole, _orm_full_deannotate(c)
  132. )
  133. for c in columns
  134. ]
  135. self.group = kwargs.pop("group", None)
  136. self.deferred = kwargs.pop("deferred", False)
  137. self.raiseload = kwargs.pop("raiseload", False)
  138. self.instrument = kwargs.pop("_instrument", True)
  139. self.comparator_factory = kwargs.pop(
  140. "comparator_factory", self.__class__.Comparator
  141. )
  142. self.descriptor = kwargs.pop("descriptor", None)
  143. self.active_history = kwargs.pop("active_history", False)
  144. self.expire_on_flush = kwargs.pop("expire_on_flush", True)
  145. if "info" in kwargs:
  146. self.info = kwargs.pop("info")
  147. if "doc" in kwargs:
  148. self.doc = kwargs.pop("doc")
  149. else:
  150. for col in reversed(self.columns):
  151. doc = getattr(col, "doc", None)
  152. if doc is not None:
  153. self.doc = doc
  154. break
  155. else:
  156. self.doc = None
  157. if kwargs:
  158. raise TypeError(
  159. "%s received unexpected keyword argument(s): %s"
  160. % (self.__class__.__name__, ", ".join(sorted(kwargs.keys())))
  161. )
  162. util.set_creation_order(self)
  163. self.strategy_key = (
  164. ("deferred", self.deferred),
  165. ("instrument", self.instrument),
  166. )
  167. if self.raiseload:
  168. self.strategy_key += (("raiseload", True),)
  169. def _memoized_attr__renders_in_subqueries(self):
  170. return ("deferred", True) not in self.strategy_key or (
  171. self not in self.parent._readonly_props
  172. )
  173. @util.preload_module("sqlalchemy.orm.state", "sqlalchemy.orm.strategies")
  174. def _memoized_attr__deferred_column_loader(self):
  175. state = util.preloaded.orm_state
  176. strategies = util.preloaded.orm_strategies
  177. return state.InstanceState._instance_level_callable_processor(
  178. self.parent.class_manager,
  179. strategies.LoadDeferredColumns(self.key),
  180. self.key,
  181. )
  182. @util.preload_module("sqlalchemy.orm.state", "sqlalchemy.orm.strategies")
  183. def _memoized_attr__raise_column_loader(self):
  184. state = util.preloaded.orm_state
  185. strategies = util.preloaded.orm_strategies
  186. return state.InstanceState._instance_level_callable_processor(
  187. self.parent.class_manager,
  188. strategies.LoadDeferredColumns(self.key, True),
  189. self.key,
  190. )
  191. def __clause_element__(self):
  192. """Allow the ColumnProperty to work in expression before it is turned
  193. into an instrumented attribute.
  194. """
  195. return self.expression
  196. @property
  197. def expression(self):
  198. """Return the primary column or expression for this ColumnProperty.
  199. E.g.::
  200. class File(Base):
  201. # ...
  202. name = Column(String(64))
  203. extension = Column(String(8))
  204. filename = column_property(name + '.' + extension)
  205. path = column_property('C:/' + filename.expression)
  206. .. seealso::
  207. :ref:`mapper_column_property_sql_expressions_composed`
  208. """
  209. return self.columns[0]
  210. def instrument_class(self, mapper):
  211. if not self.instrument:
  212. return
  213. attributes.register_descriptor(
  214. mapper.class_,
  215. self.key,
  216. comparator=self.comparator_factory(self, mapper),
  217. parententity=mapper,
  218. doc=self.doc,
  219. )
  220. def do_init(self):
  221. super(ColumnProperty, self).do_init()
  222. if len(self.columns) > 1 and set(self.parent.primary_key).issuperset(
  223. self.columns
  224. ):
  225. util.warn(
  226. (
  227. "On mapper %s, primary key column '%s' is being combined "
  228. "with distinct primary key column '%s' in attribute '%s'. "
  229. "Use explicit properties to give each column its own "
  230. "mapped attribute name."
  231. )
  232. % (self.parent, self.columns[1], self.columns[0], self.key)
  233. )
  234. def copy(self):
  235. return ColumnProperty(
  236. deferred=self.deferred,
  237. group=self.group,
  238. active_history=self.active_history,
  239. *self.columns
  240. )
  241. def _getcommitted(
  242. self, state, dict_, column, passive=attributes.PASSIVE_OFF
  243. ):
  244. return state.get_impl(self.key).get_committed_value(
  245. state, dict_, passive=passive
  246. )
  247. def merge(
  248. self,
  249. session,
  250. source_state,
  251. source_dict,
  252. dest_state,
  253. dest_dict,
  254. load,
  255. _recursive,
  256. _resolve_conflict_map,
  257. ):
  258. if not self.instrument:
  259. return
  260. elif self.key in source_dict:
  261. value = source_dict[self.key]
  262. if not load:
  263. dest_dict[self.key] = value
  264. else:
  265. impl = dest_state.get_impl(self.key)
  266. impl.set(dest_state, dest_dict, value, None)
  267. elif dest_state.has_identity and self.key not in dest_dict:
  268. dest_state._expire_attributes(
  269. dest_dict, [self.key], no_loader=True
  270. )
  271. class Comparator(util.MemoizedSlots, PropComparator):
  272. """Produce boolean, comparison, and other operators for
  273. :class:`.ColumnProperty` attributes.
  274. See the documentation for :class:`.PropComparator` for a brief
  275. overview.
  276. .. seealso::
  277. :class:`.PropComparator`
  278. :class:`.ColumnOperators`
  279. :ref:`types_operators`
  280. :attr:`.TypeEngine.comparator_factory`
  281. """
  282. __slots__ = "__clause_element__", "info", "expressions"
  283. def _orm_annotate_column(self, column):
  284. """annotate and possibly adapt a column to be returned
  285. as the mapped-attribute exposed version of the column.
  286. The column in this context needs to act as much like the
  287. column in an ORM mapped context as possible, so includes
  288. annotations to give hints to various ORM functions as to
  289. the source entity of this column. It also adapts it
  290. to the mapper's with_polymorphic selectable if one is
  291. present.
  292. """
  293. pe = self._parententity
  294. annotations = {
  295. "entity_namespace": pe,
  296. "parententity": pe,
  297. "parentmapper": pe,
  298. "proxy_key": self.prop.key,
  299. }
  300. col = column
  301. # for a mapper with polymorphic_on and an adapter, return
  302. # the column against the polymorphic selectable.
  303. # see also orm.util._orm_downgrade_polymorphic_columns
  304. # for the reverse operation.
  305. if self._parentmapper._polymorphic_adapter:
  306. mapper_local_col = col
  307. col = self._parentmapper._polymorphic_adapter.traverse(col)
  308. # this is a clue to the ORM Query etc. that this column
  309. # was adapted to the mapper's polymorphic_adapter. the
  310. # ORM uses this hint to know which column its adapting.
  311. annotations["adapt_column"] = mapper_local_col
  312. return col._annotate(annotations)._set_propagate_attrs(
  313. {"compile_state_plugin": "orm", "plugin_subject": pe}
  314. )
  315. def _memoized_method___clause_element__(self):
  316. if self.adapter:
  317. return self.adapter(self.prop.columns[0], self.prop.key)
  318. else:
  319. return self._orm_annotate_column(self.prop.columns[0])
  320. def _memoized_attr_info(self):
  321. """The .info dictionary for this attribute."""
  322. ce = self.__clause_element__()
  323. try:
  324. return ce.info
  325. except AttributeError:
  326. return self.prop.info
  327. def _memoized_attr_expressions(self):
  328. """The full sequence of columns referenced by this
  329. attribute, adjusted for any aliasing in progress.
  330. .. versionadded:: 1.3.17
  331. """
  332. if self.adapter:
  333. return [
  334. self.adapter(col, self.prop.key)
  335. for col in self.prop.columns
  336. ]
  337. else:
  338. return [
  339. self._orm_annotate_column(col) for col in self.prop.columns
  340. ]
  341. def _fallback_getattr(self, key):
  342. """proxy attribute access down to the mapped column.
  343. this allows user-defined comparison methods to be accessed.
  344. """
  345. return getattr(self.__clause_element__(), key)
  346. def operate(self, op, *other, **kwargs):
  347. return op(self.__clause_element__(), *other, **kwargs)
  348. def reverse_operate(self, op, other, **kwargs):
  349. col = self.__clause_element__()
  350. return op(col._bind_param(op, other), col, **kwargs)
  351. def __str__(self):
  352. return str(self.parent.class_.__name__) + "." + self.key