From: Sebastian Bank Date: Fri, 19 Jun 2015 15:37:21 +0000 (+0200) Subject: add ClauseElement.cast() shortcut-method X-Git-Tag: rel_1_0_7~2^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=84e813aee4b47edb5220bc76083525d1ac4e45ba;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git add ClauseElement.cast() shortcut-method --- diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 27ecce2b0b..5df736ac7c 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -837,6 +837,13 @@ class ColumnElement(operators.ColumnOperators, ClauseElement): else: return False + def cast(self, type_): + """Produce a type cast, i.e. ``CAST( AS )``. + + This is a shortcut to the :func:`.cast` function. + """ + return Cast(self, type_) + def label(self, name): """Produce a column label, i.e. `` AS ``. diff --git a/test/sql/test_type_expressions.py b/test/sql/test_type_expressions.py index 574edfe9e8..c35e9ff538 100644 --- a/test/sql/test_type_expressions.py +++ b/test/sql/test_type_expressions.py @@ -40,6 +40,13 @@ class SelectTest(_ExprFixture, fixtures.TestBase, AssertsCompiledSQL): "SELECT CAST(test_table.y AS VARCHAR) AS anon_1 FROM test_table" ) + def test_cast_method(self): + table = self._fixture() + self.assert_compile( + select([table.c.y.cast(String)]), + "SELECT CAST(test_table.y AS VARCHAR) AS anon_1 FROM test_table" + ) + def test_select_cols_use_labels(self): table = self._fixture()