]> 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:03:59 +0000 (19:03 -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 2d5ea0cbff8c96c1b5077e0395757baccb93428e..2352d33cb15864fc1daca9709594e064e39012bf 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 6ca778007e59600493082d3444703da526de2ece..c387a602bbb75164d51aa36f2db22943fd0a70fa 100644 (file)
@@ -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 ()
index de61b8f1a3b5eb1ac41ef3c474406abf1afe727e..1ba141d6f1687df6844f7bcc576de8a85be0d3fc 100644 (file)
@@ -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