]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Change setinputsizes behavior for mssql+pyodbc
authorGord Thompson <gord@gordthompson.com>
Sat, 25 Jun 2022 16:34:51 +0000 (10:34 -0600)
committermike bayer <mike_mp@zzzcomputing.com>
Wed, 29 Jun 2022 13:02:39 +0000 (13:02 +0000)
The ``use_setinputsizes`` parameter for the ``mssql+pyodbc`` dialect now
defaults to ``True``; this is so that non-unicode string comparisons are
bound by pyodbc to pyodbc.SQL_VARCHAR rather than pyodbc.SQL_WVARCHAR,
allowing indexes against VARCHAR columns to take effect. In order for the
``fast_executemany=True`` parameter to continue functioning, the
``use_setinputsizes`` mode now skips the ``cursor.setinputsizes()`` call
specifically when ``fast_executemany`` is True and the specific method in
use is ``cursor.executemany()``, which doesn't support setinputsizes. The
change also adds appropriate pyodbc DBAPI typing to values that are typed
as :class:`_types.Unicode` or :class:`_types.UnicodeText`, as well as
altered the base :class:`_types.JSON` datatype to consider JSON string
values as :class:`_types.Unicode` rather than :class:`_types.String`.

Fixes: #8177
Change-Id: I6c8886663254ae55cf904ad256c906e8f5e11f48

doc/build/changelog/unreleased_20/8177.rst [new file with mode: 0644]
lib/sqlalchemy/connectors/pyodbc.py
lib/sqlalchemy/dialects/mssql/pyodbc.py
lib/sqlalchemy/sql/sqltypes.py
test/dialect/mssql/test_engine.py

diff --git a/doc/build/changelog/unreleased_20/8177.rst b/doc/build/changelog/unreleased_20/8177.rst
new file mode 100644 (file)
index 0000000..2d3ba45
--- /dev/null
@@ -0,0 +1,16 @@
+.. change::
+    :tags: mssql, bug
+    :tickets: 8177
+
+    The ``use_setinputsizes`` parameter for the ``mssql+pyodbc`` dialect now
+    defaults to ``True``; this is so that non-unicode string comparisons are
+    bound by pyodbc to pyodbc.SQL_VARCHAR rather than pyodbc.SQL_WVARCHAR,
+    allowing indexes against VARCHAR columns to take effect. In order for the
+    ``fast_executemany=True`` parameter to continue functioning, the
+    ``use_setinputsizes`` mode now skips the ``cursor.setinputsizes()`` call
+    specifically when ``fast_executemany`` is True and the specific method in
+    use is ``cursor.executemany()``, which doesn't support setinputsizes. The
+    change also adds appropriate pyodbc DBAPI typing to values that are typed
+    as :class:`_types.Unicode` or :class:`_types.UnicodeText`, as well as
+    altered the base :class:`_types.JSON` datatype to consider JSON string
+    values as :class:`_types.Unicode` rather than :class:`_types.String`.
index e24acda75a99f9f5c7f98828ed672b1e2f68529c..7c5e62faeb8a0c47048ce84591025a35e06c6874 100644 (file)
@@ -42,6 +42,8 @@ class PyODBCConnector(Connector):
     supports_native_decimal = True
     default_paramstyle = "named"
 
+    fast_executemany = False
+
     # for non-DSN connections, this *may* be used to
     # hold the desired driver name
     pyodbc_driver_name: Optional[str] = None
@@ -203,6 +205,13 @@ class PyODBCConnector(Connector):
         # parameter were not passed to the dialect, or if no types were
         # specified in list_of_tuples
 
