pyodbc.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. # mssql/pyodbc.py
  2. # Copyright (C) 2005-2022 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. r"""
  8. .. dialect:: mssql+pyodbc
  9. :name: PyODBC
  10. :dbapi: pyodbc
  11. :connectstring: mssql+pyodbc://<username>:<password>@<dsnname>
  12. :url: https://pypi.org/project/pyodbc/
  13. Connecting to PyODBC
  14. --------------------
  15. The URL here is to be translated to PyODBC connection strings, as
  16. detailed in `ConnectionStrings <https://code.google.com/p/pyodbc/wiki/ConnectionStrings>`_.
  17. DSN Connections
  18. ^^^^^^^^^^^^^^^
  19. A DSN connection in ODBC means that a pre-existing ODBC datasource is
  20. configured on the client machine. The application then specifies the name
  21. of this datasource, which encompasses details such as the specific ODBC driver
  22. in use as well as the network address of the database. Assuming a datasource
  23. is configured on the client, a basic DSN-based connection looks like::
  24. engine = create_engine("mssql+pyodbc://scott:tiger@some_dsn")
  25. Which above, will pass the following connection string to PyODBC::
  26. DSN=some_dsn;UID=scott;PWD=tiger
  27. If the username and password are omitted, the DSN form will also add
  28. the ``Trusted_Connection=yes`` directive to the ODBC string.
  29. Hostname Connections
  30. ^^^^^^^^^^^^^^^^^^^^
  31. Hostname-based connections are also supported by pyodbc. These are often
  32. easier to use than a DSN and have the additional advantage that the specific
  33. database name to connect towards may be specified locally in the URL, rather
  34. than it being fixed as part of a datasource configuration.
  35. When using a hostname connection, the driver name must also be specified in the
  36. query parameters of the URL. As these names usually have spaces in them, the
  37. name must be URL encoded which means using plus signs for spaces::
  38. engine = create_engine("mssql+pyodbc://scott:tiger@myhost:port/databasename?driver=ODBC+Driver+17+for+SQL+Server")
  39. Other keywords interpreted by the Pyodbc dialect to be passed to
  40. ``pyodbc.connect()`` in both the DSN and hostname cases include:
  41. ``odbc_autotranslate``, ``ansi``, ``unicode_results``, ``autocommit``,
  42. ``authentication``.
  43. Note that in order for the dialect to recognize these keywords
  44. (including the ``driver`` keyword above) they must be all lowercase.
  45. Multiple additional keyword arguments must be separated by an
  46. ampersand (``&``), not a semicolon::
  47. engine = create_engine(
  48. "mssql+pyodbc://scott:tiger@myhost:49242/databasename"
  49. "?driver=ODBC+Driver+17+for+SQL+Server"
  50. "&authentication=ActiveDirectoryIntegrated"
  51. )
  52. The equivalent URL can be constructed using :class:`_sa.engine.URL`::
  53. from sqlalchemy.engine import URL
  54. connection_url = URL.create(
  55. "mssql+pyodbc",
  56. username="scott",
  57. password="tiger",
  58. host="myhost",
  59. port=49242,
  60. database="databasename",
  61. query={
  62. "driver": "ODBC Driver 17 for SQL Server",
  63. "authentication": "ActiveDirectoryIntegrated",
  64. },
  65. )
  66. Pass through exact Pyodbc string
  67. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  68. A PyODBC connection string can also be sent in pyodbc's format directly, as
  69. specified in `the PyODBC documentation
  70. <https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-databases>`_,
  71. using the parameter ``odbc_connect``. A :class:`_sa.engine.URL` object
  72. can help make this easier::
  73. from sqlalchemy.engine import URL
  74. connection_string = "DRIVER={SQL Server Native Client 10.0};SERVER=dagger;DATABASE=test;UID=user;PWD=password"
  75. connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
  76. engine = create_engine(connection_url)
  77. .. _mssql_pyodbc_access_tokens:
  78. Connecting to databases with access tokens
  79. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  80. Some database servers are set up to only accept access tokens for login. For
  81. example, SQL Server allows the use of Azure Active Directory tokens to connect
  82. to databases. This requires creating a credential object using the
  83. ``azure-identity`` library. More information about the authentication step can be
  84. found in `Microsoft's documentation
  85. <https://docs.microsoft.com/en-us/azure/developer/python/azure-sdk-authenticate?tabs=bash>`_.
  86. After getting an engine, the credentials need to be sent to ``pyodbc.connect``
  87. each time a connection is requested. One way to do this is to set up an event
  88. listener on the engine that adds the credential token to the dialect's connect
  89. call. This is discussed more generally in :ref:`engines_dynamic_tokens`. For
  90. SQL Server in particular, this is passed as an ODBC connection attribute with
  91. a data structure `described by Microsoft
  92. <https://docs.microsoft.com/en-us/sql/connect/odbc/using-azure-active-directory#authenticating-with-an-access-token>`_.
  93. The following code snippet will create an engine that connects to an Azure SQL
  94. database using Azure credentials::
  95. import struct
  96. from sqlalchemy import create_engine, event
  97. from sqlalchemy.engine.url import URL
  98. from azure import identity
  99. SQL_COPT_SS_ACCESS_TOKEN = 1256 # Connection option for access tokens, as defined in msodbcsql.h
  100. TOKEN_URL = "https://database.windows.net/" # The token URL for any Azure SQL database
  101. connection_string = "mssql+pyodbc://@my-server.database.windows.net/myDb?driver=ODBC+Driver+17+for+SQL+Server"
  102. engine = create_engine(connection_string)
  103. azure_credentials = identity.DefaultAzureCredential()
  104. @event.listens_for(engine, "do_connect")
  105. def provide_token(dialect, conn_rec, cargs, cparams):
  106. # remove the "Trusted_Connection" parameter that SQLAlchemy adds
  107. cargs[0] = cargs[0].replace(";Trusted_Connection=Yes", "")
  108. # create token credential
  109. raw_token = azure_credentials.get_token(TOKEN_URL).token.encode("utf-16-le")
  110. token_struct = struct.pack(f"<I{len(raw_token)}s", len(raw_token), raw_token)
  111. # apply it to keyword arguments
  112. cparams["attrs_before"] = {SQL_COPT_SS_ACCESS_TOKEN: token_struct}
  113. .. tip::
  114. The ``Trusted_Connection`` token is currently added by the SQLAlchemy
  115. pyodbc dialect when no username or password is present. This needs
  116. to be removed per Microsoft's
  117. `documentation for Azure access tokens
  118. <https://docs.microsoft.com/en-us/sql/connect/odbc/using-azure-active-directory#authenticating-with-an-access-token>`_,
  119. stating that a connection string when using an access token must not contain
  120. ``UID``, ``PWD``, ``Authentication`` or ``Trusted_Connection`` parameters.
  121. Enable autocommit for Azure SQL Data Warehouse (DW) connections
  122. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  123. Azure SQL Data Warehouse does not support transactions,
  124. and that can cause problems with SQLAlchemy's "autobegin" (and implicit
  125. commit/rollback) behavior. We can avoid these problems by enabling autocommit
  126. at both the pyodbc and engine levels::
  127. connection_url = sa.engine.URL.create(
  128. "mssql+pyodbc",
  129. username="scott",
  130. password="tiger",
  131. host="dw.azure.example.com",
  132. database="mydb",
  133. query={
  134. "driver": "ODBC Driver 17 for SQL Server",
  135. "autocommit": "True",
  136. },
  137. )
  138. engine = create_engine(connection_url).execution_options(
  139. isolation_level="AUTOCOMMIT"
  140. )
  141. Pyodbc Pooling / connection close behavior
  142. ------------------------------------------
  143. PyODBC uses internal `pooling
  144. <https://github.com/mkleehammer/pyodbc/wiki/The-pyodbc-Module#pooling>`_ by
  145. default, which means connections will be longer lived than they are within
  146. SQLAlchemy itself. As SQLAlchemy has its own pooling behavior, it is often
  147. preferable to disable this behavior. This behavior can only be disabled
  148. globally at the PyODBC module level, **before** any connections are made::
  149. import pyodbc
  150. pyodbc.pooling = False
  151. # don't use the engine before pooling is set to False
  152. engine = create_engine("mssql+pyodbc://user:pass@dsn")
  153. If this variable is left at its default value of ``True``, **the application
  154. will continue to maintain active database connections**, even when the
  155. SQLAlchemy engine itself fully discards a connection or if the engine is
  156. disposed.
  157. .. seealso::
  158. `pooling <https://github.com/mkleehammer/pyodbc/wiki/The-pyodbc-Module#pooling>`_ -
  159. in the PyODBC documentation.
  160. Driver / Unicode Support
  161. -------------------------
  162. PyODBC works best with Microsoft ODBC drivers, particularly in the area
  163. of Unicode support on both Python 2 and Python 3.
  164. Using the FreeTDS ODBC drivers on Linux or OSX with PyODBC is **not**
  165. recommended; there have been historically many Unicode-related issues
  166. in this area, including before Microsoft offered ODBC drivers for Linux
  167. and OSX. Now that Microsoft offers drivers for all platforms, for
  168. PyODBC support these are recommended. FreeTDS remains relevant for
  169. non-ODBC drivers such as pymssql where it works very well.
  170. Rowcount Support
  171. ----------------
  172. Pyodbc only has partial support for rowcount. See the notes at
  173. :ref:`mssql_rowcount_versioning` for important notes when using ORM
  174. versioning.
  175. .. _mssql_pyodbc_fastexecutemany:
  176. Fast Executemany Mode
  177. ---------------------
  178. The Pyodbc driver has added support for a "fast executemany" mode of execution
  179. which greatly reduces round trips for a DBAPI ``executemany()`` call when using
  180. Microsoft ODBC drivers, for **limited size batches that fit in memory**. The
  181. feature is enabled by setting the flag ``.fast_executemany`` on the DBAPI
  182. cursor when an executemany call is to be used. The SQLAlchemy pyodbc SQL
  183. Server dialect supports setting this flag automatically when the
  184. ``.fast_executemany`` flag is passed to
  185. :func:`_sa.create_engine` ; note that the ODBC driver must be the Microsoft
  186. driver in order to use this flag::
  187. engine = create_engine(
  188. "mssql+pyodbc://scott:tiger@mssql2017:1433/test?driver=ODBC+Driver+13+for+SQL+Server",
  189. fast_executemany=True)
  190. .. warning:: The pyodbc fast_executemany mode **buffers all rows in memory** and is
  191. not compatible with very large batches of data. A future version of SQLAlchemy
  192. may support this flag as a per-execution option instead.
  193. .. versionadded:: 1.3
  194. .. seealso::
  195. `fast executemany <https://github.com/mkleehammer/pyodbc/wiki/Features-beyond-the-DB-API#fast_executemany>`_
  196. - on github
  197. .. _mssql_pyodbc_setinputsizes:
  198. Setinputsizes Support
  199. -----------------------
  200. The pyodbc ``cursor.setinputsizes()`` method can be used if necessary. To
  201. enable this hook, pass ``use_setinputsizes=True`` to :func:`_sa.create_engine`::
  202. engine = create_engine("mssql+pyodbc://...", use_setinputsizes=True)
  203. The behavior of the hook can then be customized, as may be necessary
  204. particularly if fast_executemany is in use, via the
  205. :meth:`.DialectEvents.do_setinputsizes` hook. See that method for usage
  206. examples.
  207. .. versionchanged:: 1.4.1 The pyodbc dialects will not use setinputsizes
  208. unless ``use_setinputsizes=True`` is passed.
  209. """ # noqa
  210. import datetime
  211. import decimal
  212. import re
  213. import struct
  214. from .base import BINARY
  215. from .base import DATETIMEOFFSET
  216. from .base import MSDialect
  217. from .base import MSExecutionContext
  218. from .base import VARBINARY
  219. from ... import exc
  220. from ... import types as sqltypes
  221. from ... import util
  222. from ...connectors.pyodbc import PyODBCConnector
  223. class _ms_numeric_pyodbc(object):
  224. """Turns Decimals with adjusted() < 0 or > 7 into strings.
  225. The routines here are needed for older pyodbc versions
  226. as well as current mxODBC versions.
  227. """
  228. def bind_processor(self, dialect):
  229. super_process = super(_ms_numeric_pyodbc, self).bind_processor(dialect)
  230. if not dialect._need_decimal_fix:
  231. return super_process
  232. def process(value):
  233. if self.asdecimal and isinstance(value, decimal.Decimal):
  234. adjusted = value.adjusted()
  235. if adjusted < 0:
  236. return self._small_dec_to_string(value)
  237. elif adjusted > 7:
  238. return self._large_dec_to_string(value)
  239. if super_process:
  240. return super_process(value)
  241. else:
  242. return value
  243. return process
  244. # these routines needed for older versions of pyodbc.
  245. # as of 2.1.8 this logic is integrated.
  246. def _small_dec_to_string(self, value):
  247. return "%s0.%s%s" % (
  248. (value < 0 and "-" or ""),
  249. "0" * (abs(value.adjusted()) - 1),
  250. "".join([str(nint) for nint in value.as_tuple()[1]]),
  251. )
  252. def _large_dec_to_string(self, value):
  253. _int = value.as_tuple()[1]
  254. if "E" in str(value):
  255. result = "%s%s%s" % (
  256. (value < 0 and "-" or ""),
  257. "".join([str(s) for s in _int]),
  258. "0" * (value.adjusted() - (len(_int) - 1)),
  259. )
  260. else:
  261. if (len(_int) - 1) > value.adjusted():
  262. result = "%s%s.%s" % (
  263. (value < 0 and "-" or ""),
  264. "".join([str(s) for s in _int][0 : value.adjusted() + 1]),
  265. "".join([str(s) for s in _int][value.adjusted() + 1 :]),
  266. )
  267. else:
  268. result = "%s%s" % (
  269. (value < 0 and "-" or ""),
  270. "".join([str(s) for s in _int][0 : value.adjusted() + 1]),
  271. )
  272. return result
  273. class _MSNumeric_pyodbc(_ms_numeric_pyodbc, sqltypes.Numeric):
  274. pass
  275. class _MSFloat_pyodbc(_ms_numeric_pyodbc, sqltypes.Float):
  276. pass
  277. class _ms_binary_pyodbc(object):
  278. """Wraps binary values in dialect-specific Binary wrapper.
  279. If the value is null, return a pyodbc-specific BinaryNull
  280. object to prevent pyODBC [and FreeTDS] from defaulting binary
  281. NULL types to SQLWCHAR and causing implicit conversion errors.
  282. """
  283. def bind_processor(self, dialect):
  284. if dialect.dbapi is None:
  285. return None
  286. DBAPIBinary = dialect.dbapi.Binary
  287. def process(value):
  288. if value is not None:
  289. return DBAPIBinary(value)
  290. else:
  291. # pyodbc-specific
  292. return dialect.dbapi.BinaryNull
  293. return process
  294. class _ODBCDateTimeBindProcessor(object):
  295. """Add bind processors to handle datetimeoffset behaviors"""
  296. has_tz = False
  297. def bind_processor(self, dialect):
  298. def process(value):
  299. if value is None:
  300. return None
  301. elif isinstance(value, util.string_types):
  302. # if a string was passed directly, allow it through
  303. return value
  304. elif not value.tzinfo or (not self.timezone and not self.has_tz):
  305. # for DateTime(timezone=False)
  306. return value
  307. else:
  308. # for DATETIMEOFFSET or DateTime(timezone=True)
  309. #
  310. # Convert to string format required by T-SQL
  311. dto_string = value.strftime("%Y-%m-%d %H:%M:%S.%f %z")
  312. # offset needs a colon, e.g., -0700 -> -07:00
  313. # "UTC offset in the form (+-)HHMM[SS[.ffffff]]"
  314. # backend currently rejects seconds / fractional seconds
  315. dto_string = re.sub(
  316. r"([\+\-]\d{2})([\d\.]+)$", r"\1:\2", dto_string
  317. )
  318. return dto_string
  319. return process
  320. class _ODBCDateTime(_ODBCDateTimeBindProcessor, sqltypes.DateTime):
  321. pass
  322. class _ODBCDATETIMEOFFSET(_ODBCDateTimeBindProcessor, DATETIMEOFFSET):
  323. has_tz = True
  324. class _VARBINARY_pyodbc(_ms_binary_pyodbc, VARBINARY):
  325. pass
  326. class _BINARY_pyodbc(_ms_binary_pyodbc, BINARY):
  327. pass
  328. class MSExecutionContext_pyodbc(MSExecutionContext):
  329. _embedded_scope_identity = False
  330. def pre_exec(self):
  331. """where appropriate, issue "select scope_identity()" in the same
  332. statement.
  333. Background on why "scope_identity()" is preferable to "@@identity":
  334. https://msdn.microsoft.com/en-us/library/ms190315.aspx
  335. Background on why we attempt to embed "scope_identity()" into the same
  336. statement as the INSERT:
  337. https://code.google.com/p/pyodbc/wiki/FAQs#How_do_I_retrieve_autogenerated/identity_values?
  338. """
  339. super(MSExecutionContext_pyodbc, self).pre_exec()
  340. # don't embed the scope_identity select into an
  341. # "INSERT .. DEFAULT VALUES"
  342. if (
  343. self._select_lastrowid
  344. and self.dialect.use_scope_identity
  345. and len(self.parameters[0])
  346. ):
  347. self._embedded_scope_identity = True
  348. self.statement += "; select scope_identity()"
  349. def post_exec(self):
  350. if self._embedded_scope_identity:
  351. # Fetch the last inserted id from the manipulated statement
  352. # We may have to skip over a number of result sets with
  353. # no data (due to triggers, etc.)
  354. while True:
  355. try:
  356. # fetchall() ensures the cursor is consumed
  357. # without closing it (FreeTDS particularly)
  358. row = self.cursor.fetchall()[0]
  359. break
  360. except self.dialect.dbapi.Error:
  361. # no way around this - nextset() consumes the previous set
  362. # so we need to just keep flipping
  363. self.cursor.nextset()
  364. self._lastrowid = int(row[0])
  365. else:
  366. super(MSExecutionContext_pyodbc, self).post_exec()
  367. class MSDialect_pyodbc(PyODBCConnector, MSDialect):
  368. supports_statement_cache = True
  369. # mssql still has problems with this on Linux
  370. supports_sane_rowcount_returning = False
  371. execution_ctx_cls = MSExecutionContext_pyodbc
  372. colspecs = util.update_copy(
  373. MSDialect.colspecs,
  374. {
  375. sqltypes.Numeric: _MSNumeric_pyodbc,
  376. sqltypes.Float: _MSFloat_pyodbc,
  377. BINARY: _BINARY_pyodbc,
  378. # support DateTime(timezone=True)
  379. sqltypes.DateTime: _ODBCDateTime,
  380. DATETIMEOFFSET: _ODBCDATETIMEOFFSET,
  381. # SQL Server dialect has a VARBINARY that is just to support
  382. # "deprecate_large_types" w/ VARBINARY(max), but also we must
  383. # handle the usual SQL standard VARBINARY
  384. VARBINARY: _VARBINARY_pyodbc,
  385. sqltypes.VARBINARY: _VARBINARY_pyodbc,
  386. sqltypes.LargeBinary: _VARBINARY_pyodbc,
  387. },
  388. )
  389. def __init__(
  390. self, description_encoding=None, fast_executemany=False, **params
  391. ):
  392. if "description_encoding" in params:
  393. self.description_encoding = params.pop("description_encoding")
  394. super(MSDialect_pyodbc, self).__init__(**params)
  395. self.use_scope_identity = (
  396. self.use_scope_identity
  397. and self.dbapi
  398. and hasattr(self.dbapi.Cursor, "nextset")
  399. )
  400. self._need_decimal_fix = self.dbapi and self._dbapi_version() < (
  401. 2,
  402. 1,
  403. 8,
  404. )
  405. self.fast_executemany = fast_executemany
  406. def _get_server_version_info(self, connection):
  407. try:
  408. # "Version of the instance of SQL Server, in the form
  409. # of 'major.minor.build.revision'"
  410. raw = connection.exec_driver_sql(
  411. "SELECT CAST(SERVERPROPERTY('ProductVersion') AS VARCHAR)"
  412. ).scalar()
  413. except exc.DBAPIError:
  414. # SQL Server docs indicate this function isn't present prior to
  415. # 2008. Before we had the VARCHAR cast above, pyodbc would also
  416. # fail on this query.
  417. return super(MSDialect_pyodbc, self)._get_server_version_info(
  418. connection, allow_chars=False
  419. )
  420. else:
  421. version = []
  422. r = re.compile(r"[.\-]")
  423. for n in r.split(raw):
  424. try:
  425. version.append(int(n))
  426. except ValueError:
  427. pass
  428. return tuple(version)
  429. def on_connect(self):
  430. super_ = super(MSDialect_pyodbc, self).on_connect()
  431. def on_connect(conn):
  432. if super_ is not None:
  433. super_(conn)
  434. self._setup_timestampoffset_type(conn)
  435. return on_connect
  436. def _setup_timestampoffset_type(self, connection):
  437. # output converter function for datetimeoffset
  438. def _handle_datetimeoffset(dto_value):
  439. tup = struct.unpack("<6hI2h", dto_value)
  440. return datetime.datetime(
  441. tup[0],
  442. tup[1],
  443. tup[2],
  444. tup[3],
  445. tup[4],
  446. tup[5],
  447. tup[6] // 1000,
  448. util.timezone(
  449. datetime.timedelta(hours=tup[7], minutes=tup[8])
  450. ),
  451. )
  452. odbc_SQL_SS_TIMESTAMPOFFSET = -155 # as defined in SQLNCLI.h
  453. connection.add_output_converter(
  454. odbc_SQL_SS_TIMESTAMPOFFSET, _handle_datetimeoffset
  455. )
  456. def do_executemany(self, cursor, statement, parameters, context=None):
  457. if self.fast_executemany:
  458. cursor.fast_executemany = True
  459. super(MSDialect_pyodbc, self).do_executemany(
  460. cursor, statement, parameters, context=context
  461. )
  462. def is_disconnect(self, e, connection, cursor):
  463. if isinstance(e, self.dbapi.Error):
  464. code = e.args[0]
  465. if code in {
  466. "08S01",
  467. "01000",
  468. "01002",
  469. "08003",
  470. "08007",
  471. "08S02",
  472. "08001",
  473. "HYT00",
  474. "HY010",
  475. "10054",
  476. }:
  477. return True
  478. return super(MSDialect_pyodbc, self).is_disconnect(
  479. e, connection, cursor
  480. )
  481. dialect = MSDialect_pyodbc