From: Mike Bayer Date: Thu, 6 Jun 2013 16:52:18 +0000 (-0400) Subject: When querying the information schema on SQL Server 2000, removed X-Git-Tag: rel_0_8_2~57 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=45f8ff88c9209374586062d536735e484014d665;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git When querying the information schema on SQL Server 2000, removed a CAST call that was added in 0.8.1 to help with driver issues, which apparently is not compatible on 2000. The CAST remains in place for SQL Server 2005 and greater. [ticket:2747] Conflicts: doc/build/changelog/changelog_09.rst lib/sqlalchemy/dialects/mssql/information_schema.py --- diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 82e65272b7..e3b14bc28e 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -6,6 +6,15 @@ .. changelog:: :version: 0.8.2 + .. change:: + :tags: bug, mssql + :tickets: 2747 + + When querying the information schema on SQL Server 2000, removed + a CAST call that was added in 0.8.1 to help with driver issues, + which apparently is not compatible on 2000. + The CAST remains in place for SQL Server 2005 and greater. + .. change:: :tags: bug, mysql :tickets: 2721 diff --git a/lib/sqlalchemy/dialects/mssql/information_schema.py b/lib/sqlalchemy/dialects/mssql/information_schema.py index 80e59d323e..030074fbe9 100644 --- a/lib/sqlalchemy/dialects/mssql/information_schema.py +++ b/lib/sqlalchemy/dialects/mssql/information_schema.py @@ -9,10 +9,12 @@ from ... import Table, MetaData, Column from ...types import String, Unicode, Integer, TypeDecorator from ... import cast +from ... import util +from ...sql import expression +from ...ext.compiler import compiles ischema = MetaData() - class CoerceUnicode(TypeDecorator): impl = Unicode @@ -24,7 +26,19 @@ class CoerceUnicode(TypeDecorator): return value def bind_expression(self, bindvalue): - return cast(bindvalue, Unicode) + return _cast_on_2005(bindvalue) + +class _cast_on_2005(expression.ColumnElement): + def __init__(self, bindvalue): + self.bindvalue = bindvalue + +@compiles(_cast_on_2005) +def _compile(element, compiler, **kw): + from . import base + if compiler.dialect.server_version_info < base.MS_2005_VERSION: + return compiler.process(element.bindvalue, **kw) + else: + return compiler.process(cast(element.bindvalue, Unicode), **kw) schemata = Table("SCHEMATA", ischema, Column("CATALOG_NAME", CoerceUnicode, key="catalog_name"), diff --git a/test/dialect/test_mssql.py b/test/dialect/test_mssql.py index 7b8f533908..28dcd1dd43 100644 --- a/test/dialect/test_mssql.py +++ b/test/dialect/test_mssql.py @@ -2078,14 +2078,36 @@ class BinaryTest(fixtures.TestBase, AssertsExecutionResults): fp.close() return stream -class InfoCoerceUnicodeTest(fixtures.TestBase): +from sqlalchemy.dialects.mssql.information_schema import CoerceUnicode, tables +from sqlalchemy.dialects.mssql import base + +class InfoCoerceUnicodeTest(fixtures.TestBase, AssertsCompiledSQL): def test_info_unicode_coercion(self): - from sqlalchemy.dialects.mssql.information_schema import CoerceUnicode dialect = mssql.dialect() value = CoerceUnicode().bind_processor(dialect)('a string') assert isinstance(value, unicode) + def test_info_unicode_cast_no_2000(self): + dialect = mssql.dialect() + dialect.server_version_info = base.MS_2000_VERSION + stmt = tables.c.table_name == 'somename' + self.assert_compile( + stmt, + "[TABLES_1].[TABLE_NAME] = :TABLE_NAME_1", + dialect=dialect + ) + + def test_info_unicode_cast(self): + dialect = mssql.dialect() + dialect.server_version_info = base.MS_2005_VERSION + stmt = tables.c.table_name == 'somename' + self.assert_compile( + stmt, + "[TABLES_1].[TABLE_NAME] = CAST(:TABLE_NAME_1 AS NVARCHAR(max))", + dialect=dialect + ) + class ReflectHugeViewTest(fixtures.TestBase): __only_on__ = 'mssql'