diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-02-09 20:43:28 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-02-09 20:43:28 -0500 |
commit | af4239874242000f0dd38252ef0d35550d7bd21a (patch) | |
tree | baf257145a2a9aa41e843764e68a3a15b9f3b003 | |
parent | 0843bbcfbb4ab57253e8de424b7b1b7538ba0781 (diff) | |
download | sqlalchemy-af4239874242000f0dd38252ef0d35550d7bd21a.tar.gz |
- The MySQL dialect now supports CAST on types that are constructed
as :class:`.TypeDecorator` objects.
-rw-r--r-- | doc/build/changelog/changelog_10.rst | 6 | ||||
-rw-r--r-- | lib/sqlalchemy/dialects/mysql/base.py | 9 | ||||
-rw-r--r-- | test/dialect/mysql/test_compiler.py | 12 |
3 files changed, 23 insertions, 4 deletions
diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst index 1356c8eba..85681fbba 100644 --- a/doc/build/changelog/changelog_10.rst +++ b/doc/build/changelog/changelog_10.rst @@ -25,6 +25,12 @@ .. 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 A warning is emitted when :func:`.cast` is used with the MySQL diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 0a999a85f..0d2c36189 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 23341b9d2..304c31012 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"), |