1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
U
¸ý°d6kã@sþdZddlZddlZddlZddlZddlmZddlmZddlmZddlm    Z    ddlm
Z
dd    lm Z dd
lm Z dd lm Z dd lmZdd lmZddlmZddlmZddlmZddlmZddlmZGdd„dƒZGdd„deejƒZGdd„deejƒZ Gdd„dƒZ!Gdd„dƒZ"Gdd„de"eƒZ#Gd d!„d!e"e
ƒZ$Gd"d#„d#e!e ƒZ%Gd$d%„d%e!e    ƒZ&Gd&d'„d'ej'ƒZ(Gd(d)„d)eƒZ)Gd*d+„d+eƒZ*Gd,d-„d-eƒZ+Gd.d/„d/eƒZ,Gd0d1„d1eƒZ-Gd2d3„d3e ƒZ.Gd4d5„d5ee ƒZ/e/Z0dS)6ay6
.. dialect:: mssql+pyodbc
    :name: PyODBC
    :dbapi: pyodbc
    :connectstring: mssql+pyodbc://<username>:<password>@<dsnname>
    :url: https://pypi.org/project/pyodbc/
 
Connecting to PyODBC
--------------------
 
The URL here is to be translated to PyODBC connection strings, as
detailed in `ConnectionStrings <https://code.google.com/p/pyodbc/wiki/ConnectionStrings>`_.
 
DSN Connections
^^^^^^^^^^^^^^^
 
A DSN connection in ODBC means that a pre-existing ODBC datasource is
configured on the client machine.   The application then specifies the name
of this datasource, which encompasses details such as the specific ODBC driver
in use as well as the network address of the database.   Assuming a datasource
is configured on the client, a basic DSN-based connection looks like::
 
    engine = create_engine("mssql+pyodbc://scott:tiger@some_dsn")
 
Which above, will pass the following connection string to PyODBC::
 
    DSN=some_dsn;UID=scott;PWD=tiger
 
If the username and password are omitted, the DSN form will also add
the ``Trusted_Connection=yes`` directive to the ODBC string.
 
Hostname Connections
^^^^^^^^^^^^^^^^^^^^
 
Hostname-based connections are also supported by pyodbc.  These are often
easier to use than a DSN and have the additional advantage that the specific
database name to connect towards may be specified locally in the URL, rather
than it being fixed as part of a datasource configuration.
 
When using a hostname connection, the driver name must also be specified in the
query parameters of the URL.  As these names usually have spaces in them, the
name must be URL encoded which means using plus signs for spaces::
 
    engine = create_engine("mssql+pyodbc://scott:tiger@myhost:port/databasename?driver=ODBC+Driver+17+for+SQL+Server")
 
The ``driver`` keyword is significant to the pyodbc dialect and must be
specified in lowercase.
 
Any other names passed in the query string are passed through in the pyodbc
connect string, such as ``authentication``, ``TrustServerCertificate``, etc.
Multiple keyword arguments must be separated by an ampersand (``&``); these
will be translated to semicolons when the pyodbc connect string is generated
internally::
 
    e = create_engine(
        "mssql+pyodbc://scott:tiger@mssql2017:1433/test?"
        "driver=ODBC+Driver+18+for+SQL+Server&TrustServerCertificate=yes"
        "&authentication=ActiveDirectoryIntegrated"
    )
 
The equivalent URL can be constructed using :class:`_sa.engine.URL`::
 
    from sqlalchemy.engine import URL
    connection_url = URL.create(
        "mssql+pyodbc",
        username="scott",
        password="tiger",
        host="mssql2017",
        port=1433,
        database="test",
        query={
            "driver": "ODBC Driver 18 for SQL Server",
            "TrustServerCertificate": "yes",
            "authentication": "ActiveDirectoryIntegrated",
        },
    )
 
 
Pass through exact Pyodbc string
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
A PyODBC connection string can also be sent in pyodbc's format directly, as
specified in `the PyODBC documentation
<https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-databases>`_,
using the parameter ``odbc_connect``.  A :class:`_sa.engine.URL` object
can help make this easier::
 
    from sqlalchemy.engine import URL
    connection_string = "DRIVER={SQL Server Native Client 10.0};SERVER=dagger;DATABASE=test;UID=user;PWD=password"
    connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
 
    engine = create_engine(connection_url)
 
.. _mssql_pyodbc_access_tokens:
 
