]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- The MySQL dialect now supports CAST on types that are constructed
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 10 Feb 2015 01:43:28 +0000 (20:43 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 10 Feb 2015 01:43:28 +0000 (20:43 -0500)
as :class:`.TypeDecorator` objects.

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/dialects/mysql/base.py
test/dialect/mysql/test_compiler.py

index 1356c8eba6352562007ab36d6ec6459be1732ea9..85681fbbada8b7f2cac90cf7236dd4d43bc5740d 100644 (file)
     series as well.  For changes that are specific to 1.0 with an emphasis
     on compatibility concerns, see :doc:`/changelog/migration_10`.
 
+    .. change::
+        :tags: bug, mysql
+
+        The MySQL dialect now supports CAST on types that are constructed
+        as :class:`.TypeDecorator` objects.
+
     .. change::
         :tags: bug, mysql
         :tickets: 3237
index 0a999a85fa113f91573e11dbdd25a4b8a3c4f6a1..0d2c361897cbab2de42e1344681d12024925f93d 100644 (file)
@@ -1683,9 +1683,12 @@ class MySQLCompiler(compiler.SQLCompiler):
     def get_from_hint_text(self, table, text):
         return text
 
-    def visit_typeclause(self, typeclause):
-        type_ = typeclause.type.dialect_impl(self.dialect)
-        if isinstance(type_, sqltypes.Integer):
+    def visit_typeclause(self, typeclause, type_=None):
+        if type_ is None:
+            type_ = typeclause.type.dialect_impl(self.dialect)
+        if isinstance(type_, sqltypes.TypeDecorator):
+            return self.visit_typeclause(typeclause, type_.impl)
+        elif isinstance(type_, sqltypes.Integer):
             if getattr(type_, 'unsigned', False):
                 return 'UNSIGNED INTEGER'
             else:
index 23341b9d28daab1dfd81a5b33fd72215178a8445..304c310124cd2ce600d0ed566105b52442f48e88 100644 (file)
@@ -392,11 +392,22 @@ class SQLTest(fixtures.TestBase, AssertsCompiledSQL):
             (m.MSVarBinary, "CAST(t.col AS BINARY)"),
             (m.MSVarBinary(32), "CAST(t.col AS BINARY)"),
 
+            (Interval, "CAST(t.col AS DATETIME)"),
+
         ]
 
         for type_, expected in specs:
             self.assert_compile(cast(t.c.col, type_), expected)
 
+    def test_cast_type_decorator(self):
+        class MyInteger(sqltypes.TypeDecorator):
+            impl = Integer
+
+        type_ = MyInteger()
+        t = sql.table('t', sql.column('col'))
+        self.assert_compile(
+            cast(t.c.col, type_), "CAST(t.col AS SIGNED INTEGER)")
+
     def test_unsupported_casts(self):
 
         t = sql.table('t', sql.column('col'))
@@ -413,7 +424,6 @@ class SQLTest(fixtures.TestBase, AssertsCompiledSQL):
 
             (m.MSYear, "t.col"),
             (m.MSYear(2), "t.col"),
-            (Interval, "t.col"),
 
             (Boolean, "t.col"),
             (BOOLEAN, "t.col"),