summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql/expression.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2009-03-21 16:12:37 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2009-03-21 16:12:37 +0000
commit0983b610b47d2cbe502837ded365a2d2dbcdc883 (patch)
treea43a8311554a871cefa6f691dc5a3b62f463997b /lib/sqlalchemy/sql/expression.py
parent3ecf84f5adb428f814cd18b47ac65d133112cbf0 (diff)
downloadsqlalchemy-0983b610b47d2cbe502837ded365a2d2dbcdc883.tar.gz
- An alias() of a select() will convert to a "scalar subquery"
when used in an unambiguously scalar context, i.e. it's used in a comparison operation. This applies to the ORM when using query.subquery() as well.
Diffstat (limited to 'lib/sqlalchemy/sql/expression.py')
-rw-r--r--lib/sqlalchemy/sql/expression.py24
1 files changed, 22 insertions, 2 deletions
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py
index 903b3052d..f94e849e5 100644
--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -1527,7 +1527,7 @@ class _CompareMixin(ColumnOperators):
return other.__clause_element__()
elif not isinstance(other, ClauseElement):
return self._bind_param(other)
- elif isinstance(other, _SelectBaseMixin):
+ elif isinstance(other, (_SelectBaseMixin, Alias)):
return other.as_scalar()
else:
return other
@@ -1769,7 +1769,21 @@ class FromClause(Selectable):
return Join(self, right, onclause, True)
def alias(self, name=None):
- """return an alias of this ``FromClause`` against another ``FromClause``."""
+ """return an alias of this ``FromClause``.
+
+ For table objects, this has the effect of the table being rendered
+ as ``tablename AS aliasname`` in a SELECT statement.
+ For select objects, the effect is that of creating a named
+ subquery, i.e. ``(select ...) AS aliasname``.
+ The ``alias()`` method is the general way to create
+ a "subquery" out of an existing SELECT.
+
+ The ``name`` parameter is optional, and if left blank an
+ "anonymous" name will be generated at compile time, guaranteed
+ to be unique against other anonymous constructs used in the
+ same statement.
+
+ """
return Alias(self, name)
@@ -2600,6 +2614,12 @@ class Alias(FromClause):
def description(self):
return self.name.encode('ascii', 'backslashreplace')
+ def as_scalar(self):
+ try:
+ return self.element.as_scalar()
+ except AttributeError:
+ raise AttributeError("Element %s does not support 'as_scalar()'" % self.element)
+
def is_derived_from(self, fromclause):
if fromclause in self._cloned_set:
return True