Connecting to databases with access tokens
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
Some database servers are set up to only accept access tokens for login. For
example, SQL Server allows the use of Azure Active Directory tokens to connect
to databases. This requires creating a credential object using the
``azure-identity`` library. More information about the authentication step can be
found in `Microsoft's documentation
<https://docs.microsoft.com/en-us/azure/developer/python/azure-sdk-authenticate?tabs=bash>`_.
 
After getting an engine, the credentials need to be sent to ``pyodbc.connect``
each time a connection is requested. One way to do this is to set up an event
listener on the engine that adds the credential token to the dialect's connect
call. This is discussed more generally in :ref:`engines_dynamic_tokens`. For
SQL Server in particular, this is passed as an ODBC connection attribute with
a data structure `described by Microsoft
<https://docs.microsoft.com/en-us/sql/connect/odbc/using-azure-active-directory#authenticating-with-an-access-token>`_.
 
The following code snippet will create an engine that connects to an Azure SQL
database using Azure credentials::
 
    import struct
    from sqlalchemy import create_engine, event
    from sqlalchemy.engine.url import URL
    from azure import identity
 
    SQL_COPT_SS_ACCESS_TOKEN = 1256  # Connection option for access tokens, as defined in msodbcsql.h
    TOKEN_URL = "https://database.windows.net/"  # The token URL for any Azure SQL database
 
    connection_string = "mssql+pyodbc://@my-server.database.windows.net/myDb?driver=ODBC+Driver+17+for+SQL+Server"
 
    engine = create_engine(connection_string)
 
    azure_credentials = identity.DefaultAzureCredential()
 
    @event.listens_for(engine, "do_connect")
    def provide_token(dialect, conn_rec, cargs, cparams):
        # remove the "Trusted_Connection" parameter that SQLAlchemy adds
        cargs[0] = cargs[0].replace(";Trusted_Connection=Yes", "")
 
        # create token credential
        raw_token = azure_credentials.get_token(TOKEN_URL).token.encode("utf-16-le")
        token_struct = struct.pack(f"<I{len(raw_token)}s", len(raw_token), raw_token)
 
        # apply it to keyword arguments
        cparams["attrs_before"] = {SQL_COPT_SS_ACCESS_TOKEN: token_struct}
 
.. tip::
 
    The ``Trusted_Connection`` token is currently added by the SQLAlchemy
    pyodbc dialect when no username or password is present.  This needs
    to be removed per Microsoft's
    `documentation for Azure access tokens
    <https://docs.microsoft.com/en-us/sql/connect/odbc/using-azure-active-directory#authenticating-with-an-access-token>`_,
    stating that a connection string when using an access token must not contain
    ``UID``, ``PWD``, ``Authentication`` or ``Trusted_Connection`` parameters.
 
.. _azure_synapse_ignore_no_transaction_on_rollback:
 
Avoiding transaction-related exceptions on Azure Synapse Analytics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
Azure Synapse Analytics has a significant difference in its transaction
handling compared to plain SQL Server; in some cases an error within a Synapse
transaction can cause it to be arbitrarily terminated on the server side, which
then causes the DBAPI ``.rollback()`` method (as well as ``.commit()``) to
fail. The issue prevents the usual DBAPI contract of allowing ``.rollback()``
to pass silently if no transaction is present as the driver does not expect
this condition. The symptom of this failure is an exception with a message
resembling 'No corresponding transaction found. (111214)' when attempting to
emit a ``.rollback()`` after an operation had a failure of some kind.
 
This specific case can be handled by passing ``ignore_no_transaction_on_rollback=True`` to
the SQL Server dialect via the :func:`_sa.create_engine` function as follows::
 
    engine = create_engine(connection_url, ignore_no_transaction_on_rollback=True)
 
Using the above parameter, the dialect will catch ``ProgrammingError``
exceptions raised during ``connection.rollback()`` and emit a warning
if the error message contains code ``111214``, however will not raise
an exception.
 
.. versionadded:: 1.4.40  Added the
   ``ignore_no_transaction_on_rollback=True`` parameter.
 
Enable autocommit for Azure SQL Data Warehouse (DW) connections
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
Azure SQL Data Warehouse does not support transactions,
and that can cause problems with SQLAlchemy's "autobegin" (and implicit
commit/rollback) behavior. We can avoid these problems by enabling autocommit
at both the pyodbc and engine levels::
 
    connection_url = sa.engine.URL.create(
        "mssql+pyodbc",
        username="scott",
        password="tiger",
        host="dw.azure.example.com",
        database="mydb",
        query={
            "driver": "ODBC Driver 17 for SQL Server",
            "autocommit": "True",
        },
    )
 
    engine = create_engine(connection_url).execution_options(
        isolation_level="AUTOCOMMIT"
    )
 
