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.

927 lines
28KB

  1. # orm/interfaces.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. """
  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 absolute_import
  14. import collections
  15. from . import exc as orm_exc
  16. from . import path_registry
  17. from .base import _MappedAttribute # noqa
  18. from .base import EXT_CONTINUE
  19. from .base import EXT_SKIP
  20. from .base import EXT_STOP
  21. from .base import InspectionAttr # noqa
  22. from .base import InspectionAttrInfo # noqa
  23. from .base import MANYTOMANY
  24. from .base import MANYTOONE
  25. from .base import NOT_EXTENSION
  26. from .base import ONETOMANY
  27. from .. import inspect
  28. from .. import inspection
  29. from .. import util
  30. from ..sql import operators
  31. from ..sql import roles
  32. from ..sql import visitors
  33. from ..sql.base import ExecutableOption
  34. from ..sql.traversals import HasCacheKey
  35. __all__ = (
  36. "EXT_CONTINUE",
  37. "EXT_STOP",
  38. "EXT_SKIP",
  39. "ONETOMANY",
  40. "MANYTOMANY",
  41. "MANYTOONE",
  42. "NOT_EXTENSION",
  43. "LoaderStrategy",
  44. "MapperOption",
  45. "LoaderOption",
  46. "MapperProperty",
  47. "PropComparator",
  48. "StrategizedProperty",
  49. )
  50. class ORMStatementRole(roles.StatementRole):
  51. _role_name = (
  52. "Executable SQL or text() construct, including ORM " "aware objects"
  53. )
  54. class ORMColumnsClauseRole(roles.ColumnsClauseRole):
  55. _role_name = "ORM mapped entity, aliased entity, or Column expression"
  56. class ORMEntityColumnsClauseRole(ORMColumnsClauseRole):
  57. _role_name = "ORM mapped or aliased entity"
  58. class ORMFromClauseRole(roles.StrictFromClauseRole):
  59. _role_name = "ORM mapped entity, aliased entity, or FROM expression"
  60. @inspection._self_inspects
  61. class MapperProperty(
  62. HasCacheKey, _MappedAttribute, InspectionAttr, util.MemoizedSlots
  63. ):
  64. """Represent a particular class attribute mapped by :class:`_orm.Mapper`.
  65. The most common occurrences of :class:`.MapperProperty` are the
  66. mapped :class:`_schema.Column`, which is represented in a mapping as
  67. an instance of :class:`.ColumnProperty`,
  68. and a reference to another class produced by :func:`_orm.relationship`,
  69. represented in the mapping as an instance of
  70. :class:`.RelationshipProperty`.
  71. """
  72. __slots__ = (
  73. "_configure_started",
  74. "_configure_finished",
  75. "parent",
  76. "key",
  77. "info",
  78. )
  79. _cache_key_traversal = [
  80. ("parent", visitors.ExtendedInternalTraversal.dp_has_cache_key),
  81. ("key", visitors.ExtendedInternalTraversal.dp_string),
  82. ]
  83. cascade = frozenset()
  84. """The set of 'cascade' attribute names.
  85. This collection is checked before the 'cascade_iterator' method is called.
  86. The collection typically only applies to a RelationshipProperty.
  87. """
  88. is_property = True
  89. """Part of the InspectionAttr interface; states this object is a
  90. mapper property.
  91. """
  92. def _memoized_attr_info(self):
  93. """Info dictionary associated with the object, allowing user-defined
  94. data to be associated with this :class:`.InspectionAttr`.
  95. The dictionary is generated when first accessed. Alternatively,
  96. it can be specified as a constructor argument to the
  97. :func:`.column_property`, :func:`_orm.relationship`, or
  98. :func:`.composite`
  99. functions.
  100. .. versionchanged:: 1.0.0 :attr:`.MapperProperty.info` is also
  101. available on extension types via the
  102. :attr:`.InspectionAttrInfo.info` attribute, so that it can apply
  103. to a wider variety of ORM and extension constructs.
  104. .. seealso::
  105. :attr:`.QueryableAttribute.info`
  106. :attr:`.SchemaItem.info`
  107. """
  108. return {}
  109. def setup(self, context, query_entity, path, adapter, **kwargs):
  110. """Called by Query for the purposes of constructing a SQL statement.
  111. Each MapperProperty associated with the target mapper processes the
  112. statement referenced by the query context, adding columns and/or
  113. criterion as appropriate.
  114. """
  115. def create_row_processor(
  116. self, context, query_entity, path, mapper, result, adapter, populators
  117. ):
  118. """Produce row processing functions and append to the given
  119. set of populators lists.
  120. """
  121. def cascade_iterator(
  122. self, type_, state, dict_, visited_states, halt_on=None
  123. ):
  124. """Iterate through instances related to the given instance for
  125. a particular 'cascade', starting with this MapperProperty.
  126. Return an iterator3-tuples (instance, mapper, state).
  127. Note that the 'cascade' collection on this MapperProperty is
  128. checked first for the given type before cascade_iterator is called.
  129. This method typically only applies to RelationshipProperty.
  130. """
  131. return iter(())
  132. def set_parent(self, parent, init):
  133. """Set the parent mapper that references this MapperProperty.
  134. This method is overridden by some subclasses to perform extra
  135. setup when the mapper is first known.
  136. """
  137. self.parent = parent
  138. def instrument_class(self, mapper):
  139. """Hook called by the Mapper to the property to initiate
  140. instrumentation of the class attribute managed by this
  141. MapperProperty.
  142. The MapperProperty here will typically call out to the
  143. attributes module to set up an InstrumentedAttribute.
  144. This step is the first of two steps to set up an InstrumentedAttribute,
  145. and is called early in the mapper setup process.
  146. The second step is typically the init_class_attribute step,
  147. called from StrategizedProperty via the post_instrument_class()
  148. hook. This step assigns additional state to the InstrumentedAttribute
  149. (specifically the "impl") which has been determined after the
  150. MapperProperty has determined what kind of persistence
  151. management it needs to do (e.g. scalar, object, collection, etc).
  152. """
  153. def __init__(self):
  154. self._configure_started = False
  155. self._configure_finished = False
  156. def init(self):
  157. """Called after all mappers are created to assemble
  158. relationships between mappers and perform other post-mapper-creation
  159. initialization steps.
  160. """
  161. self._configure_started = True
  162. self.do_init()
  163. self._configure_finished = True
  164. @property
  165. def class_attribute(self):
  166. """Return the class-bound descriptor corresponding to this
  167. :class:`.MapperProperty`.
  168. This is basically a ``getattr()`` call::
  169. return getattr(self.parent.class_, self.key)
  170. I.e. if this :class:`.MapperProperty` were named ``addresses``,
  171. and the class to which it is mapped is ``User``, this sequence
  172. is possible::
  173. >>> from sqlalchemy import inspect
  174. >>> mapper = inspect(User)
  175. >>> addresses_property = mapper.attrs.addresses
  176. >>> addresses_property.class_attribute is User.addresses
  177. True
  178. >>> User.addresses.property is addresses_property
  179. True
  180. """
  181. return getattr(self.parent.class_, self.key)
  182. def do_init(self):
  183. """Perform subclass-specific initialization post-mapper-creation
  184. steps.
  185. This is a template method called by the ``MapperProperty``
  186. object's init() method.
  187. """
  188. def post_instrument_class(self, mapper):
  189. """Perform instrumentation adjustments that need to occur
  190. after init() has completed.
  191. The given Mapper is the Mapper invoking the operation, which
  192. may not be the same Mapper as self.parent in an inheritance
  193. scenario; however, Mapper will always at least be a sub-mapper of
  194. self.parent.
  195. This method is typically used by StrategizedProperty, which delegates
  196. it to LoaderStrategy.init_class_attribute() to perform final setup
  197. on the class-bound InstrumentedAttribute.
  198. """
  199. def merge(
  200. self,
  201. session,
  202. source_state,
  203. source_dict,
  204. dest_state,
  205. dest_dict,
  206. load,
  207. _recursive,
  208. _resolve_conflict_map,
  209. ):
  210. """Merge the attribute represented by this ``MapperProperty``
  211. from source to destination object.
  212. """
  213. def __repr__(self):
  214. return "<%s at 0x%x; %s>" % (
  215. self.__class__.__name__,
  216. id(self),
  217. getattr(self, "key", "no key"),
  218. )
  219. @inspection._self_inspects
  220. class PropComparator(operators.ColumnOperators):
  221. r"""Defines SQL operators for :class:`.MapperProperty` objects.
  222. SQLAlchemy allows for operators to
  223. be redefined at both the Core and ORM level. :class:`.PropComparator`
  224. is the base class of operator redefinition for ORM-level operations,
  225. including those of :class:`.ColumnProperty`,
  226. :class:`.RelationshipProperty`, and :class:`.CompositeProperty`.
  227. .. note:: With the advent of Hybrid properties introduced in SQLAlchemy
  228. 0.7, as well as Core-level operator redefinition in
  229. SQLAlchemy 0.8, the use case for user-defined :class:`.PropComparator`
  230. instances is extremely rare. See :ref:`hybrids_toplevel` as well
  231. as :ref:`types_operators`.
  232. User-defined subclasses of :class:`.PropComparator` may be created. The
  233. built-in Python comparison and math operator methods, such as
  234. :meth:`.operators.ColumnOperators.__eq__`,
  235. :meth:`.operators.ColumnOperators.__lt__`, and
  236. :meth:`.operators.ColumnOperators.__add__`, can be overridden to provide
  237. new operator behavior. The custom :class:`.PropComparator` is passed to
  238. the :class:`.MapperProperty` instance via the ``comparator_factory``
  239. argument. In each case,
  240. the appropriate subclass of :class:`.PropComparator` should be used::
  241. # definition of custom PropComparator subclasses
  242. from sqlalchemy.orm.properties import \
  243. ColumnProperty,\
  244. CompositeProperty,\
  245. RelationshipProperty
  246. class MyColumnComparator(ColumnProperty.Comparator):
  247. def __eq__(self, other):
  248. return self.__clause_element__() == other
  249. class MyRelationshipComparator(RelationshipProperty.Comparator):
  250. def any(self, expression):
  251. "define the 'any' operation"
  252. # ...
  253. class MyCompositeComparator(CompositeProperty.Comparator):
  254. def __gt__(self, other):
  255. "redefine the 'greater than' operation"
  256. return sql.and_(*[a>b for a, b in
  257. zip(self.__clause_element__().clauses,
  258. other.__composite_values__())])
  259. # application of custom PropComparator subclasses
  260. from sqlalchemy.orm import column_property, relationship, composite
  261. from sqlalchemy import Column, String
  262. class SomeMappedClass(Base):
  263. some_column = column_property(Column("some_column", String),
  264. comparator_factory=MyColumnComparator)
  265. some_relationship = relationship(SomeOtherClass,
  266. comparator_factory=MyRelationshipComparator)
  267. some_composite = composite(
  268. Column("a", String), Column("b", String),
  269. comparator_factory=MyCompositeComparator
  270. )
  271. Note that for column-level operator redefinition, it's usually
  272. simpler to define the operators at the Core level, using the
  273. :attr:`.TypeEngine.comparator_factory` attribute. See
  274. :ref:`types_operators` for more detail.
  275. .. seealso::
  276. :class:`.ColumnProperty.Comparator`
  277. :class:`.RelationshipProperty.Comparator`
  278. :class:`.CompositeProperty.Comparator`
  279. :class:`.ColumnOperators`
  280. :ref:`types_operators`
  281. :attr:`.TypeEngine.comparator_factory`
  282. """
  283. __slots__ = "prop", "property", "_parententity", "_adapt_to_entity"
  284. __visit_name__ = "orm_prop_comparator"
  285. def __init__(
  286. self,
  287. prop,
  288. parentmapper,
  289. adapt_to_entity=None,
  290. ):
  291. self.prop = self.property = prop
  292. self._parententity = adapt_to_entity or parentmapper
  293. self._adapt_to_entity = adapt_to_entity
  294. def __clause_element__(self):
  295. raise NotImplementedError("%r" % self)
  296. def _bulk_update_tuples(self, value):
  297. """Receive a SQL expression that represents a value in the SET
  298. clause of an UPDATE statement.
  299. Return a tuple that can be passed to a :class:`_expression.Update`
  300. construct.
  301. """
  302. return [(self.__clause_element__(), value)]
  303. def adapt_to_entity(self, adapt_to_entity):
  304. """Return a copy of this PropComparator which will use the given
  305. :class:`.AliasedInsp` to produce corresponding expressions.
  306. """
  307. return self.__class__(self.prop, self._parententity, adapt_to_entity)
  308. @property
  309. def _parentmapper(self):
  310. """legacy; this is renamed to _parententity to be
  311. compatible with QueryableAttribute."""
  312. return inspect(self._parententity).mapper
  313. @property
  314. def _propagate_attrs(self):
  315. # this suits the case in coercions where we don't actually
  316. # call ``__clause_element__()`` but still need to get
  317. # resolved._propagate_attrs. See #6558.
  318. return util.immutabledict(
  319. {
  320. "compile_state_plugin": "orm",
  321. "plugin_subject": self._parentmapper,
  322. }
  323. )
  324. @property
  325. def adapter(self):
  326. """Produce a callable that adapts column expressions
  327. to suit an aliased version of this comparator.
  328. """
  329. if self._adapt_to_entity is None:
  330. return None
  331. else:
  332. return self._adapt_to_entity._adapt_element
  333. @property
  334. def info(self):
  335. return self.property.info
  336. @staticmethod
  337. def any_op(a, b, **kwargs):
  338. return a.any(b, **kwargs)
  339. @staticmethod
  340. def has_op(a, b, **kwargs):
  341. return a.has(b, **kwargs)
  342. @staticmethod
  343. def of_type_op(a, class_):
  344. return a.of_type(class_)
  345. def of_type(self, class_):
  346. r"""Redefine this object in terms of a polymorphic subclass,
  347. :func:`.with_polymorphic` construct, or :func:`.aliased` construct.
  348. Returns a new PropComparator from which further criterion can be
  349. evaluated.
  350. e.g.::
  351. query.join(Company.employees.of_type(Engineer)).\
  352. filter(Engineer.name=='foo')
  353. :param \class_: a class or mapper indicating that criterion will be
  354. against this specific subclass.
  355. .. seealso::
  356. :ref:`inheritance_of_type`
  357. """
  358. return self.operate(PropComparator.of_type_op, class_)
  359. def and_(self, *criteria):
  360. """Add additional criteria to the ON clause that's represented by this
  361. relationship attribute.
  362. E.g.::
  363. stmt = select(User).join(
  364. User.addresses.and_(Address.email_address != 'foo')
  365. )
  366. stmt = select(User).options(
  367. joinedload(User.addresses.and_(Address.email_address != 'foo'))
  368. )
  369. .. versionadded:: 1.4
  370. .. seealso::
  371. :ref:`orm_queryguide_join_on_augmented`
  372. :ref:`loader_option_criteria`
  373. :func:`.with_loader_criteria`
  374. """
  375. return self.operate(operators.and_, *criteria)
  376. def any(self, criterion=None, **kwargs):
  377. r"""Return true if this collection contains any member that meets the
  378. given criterion.
  379. The usual implementation of ``any()`` is
  380. :meth:`.RelationshipProperty.Comparator.any`.
  381. :param criterion: an optional ClauseElement formulated against the
  382. member class' table or attributes.
  383. :param \**kwargs: key/value pairs corresponding to member class
  384. attribute names which will be compared via equality to the
  385. corresponding values.
  386. """
  387. return self.operate(PropComparator.any_op, criterion, **kwargs)
  388. def has(self, criterion=None, **kwargs):
  389. r"""Return true if this element references a member which meets the
  390. given criterion.
  391. The usual implementation of ``has()`` is
  392. :meth:`.RelationshipProperty.Comparator.has`.
  393. :param criterion: an optional ClauseElement formulated against the
  394. member class' table or attributes.
  395. :param \**kwargs: key/value pairs corresponding to member class
  396. attribute names which will be compared via equality to the
  397. corresponding values.
  398. """
  399. return self.operate(PropComparator.has_op, criterion, **kwargs)
  400. class StrategizedProperty(MapperProperty):
  401. """A MapperProperty which uses selectable strategies to affect
  402. loading behavior.
  403. There is a single strategy selected by default. Alternate
  404. strategies can be selected at Query time through the usage of
  405. ``StrategizedOption`` objects via the Query.options() method.
  406. The mechanics of StrategizedProperty are used for every Query
  407. invocation for every mapped attribute participating in that Query,
  408. to determine first how the attribute will be rendered in SQL
  409. and secondly how the attribute will retrieve a value from a result
  410. row and apply it to a mapped object. The routines here are very
  411. performance-critical.
  412. """
  413. __slots__ = (
  414. "_strategies",
  415. "strategy",
  416. "_wildcard_token",
  417. "_default_path_loader_key",
  418. )
  419. inherit_cache = True
  420. strategy_wildcard_key = None
  421. def _memoized_attr__wildcard_token(self):
  422. return (
  423. "%s:%s"
  424. % (self.strategy_wildcard_key, path_registry._WILDCARD_TOKEN),
  425. )
  426. def _memoized_attr__default_path_loader_key(self):
  427. return (
  428. "loader",
  429. (
  430. "%s:%s"
  431. % (self.strategy_wildcard_key, path_registry._DEFAULT_TOKEN),
  432. ),
  433. )
  434. def _get_context_loader(self, context, path):
  435. load = None
  436. search_path = path[self]
  437. # search among: exact match, "attr.*", "default" strategy
  438. # if any.
  439. for path_key in (
  440. search_path._loader_key,
  441. search_path._wildcard_path_loader_key,
  442. search_path._default_path_loader_key,
  443. ):
  444. if path_key in context.attributes:
  445. load = context.attributes[path_key]
  446. break
  447. return load
  448. def _get_strategy(self, key):
  449. try:
  450. return self._strategies[key]
  451. except KeyError:
  452. pass
  453. # run outside to prevent transfer of exception context
  454. cls = self._strategy_lookup(self, *key)
  455. # this previously was setting self._strategies[cls], that's
  456. # a bad idea; should use strategy key at all times because every
  457. # strategy has multiple keys at this point
  458. self._strategies[key] = strategy = cls(self, key)
  459. return strategy
  460. def setup(self, context, query_entity, path, adapter, **kwargs):
  461. loader = self._get_context_loader(context, path)
  462. if loader and loader.strategy:
  463. strat = self._get_strategy(loader.strategy)
  464. else:
  465. strat = self.strategy
  466. strat.setup_query(
  467. context, query_entity, path, loader, adapter, **kwargs
  468. )
  469. def create_row_processor(
  470. self, context, query_entity, path, mapper, result, adapter, populators
  471. ):
  472. loader = self._get_context_loader(context, path)
  473. if loader and loader.strategy:
  474. strat = self._get_strategy(loader.strategy)
  475. else:
  476. strat = self.strategy
  477. strat.create_row_processor(
  478. context,
  479. query_entity,
  480. path,
  481. loader,
  482. mapper,
  483. result,
  484. adapter,
  485. populators,
  486. )
  487. def do_init(self):
  488. self._strategies = {}
  489. self.strategy = self._get_strategy(self.strategy_key)
  490. def post_instrument_class(self, mapper):
  491. if (
  492. not self.parent.non_primary
  493. and not mapper.class_manager._attr_has_impl(self.key)
  494. ):
  495. self.strategy.init_class_attribute(mapper)
  496. _all_strategies = collections.defaultdict(dict)
  497. @classmethod
  498. def strategy_for(cls, **kw):
  499. def decorate(dec_cls):
  500. # ensure each subclass of the strategy has its
  501. # own _strategy_keys collection
  502. if "_strategy_keys" not in dec_cls.__dict__:
  503. dec_cls._strategy_keys = []
  504. key = tuple(sorted(kw.items()))
  505. cls._all_strategies[cls][key] = dec_cls
  506. dec_cls._strategy_keys.append(key)
  507. return dec_cls
  508. return decorate
  509. @classmethod
  510. def _strategy_lookup(cls, requesting_property, *key):
  511. requesting_property.parent._with_polymorphic_mappers
  512. for prop_cls in cls.__mro__:
  513. if prop_cls in cls._all_strategies:
  514. strategies = cls._all_strategies[prop_cls]
  515. try:
  516. return strategies[key]
  517. except KeyError:
  518. pass
  519. for property_type, strats in cls._all_strategies.items():
  520. if key in strats:
  521. intended_property_type = property_type
  522. actual_strategy = strats[key]
  523. break
  524. else:
  525. intended_property_type = None
  526. actual_strategy = None
  527. raise orm_exc.LoaderStrategyException(
  528. cls,
  529. requesting_property,
  530. intended_property_type,
  531. actual_strategy,
  532. key,
  533. )
  534. class ORMOption(ExecutableOption):
  535. """Base class for option objects that are passed to ORM queries.
  536. These options may be consumed by :meth:`.Query.options`,
  537. :meth:`.Select.options`, or in a more general sense by any
  538. :meth:`.Executable.options` method. They are interpreted at
  539. statement compile time or execution time in modern use. The
  540. deprecated :class:`.MapperOption` is consumed at ORM query construction
  541. time.
  542. .. versionadded:: 1.4
  543. """
  544. __slots__ = ()
  545. _is_legacy_option = False
  546. propagate_to_loaders = False
  547. """if True, indicate this option should be carried along
  548. to "secondary" SELECT statements that occur for relationship
  549. lazy loaders as well as attribute load / refresh operations.
  550. """
  551. _is_compile_state = False
  552. _is_criteria_option = False
  553. class LoaderOption(ORMOption):
  554. """Describe a loader modification to an ORM statement at compilation time.
  555. .. versionadded:: 1.4
  556. """
  557. _is_compile_state = True
  558. def process_compile_state_replaced_entities(
  559. self, compile_state, mapper_entities
  560. ):
  561. """Apply a modification to a given :class:`.CompileState`,
  562. given entities that were replaced by with_only_columns() or
  563. with_entities().
  564. .. versionadded:: 1.4.19
  565. """
  566. self.process_compile_state(compile_state)
  567. def process_compile_state(self, compile_state):
  568. """Apply a modification to a given :class:`.CompileState`."""
  569. class CriteriaOption(ORMOption):
  570. """Describe a WHERE criteria modification to an ORM statement at
  571. compilation time.
  572. .. versionadded:: 1.4
  573. """
  574. _is_compile_state = True
  575. _is_criteria_option = True
  576. def process_compile_state(self, compile_state):
  577. """Apply a modification to a given :class:`.CompileState`."""
  578. def get_global_criteria(self, attributes):
  579. """update additional entity criteria options in the given
  580. attributes dictionary.
  581. """
  582. class UserDefinedOption(ORMOption):
  583. """Base class for a user-defined option that can be consumed from the
  584. :meth:`.SessionEvents.do_orm_execute` event hook.
  585. """
  586. _is_legacy_option = False
  587. propagate_to_loaders = False
  588. """if True, indicate this option should be carried along
  589. to "secondary" Query objects produced during lazy loads
  590. or refresh operations.
  591. """
  592. def __init__(self, payload=None):
  593. self.payload = payload
  594. @util.deprecated_cls(
  595. "1.4",
  596. "The :class:`.MapperOption class is deprecated and will be removed "
  597. "in a future release. For "
  598. "modifications to queries on a per-execution basis, use the "
  599. ":class:`.UserDefinedOption` class to establish state within a "
  600. ":class:`.Query` or other Core statement, then use the "
  601. ":meth:`.SessionEvents.before_orm_execute` hook to consume them.",
  602. constructor=None,
  603. )
  604. class MapperOption(ORMOption):
  605. """Describe a modification to a Query"""
  606. _is_legacy_option = True
  607. propagate_to_loaders = False
  608. """if True, indicate this option should be carried along
  609. to "secondary" Query objects produced during lazy loads
  610. or refresh operations.
  611. """
  612. def process_query(self, query):
  613. """Apply a modification to the given :class:`_query.Query`."""
  614. def process_query_conditionally(self, query):
  615. """same as process_query(), except that this option may not
  616. apply to the given query.
  617. This is typically applied during a lazy load or scalar refresh
  618. operation to propagate options stated in the original Query to the
  619. new Query being used for the load. It occurs for those options that
  620. specify propagate_to_loaders=True.
  621. """
  622. self.process_query(query)
  623. class LoaderStrategy(object):
  624. """Describe the loading behavior of a StrategizedProperty object.
  625. The ``LoaderStrategy`` interacts with the querying process in three
  626. ways:
  627. * it controls the configuration of the ``InstrumentedAttribute``
  628. placed on a class to handle the behavior of the attribute. this
  629. may involve setting up class-level callable functions to fire
  630. off a select operation when the attribute is first accessed
  631. (i.e. a lazy load)
  632. * it processes the ``QueryContext`` at statement construction time,
  633. where it can modify the SQL statement that is being produced.
  634. For example, simple column attributes will add their represented
  635. column to the list of selected columns, a joined eager loader
  636. may establish join clauses to add to the statement.
  637. * It produces "row processor" functions at result fetching time.
  638. These "row processor" functions populate a particular attribute
  639. on a particular mapped instance.
  640. """
  641. __slots__ = (
  642. "parent_property",
  643. "is_class_level",
  644. "parent",
  645. "key",
  646. "strategy_key",
  647. "strategy_opts",
  648. )
  649. def __init__(self, parent, strategy_key):
  650. self.parent_property = parent
  651. self.is_class_level = False
  652. self.parent = self.parent_property.parent
  653. self.key = self.parent_property.key
  654. self.strategy_key = strategy_key
  655. self.strategy_opts = dict(strategy_key)
  656. def init_class_attribute(self, mapper):
  657. pass
  658. def setup_query(
  659. self, compile_state, query_entity, path, loadopt, adapter, **kwargs
  660. ):
  661. """Establish column and other state for a given QueryContext.
  662. This method fulfills the contract specified by MapperProperty.setup().
  663. StrategizedProperty delegates its setup() method
  664. directly to this method.
  665. """
  666. def create_row_processor(
  667. self,
  668. context,
  669. query_entity,
  670. path,
  671. loadopt,
  672. mapper,
  673. result,
  674. adapter,
  675. populators,
  676. ):
  677. """Establish row processing functions for a given QueryContext.
  678. This method fulfills the contract specified by
  679. MapperProperty.create_row_processor().
  680. StrategizedProperty delegates its create_row_processor() method
  681. directly to this method.
  682. """
  683. def __str__(self):
  684. return str(self.parent_property)