diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-12-16 17:06:43 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-07-03 23:39:51 -0400 |
commit | 3dc9a4a2392d033f9d1bd79dd6b6ecea6281a61c (patch) | |
tree | 1041bccb37422f526dccb5b1e57ffad1c702549b /lib/sqlalchemy/orm/context.py | |
parent | 5060043e8e95ab0aab5f63ed288c1426c46da66e (diff) | |
download | sqlalchemy-3dc9a4a2392d033f9d1bd79dd6b6ecea6281a61c.tar.gz |
introduce deferred lambdas
The coercions system allows us to add in lambdas as arguments
to Core and ORM elements without changing them at all. By allowing
the lambda to produce a deterministic cache key where we can also
cheat and yank out literal parameters means we can move towards
having 90% of "baked" functionality in a clearer way right in
Core / ORM.
As a second step, we can have whole statements inside the lambda,
and can then add generation with __add__(), so then we have
100% of "baked" functionality with full support of ad-hoc
literal values.
Adds some more short_selects tests for the moment for comparison.
Other tweaks inside cache key generation as we're trying to
approach a certain level of performance such that we can
remove the use of "baked" from the loader strategies.
As we have not yet closed #4639, however the caching feature
has been fully integrated as of
b0cfa7379cf8513a821a3dbe3028c4965d9f85bd, we will also
add complete caching documentation here and close that issue
as well.
Closes: #4639
Fixes: #5380
Change-Id: If91f61527236fd4d7ae3cad1f24c38be921c90ba
Diffstat (limited to 'lib/sqlalchemy/orm/context.py')
-rw-r--r-- | lib/sqlalchemy/orm/context.py | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/lib/sqlalchemy/orm/context.py b/lib/sqlalchemy/orm/context.py index 3a0cce609..09163d4e9 100644 --- a/lib/sqlalchemy/orm/context.py +++ b/lib/sqlalchemy/orm/context.py @@ -4,7 +4,6 @@ # # This module is part of SQLAlchemy and is released under # the MIT License: http://www.opensource.org/licenses/mit-license.php - from . import attributes from . import interfaces from . import loading @@ -664,10 +663,13 @@ class ORMSelectCompileState(ORMCompileState, SelectState): self._aliased_generations = {} self._polymorphic_adapters = {} + compile_options = cls.default_compile_options.safe_merge( + query.compile_options + ) # legacy: only for query.with_polymorphic() - if query.compile_options._with_polymorphic_adapt_map: + if compile_options._with_polymorphic_adapt_map: self._with_polymorphic_adapt_map = dict( - query.compile_options._with_polymorphic_adapt_map + compile_options._with_polymorphic_adapt_map ) self._setup_with_polymorphics() @@ -1065,6 +1067,10 @@ class ORMSelectCompileState(ORMCompileState, SelectState): # maybe? self._reset_joinpoint() + right = inspect(right) + if onclause is not None: + onclause = inspect(onclause) + if onclause is None and isinstance( right, interfaces.PropComparator ): @@ -1084,23 +1090,23 @@ class ORMSelectCompileState(ORMCompileState, SelectState): onclause = right right = None elif "parententity" in right._annotations: - right = right._annotations["parententity"].entity + right = right._annotations["parententity"] if onclause is None: - r_info = inspect(right) - if not r_info.is_selectable and not hasattr(r_info, "mapper"): + if not right.is_selectable and not hasattr(right, "mapper"): raise sa_exc.ArgumentError( "Expected mapped entity or " "selectable/table as join target" ) - if isinstance(onclause, interfaces.PropComparator): - of_type = getattr(onclause, "_of_type", None) - else: - of_type = None + + of_type = None if isinstance(onclause, interfaces.PropComparator): # descriptor/property given (or determined); this tells us # explicitly what the expected "left" side of the join is. + + of_type = getattr(onclause, "_of_type", None) + if right is None: if of_type: right = of_type @@ -1164,6 +1170,14 @@ class ORMSelectCompileState(ORMCompileState, SelectState): full = flags["full"] aliased_generation = flags["aliased_generation"] + # do a quick inspect to accommodate for a lambda + if right is not None and not isinstance(right, util.string_types): + right = inspect(right) + if onclause is not None and not isinstance( + onclause, util.string_types + ): + onclause = inspect(onclause) + # legacy vvvvvvvvvvvvvvvvvvvvvvvvvv if not from_joinpoint: self._reset_joinpoint() @@ -1190,11 +1204,10 @@ class ORMSelectCompileState(ORMCompileState, SelectState): onclause = right right = None elif "parententity" in right._annotations: - right = right._annotations["parententity"].entity + right = right._annotations["parententity"] if onclause is None: - r_info = inspect(right) - if not r_info.is_selectable and not hasattr(r_info, "mapper"): + if not right.is_selectable and not hasattr(right, "mapper"): raise sa_exc.ArgumentError( "Expected mapped entity or " "selectable/table as join target" @@ -1379,7 +1392,7 @@ class ORMSelectCompileState(ORMCompileState, SelectState): self.from_clauses = self.from_clauses + [ orm_join( - left_clause, right, onclause, isouter=outerjoin, full=full + left_clause, r_info, onclause, isouter=outerjoin, full=full ) ] @@ -1964,6 +1977,13 @@ class _QueryEntity(object): @classmethod def to_compile_state(cls, compile_state, entities): for entity in entities: + if entity._is_lambda_element: + if entity._is_sequence: + cls.to_compile_state(compile_state, entity._resolved) + continue + else: + entity = entity._resolved + if entity.is_clause_element: if entity.is_selectable: if "parententity" in entity._annotations: |