From: Mike Bayer Date: Tue, 10 Feb 2015 01:43:28 +0000 (-0500) Subject: - The MySQL dialect now supports CAST on types that are constructed X-Git-Tag: rel_1_0_0b1~62 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=af4239874242000f0dd38252ef0d35550d7bd21a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - The MySQL dialect now supports CAST on types that are constructed as :class:`.TypeDecorator` objects. --- diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst index 1356c8eba6..85681fbbad 100644 --- a/doc/build/changelog/changelog_10.rst +++ b/doc/build/changelog/changelog_10.rst @@ -23,6 +23,12 @@ 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 diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 0a999a85fa..0d2c361897 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -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: diff --git a/test/dialect/mysql/test_compiler.py b/test/dialect/mysql/test_compiler.py index 23341b9d28..304c310124 100644 --- a/test/dialect/mysql/test_compiler.py +++ b/test/dialect/mysql/test_compiler.py @@ -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"),