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.

438 lines
15KB

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