Avoiding sending large string parameters as TEXT/NTEXT
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
By default, for historical reasons, Microsoft's ODBC drivers for SQL Server
send long string parameters (greater than 4000 SBCS characters or 2000 Unicode
characters) as TEXT/NTEXT values. TEXT and NTEXT have been deprecated for many
years and are starting to cause compatibility issues with newer versions of
SQL_Server/Azure. For example, see `this
issue <https://github.com/mkleehammer/pyodbc/issues/835>`_.
 
Starting with ODBC Driver 18 for SQL Server we can override the legacy
behavior and pass long strings as varchar(max)/nvarchar(max) using the
``LongAsMax=Yes`` connection string parameter::
 
    connection_url = sa.engine.URL.create(
        "mssql+pyodbc",
        username="scott",
        password="tiger",
        host="mssqlserver.example.com",
        database="mydb",
        query={
            "driver": "ODBC Driver 18 for SQL Server",
            "LongAsMax": "Yes",
        },
    )
 
 
Pyodbc Pooling / connection close behavior
------------------------------------------
 
PyODBC uses internal `pooling
<https://github.com/mkleehammer/pyodbc/wiki/The-pyodbc-Module#pooling>`_ by
default, which means connections will be longer lived than they are within
SQLAlchemy itself.  As SQLAlchemy has its own pooling behavior, it is often
preferable to disable this behavior.  This behavior can only be disabled
globally at the PyODBC module level, **before** any connections are made::
 
    import pyodbc
 
    pyodbc.pooling = False
 
    # don't use the engine before pooling is set to False
    engine = create_engine("mssql+pyodbc://user:pass@dsn")
 
If this variable is left at its default value of ``True``, **the application
will continue to maintain active database connections**, even when the
SQLAlchemy engine itself fully discards a connection or if the engine is
disposed.
 
.. seealso::
 
    `pooling <https://github.com/mkleehammer/pyodbc/wiki/The-pyodbc-Module#pooling>`_ -
    in the PyODBC documentation.
 
Driver / Unicode Support
-------------------------
 
PyODBC works best with Microsoft ODBC drivers, particularly in the area
of Unicode support on both Python 2 and Python 3.
 
Using the FreeTDS ODBC drivers on Linux or OSX with PyODBC is **not**
recommended; there have been historically many Unicode-related issues
in this area, including before Microsoft offered ODBC drivers for Linux
and OSX.   Now that Microsoft offers drivers for all platforms, for
PyODBC support these are recommended.  FreeTDS remains relevant for
non-ODBC drivers such as pymssql where it works very well.
 
 
Rowcount Support
----------------
 
Previous limitations with the SQLAlchemy ORM's "versioned rows" feature with
Pyodbc have been resolved as of SQLAlchemy 2.0.5. See the notes at
:ref:`mssql_rowcount_versioning`.
 
.. _mssql_pyodbc_fastexecutemany:
 
Fast Executemany Mode
---------------------
 
The PyODBC driver includes support for a "fast executemany" mode of execution
which greatly reduces round trips for a DBAPI ``executemany()`` call when using
Microsoft ODBC drivers, for **limited size batches that fit in memory**.  The
feature is enabled by setting the attribute ``.fast_executemany`` on the DBAPI
cursor when an executemany call is to be used.   The SQLAlchemy PyODBC SQL
Server dialect supports this parameter by passing the
``fast_executemany`` parameter to
:func:`_sa.create_engine` , when using the **Microsoft ODBC driver only**::
 
    engine = create_engine(
        "mssql+pyodbc://scott:tiger@mssql2017:1433/test?driver=ODBC+Driver+17+for+SQL+Server",
        fast_executemany=True)
 
.. versionchanged:: 2.0.9 - the ``fast_executemany`` parameter now has its
   intended effect of this PyODBC feature taking effect for all INSERT
   statements that are executed with multiple parameter sets, which don't
   include RETURNING.  Previously, SQLAlchemy 2.0's :term:`insertmanyvalues`
   feature would cause ``fast_executemany`` to not be used in most cases
   even if specified.
 
.. versionadded:: 1.3
 
.. seealso::
 
    `fast executemany <https://github.com/mkleehammer/pyodbc/wiki/Features-beyond-the-DB-API#fast_executemany>`_
    - on github
 
