diff options
author | Angus Lees <gus@inodes.org> | 2014-07-04 18:06:34 +1000 |
---|---|---|
committer | Angus Lees <gus@inodes.org> | 2014-07-10 11:25:44 +1000 |
commit | 2081502bfdf7414298d0b2c59cc74852536ce78f (patch) | |
tree | 73bd6a1dcbc4089d7a80150b1cd0ba95a4cad5e7 | |
parent | e0a9b94abb92c6b62d6a6f70dec680d7ca35eed6 (diff) | |
download | sqlalchemy-pr/102.tar.gz |
Tell mysql that Unicode columns are unicodepr/102
Without this patch, Unicode columns get no special charset by default,
and fall back to whatever the table, database, server configuration
and (eventually) server hardcoded default encoding is. The hardcoded
default mysql encoding is latin1.
This change maps Unicode to NVARCHAR (for consistency with other
sqlalchemy dialects), and UnicodeText to 'TEXT UNICODE'.
-rw-r--r-- | lib/sqlalchemy/dialects/mysql/base.py | 9 | ||||
-rw-r--r-- | test/dialect/mysql/test_types.py | 11 |
2 files changed, 15 insertions, 5 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 77fb34fd8..4fac5fb18 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -1991,6 +1991,13 @@ class MySQLTypeCompiler(compiler.GenericTypeCompiler): "VARCHAR requires a length on dialect %s" % self.dialect.name) + def visit_unicode(self, type_): + return self.visit_NVARCHAR(type_) + + def visit_unicode_text(self, type_): + spec = "TEXT(%d)" % type_.length if type_.length else "TEXT" + return self._extend_string(type_, {"unicode": True}, spec) + def visit_CHAR(self, type_): if type_.length: return self._extend_string(type_, {}, "CHAR(%(length)s)" % @@ -2591,8 +2598,6 @@ class MySQLDialect(default.DefaultDialect): raise exc.NoSuchTableError(full_name) return row[1].strip() - return sql - def _describe_table(self, connection, table, charset=None, full_name=None): """Run DESCRIBE for a ``Table`` and return processed rows.""" diff --git a/test/dialect/mysql/test_types.py b/test/dialect/mysql/test_types.py index b7d261a88..587218266 100644 --- a/test/dialect/mysql/test_types.py +++ b/test/dialect/mysql/test_types.py @@ -238,7 +238,13 @@ class TypesTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL): (mysql.ENUM, ["foo", "bar"], {'unicode':True}, '''ENUM('foo','bar') UNICODE'''), - (String, [20], {"collation": "utf8"}, 'VARCHAR(20) COLLATE utf8') + (String, [20], {"collation": "utf8"}, 'VARCHAR(20) COLLATE utf8'), + + (Unicode, [20], {}, 'NATIONAL VARCHAR(20)'), + + (UnicodeText, [], {}, 'TEXT UNICODE'), + + (UnicodeText, [20], {}, 'TEXT(20) UNICODE'), ] @@ -252,7 +258,7 @@ class TypesTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL): # test that repr() copies out all arguments self.assert_compile( eval("mysql.%r" % type_inst) - if type_ is not String + if type_ not in (String, Unicode, UnicodeText) else eval("%r" % type_inst), res ) @@ -820,4 +826,3 @@ class EnumSetTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL def colspec(c): return testing.db.dialect.ddl_compiler( testing.db.dialect, None).get_column_specification(c) - |