summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/selectable.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2019-05-18 20:35:01 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2019-05-27 21:19:06 -0400
commit47552aa93bedcce3756dc89774f02db9f1868e68 (patch)
tree4a00be1f85b5964e8cf6d035ed94345eba0459e4 /lib/sqlalchemy/sql/selectable.py
parent8b2eb2a2d0f4c7e283d96cf3e5263b5dd48e3b14 (diff)
downloadsqlalchemy-47552aa93bedcce3756dc89774f02db9f1868e68.tar.gz
Use roles for ORM alias() conversion
as SELECT statements will have subquery() and not alias(), start getting ready for the places where the ORM coerces SELECTs into subqueries and be ready to warn about it Change-Id: I90d4b6cae2c72816c6b192016ce074589caf4731
Diffstat (limited to 'lib/sqlalchemy/sql/selectable.py')
-rw-r--r--lib/sqlalchemy/sql/selectable.py42
1 files changed, 39 insertions, 3 deletions
diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py
index 41be9fc5a..b0d6002b7 100644
--- a/lib/sqlalchemy/sql/selectable.py
+++ b/lib/sqlalchemy/sql/selectable.py
@@ -662,7 +662,7 @@ class FromClause(roles.FromClauseRole, Selectable):
return None
-class Join(FromClause):
+class Join(roles.AnonymizedFromClauseRole, FromClause):
"""represent a ``JOIN`` construct between two :class:`.FromClause`
elements.
@@ -1178,7 +1178,7 @@ class Join(FromClause):
)
-class Alias(FromClause):
+class Alias(roles.AnonymizedFromClauseRole, FromClause):
"""Represents an table or selectable alias (AS).
Represents an alias, as typically applied to any table or
@@ -1772,7 +1772,7 @@ class FromGrouping(FromClause):
self.element = state["element"]
-class TableClause(Immutable, FromClause):
+class TableClause(roles.AnonymizedFromClauseRole, Immutable, FromClause):
"""Represents a minimal "table" construct.
This is a lightweight table object that has only a name and a
@@ -2116,6 +2116,42 @@ class SelectBase(
def _from_objects(self):
return [self]
+ def subquery(self, name=None):
+ """Return a subquery of this :class:`.SelectBase`.
+
+ A subquery is from a SQL perspective a parentheized, named construct
+ that can be placed in the FROM clause of another SELECT statement.
+
+ Given a SELECT statement such as::
+
+ stmt = select([table.c.id, table.c.name])
+
+ The above statement might look like::
+
+ SELECT table.id, table.name FROM table
+
+ The subquery form by itself renders the same way, however when
+ embedded into the FROM clause of another SELECT statement, it becomes
+ a named sub-element::
+
+ subq = stmt.subquery()
+ new_stmt = select([subq])
+
+ The above renders as::
+
+ SELECT anon_1.id, anon_1.name
+ FROM (SELECT table.id, table.name FROM table) AS anon_1
+
+ Historically, :meth:`.SelectBase.subquery` is equivalent to calling
+ the :meth:`.FromClause.alias` method on a FROM object; however,
+ as a :class:`.SelectBase` object is not directly FROM object,
+ the :meth:`.SelectBase.subquery` method provides clearer semantics.
+
+ .. versionadded:: 1.4
+
+ """
+ return self.alias()
+
class GenerativeSelect(SelectBase):
"""Base class for SELECT statements where additional elements can be