+        # as of #8177 for 2.0 we assume use_setinputsizes=True and only
+        # omit the setinputsizes calls for .executemany() with
+        # fast_executemany=True
+
+        if context.executemany and self.fast_executemany:
+            return
+
         cursor.setinputsizes(
             [
                 (dbtype, None, None)
index 6d64fdc3ed3d7e35728da1b07eba8b9b451543d3..9f73ed28c1a14a1536360644f40ec9053914e47b 100644 (file)
@@ -291,12 +291,10 @@ driver in order to use this flag::
 Setinputsizes Support
 -----------------------
 
-The pyodbc ``cursor.setinputsizes()`` method can be used if necessary.  To
-enable this hook, pass ``use_setinputsizes=True`` to :func:`_sa.create_engine`::
+As of version 2.0, the pyodbc ``cursor.setinputsizes()`` method is used by
+default except for .executemany() calls when fast_executemany=True.
 
-    engine = create_engine("mssql+pyodbc://...", use_setinputsizes=True)
-
-The behavior of the hook can then be customized, as may be necessary
+The behavior of setinputsizes can be customized, as may be necessary
 particularly if fast_executemany is in use, via the
 :meth:`.DialectEvents.do_setinputsizes` hook. See that method for usage
 examples.
@@ -304,6 +302,9 @@ examples.
 .. versionchanged:: 1.4.1  The pyodbc dialects will not use setinputsizes
    unless ``use_setinputsizes=True`` is passed.
 
+.. versionchanged:: 2.0  The mssql+pyodbc dialect now defaults to using
+   setinputsizes except for .executemany() calls when fast_executemany=True.
+
 """  # noqa
 
 
@@ -313,11 +314,16 @@ import re
 import struct
 
 from .base import _MSDateTime
+from .base import _MSUnicode
+from .base import _MSUnicodeText
 from .base import BINARY
 from .base import DATETIMEOFFSET
 from .base import MSDialect
 from .base import MSExecutionContext
 from .base import VARBINARY
+from .json import JSON as _MSJson
+from .json import JSONIndexType as _MSJsonIndexType
+from .json import JSONPathType as _MSJsonPathType
 from ... import exc
 from ... import types as sqltypes
 from ... import util
@@ -466,6 +472,36 @@ class _BINARY_pyodbc(_ms_binary_pyodbc, BINARY):
     pass
 
 
+class _String_pyodbc(sqltypes.String):
+    def get_dbapi_type(self, dbapi):
+        return dbapi.SQL_VARCHAR
+
+
+class _Unicode_pyodbc(_MSUnicode):
+    def get_dbapi_type(self, dbapi):
+        return dbapi.SQL_WVARCHAR
+
+
+class _UnicodeText_pyodbc(_MSUnicodeText):
+    def get_dbapi_type(self, dbapi):
+        return dbapi.SQL_WVARCHAR
+
+
+class _JSON_pyodbc(_MSJson):
+    def get_dbapi_type(self, dbapi):
+        return dbapi.SQL_WVARCHAR
+
+
+class _JSONIndexType_pyodbc(_MSJsonIndexType):
+    def get_dbapi_type(self, dbapi):
+        return dbapi.SQL_WVARCHAR
+
+
+class _JSONPathType_pyodbc(_MSJsonPathType):
+    def get_dbapi_type(self, dbapi):
+        return dbapi.SQL_WVARCHAR
+
+
 class MSExecutionContext_pyodbc(MSExecutionContext):
     _embedded_scope_identity = False
 
@@ -541,11 +577,25 @@ class MSDialect_pyodbc(PyODBCConnector, MSDialect):
             VARBINARY: _VARBINARY_pyodbc,
             sqltypes.VARBINARY: _VARBINARY_pyodbc,
             sqltypes.LargeBinary: _VARBINARY_pyodbc,
+            sqltypes.String: _String_pyodbc,
+            sqltypes.Unicode: _Unicode_pyodbc,
+            sqltypes.UnicodeText: _UnicodeText_pyodbc,
+            sqltypes.JSON: _JSON_pyodbc,
+            sqltypes.JSON.JSONIndexType: _JSONIndexType_pyodbc,
+            sqltypes.JSON.JSONPathType: _JSONPathType_pyodbc,
+            # this excludes Enum from the string/VARCHAR thing for now
+            # it looks like Enum's adaptation doesn't really support the
+            # String type itself having a dialect-level impl
+            sqltypes.Enum: sqltypes.Enum,
         },
     )
 
-    def __init__(self, fast_executemany=False, **params):
-        super(MSDialect_pyodbc, self).__init__(**params)
+    def __init__(
+        self, fast_executemany=False, use_setinputsizes=True, **params
+    ):
+        super(MSDialect_pyodbc, self).__init__(
+            use_setinputsizes=use_setinputsizes, **params
+        )
         self.use_scope_identity = (
             self.use_scope_identity
             and self.dbapi
index 6adc6ca77b05560f924c852243743a722583bad0..c67614070f88f75f117239f5b1e104b24463ccb9 100644 (file)
@@ -2482,7 +2482,7 @@ class JSON(Indexable, TypeEngine[Any]):
             .. versionadded:: 1.3.11
 
             """
-            return self._binary_w_type(String(), "as_string")
+            return self._binary_w_type(Unicode(), "as_string")
 
         def as_integer(self):
             """Cast an indexed value as integer.
index 296820539f2c0197c3c14348e4e7e8bcc9f0a984..967ca85fe9327356a04e63c52cd5657e19c3adef 100644 (file)
@@ -474,11 +474,10 @@ class FastExecutemanyTest(fixtures.TestBase):
         use_fastexecutemany,
         apply_setinputsizes_flag,
     ):
-        expect_failure = (
-            apply_setinputsizes_flag
-            and not include_setinputsizes
-            and use_fastexecutemany
-        )
+
+        # changes for issue #8177 have eliminated all current expected
+        # failures, but we'll leave this here in case we need it again
+        expect_failure = False
 
         engine = fe_engine(use_fastexecutemany, apply_setinputsizes_flag)