interfaces.py 30 KB

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