.. _mssql_pyodbc_setinputsizes:
 
Setinputsizes Support
-----------------------
 
As of version 2.0, the pyodbc ``cursor.setinputsizes()`` method is used for
all statement executions, except for ``cursor.executemany()`` calls when
fast_executemany=True where it is not supported (assuming
:ref:`insertmanyvalues <engine_insertmanyvalues>` is kept enabled,
"fastexecutemany" will not take place for INSERT statements in any case).
 
The use of ``cursor.setinputsizes()`` can be disabled by passing
``use_setinputsizes=False`` to :func:`_sa.create_engine`.
 
When ``use_setinputsizes`` is left at its default of ``True``, the
specific per-type symbols passed to ``cursor.setinputsizes()`` can be
programmatically customized using the :meth:`.DialectEvents.do_setinputsizes`
hook. See that method for usage examples.
 
.. versionchanged:: 2.0  The mssql+pyodbc dialect now defaults to using
   ``use_setinputsizes=True`` for all statement executions with the exception of
   cursor.executemany() calls when fast_executemany=True.  The behavior can
   be turned off by passing ``use_setinputsizes=False`` to
   :func:`_sa.create_engine`.
 
éNé)Ú _MSDateTime)Ú
_MSUnicode)Ú_MSUnicodeText)ÚBINARY)ÚDATETIMEOFFSET)Ú    MSDialect)ÚMSExecutionContext)Ú    VARBINARY)ÚJSON)Ú JSONIndexType)Ú JSONPathTypeé)Úexc)Útypes)Úutil)ÚPyODBCConnectorcs0eZdZdZ‡fdd„Zdd„Zdd„Z‡ZS)Ú_ms_numeric_pyodbcz¡Turns Decimals with adjusted() < 0 or > 7 into strings.
 
    The routines here are needed for older pyodbc versions
    as well as current mxODBC versions.
 
    cs(tƒ |¡‰|jsˆS‡‡fdd„}|S)NcsRˆjr>t|tjƒr>| ¡}|dkr,ˆ |¡S|dkr>ˆ |¡SˆrJˆ|ƒS|SdS)Nré)Z    asdecimalÚ
isinstanceÚdecimalÚDecimalÚadjustedÚ_small_dec_to_stringÚ_large_dec_to_string)Úvaluer©ÚselfZ super_process©úWd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\sqlalchemy/dialects/mssql/pyodbc.pyÚprocess‚s
 
z2_ms_numeric_pyodbc.bind_processor.<locals>.process)ÚsuperÚbind_processorÚ_need_decimal_fix©rÚdialectr ©Ú    __class__rrr"{s
  z!_ms_numeric_pyodbc.bind_processorcCsBd|dkrdpddt| ¡ƒdd dd„| ¡dDƒ¡fS)    Nz%s0.%s%srú-ÚÚ0rcSsg|] }t|ƒ‘qSr©Ústr)Ú.0ZnintrrrÚ
<listcomp>˜sz;_ms_numeric_pyodbc._small_dec_to_string.<locals>.<listcomp>)ÚabsrÚjoinÚas_tuple)rrrrrr”s
ýz'_ms_numeric_pyodbc._small_dec_to_stringcCs| ¡d}dt|ƒkrXd|dkr&dp(dd dd„|Dƒ¡d    | ¡t|ƒdf}n¨t|ƒd| ¡krÈd
|dkrzdp|dd d d„|Dƒd| ¡d…¡d d d„|Dƒ| ¡dd…¡f}n8d |dkrÖdpØdd dd„|Dƒd| ¡d…¡f}|S)NrÚEz%s%s%srr(r)cSsg|] }t|ƒ‘qSrr+©r-Úsrrrr. sz;_ms_numeric_pyodbc._large_dec_to_string.<locals>.<listcomp>r*z%s%s.%scSsg|] }t|ƒ‘qSrr+r3rrrr.§scSsg|] }t|ƒ‘qSrr+r3rrrr.¨sz%s%scSsg|] }t|ƒ‘qSrr+r3rrrr.­s)r1r,r0rÚlen)rrÚ_intÚresultrrrr›s$  ý""ý"þz'_ms_numeric_pyodbc._large_dec_to_string)Ú__name__Ú
__module__Ú __qualname__Ú__doc__r"rrÚ __classcell__rrr&rrrs rc@s eZdZdS)Ú_MSNumeric_pyodbcN©r8r9r:rrrrr=²sr=c@s eZdZdS)Ú_MSFloat_pyodbcNr>rrrrr?¶sr?c@seZdZdZdd„ZdS)Ú_ms_binary_pyodbczÿWraps binary values in dialect-specific Binary wrapper.
    If the value is null, return a pyodbc-specific BinaryNull
    object to prevent pyODBC [and FreeTDS] from defaulting binary
    NULL types to SQLWCHAR and causing implicit conversion errors.
    cs(ˆjdkrdSˆjj‰‡‡fdd„}|S)Ncs|dk    rˆ|ƒSˆjjSdS©N)ÚdbapiZ
