]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Added statement encoding to the "SET IDENTITY_INSERT"
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 14 Jul 2014 23:02:20 +0000 (19:02 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 14 Jul 2014 23:02:20 +0000 (19:02 -0400)
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.

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/connectors/pyodbc.py
lib/sqlalchemy/dialects/mssql/base.py

index 15f38fc457a1fc6c9eddffcb5b78696c1247a6aa..1c9e394691e74b30c49e70fd8f12a39b3917c0e8 100644 (file)
 .. 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
index bc8a2f0b89fbeef0d07e47f8d29ef05ff29c177c..ef72c80496837b5cb8a7b411b025660e8705e465 100644 (file)
@@ -142,6 +142,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 ()
index 547df82593301606306d6dc168d1dbd971c9743a..eebd35405b9575bb627fd754fcde79fa392fd9e0 100644 (file)
@@ -746,6 +746,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."""
 
@@ -767,8 +773,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):
@@ -792,9 +798,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):
@@ -804,9 +810,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