From 6c2e82ff8ce42d22a974ccd8890f96f671680faa Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 11 Mar 2010 21:52:54 -0500 Subject: [PATCH] add a third state to converts_unicode_strings - "conditional". at the moment this will have us do a check. i.e. for MSSQL where NVARCHAR is unicode and VARCHAR is not. --- lib/sqlalchemy/engine/default.py | 20 ++++++++++++++++++-- lib/sqlalchemy/types.py | 5 +++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/sqlalchemy/engine/default.py b/lib/sqlalchemy/engine/default.py index cd2c103938..454d2a593e 100644 --- a/lib/sqlalchemy/engine/default.py +++ b/lib/sqlalchemy/engine/default.py @@ -148,9 +148,25 @@ class DefaultDialect(base.Dialect): ) row = cursor.fetchone() - result = isinstance(row[0], unicode) + unicode_for_varchar = isinstance(row[0], unicode) + + cursor.execute( + str( + expression.select( + [expression.cast( + expression.literal_column("'test unicode returns'"),sqltypes.VARCHAR(60)) + ]).compile(dialect=self) + ) + ) + + row = cursor.fetchone() + unicode_for_unicode = isinstance(row[0], unicode) cursor.close() - return result + + if unicode_for_unicode and not unicode_for_varchar: + return "conditional" + else: + return unicode_for_varchar def type_descriptor(self, typeobj): """Provide a database-specific ``TypeEngine`` object, given diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index bf06ec99b7..3b7027e23c 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -688,7 +688,7 @@ class String(Concatenable, TypeEngine): def result_processor(self, dialect, coltype): wants_unicode = self.convert_unicode or dialect.convert_unicode needs_convert = wants_unicode and \ - (not dialect.returns_unicode_strings or + (dialect.returns_unicode_strings is not True or self.convert_unicode == 'force') if needs_convert: @@ -697,7 +697,8 @@ class String(Concatenable, TypeEngine): if dialect.returns_unicode_strings: # we wouldn't be here unless convert_unicode='force' - # was specified. since we will be getting back unicode + # was specified, or the driver has erratic unicode-returning + # habits. since we will be getting back unicode # in most cases, we check for it (decode will fail). def process(value): if isinstance(value, unicode): -- 2.47.3