"""
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
@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):
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`.
expr = func.extract("year", datetime.date(2010, 12, 5))
self.assert_compile(expr, "EXTRACT(year FROM :param_1)")
+ def test_select_method_one(self):
+ expr = func.rows("foo")
+ self.assert_compile(
+ expr.select(),
+ "SELECT rows(:rows_2) AS rows_1"
+ )
+
+ def test_alias_method_one(self):
+ expr = func.rows("foo")
+ self.assert_compile(
+ expr.alias(),
+ "rows(:rows_1)"
+ )
+
+ def test_select_method_two(self):
+ expr = func.rows("foo")
+ self.assert_compile(
+ select(['*']).select_from(expr.select()),
+ "SELECT * FROM (SELECT rows(:rows_2) AS rows_1)"
+ )
+
+ def test_select_method_three(self):
+ expr = func.rows("foo")
+ self.assert_compile(
+ select(['foo']).select_from(expr),
+ "SELECT foo FROM rows(:rows_1)"
+ )
+
+ def test_alias_method_two(self):
+ expr = func.rows("foo")
+ self.assert_compile(
+ select(['*']).select_from(expr.alias('bar')),
+ "SELECT * FROM rows(:rows_1) AS bar"
+ )
+
+ def test_alias_method_columns(self):
+ expr = func.rows("foo").alias('bar')
+
+ # this isn't very useful but is the old behavior
+ # prior to #2974.
+ # testing here that the expression exports its column
+ # list in a way that at least doesn't break.
+ self.assert_compile(
+ select([expr]),
+ "SELECT bar.rows_1 FROM rows(:rows_2) AS bar"
+ )
+
+ def test_alias_method_columns_two(self):
+ expr = func.rows("foo").alias('bar')
+ assert len(expr.c)
+
class ExecuteTest(fixtures.TestBase):