interfaces.py 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. # orm/interfaces.py
  2. # Copyright (C) 2005-2025 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. """
  8. Contains various base classes used throughout the ORM.
  9. Defines some key base classes prominent within the internals.
  10. This module and the classes within are mostly private, though some attributes
  11. are exposed when inspecting mappings.
  12. """
  13. from __future__ import annotations
  14. import collections
  15. import dataclasses
  16. import typing
  17. from typing import Any
  18. from typing import Callable
  19. from typing import cast
  20. from typing import ClassVar
  21. from typing import Dict
  22. from typing import Generic
  23. from typing import Iterator
  24. from typing import List
  25. from typing import Mapping
  26. from typing import NamedTuple
  27. from typing import NoReturn
  28. from typing import Optional
  29. from typing import Sequence
  30. from typing import Set
  31. from typing import Tuple
  32. from typing import Type
  33. from typing import TYPE_CHECKING
  34. from typing import TypeVar
  35. from typing import Union
  36. from . import exc as orm_exc
  37. from . import path_registry
  38. from .base import _MappedAttribute as _MappedAttribute
  39. from .base import EXT_CONTINUE as EXT_CONTINUE # noqa: F401
  40. from .base import EXT_SKIP as EXT_SKIP # noqa: F401
  41. from .base import EXT_STOP as EXT_STOP # noqa: F401
  42. from .base import InspectionAttr as InspectionAttr # noqa: F401
  43. from .base import InspectionAttrInfo as InspectionAttrInfo
  44. from .base import MANYTOMANY as MANYTOMANY # noqa: F401
  45. from .base import MANYTOONE as MANYTOONE # noqa: F401
  46. from .base import NO_KEY as NO_KEY # noqa: F401
  47. from .base import NO_VALUE as NO_VALUE # noqa: F401
  48. from .base import NotExtension as NotExtension # noqa: F401
  49. from .base import ONETOMANY as ONETOMANY # noqa: F401
  50. from .base import RelationshipDirection as RelationshipDirection # noqa: F401
  51. from .base import SQLORMOperations
  52. from .. import ColumnElement
  53. from .. import exc as sa_exc
  54. from .. import inspection
  55. from .. import util
  56. from ..sql import operators
  57. from ..sql import roles
  58. from ..sql import visitors
  59. from ..sql.base import _NoArg
  60. from ..sql.base import ExecutableOption
  61. from ..sql.cache_key import HasCacheKey
  62. from ..sql.operators import ColumnOperators
  63. from ..sql.schema import Column
  64. from ..sql.type_api import TypeEngine
  65. from ..util import warn_deprecated
  66. from ..util.typing import RODescriptorReference
  67. from ..util.typing import TypedDict
  68. if typing.TYPE_CHECKING:
  69. from ._typing import _EntityType
  70. from ._typing import _IdentityKeyType
  71. from ._typing import _InstanceDict
  72. from ._typing import _InternalEntityType
  73. from ._typing import _ORMAdapterProto
  74. from .attributes import InstrumentedAttribute
  75. from .base import Mapped
  76. from .context import _MapperEntity
  77. from .context import ORMCompileState
  78. from .context import QueryContext
  79. from .decl_api import RegistryType
  80. from .decl_base import _ClassScanMapperConfig
  81. from .loading import _PopulatorDict
  82. from .mapper import Mapper
  83. from .path_registry import AbstractEntityRegistry
  84. from .query import Query
  85. from .session import Session
  86. from .state import InstanceState
  87. from .strategy_options import _LoadElement
  88. from .util import AliasedInsp
  89. from .util import ORMAdapter
  90. from ..engine.result import Result
  91. from ..sql._typing import _ColumnExpressionArgument
  92. from ..sql._typing import _ColumnsClauseArgument
  93. from ..sql._typing import _DMLColumnArgument
  94. from ..sql._typing import _InfoType
  95. from ..sql.operators import OperatorType
  96. from ..sql.visitors import _TraverseInternalsType
  97. from ..util.typing import _AnnotationScanType
  98. _StrategyKey = Tuple[Any, ...]
  99. _T = TypeVar("_T", bound=Any)
  100. _T_co = TypeVar("_T_co", bound=Any, covariant=True)
  101. _TLS = TypeVar("_TLS", bound="Type[LoaderStrategy]")
  102. class ORMStatementRole(roles.StatementRole):
  103. __slots__ = ()
  104. _role_name = (
  105. "Executable SQL or text() construct, including ORM aware objects"
  106. )
  107. class ORMColumnsClauseRole(
  108. roles.ColumnsClauseRole, roles.TypedColumnsClauseRole[_T]
  109. ):
  110. __slots__ = ()
  111. _role_name = "ORM mapped entity, aliased entity, or Column expression"
  112. class ORMEntityColumnsClauseRole(ORMColumnsClauseRole[_T]):
  113. __slots__ = ()
  114. _role_name = "ORM mapped or aliased entity"
  115. class ORMFromClauseRole(roles.StrictFromClauseRole):
  116. __slots__ = ()
  117. _role_name = "ORM mapped entity, aliased entity, or FROM expression"
  118. class ORMColumnDescription(TypedDict):
  119. name: str
  120. # TODO: add python_type and sql_type here; combining them
  121. # into "type" is a bad idea
  122. type: Union[Type[Any], TypeEngine[Any]]
  123. aliased: bool
  124. expr: _ColumnsClauseArgument[Any]
  125. entity: Optional[_ColumnsClauseArgument[Any]]
  126. class _IntrospectsAnnotations:
  127. __slots__ = ()
  128. @classmethod
  129. def _mapper_property_name(cls) -> str:
  130. return cls.__name__
  131. def found_in_pep593_annotated(self) -> Any:
  132. """return a copy of this object to use in declarative when the
  133. object is found inside of an Annotated object."""
  134. raise NotImplementedError(
  135. f"Use of the {self._mapper_property_name()!r} "
  136. "construct inside of an Annotated object is not yet supported."
  137. )
  138. def declarative_scan(
  139. self,
  140. decl_scan: _ClassScanMapperConfig,
  141. registry: RegistryType,
  142. cls: Type[Any],
  143. originating_module: Optional[str],
  144. key: str,
  145. mapped_container: Optional[Type[Mapped[Any]]],
  146. annotation: Optional[_AnnotationScanType],
  147. extracted_mapped_annotation: Optional[_AnnotationScanType],
  148. is_dataclass_field: bool,
  149. ) -> None:
  150. """Perform class-specific initializaton at early declarative scanning
  151. time.
  152. .. versionadded:: 2.0
  153. """
  154. def _raise_for_required(self, key: str, cls: Type[Any]) -> NoReturn:
  155. raise sa_exc.ArgumentError(
  156. f"Python typing annotation is required for attribute "
  157. f'"{cls.__name__}.{key}" when primary argument(s) for '
  158. f'"{self._mapper_property_name()}" '
  159. "construct are None or not present"
  160. )
  161. class _AttributeOptions(NamedTuple):
  162. """define Python-local attribute behavior options common to all
  163. :class:`.MapperProperty` objects.
  164. Currently this includes dataclass-generation arguments.
  165. .. versionadded:: 2.0
  166. """
  167. dataclasses_init: Union[_NoArg, bool]
  168. dataclasses_repr: Union[_NoArg, bool]
  169. dataclasses_default: Union[_NoArg, Any]
  170. dataclasses_default_factory: Union[_NoArg, Callable[[], Any]]
  171. dataclasses_compare: Union[_NoArg, bool]
  172. dataclasses_kw_only: Union[_NoArg, bool]
  173. dataclasses_hash: Union[_NoArg, bool, None]
  174. dataclasses_dataclass_metadata: Union[_NoArg, Mapping[Any, Any], None]
  175. def _as_dataclass_field(self, key: str) -> Any:
  176. """Return a ``dataclasses.Field`` object given these arguments."""
  177. kw: Dict[str, Any] = {}
  178. if self.dataclasses_default_factory is not _NoArg.NO_ARG:
  179. kw["default_factory"] = self.dataclasses_default_factory
  180. if self.dataclasses_default is not _NoArg.NO_ARG:
  181. kw["default"] = self.dataclasses_default
  182. if self.dataclasses_init is not _NoArg.NO_ARG:
  183. kw["init"] = self.dataclasses_init
  184. if self.dataclasses_repr is not _NoArg.NO_ARG:
  185. kw["repr"] = self.dataclasses_repr
  186. if self.dataclasses_compare is not _NoArg.NO_ARG:
  187. kw["compare"] = self.dataclasses_compare
  188. if self.dataclasses_kw_only is not _NoArg.NO_ARG:
  189. kw["kw_only"] = self.dataclasses_kw_only
  190. if self.dataclasses_hash is not _NoArg.NO_ARG:
  191. kw["hash"] = self.dataclasses_hash
  192. if self.dataclasses_dataclass_metadata is not _NoArg.NO_ARG:
  193. kw["metadata"] = self.dataclasses_dataclass_metadata
  194. if "default" in kw and callable(kw["default"]):
  195. # callable defaults are ambiguous. deprecate them in favour of
  196. # insert_default or default_factory. #9936
  197. warn_deprecated(
  198. f"Callable object passed to the ``default`` parameter for "
  199. f"attribute {key!r} in a ORM-mapped Dataclasses context is "
  200. "ambiguous, "
  201. "and this use will raise an error in a future release. "
  202. "If this callable is intended to produce Core level INSERT "
  203. "default values for an underlying ``Column``, use "
  204. "the ``mapped_column.insert_default`` parameter instead. "
  205. "To establish this callable as providing a default value "
  206. "for instances of the dataclass itself, use the "
  207. "``default_factory`` dataclasses parameter.",
  208. "2.0",
  209. )
  210. if (
  211. "init" in kw
  212. and not kw["init"]
  213. and "default" in kw
  214. and not callable(kw["default"]) # ignore callable defaults. #9936
  215. and "default_factory" not in kw # illegal but let dc.field raise
  216. ):
  217. # fix for #9879
  218. default = kw.pop("default")
  219. kw["default_factory"] = lambda: default
  220. return dataclasses.field(**kw)
  221. @classmethod
  222. def _get_arguments_for_make_dataclass(
  223. cls,
  224. key: str,
  225. annotation: _AnnotationScanType,
  226. mapped_container: Optional[Any],
  227. elem: Any,
  228. ) -> Union[
  229. Tuple[str, _AnnotationScanType],
  230. Tuple[str, _AnnotationScanType, dataclasses.Field[Any]],
  231. ]:
  232. """given attribute key, annotation, and value from a class, return
  233. the argument tuple we would pass to dataclasses.make_dataclass()
  234. for this attribute.
  235. """
  236. if isinstance(elem, _DCAttributeOptions):
  237. dc_field = elem._attribute_options._as_dataclass_field(key)
  238. return (key, annotation, dc_field)
  239. elif elem is not _NoArg.NO_ARG:
  240. # why is typing not erroring on this?
  241. return (key, annotation, elem)
  242. elif mapped_container is not None:
  243. # it's Mapped[], but there's no "element", which means declarative
  244. # did not actually do anything for this field. this shouldn't
  245. # happen.
  246. # previously, this would occur because _scan_attributes would
  247. # skip a field that's on an already mapped superclass, but it
  248. # would still include it in the annotations, leading
  249. # to issue #8718
  250. assert False, "Mapped[] received without a mapping declaration"
  251. else:
  252. # plain dataclass field, not mapped. Is only possible
  253. # if __allow_unmapped__ is set up. I can see this mode causing
  254. # problems...
  255. return (key, annotation)
  256. _DEFAULT_ATTRIBUTE_OPTIONS = _AttributeOptions(
  257. _NoArg.NO_ARG,
  258. _NoArg.NO_ARG,
  259. _NoArg.NO_ARG,
  260. _NoArg.NO_ARG,
  261. _NoArg.NO_ARG,
  262. _NoArg.NO_ARG,
  263. _NoArg.NO_ARG,
  264. _NoArg.NO_ARG,
  265. )
  266. _DEFAULT_READONLY_ATTRIBUTE_OPTIONS = _AttributeOptions(
  267. False,
  268. _NoArg.NO_ARG,
  269. _NoArg.NO_ARG,
  270. _NoArg.NO_ARG,
  271. _NoArg.NO_ARG,
  272. _NoArg.NO_ARG,
  273. _NoArg.NO_ARG,
  274. _NoArg.NO_ARG,
  275. )
  276. class _DCAttributeOptions:
  277. """mixin for descriptors or configurational objects that include dataclass
  278. field options.
  279. This includes :class:`.MapperProperty`, :class:`._MapsColumn` within
  280. the ORM, but also includes :class:`.AssociationProxy` within ext.
  281. Can in theory be used for other descriptors that serve a similar role
  282. as association proxy. (*maybe* hybrids, not sure yet.)
  283. """
  284. __slots__ = ()
  285. _attribute_options: _AttributeOptions
  286. """behavioral options for ORM-enabled Python attributes
  287. .. versionadded:: 2.0
  288. """
  289. _has_dataclass_arguments: bool
  290. class _MapsColumns(_DCAttributeOptions, _MappedAttribute[_T]):
  291. """interface for declarative-capable construct that delivers one or more
  292. Column objects to the declarative process to be part of a Table.
  293. """
  294. __slots__ = ()
  295. @property
  296. def mapper_property_to_assign(self) -> Optional[MapperProperty[_T]]:
  297. """return a MapperProperty to be assigned to the declarative mapping"""
  298. raise NotImplementedError()
  299. @property
  300. def columns_to_assign(self) -> List[Tuple[Column[_T], int]]:
  301. """A list of Column objects that should be declaratively added to the
  302. new Table object.
  303. """
  304. raise NotImplementedError()
  305. # NOTE: MapperProperty needs to extend _MappedAttribute so that declarative
  306. # typing works, i.e. "Mapped[A] = relationship()". This introduces an
  307. # inconvenience which is that all the MapperProperty objects are treated
  308. # as descriptors by typing tools, which are misled by this as assignment /
  309. # access to a descriptor attribute wants to move through __get__.
  310. # Therefore, references to MapperProperty as an instance variable, such
  311. # as in PropComparator, may have some special typing workarounds such as the
  312. # use of sqlalchemy.util.typing.DescriptorReference to avoid mis-interpretation
  313. # by typing tools
  314. @inspection._self_inspects
  315. class MapperProperty(
  316. HasCacheKey,
  317. _DCAttributeOptions,
  318. _MappedAttribute[_T],
  319. InspectionAttrInfo,
  320. util.MemoizedSlots,
  321. ):
  322. """Represent a particular class attribute mapped by :class:`_orm.Mapper`.
  323. The most common occurrences of :class:`.MapperProperty` are the
  324. mapped :class:`_schema.Column`, which is represented in a mapping as
  325. an instance of :class:`.ColumnProperty`,
  326. and a reference to another class produced by :func:`_orm.relationship`,
  327. represented in the mapping as an instance of
  328. :class:`.Relationship`.
  329. """
  330. __slots__ = (
  331. "_configure_started",
  332. "_configure_finished",
  333. "_attribute_options",
  334. "_has_dataclass_arguments",
  335. "parent",
  336. "key",
  337. "info",
  338. "doc",
  339. )
  340. _cache_key_traversal: _TraverseInternalsType = [
  341. ("parent", visitors.ExtendedInternalTraversal.dp_has_cache_key),
  342. ("key", visitors.ExtendedInternalTraversal.dp_string),
  343. ]
  344. if not TYPE_CHECKING:
  345. cascade = None
  346. is_property = True
  347. """Part of the InspectionAttr interface; states this object is a
  348. mapper property.
  349. """
  350. comparator: PropComparator[_T]
  351. """The :class:`_orm.PropComparator` instance that implements SQL
  352. expression construction on behalf of this mapped attribute."""
  353. key: str
  354. """name of class attribute"""
  355. parent: Mapper[Any]
  356. """the :class:`.Mapper` managing this property."""
  357. _is_relationship = False
  358. _links_to_entity: bool
  359. """True if this MapperProperty refers to a mapped entity.
  360. Should only be True for Relationship, False for all others.
  361. """
  362. doc: Optional[str]
  363. """optional documentation string"""
  364. info: _InfoType
  365. """Info dictionary associated with the object, allowing user-defined
  366. data to be associated with this :class:`.InspectionAttr`.
  367. The dictionary is generated when first accessed. Alternatively,
  368. it can be specified as a constructor argument to the
  369. :func:`.column_property`, :func:`_orm.relationship`, or :func:`.composite`
  370. functions.
  371. .. seealso::
  372. :attr:`.QueryableAttribute.info`
  373. :attr:`.SchemaItem.info`
  374. """
  375. def _memoized_attr_info(self) -> _InfoType:
  376. """Info dictionary associated with the object, allowing user-defined
  377. data to be associated with this :class:`.InspectionAttr`.
  378. The dictionary is generated when first accessed. Alternatively,
  379. it can be specified as a constructor argument to the
  380. :func:`.column_property`, :func:`_orm.relationship`, or
  381. :func:`.composite`
  382. functions.
  383. .. seealso::
  384. :attr:`.QueryableAttribute.info`
  385. :attr:`.SchemaItem.info`
  386. """
  387. return {}
  388. def setup(
  389. self,
  390. context: ORMCompileState,
  391. query_entity: _MapperEntity,
  392. path: AbstractEntityRegistry,
  393. adapter: Optional[ORMAdapter],
  394. **kwargs: Any,
  395. ) -> None:
  396. """Called by Query for the purposes of constructing a SQL statement.
  397. Each MapperProperty associated with the target mapper processes the
  398. statement referenced by the query context, adding columns and/or
  399. criterion as appropriate.
  400. """
  401. def create_row_processor(
  402. self,
  403. context: ORMCompileState,
  404. query_entity: _MapperEntity,
  405. path: AbstractEntityRegistry,
  406. mapper: Mapper[Any],
  407. result: Result[Any],
  408. adapter: Optional[ORMAdapter],
  409. populators: _PopulatorDict,
  410. ) -> None:
  411. """Produce row processing functions and append to the given
  412. set of populators lists.
  413. """
  414. def cascade_iterator(
  415. self,
  416. type_: str,
  417. state: InstanceState[Any],
  418. dict_: _InstanceDict,
  419. visited_states: Set[InstanceState[Any]],
  420. halt_on: Optional[Callable[[InstanceState[Any]], bool]] = None,
  421. ) -> Iterator[
  422. Tuple[object, Mapper[Any], InstanceState[Any], _InstanceDict]
  423. ]:
  424. """Iterate through instances related to the given instance for
  425. a particular 'cascade', starting with this MapperProperty.
  426. Return an iterator3-tuples (instance, mapper, state).
  427. Note that the 'cascade' collection on this MapperProperty is
  428. checked first for the given type before cascade_iterator is called.
  429. This method typically only applies to Relationship.
  430. """
  431. return iter(())
  432. def set_parent(self, parent: Mapper[Any], init: bool) -> None:
  433. """Set the parent mapper that references this MapperProperty.
  434. This method is overridden by some subclasses to perform extra
  435. setup when the mapper is first known.
  436. """
  437. self.parent = parent
  438. def instrument_class(self, mapper: Mapper[Any]) -> None:
  439. """Hook called by the Mapper to the property to initiate
  440. instrumentation of the class attribute managed by this
  441. MapperProperty.
  442. The MapperProperty here will typically call out to the
  443. attributes module to set up an InstrumentedAttribute.
  444. This step is the first of two steps to set up an InstrumentedAttribute,
  445. and is called early in the mapper setup process.
  446. The second step is typically the init_class_attribute step,
  447. called from StrategizedProperty via the post_instrument_class()
  448. hook. This step assigns additional state to the InstrumentedAttribute
  449. (specifically the "impl") which has been determined after the
  450. MapperProperty has determined what kind of persistence
  451. management it needs to do (e.g. scalar, object, collection, etc).
  452. """
  453. def __init__(
  454. self,
  455. attribute_options: Optional[_AttributeOptions] = None,
  456. _assume_readonly_dc_attributes: bool = False,
  457. ) -> None:
  458. self._configure_started = False
  459. self._configure_finished = False
  460. if _assume_readonly_dc_attributes:
  461. default_attrs = _DEFAULT_READONLY_ATTRIBUTE_OPTIONS
  462. else:
  463. default_attrs = _DEFAULT_ATTRIBUTE_OPTIONS
  464. if attribute_options and attribute_options != default_attrs:
  465. self._has_dataclass_arguments = True
  466. self._attribute_options = attribute_options
  467. else:
  468. self._has_dataclass_arguments = False
  469. self._attribute_options = default_attrs
  470. def init(self) -> None:
  471. """Called after all mappers are created to assemble
  472. relationships between mappers and perform other post-mapper-creation
  473. initialization steps.
  474. """
  475. self._configure_started = True
  476. self.do_init()
  477. self._configure_finished = True
  478. @property
  479. def class_attribute(self) -> InstrumentedAttribute[_T]:
  480. """Return the class-bound descriptor corresponding to this
  481. :class:`.MapperProperty`.
  482. This is basically a ``getattr()`` call::
  483. return getattr(self.parent.class_, self.key)
  484. I.e. if this :class:`.MapperProperty` were named ``addresses``,
  485. and the class to which it is mapped is ``User``, this sequence
  486. is possible::
  487. >>> from sqlalchemy import inspect
  488. >>> mapper = inspect(User)
  489. >>> addresses_property = mapper.attrs.addresses
  490. >>> addresses_property.class_attribute is User.addresses
  491. True
  492. >>> User.addresses.property is addresses_property
  493. True
  494. """
  495. return getattr(self.parent.class_, self.key) # type: ignore
  496. def do_init(self) -> None:
  497. """Perform subclass-specific initialization post-mapper-creation
  498. steps.
  499. This is a template method called by the ``MapperProperty``
  500. object's init() method.
  501. """
  502. def post_instrument_class(self, mapper: Mapper[Any]) -> None:
  503. """Perform instrumentation adjustments that need to occur
  504. after init() has completed.
  505. The given Mapper is the Mapper invoking the operation, which
  506. may not be the same Mapper as self.parent in an inheritance
  507. scenario; however, Mapper will always at least be a sub-mapper of
  508. self.parent.
  509. This method is typically used by StrategizedProperty, which delegates
  510. it to LoaderStrategy.init_class_attribute() to perform final setup
  511. on the class-bound InstrumentedAttribute.
  512. """
  513. def merge(
  514. self,
  515. session: Session,
  516. source_state: InstanceState[Any],
  517. source_dict: _InstanceDict,
  518. dest_state: InstanceState[Any],
  519. dest_dict: _InstanceDict,
  520. load: bool,
  521. _recursive: Dict[Any, object],
  522. _resolve_conflict_map: Dict[_IdentityKeyType[Any], object],
  523. ) -> None:
  524. """Merge the attribute represented by this ``MapperProperty``
  525. from source to destination object.
  526. """
  527. def __repr__(self) -> str:
  528. return "<%s at 0x%x; %s>" % (
  529. self.__class__.__name__,
  530. id(self),
  531. getattr(self, "key", "no key"),
  532. )
  533. @inspection._self_inspects
  534. class PropComparator(SQLORMOperations[_T_co], Generic[_T_co], ColumnOperators):
  535. r"""Defines SQL operations for ORM mapped attributes.
  536. SQLAlchemy allows for operators to
  537. be redefined at both the Core and ORM level. :class:`.PropComparator`
  538. is the base class of operator redefinition for ORM-level operations,
  539. including those of :class:`.ColumnProperty`,
  540. :class:`.Relationship`, and :class:`.Composite`.
  541. User-defined subclasses of :class:`.PropComparator` may be created. The
  542. built-in Python comparison and math operator methods, such as
  543. :meth:`.operators.ColumnOperators.__eq__`,
  544. :meth:`.operators.ColumnOperators.__lt__`, and
  545. :meth:`.operators.ColumnOperators.__add__`, can be overridden to provide
  546. new operator behavior. The custom :class:`.PropComparator` is passed to
  547. the :class:`.MapperProperty` instance via the ``comparator_factory``
  548. argument. In each case,
  549. the appropriate subclass of :class:`.PropComparator` should be used::
  550. # definition of custom PropComparator subclasses
  551. from sqlalchemy.orm.properties import (
  552. ColumnProperty,
  553. Composite,
  554. Relationship,
  555. )
  556. class MyColumnComparator(ColumnProperty.Comparator):
  557. def __eq__(self, other):
  558. return self.__clause_element__() == other
  559. class MyRelationshipComparator(Relationship.Comparator):
  560. def any(self, expression):
  561. "define the 'any' operation"
  562. # ...
  563. class MyCompositeComparator(Composite.Comparator):
  564. def __gt__(self, other):
  565. "redefine the 'greater than' operation"
  566. return sql.and_(
  567. *[
  568. a > b
  569. for a, b in zip(
  570. self.__clause_element__().clauses,
  571. other.__composite_values__(),
  572. )
  573. ]
  574. )
  575. # application of custom PropComparator subclasses
  576. from sqlalchemy.orm import column_property, relationship, composite
  577. from sqlalchemy import Column, String
  578. class SomeMappedClass(Base):
  579. some_column = column_property(
  580. Column("some_column", String),
  581. comparator_factory=MyColumnComparator,
  582. )
  583. some_relationship = relationship(
  584. SomeOtherClass, comparator_factory=MyRelationshipComparator
  585. )
  586. some_composite = composite(
  587. Column("a", String),
  588. Column("b", String),
  589. comparator_factory=MyCompositeComparator,
  590. )
  591. Note that for column-level operator redefinition, it's usually
  592. simpler to define the operators at the Core level, using the
  593. :attr:`.TypeEngine.comparator_factory` attribute. See
  594. :ref:`types_operators` for more detail.
  595. .. seealso::
  596. :class:`.ColumnProperty.Comparator`
  597. :class:`.Relationship.Comparator`
  598. :class:`.Composite.Comparator`
  599. :class:`.ColumnOperators`
  600. :ref:`types_operators`
  601. :attr:`.TypeEngine.comparator_factory`
  602. """
  603. __slots__ = "prop", "_parententity", "_adapt_to_entity"
  604. __visit_name__ = "orm_prop_comparator"
  605. _parententity: _InternalEntityType[Any]
  606. _adapt_to_entity: Optional[AliasedInsp[Any]]
  607. prop: RODescriptorReference[MapperProperty[_T_co]]
  608. def __init__(
  609. self,
  610. prop: MapperProperty[_T],
  611. parentmapper: _InternalEntityType[Any],
  612. adapt_to_entity: Optional[AliasedInsp[Any]] = None,
  613. ):
  614. self.prop = prop
  615. self._parententity = adapt_to_entity or parentmapper
  616. self._adapt_to_entity = adapt_to_entity
  617. @util.non_memoized_property
  618. def property(self) -> MapperProperty[_T_co]:
  619. """Return the :class:`.MapperProperty` associated with this
  620. :class:`.PropComparator`.
  621. Return values here will commonly be instances of
  622. :class:`.ColumnProperty` or :class:`.Relationship`.
  623. """
  624. return self.prop
  625. def __clause_element__(self) -> roles.ColumnsClauseRole:
  626. raise NotImplementedError("%r" % self)
  627. def _bulk_update_tuples(
  628. self, value: Any
  629. ) -> Sequence[Tuple[_DMLColumnArgument, Any]]:
  630. """Receive a SQL expression that represents a value in the SET
  631. clause of an UPDATE statement.
  632. Return a tuple that can be passed to a :class:`_expression.Update`
  633. construct.
  634. """
  635. return [(cast("_DMLColumnArgument", self.__clause_element__()), value)]
  636. def adapt_to_entity(
  637. self, adapt_to_entity: AliasedInsp[Any]
  638. ) -> PropComparator[_T_co]:
  639. """Return a copy of this PropComparator which will use the given
  640. :class:`.AliasedInsp` to produce corresponding expressions.
  641. """
  642. return self.__class__(self.prop, self._parententity, adapt_to_entity)
  643. @util.ro_non_memoized_property
  644. def _parentmapper(self) -> Mapper[Any]:
  645. """legacy; this is renamed to _parententity to be
  646. compatible with QueryableAttribute."""
  647. return self._parententity.mapper
  648. def _criterion_exists(
  649. self,
  650. criterion: Optional[_ColumnExpressionArgument[bool]] = None,
  651. **kwargs: Any,
  652. ) -> ColumnElement[Any]:
  653. return self.prop.comparator._criterion_exists(criterion, **kwargs)
  654. @util.ro_non_memoized_property
  655. def adapter(self) -> Optional[_ORMAdapterProto]:
  656. """Produce a callable that adapts column expressions
  657. to suit an aliased version of this comparator.
  658. """
  659. if self._adapt_to_entity is None:
  660. return None
  661. else:
  662. return self._adapt_to_entity._orm_adapt_element
  663. @util.ro_non_memoized_property
  664. def info(self) -> _InfoType:
  665. return self.prop.info
  666. @staticmethod
  667. def _any_op(a: Any, b: Any, **kwargs: Any) -> Any:
  668. return a.any(b, **kwargs)
  669. @staticmethod
  670. def _has_op(left: Any, other: Any, **kwargs: Any) -> Any:
  671. return left.has(other, **kwargs)
  672. @staticmethod
  673. def _of_type_op(a: Any, class_: Any) -> Any:
  674. return a.of_type(class_)
  675. any_op = cast(operators.OperatorType, _any_op)
  676. has_op = cast(operators.OperatorType, _has_op)
  677. of_type_op = cast(operators.OperatorType, _of_type_op)
  678. if typing.TYPE_CHECKING:
  679. def operate(
  680. self, op: OperatorType, *other: Any, **kwargs: Any
  681. ) -> ColumnElement[Any]: ...
  682. def reverse_operate(
  683. self, op: OperatorType, other: Any, **kwargs: Any
  684. ) -> ColumnElement[Any]: ...
  685. def of_type(self, class_: _EntityType[Any]) -> PropComparator[_T_co]:
  686. r"""Redefine this object in terms of a polymorphic subclass,
  687. :func:`_orm.with_polymorphic` construct, or :func:`_orm.aliased`
  688. construct.
  689. Returns a new PropComparator from which further criterion can be
  690. evaluated.
  691. e.g.::
  692. query.join(Company.employees.of_type(Engineer)).filter(
  693. Engineer.name == "foo"
  694. )
  695. :param \class_: a class or mapper indicating that criterion will be
  696. against this specific subclass.
  697. .. seealso::
  698. :ref:`orm_queryguide_joining_relationships_aliased` - in the
  699. :ref:`queryguide_toplevel`
  700. :ref:`inheritance_of_type`
  701. """
  702. return self.operate(PropComparator.of_type_op, class_) # type: ignore
  703. def and_(
  704. self, *criteria: _ColumnExpressionArgument[bool]
  705. ) -> PropComparator[bool]:
  706. """Add additional criteria to the ON clause that's represented by this
  707. relationship attribute.
  708. E.g.::
  709. stmt = select(User).join(
  710. User.addresses.and_(Address.email_address != "foo")
  711. )
  712. stmt = select(User).options(
  713. joinedload(User.addresses.and_(Address.email_address != "foo"))
  714. )
  715. .. versionadded:: 1.4
  716. .. seealso::
  717. :ref:`orm_queryguide_join_on_augmented`
  718. :ref:`loader_option_criteria`
  719. :func:`.with_loader_criteria`
  720. """
  721. return self.operate(operators.and_, *criteria) # type: ignore
  722. def any(
  723. self,
  724. criterion: Optional[_ColumnExpressionArgument[bool]] = None,
  725. **kwargs: Any,
  726. ) -> ColumnElement[bool]:
  727. r"""Return a SQL expression representing true if this element
  728. references a member which meets the given criterion.
  729. The usual implementation of ``any()`` is
  730. :meth:`.Relationship.Comparator.any`.
  731. :param criterion: an optional ClauseElement formulated against the
  732. member class' table or attributes.
  733. :param \**kwargs: key/value pairs corresponding to member class
  734. attribute names which will be compared via equality to the
  735. corresponding values.
  736. """
  737. return self.operate(PropComparator.any_op, criterion, **kwargs)
  738. def has(
  739. self,
  740. criterion: Optional[_ColumnExpressionArgument[bool]] = None,
  741. **kwargs: Any,
  742. ) -> ColumnElement[bool]:
  743. r"""Return a SQL expression representing true if this element
  744. references a member which meets the given criterion.
  745. The usual implementation of ``has()`` is
  746. :meth:`.Relationship.Comparator.has`.
  747. :param criterion: an optional ClauseElement formulated against the
  748. member class' table or attributes.
  749. :param \**kwargs: key/value pairs corresponding to member class
  750. attribute names which will be compared via equality to the
  751. corresponding values.
  752. """
  753. return self.operate(PropComparator.has_op, criterion, **kwargs)
  754. class StrategizedProperty(MapperProperty[_T]):
  755. """A MapperProperty which uses selectable strategies to affect
  756. loading behavior.
  757. There is a single strategy selected by default. Alternate
  758. strategies can be selected at Query time through the usage of
  759. ``StrategizedOption`` objects via the Query.options() method.
  760. The mechanics of StrategizedProperty are used for every Query
  761. invocation for every mapped attribute participating in that Query,
  762. to determine first how the attribute will be rendered in SQL
  763. and secondly how the attribute will retrieve a value from a result
  764. row and apply it to a mapped object. The routines here are very
  765. performance-critical.
  766. """
  767. __slots__ = (
  768. "_strategies",
  769. "strategy",
  770. "_wildcard_token",
  771. "_default_path_loader_key",
  772. "strategy_key",
  773. )
  774. inherit_cache = True
  775. strategy_wildcard_key: ClassVar[str]
  776. strategy_key: _StrategyKey
  777. _strategies: Dict[_StrategyKey, LoaderStrategy]
  778. def _memoized_attr__wildcard_token(self) -> Tuple[str]:
  779. return (
  780. f"{self.strategy_wildcard_key}:{path_registry._WILDCARD_TOKEN}",
  781. )
  782. def _memoized_attr__default_path_loader_key(
  783. self,
  784. ) -> Tuple[str, Tuple[str]]:
  785. return (
  786. "loader",
  787. (f"{self.strategy_wildcard_key}:{path_registry._DEFAULT_TOKEN}",),
  788. )
  789. def _get_context_loader(
  790. self, context: ORMCompileState, path: AbstractEntityRegistry
  791. ) -> Optional[_LoadElement]:
  792. load: Optional[_LoadElement] = None
  793. search_path = path[self]
  794. # search among: exact match, "attr.*", "default" strategy
  795. # if any.
  796. for path_key in (
  797. search_path._loader_key,
  798. search_path._wildcard_path_loader_key,
  799. search_path._default_path_loader_key,
  800. ):
  801. if path_key in context.attributes:
  802. load = context.attributes[path_key]
  803. break
  804. # note that if strategy_options.Load is placing non-actionable
  805. # objects in the context like defaultload(), we would
  806. # need to continue the loop here if we got such an
  807. # option as below.
  808. # if load.strategy or load.local_opts:
  809. # break
  810. return load
  811. def _get_strategy(self, key: _StrategyKey) -> LoaderStrategy:
  812. try:
  813. return self._strategies[key]
  814. except KeyError:
  815. pass
  816. # run outside to prevent transfer of exception context
  817. cls = self._strategy_lookup(self, *key)
  818. # this previously was setting self._strategies[cls], that's
  819. # a bad idea; should use strategy key at all times because every
  820. # strategy has multiple keys at this point
  821. self._strategies[key] = strategy = cls(self, key)
  822. return strategy
  823. def setup(
  824. self,
  825. context: ORMCompileState,
  826. query_entity: _MapperEntity,
  827. path: AbstractEntityRegistry,
  828. adapter: Optional[ORMAdapter],
  829. **kwargs: Any,
  830. ) -> None:
  831. loader = self._get_context_loader(context, path)
  832. if loader and loader.strategy:
  833. strat = self._get_strategy(loader.strategy)
  834. else:
  835. strat = self.strategy
  836. strat.setup_query(
  837. context, query_entity, path, loader, adapter, **kwargs
  838. )
  839. def create_row_processor(
  840. self,
  841. context: ORMCompileState,
  842. query_entity: _MapperEntity,
  843. path: AbstractEntityRegistry,
  844. mapper: Mapper[Any],
  845. result: Result[Any],
  846. adapter: Optional[ORMAdapter],
  847. populators: _PopulatorDict,
  848. ) -> None:
  849. loader = self._get_context_loader(context, path)
  850. if loader and loader.strategy:
  851. strat = self._get_strategy(loader.strategy)
  852. else:
  853. strat = self.strategy
  854. strat.create_row_processor(
  855. context,
  856. query_entity,
  857. path,
  858. loader,
  859. mapper,
  860. result,
  861. adapter,
  862. populators,
  863. )
  864. def do_init(self) -> None:
  865. self._strategies = {}
  866. self.strategy = self._get_strategy(self.strategy_key)
  867. def post_instrument_class(self, mapper: Mapper[Any]) -> None:
  868. if (
  869. not self.parent.non_primary
  870. and not mapper.class_manager._attr_has_impl(self.key)
  871. ):
  872. self.strategy.init_class_attribute(mapper)
  873. _all_strategies: collections.defaultdict[
  874. Type[MapperProperty[Any]], Dict[_StrategyKey, Type[LoaderStrategy]]
  875. ] = collections.defaultdict(dict)
  876. @classmethod
  877. def strategy_for(cls, **kw: Any) -> Callable[[_TLS], _TLS]:
  878. def decorate(dec_cls: _TLS) -> _TLS:
  879. # ensure each subclass of the strategy has its
  880. # own _strategy_keys collection
  881. if "_strategy_keys" not in dec_cls.__dict__:
  882. dec_cls._strategy_keys = []
  883. key = tuple(sorted(kw.items()))
  884. cls._all_strategies[cls][key] = dec_cls
  885. dec_cls._strategy_keys.append(key)
  886. return dec_cls
  887. return decorate
  888. @classmethod
  889. def _strategy_lookup(
  890. cls, requesting_property: MapperProperty[Any], *key: Any
  891. ) -> Type[LoaderStrategy]:
  892. requesting_property.parent._with_polymorphic_mappers
  893. for prop_cls in cls.__mro__:
  894. if prop_cls in cls._all_strategies:
  895. if TYPE_CHECKING:
  896. assert issubclass(prop_cls, MapperProperty)
  897. strategies = cls._all_strategies[prop_cls]
  898. try:
  899. return strategies[key]
  900. except KeyError:
  901. pass
  902. for property_type, strats in cls._all_strategies.items():
  903. if key in strats:
  904. intended_property_type = property_type
  905. actual_strategy = strats[key]
  906. break
  907. else:
  908. intended_property_type = None
  909. actual_strategy = None
  910. raise orm_exc.LoaderStrategyException(
  911. cls,
  912. requesting_property,
  913. intended_property_type,
  914. actual_strategy,
  915. key,
  916. )
  917. class ORMOption(ExecutableOption):
  918. """Base class for option objects that are passed to ORM queries.
  919. These options may be consumed by :meth:`.Query.options`,
  920. :meth:`.Select.options`, or in a more general sense by any
  921. :meth:`.Executable.options` method. They are interpreted at
  922. statement compile time or execution time in modern use. The
  923. deprecated :class:`.MapperOption` is consumed at ORM query construction
  924. time.
  925. .. versionadded:: 1.4
  926. """
  927. __slots__ = ()
  928. _is_legacy_option = False
  929. propagate_to_loaders = False
  930. """if True, indicate this option should be carried along
  931. to "secondary" SELECT statements that occur for relationship
  932. lazy loaders as well as attribute load / refresh operations.
  933. """
  934. _is_core = False
  935. _is_user_defined = False
  936. _is_compile_state = False
  937. _is_criteria_option = False
  938. _is_strategy_option = False
  939. def _adapt_cached_option_to_uncached_option(
  940. self, context: QueryContext, uncached_opt: ORMOption
  941. ) -> ORMOption:
  942. """adapt this option to the "uncached" version of itself in a
  943. loader strategy context.
  944. given "self" which is an option from a cached query, as well as the
  945. corresponding option from the uncached version of the same query,
  946. return the option we should use in a new query, in the context of a
  947. loader strategy being asked to load related rows on behalf of that
  948. cached query, which is assumed to be building a new query based on
  949. entities passed to us from the cached query.
  950. Currently this routine chooses between "self" and "uncached" without
  951. manufacturing anything new. If the option is itself a loader strategy
  952. option which has a path, that path needs to match to the entities being
  953. passed to us by the cached query, so the :class:`_orm.Load` subclass
  954. overrides this to return "self". For all other options, we return the
  955. uncached form which may have changing state, such as a
  956. with_loader_criteria() option which will very often have new state.
  957. This routine could in the future involve
  958. generating a new option based on both inputs if use cases arise,
  959. such as if with_loader_criteria() needed to match up to
  960. ``AliasedClass`` instances given in the parent query.
  961. However, longer term it might be better to restructure things such that
  962. ``AliasedClass`` entities are always matched up on their cache key,
  963. instead of identity, in things like paths and such, so that this whole
  964. issue of "the uncached option does not match the entities" goes away.
  965. However this would make ``PathRegistry`` more complicated and difficult
  966. to debug as well as potentially less performant in that it would be
  967. hashing enormous cache keys rather than a simple AliasedInsp. UNLESS,
  968. we could get cache keys overall to be reliably hashed into something
  969. like an md5 key.
  970. .. versionadded:: 1.4.41
  971. """
  972. if uncached_opt is not None:
  973. return uncached_opt
  974. else:
  975. return self
  976. class CompileStateOption(HasCacheKey, ORMOption):
  977. """base for :class:`.ORMOption` classes that affect the compilation of
  978. a SQL query and therefore need to be part of the cache key.
  979. .. note:: :class:`.CompileStateOption` is generally non-public and
  980. should not be used as a base class for user-defined options; instead,
  981. use :class:`.UserDefinedOption`, which is easier to use as it does not
  982. interact with ORM compilation internals or caching.
  983. :class:`.CompileStateOption` defines an internal attribute
  984. ``_is_compile_state=True`` which has the effect of the ORM compilation
  985. routines for SELECT and other statements will call upon these options when
  986. a SQL string is being compiled. As such, these classes implement
  987. :class:`.HasCacheKey` and need to provide robust ``_cache_key_traversal``
  988. structures.
  989. The :class:`.CompileStateOption` class is used to implement the ORM
  990. :class:`.LoaderOption` and :class:`.CriteriaOption` classes.
  991. .. versionadded:: 1.4.28
  992. """
  993. __slots__ = ()
  994. _is_compile_state = True
  995. def process_compile_state(self, compile_state: ORMCompileState) -> None:
  996. """Apply a modification to a given :class:`.ORMCompileState`.
  997. This method is part of the implementation of a particular
  998. :class:`.CompileStateOption` and is only invoked internally
  999. when an ORM query is compiled.
  1000. """
  1001. def process_compile_state_replaced_entities(
  1002. self,
  1003. compile_state: ORMCompileState,
  1004. mapper_entities: Sequence[_MapperEntity],
  1005. ) -> None:
  1006. """Apply a modification to a given :class:`.ORMCompileState`,
  1007. given entities that were replaced by with_only_columns() or
  1008. with_entities().
  1009. This method is part of the implementation of a particular
  1010. :class:`.CompileStateOption` and is only invoked internally
  1011. when an ORM query is compiled.
  1012. .. versionadded:: 1.4.19
  1013. """
  1014. class LoaderOption(CompileStateOption):
  1015. """Describe a loader modification to an ORM statement at compilation time.
  1016. .. versionadded:: 1.4
  1017. """
  1018. __slots__ = ()
  1019. def process_compile_state_replaced_entities(
  1020. self,
  1021. compile_state: ORMCompileState,
  1022. mapper_entities: Sequence[_MapperEntity],
  1023. ) -> None:
  1024. self.process_compile_state(compile_state)
  1025. class CriteriaOption(CompileStateOption):
  1026. """Describe a WHERE criteria modification to an ORM statement at
  1027. compilation time.
  1028. .. versionadded:: 1.4
  1029. """
  1030. __slots__ = ()
  1031. _is_criteria_option = True
  1032. def get_global_criteria(self, attributes: Dict[str, Any]) -> None:
  1033. """update additional entity criteria options in the given
  1034. attributes dictionary.
  1035. """
  1036. class UserDefinedOption(ORMOption):
  1037. """Base class for a user-defined option that can be consumed from the
  1038. :meth:`.SessionEvents.do_orm_execute` event hook.
  1039. """
  1040. __slots__ = ("payload",)
  1041. _is_legacy_option = False
  1042. _is_user_defined = True
  1043. propagate_to_loaders = False
  1044. """if True, indicate this option should be carried along
  1045. to "secondary" Query objects produced during lazy loads
  1046. or refresh operations.
  1047. """
  1048. def __init__(self, payload: Optional[Any] = None):
  1049. self.payload = payload
  1050. @util.deprecated_cls(
  1051. "1.4",
  1052. "The :class:`.MapperOption class is deprecated and will be removed "
  1053. "in a future release. For "
  1054. "modifications to queries on a per-execution basis, use the "
  1055. ":class:`.UserDefinedOption` class to establish state within a "
  1056. ":class:`.Query` or other Core statement, then use the "
  1057. ":meth:`.SessionEvents.before_orm_execute` hook to consume them.",
  1058. constructor=None,
  1059. )
  1060. class MapperOption(ORMOption):
  1061. """Describe a modification to a Query"""
  1062. __slots__ = ()
  1063. _is_legacy_option = True
  1064. propagate_to_loaders = False
  1065. """if True, indicate this option should be carried along
  1066. to "secondary" Query objects produced during lazy loads
  1067. or refresh operations.
  1068. """
  1069. def process_query(self, query: Query[Any]) -> None:
  1070. """Apply a modification to the given :class:`_query.Query`."""
  1071. def process_query_conditionally(self, query: Query[Any]) -> None:
  1072. """same as process_query(), except that this option may not
  1073. apply to the given query.
  1074. This is typically applied during a lazy load or scalar refresh
  1075. operation to propagate options stated in the original Query to the
  1076. new Query being used for the load. It occurs for those options that
  1077. specify propagate_to_loaders=True.
  1078. """
  1079. self.process_query(query)
  1080. class LoaderStrategy:
  1081. """Describe the loading behavior of a StrategizedProperty object.
  1082. The ``LoaderStrategy`` interacts with the querying process in three
  1083. ways:
  1084. * it controls the configuration of the ``InstrumentedAttribute``
  1085. placed on a class to handle the behavior of the attribute. this
  1086. may involve setting up class-level callable functions to fire
  1087. off a select operation when the attribute is first accessed
  1088. (i.e. a lazy load)
  1089. * it processes the ``QueryContext`` at statement construction time,
  1090. where it can modify the SQL statement that is being produced.
  1091. For example, simple column attributes will add their represented
  1092. column to the list of selected columns, a joined eager loader
  1093. may establish join clauses to add to the statement.
  1094. * It produces "row processor" functions at result fetching time.
  1095. These "row processor" functions populate a particular attribute
  1096. on a particular mapped instance.
  1097. """
  1098. __slots__ = (
  1099. "parent_property",
  1100. "is_class_level",
  1101. "parent",
  1102. "key",
  1103. "strategy_key",
  1104. "strategy_opts",
  1105. )
  1106. _strategy_keys: ClassVar[List[_StrategyKey]]
  1107. def __init__(
  1108. self, parent: MapperProperty[Any], strategy_key: _StrategyKey
  1109. ):
  1110. self.parent_property = parent
  1111. self.is_class_level = False
  1112. self.parent = self.parent_property.parent
  1113. self.key = self.parent_property.key
  1114. self.strategy_key = strategy_key
  1115. self.strategy_opts = dict(strategy_key)
  1116. def init_class_attribute(self, mapper: Mapper[Any]) -> None:
  1117. pass
  1118. def setup_query(
  1119. self,
  1120. compile_state: ORMCompileState,
  1121. query_entity: _MapperEntity,
  1122. path: AbstractEntityRegistry,
  1123. loadopt: Optional[_LoadElement],
  1124. adapter: Optional[ORMAdapter],
  1125. **kwargs: Any,
  1126. ) -> None:
  1127. """Establish column and other state for a given QueryContext.
  1128. This method fulfills the contract specified by MapperProperty.setup().
  1129. StrategizedProperty delegates its setup() method
  1130. directly to this method.
  1131. """
  1132. def create_row_processor(
  1133. self,
  1134. context: ORMCompileState,
  1135. query_entity: _MapperEntity,
  1136. path: AbstractEntityRegistry,
  1137. loadopt: Optional[_LoadElement],
  1138. mapper: Mapper[Any],
  1139. result: Result[Any],
  1140. adapter: Optional[ORMAdapter],
  1141. populators: _PopulatorDict,
  1142. ) -> None:
  1143. """Establish row processing functions for a given QueryContext.
  1144. This method fulfills the contract specified by
  1145. MapperProperty.create_row_processor().
  1146. StrategizedProperty delegates its create_row_processor() method
  1147. directly to this method.
  1148. """
  1149. def __str__(self) -> str:
  1150. return str(self.parent_property)