From: Mike Bayer Date: Mon, 14 Jul 2014 23:02:20 +0000 (-0400) Subject: - Added statement encoding to the "SET IDENTITY_INSERT" X-Git-Tag: rel_0_8_7~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=74d38623ff52404efdc13ae8fcb3f6a5b7d08ca8;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Added statement encoding to the "SET IDENTITY_INSERT" statements which operate when an explicit INSERT is being interjected into an IDENTITY column, to support non-ascii table identifiers on drivers such as pyodbc + unix + py2k that don't support unicode statements. ref #3091 as this fix is also in that issue's patch, but is a different issue. --- diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 2d5ea0cbff..2352d33cb1 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -11,6 +11,16 @@ .. changelog:: :version: 0.8.7 + .. change:: + :tags: bug, mssql + :versions: 1.0.0, 0.9.7 + + Added statement encoding to the "SET IDENTITY_INSERT" + statements which operate when an explicit INSERT is being + interjected into an IDENTITY column, to support non-ascii table + identifiers on drivers such as pyodbc + unix + py2k that don't + support unicode statements. + .. change:: :tags: bug, mssql :versions: 1.0.0, 0.9.7 diff --git a/lib/sqlalchemy/connectors/pyodbc.py b/lib/sqlalchemy/connectors/pyodbc.py index 6ca778007e..c387a602bb 100644 --- a/lib/sqlalchemy/connectors/pyodbc.py +++ b/lib/sqlalchemy/connectors/pyodbc.py @@ -137,6 +137,7 @@ class PyODBCConnector(Connector): # run other initialization which asks for user name, etc. super(PyODBCConnector, self).initialize(connection) + def _dbapi_version(self): if not self.dbapi: return () diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index de61b8f1a3..1ba141d6f1 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -596,6 +596,12 @@ class MSExecutionContext(default.DefaultExecutionContext): _result_proxy = None _lastrowid = None + def _opt_encode(self, statement): + if not self.dialect.supports_unicode_statements: + return self.dialect._encoder(statement)[0] + else: + return statement + def pre_exec(self): """Activate IDENTITY_INSERT if needed.""" @@ -617,8 +623,8 @@ class MSExecutionContext(default.DefaultExecutionContext): if self._enable_identity_insert: self.root_connection._cursor_execute(self.cursor, - "SET IDENTITY_INSERT %s ON" % - self.dialect.identifier_preparer.format_table(tbl), + self._opt_encode("SET IDENTITY_INSERT %s ON" % + self.dialect.identifier_preparer.format_table(tbl)), (), self) def post_exec(self): @@ -642,9 +648,9 @@ class MSExecutionContext(default.DefaultExecutionContext): if self._enable_identity_insert: conn._cursor_execute(self.cursor, - "SET IDENTITY_INSERT %s OFF" % + self._opt_encode("SET IDENTITY_INSERT %s OFF" % self.dialect.identifier_preparer. - format_table(self.compiled.statement.table), + format_table(self.compiled.statement.table)), (), self) def get_lastrowid(self): @@ -654,9 +660,9 @@ class MSExecutionContext(default.DefaultExecutionContext): if self._enable_identity_insert: try: self.cursor.execute( - "SET IDENTITY_INSERT %s OFF" % + self._opt_encode("SET IDENTITY_INSERT %s OFF" % self.dialect.identifier_preparer.\ - format_table(self.compiled.statement.table) + format_table(self.compiled.statement.table)) ) except: pass