diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-07-24 14:33:50 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-07-24 14:33:50 -0400 |
commit | 0df977ccad63831375d8a7bc6383385c331d9742 (patch) | |
tree | 18d5f67dd70a7fb8090e230b7f86e2c3eece7c26 /lib/sqlalchemy/sql/functions.py | |
parent | 07c01b77f16a08cecdad2c80c1283bed46a7f815 (diff) | |
download | sqlalchemy-0df977ccad63831375d8a7bc6383385c331d9742.tar.gz |
- Added a supported :meth:`.FunctionElement.alias` method to functions,
e.g. the ``func`` construct. Previously, behavior for this method
was undefined. The current behavior mimics that of pre-0.9.4,
which is that the function is turned into a single-column FROM
clause with the given alias name, where the column itself is
anonymously named.
fixes #3137
Diffstat (limited to 'lib/sqlalchemy/sql/functions.py')
-rw-r--r-- | lib/sqlalchemy/sql/functions.py | 52 |
1 files changed, 47 insertions, 5 deletions
diff --git a/lib/sqlalchemy/sql/functions.py b/lib/sqlalchemy/sql/functions.py index 11e758364..7efb1e916 100644 --- a/lib/sqlalchemy/sql/functions.py +++ b/lib/sqlalchemy/sql/functions.py @@ -9,11 +9,11 @@ """ from . import sqltypes, schema -from .base import Executable +from .base import Executable, ColumnCollection from .elements import ClauseList, Cast, Extract, _literal_as_binds, \ literal_column, _type_from_args, ColumnElement, _clone,\ Over, BindParameter -from .selectable import FromClause, Select +from .selectable import FromClause, Select, Alias from . import operators from .visitors import VisitableType @@ -67,12 +67,24 @@ class FunctionElement(Executable, ColumnElement, FromClause): @property def columns(self): - """Fulfill the 'columns' contract of :class:`.ColumnElement`. + """The set of columns exported by this :class:`.FunctionElement`. + + Function objects currently have no result column names built in; + this method returns a single-element column collection with + an anonymously named column. + + An interim approach to providing named columns for a function + as a FROM clause is to build a :func:`.select` with the + desired columns:: + + from sqlalchemy.sql import column + + stmt = select([column('x'), column('y')]).\ + select_from(func.myfunction()) - Returns a single-element list consisting of this object. """ - return [self] + return ColumnCollection(self.label(None)) @util.memoized_property def clauses(self): @@ -116,6 +128,36 @@ class FunctionElement(Executable, ColumnElement, FromClause): self._reset_exported() FunctionElement.clauses._reset(self) + def alias(self, name=None, flat=False): + """Produce a :class:`.Alias` construct against this + :class:`.FunctionElement`. + + This construct wraps the function in a named alias which + is suitable for the FROM clause. + + e.g.:: + + from sqlalchemy.sql import column + + stmt = select([column('data')]).select_from( + func.unnest(Table.data).alias('data_view') + ) + + Would produce: + + .. sourcecode:: sql + + SELECT data + FROM unnest(sometable.data) AS data_view + + .. versionadded:: 0.9.8 The :meth:`.FunctionElement.alias` method + is now supported. Previously, this method's behavior was + undefined and did not behave consistently across versions. + + """ + + return Alias(self, name) + def select(self): """Produce a :func:`~.expression.select` construct against this :class:`.FunctionElement`. |