BinaryNull)r©Z DBAPIBinaryr%rrr Çsz1_ms_binary_pyodbc.bind_processor.<locals>.process)rBÚBinaryr$rrCrr"Ás
 
z _ms_binary_pyodbc.bind_processorN)r8r9r:r;r"rrrrr@ºsr@c@seZdZdZdZdd„ZdS)Ú_ODBCDateTimeBindProcessorz6Add bind processors to handle datetimeoffset behaviorsFcs‡fdd„}|S)NcsP|dkr dSt|tƒr|S|jr,ˆjs0ˆjs0|S| d¡}t dd|¡}|SdS)Nz%Y-%m-%d %H:%M:%S.%f %zz([\+\-]\d{2})([\d\.]+)$z\1:\2)rr,ÚtzinfoÚtimezoneÚhas_tzÚstrftimeÚreÚsub)rZ
dto_string©rrrr ×s
 
ÿz:_ODBCDateTimeBindProcessor.bind_processor.<locals>.processrr$rrLrr"Ös z)_ODBCDateTimeBindProcessor.bind_processorN)r8r9r:r;rHr"rrrrrEÑsrEc@s eZdZdS)Ú _ODBCDateTimeNr>rrrrrMðsrMc@seZdZdZdS)Ú_ODBCDATETIMEOFFSETTN)r8r9r:rHrrrrrNôsrNc@s eZdZdS)Ú_VARBINARY_pyodbcNr>rrrrrOøsrOc@s eZdZdS)Ú_BINARY_pyodbcNr>rrrrrPüsrPc@seZdZdd„ZdS)Ú_String_pyodbccCs*|jdks|jdkr |jddfS|jSdS©N)NÚmaxiÐr)ÚlengthZ SQL_VARCHAR©rrBrrrÚget_dbapi_types z_String_pyodbc.get_dbapi_typeN©r8r9r:rVrrrrrQsrQc@seZdZdd„ZdS)Ú_Unicode_pyodbccCs*|jdks|jdkr |jddfS|jSdSrR©rTÚ SQL_WVARCHARrUrrrrV    s z_Unicode_pyodbc.get_dbapi_typeNrWrrrrrXsrXc@seZdZdd„ZdS)Ú_UnicodeText_pyodbccCs*|jdks|jdkr |jddfS|jSdSrRrYrUrrrrVs z"_UnicodeText_pyodbc.get_dbapi_typeNrWrrrrr[sr[c@seZdZdd„ZdS)Ú _JSON_pyodbccCs |jddfS©Nr©rZrUrrrrVsz_JSON_pyodbc.get_dbapi_typeNrWrrrrr\sr\c@seZdZdd„ZdS)Ú_JSONIndexType_pyodbccCs|jSrAr^rUrrrrVsz$_JSONIndexType_pyodbc.get_dbapi_typeNrWrrrrr_sr_c@seZdZdd„ZdS)Ú_JSONPathType_pyodbccCs|jSrAr^rUrrrrV#sz#_JSONPathType_pyodbc.get_dbapi_typeNrWrrrrr`"sr`cs,eZdZdZ‡fdd„Z‡fdd„Z‡ZS)ÚMSExecutionContext_pyodbcFcs>tƒ ¡|jr:|jjr:t|jdƒr:d|_|jd7_dS)a¼where appropriate, issue "select scope_identity()" in the same
        statement.
 
        Background on why "scope_identity()" is preferable to "@@identity":
        https://msdn.microsoft.com/en-us/library/ms190315.aspx
 
        Background on why we attempt to embed "scope_identity()" into the same
        statement as the INSERT:
        https://code.google.com/p/pyodbc/wiki/FAQs#How_do_I_retrieve_autogenerated/identity_values?
 
        rTz; select scope_identity()N)    r!Úpre_execZ_select_lastrowidr%Úuse_scope_identityr5Ú
parametersÚ_embedded_scope_identityÚ    statementrLr&rrrb*s
ÿþ ýz"MSExecutionContext_pyodbc.pre_execcsb|jrTz|j ¡d}WqDWq|jjjk
r@|j ¡YqXqt|dƒ|_n
t    ƒ 
¡dSr]) reÚcursorZfetchallr%rBÚErrorÚnextsetÚintZ
_lastrowidr!Ú    post_exec)rÚrowr&rrrkDsz#MSExecutionContext_pyodbc.post_exec)r8r9r:rerbrkr<rrr&rra's rac!sÌeZdZdZdZeZe e    j
e j e e jeeee jeeeeee jee jee jee jee jee je e jj!e"e jj#e$e j%e j%i¡Z
d‡fdd„    Z&‡fdd„Z'‡fdd„Z(d    d
„Z)d‡fd d „    Z*‡fdd„Z+‡Z,S)ÚMSDialect_pyodbcTFc sZtƒjfd|i|—Ž|jo.|jo.t|jjdƒ|_|joB| ¡dk|_||_|rVd|_    dS)NÚuse_setinputsizesri)éréF)
r!Ú__init__rcrBÚhasattrÚCursorZ_dbapi_versionr#Úfast_executemanyZ!use_insertmanyvalues_wo_returning)rrtrnÚparamsr&rrrq~sÿ ýzMSDialect_pyodbc.__init__c    sˆz| d¡ ¡}Wn"tjk
r4tƒ |¡YSXg}t d¡}| |¡D],}z|     t
|ƒ¡WqNt k
rxYqNXqNt |ƒSdS)Nz8SELECT CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR)z[.\-]) Zexec_driver_sqlZscalarrZ
DBAPIErrorr!Ú_get_server_version_inforJÚcompileÚsplitÚappendrjÚ
ValueErrorÚtuple)rÚ
connectionÚrawÚversionÚrÚnr&rrrv“sÿ 
z)MSDialect_pyodbc._get_server_version_infocstƒ ¡‰‡‡fdd„}|S)Ncsˆdk    rˆ|ƒˆ |¡dSrA)Ú_setup_timestampoffset_type)Úconn©rZsuper_rrÚ
on_connect¬sz/MSDialect_pyodbc.on_connect.<locals>.on_connect)r!r„)rr„r&rƒrr„©s
zMSDialect_pyodbc.on_connectcCsdd„}d}| ||¡dS)NcSs\t d|¡}t |d|d|d|d|d|d|dd    t tj|d
|d d ¡¡S) Nz<6hI2hrrroréééièrrp)ÚhoursÚminutes)ÚstructÚunpackÚdatetimerGÚ    timedelta)Z    dto_valueÚtuprrrÚ_handle_datetimeoffset¶s 
ÿøzLMSDialect_pyodbc._setup_timestampoffset_type.<locals>._handle_datetimeoffsetieÿÿÿ)Zadd_output_converter)rr|rZodbc_SQL_SS_TIMESTAMPOFFSETrrrr´s ÿz,MSDialect_pyodbc._setup_timestampoffset_typeNcs$|jr d|_tƒj||||ddS)NT)Úcontext)rtr!Údo_executemany)rrgrfrdrr&rrr‘ÊszMSDialect_pyodbc.do_executemanycs4t||jjƒr$|jd}|dkr$dStƒ |||¡S)Nr>
ÚHYT00Ú01002Ú08S02Ú08001Ú08003Ú08S01ÚHY010Ú01000Ú10054Ú08007T)rrBrhÚargsr!Ú is_disconnect)rÚer|rgÚcoder&rrrÏs
 
 zMSDialect_pyodbc.is_disconnect)FT)N)-r8r9r:Zsupports_statement_cacheZ supports_sane_rowcount_returningraZexecution_ctx_clsrZ update_copyrZcolspecsÚsqltypesÚNumericr=ÚFloatr?rrPÚDateTimerMrrNr
rOZ LargeBinaryÚStringrQZUnicoderXZ UnicodeTextr[r r\r r_r r`ÚEnumrqrvr„rr‘rr<rrr&rrmYsZêþý  rm)1r;rŒrrJrŠÚbaserrrrrrr    r
Újsonr Z_MSJsonr Z_MSJsonIndexTyper Z_MSJsonPathTyper)rrr rZconnectors.pyodbcrrr¡r=r¢r?r@rErMrNrOrPr¤rQrXr[r\r_r`rarmr%rrrrÚ<module>    sPU               @2