]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
add ClauseElement.cast() shortcut-method
authorSebastian Bank <sebastian.bank@uni-leipzig.de>
Fri, 19 Jun 2015 15:37:21 +0000 (17:37 +0200)
committerSebastian Bank <sebastian.bank@uni-leipzig.de>
Fri, 19 Jun 2015 15:37:21 +0000 (17:37 +0200)
lib/sqlalchemy/sql/elements.py
test/sql/test_type_expressions.py

index 27ecce2b0b39bd44297463180586057890367b94..5df736ac7ccf31318ab0fb968df23d2e5059e270 100644 (file)
@@ -837,6 +837,13 @@ class ColumnElement(operators.ColumnOperators, ClauseElement):
         else:
             return False
 
+    def cast(self, type_):
+        """Produce a type cast, i.e. ``CAST(<expression> AS <type>)``.
+
+        This is a shortcut to the :func:`.cast` function.
+        """
+        return Cast(self, type_)
+
     def label(self, name):
         """Produce a column label, i.e. ``<columnname> AS <name>``.
 
index 574edfe9e849dd407b00b2daa03368719e3794ae..c35e9ff538d549359beb0dcf5a52cd458eef0d4f 100644 (file)
@@ -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()