summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm/context.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2021-07-23 16:07:50 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2021-08-07 14:47:29 -0400
commitd49eef15bfb759fb33d7d23988cc5a385d9e8a40 (patch)
tree8dc301d21600f147aa1647ef1c74e718b8484925 /lib/sqlalchemy/orm/context.py
parent8822d832679cdc13bb631dd221d0f5932f6540c7 (diff)
downloadsqlalchemy-d49eef15bfb759fb33d7d23988cc5a385d9e8a40.tar.gz
add columns_clause_froms and related use cases
Added new attribute :attr:`_sql.Select.columns_clause_froms` that will retrieve the FROM list implied by the columns clause of the :class:`_sql.Select` statement. This differs from the old :attr:`_sql.Select.froms` collection in that it does not perform any ORM compilation steps, which necessarily deannotate the FROM elements and do things like compute joinedloads etc., which makes it not an appropriate candidate for the :meth:`_sql.Select.select_from` method. Additionally adds a new parameter :paramref:`_sql.Select.with_only_columns.maintain_column_froms` that transfers this collection to :meth:`_sql.Select.select_from` before replacing the columns collection. In addition, the :attr:`_sql.Select.froms` is renamed to :meth:`_sql.Select.get_final_froms`, to stress that this collection is not a simple accessor and is instead calculated given the full state of the object, which can be an expensive call when used in an ORM context. Additionally fixes a regression involving the :func:`_orm.with_only_columns` function to support applying criteria to column elements that were replaced with either :meth:`_sql.Select.with_only_columns` or :meth:`_orm.Query.with_entities` , which had broken as part of :ticket:`6503` released in 1.4.19. Fixes: #6808 Change-Id: Ib5d66cce488bbaca06dab4f68fb5cdaa73e8823e
Diffstat (limited to 'lib/sqlalchemy/orm/context.py')
-rw-r--r--lib/sqlalchemy/orm/context.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/sqlalchemy/orm/context.py b/lib/sqlalchemy/orm/context.py
index c4b695687..603477819 100644
--- a/lib/sqlalchemy/orm/context.py
+++ b/lib/sqlalchemy/orm/context.py
@@ -4,6 +4,8 @@
#
# This module is part of SQLAlchemy and is released under
# the MIT License: https://www.opensource.org/licenses/mit-license.php
+import itertools
+
from . import attributes
from . import interfaces
from . import loading
@@ -873,6 +875,19 @@ class ORMSelectCompileState(ORMCompileState, SelectState):
yield elem
@classmethod
+ def get_columns_clause_froms(cls, statement):
+ return cls._normalize_froms(
+ itertools.chain.from_iterable(
+ element._from_objects
+ if "parententity" not in element._annotations
+ else [
+ element._annotations["parententity"].__clause_element__()
+ ]
+ for element in statement._raw_columns
+ )
+ )
+
+ @classmethod
@util.preload_module("sqlalchemy.orm.query")
def from_statement(cls, statement, from_statement):
query = util.preloaded.orm_query