SQLAlchemy 2.0.45__cp314-cp314-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (274) hide show
  1. sqlalchemy/__init__.py +283 -0
  2. sqlalchemy/connectors/__init__.py +18 -0
  3. sqlalchemy/connectors/aioodbc.py +184 -0
  4. sqlalchemy/connectors/asyncio.py +429 -0
  5. sqlalchemy/connectors/pyodbc.py +250 -0
  6. sqlalchemy/cyextension/__init__.py +6 -0
  7. sqlalchemy/cyextension/collections.cp314-win32.pyd +0 -0
  8. sqlalchemy/cyextension/collections.pyx +409 -0
  9. sqlalchemy/cyextension/immutabledict.cp314-win32.pyd +0 -0
  10. sqlalchemy/cyextension/immutabledict.pxd +8 -0
  11. sqlalchemy/cyextension/immutabledict.pyx +133 -0
  12. sqlalchemy/cyextension/processors.cp314-win32.pyd +0 -0
  13. sqlalchemy/cyextension/processors.pyx +68 -0
  14. sqlalchemy/cyextension/resultproxy.cp314-win32.pyd +0 -0
  15. sqlalchemy/cyextension/resultproxy.pyx +102 -0
  16. sqlalchemy/cyextension/util.cp314-win32.pyd +0 -0
  17. sqlalchemy/cyextension/util.pyx +90 -0
  18. sqlalchemy/dialects/__init__.py +62 -0
  19. sqlalchemy/dialects/_typing.py +30 -0
  20. sqlalchemy/dialects/mssql/__init__.py +88 -0
  21. sqlalchemy/dialects/mssql/aioodbc.py +63 -0
  22. sqlalchemy/dialects/mssql/base.py +4089 -0
  23. sqlalchemy/dialects/mssql/information_schema.py +285 -0
  24. sqlalchemy/dialects/mssql/json.py +129 -0
  25. sqlalchemy/dialects/mssql/provision.py +168 -0
  26. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  27. sqlalchemy/dialects/mssql/pyodbc.py +760 -0
  28. sqlalchemy/dialects/mysql/__init__.py +104 -0
  29. sqlalchemy/dialects/mysql/aiomysql.py +250 -0
  30. sqlalchemy/dialects/mysql/asyncmy.py +231 -0
  31. sqlalchemy/dialects/mysql/base.py +3933 -0
  32. sqlalchemy/dialects/mysql/cymysql.py +106 -0
  33. sqlalchemy/dialects/mysql/dml.py +225 -0
  34. sqlalchemy/dialects/mysql/enumerated.py +282 -0
  35. sqlalchemy/dialects/mysql/expression.py +146 -0
  36. sqlalchemy/dialects/mysql/json.py +91 -0
  37. sqlalchemy/dialects/mysql/mariadb.py +73 -0
  38. sqlalchemy/dialects/mysql/mariadbconnector.py +322 -0
  39. sqlalchemy/dialects/mysql/mysqlconnector.py +302 -0
  40. sqlalchemy/dialects/mysql/mysqldb.py +314 -0
  41. sqlalchemy/dialects/mysql/provision.py +147 -0
  42. sqlalchemy/dialects/mysql/pymysql.py +158 -0
  43. sqlalchemy/dialects/mysql/pyodbc.py +157 -0
  44. sqlalchemy/dialects/mysql/reflection.py +727 -0
  45. sqlalchemy/dialects/mysql/reserved_words.py +570 -0
  46. sqlalchemy/dialects/mysql/types.py +835 -0
  47. sqlalchemy/dialects/oracle/__init__.py +81 -0
  48. sqlalchemy/dialects/oracle/base.py +3802 -0
  49. sqlalchemy/dialects/oracle/cx_oracle.py +1555 -0
  50. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  51. sqlalchemy/dialects/oracle/oracledb.py +941 -0
  52. sqlalchemy/dialects/oracle/provision.py +297 -0
  53. sqlalchemy/dialects/oracle/types.py +316 -0
  54. sqlalchemy/dialects/oracle/vector.py +365 -0
  55. sqlalchemy/dialects/postgresql/__init__.py +167 -0
  56. sqlalchemy/dialects/postgresql/_psycopg_common.py +189 -0
  57. sqlalchemy/dialects/postgresql/array.py +519 -0
  58. sqlalchemy/dialects/postgresql/asyncpg.py +1284 -0
  59. sqlalchemy/dialects/postgresql/base.py +5328 -0
  60. sqlalchemy/dialects/postgresql/dml.py +339 -0
  61. sqlalchemy/dialects/postgresql/ext.py +540 -0
  62. sqlalchemy/dialects/postgresql/hstore.py +406 -0
  63. sqlalchemy/dialects/postgresql/json.py +395 -0
  64. sqlalchemy/dialects/postgresql/named_types.py +524 -0
  65. sqlalchemy/dialects/postgresql/operators.py +129 -0
  66. sqlalchemy/dialects/postgresql/pg8000.py +669 -0
  67. sqlalchemy/dialects/postgresql/pg_catalog.py +326 -0
  68. sqlalchemy/dialects/postgresql/provision.py +175 -0
  69. sqlalchemy/dialects/postgresql/psycopg.py +862 -0
  70. sqlalchemy/dialects/postgresql/psycopg2.py +892 -0
  71. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  72. sqlalchemy/dialects/postgresql/ranges.py +1031 -0
  73. sqlalchemy/dialects/postgresql/types.py +313 -0
  74. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  75. sqlalchemy/dialects/sqlite/aiosqlite.py +446 -0
  76. sqlalchemy/dialects/sqlite/base.py +3045 -0
  77. sqlalchemy/dialects/sqlite/dml.py +263 -0
  78. sqlalchemy/dialects/sqlite/json.py +92 -0
  79. sqlalchemy/dialects/sqlite/provision.py +223 -0
  80. sqlalchemy/dialects/sqlite/pysqlcipher.py +157 -0
  81. sqlalchemy/dialects/sqlite/pysqlite.py +756 -0
  82. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  83. sqlalchemy/engine/__init__.py +62 -0
  84. sqlalchemy/engine/_py_processors.py +136 -0
  85. sqlalchemy/engine/_py_row.py +128 -0
  86. sqlalchemy/engine/_py_util.py +74 -0
  87. sqlalchemy/engine/base.py +3382 -0
  88. sqlalchemy/engine/characteristics.py +155 -0
  89. sqlalchemy/engine/create.py +893 -0
  90. sqlalchemy/engine/cursor.py +2298 -0
  91. sqlalchemy/engine/default.py +2394 -0
  92. sqlalchemy/engine/events.py +965 -0
  93. sqlalchemy/engine/interfaces.py +3471 -0
  94. sqlalchemy/engine/mock.py +134 -0
  95. sqlalchemy/engine/processors.py +61 -0
  96. sqlalchemy/engine/reflection.py +2102 -0
  97. sqlalchemy/engine/result.py +2387 -0
  98. sqlalchemy/engine/row.py +400 -0
  99. sqlalchemy/engine/strategies.py +16 -0
  100. sqlalchemy/engine/url.py +924 -0
  101. sqlalchemy/engine/util.py +167 -0
  102. sqlalchemy/event/__init__.py +26 -0
  103. sqlalchemy/event/api.py +220 -0
  104. sqlalchemy/event/attr.py +676 -0
  105. sqlalchemy/event/base.py +472 -0
  106. sqlalchemy/event/legacy.py +258 -0
  107. sqlalchemy/event/registry.py +390 -0
  108. sqlalchemy/events.py +17 -0
  109. sqlalchemy/exc.py +832 -0
  110. sqlalchemy/ext/__init__.py +11 -0
  111. sqlalchemy/ext/associationproxy.py +2027 -0
  112. sqlalchemy/ext/asyncio/__init__.py +25 -0
  113. sqlalchemy/ext/asyncio/base.py +281 -0
  114. sqlalchemy/ext/asyncio/engine.py +1471 -0
  115. sqlalchemy/ext/asyncio/exc.py +21 -0
  116. sqlalchemy/ext/asyncio/result.py +965 -0
  117. sqlalchemy/ext/asyncio/scoping.py +1599 -0
  118. sqlalchemy/ext/asyncio/session.py +1947 -0
  119. sqlalchemy/ext/automap.py +1701 -0
  120. sqlalchemy/ext/baked.py +570 -0
  121. sqlalchemy/ext/compiler.py +600 -0
  122. sqlalchemy/ext/declarative/__init__.py +65 -0
  123. sqlalchemy/ext/declarative/extensions.py +564 -0
  124. sqlalchemy/ext/horizontal_shard.py +478 -0
  125. sqlalchemy/ext/hybrid.py +1535 -0
  126. sqlalchemy/ext/indexable.py +364 -0
  127. sqlalchemy/ext/instrumentation.py +450 -0
  128. sqlalchemy/ext/mutable.py +1085 -0
  129. sqlalchemy/ext/mypy/__init__.py +6 -0
  130. sqlalchemy/ext/mypy/apply.py +324 -0
  131. sqlalchemy/ext/mypy/decl_class.py +515 -0
  132. sqlalchemy/ext/mypy/infer.py +590 -0
  133. sqlalchemy/ext/mypy/names.py +335 -0
  134. sqlalchemy/ext/mypy/plugin.py +303 -0
  135. sqlalchemy/ext/mypy/util.py +357 -0
  136. sqlalchemy/ext/orderinglist.py +439 -0
  137. sqlalchemy/ext/serializer.py +185 -0
  138. sqlalchemy/future/__init__.py +16 -0
  139. sqlalchemy/future/engine.py +15 -0
  140. sqlalchemy/inspection.py +174 -0
  141. sqlalchemy/log.py +288 -0
  142. sqlalchemy/orm/__init__.py +171 -0
  143. sqlalchemy/orm/_orm_constructors.py +2661 -0
  144. sqlalchemy/orm/_typing.py +179 -0
  145. sqlalchemy/orm/attributes.py +2845 -0
  146. sqlalchemy/orm/base.py +971 -0
  147. sqlalchemy/orm/bulk_persistence.py +2135 -0
  148. sqlalchemy/orm/clsregistry.py +571 -0
  149. sqlalchemy/orm/collections.py +1627 -0
  150. sqlalchemy/orm/context.py +3334 -0
  151. sqlalchemy/orm/decl_api.py +2004 -0
  152. sqlalchemy/orm/decl_base.py +2192 -0
  153. sqlalchemy/orm/dependency.py +1302 -0
  154. sqlalchemy/orm/descriptor_props.py +1092 -0
  155. sqlalchemy/orm/dynamic.py +300 -0
  156. sqlalchemy/orm/evaluator.py +379 -0
  157. sqlalchemy/orm/events.py +3252 -0
  158. sqlalchemy/orm/exc.py +237 -0
  159. sqlalchemy/orm/identity.py +302 -0
  160. sqlalchemy/orm/instrumentation.py +754 -0
  161. sqlalchemy/orm/interfaces.py +1496 -0
  162. sqlalchemy/orm/loading.py +1686 -0
  163. sqlalchemy/orm/mapped_collection.py +557 -0
  164. sqlalchemy/orm/mapper.py +4444 -0
  165. sqlalchemy/orm/path_registry.py +809 -0
  166. sqlalchemy/orm/persistence.py +1788 -0
  167. sqlalchemy/orm/properties.py +935 -0
  168. sqlalchemy/orm/query.py +3459 -0
  169. sqlalchemy/orm/relationships.py +3508 -0
  170. sqlalchemy/orm/scoping.py +2148 -0
  171. sqlalchemy/orm/session.py +5280 -0
  172. sqlalchemy/orm/state.py +1168 -0
  173. sqlalchemy/orm/state_changes.py +196 -0
  174. sqlalchemy/orm/strategies.py +3470 -0
  175. sqlalchemy/orm/strategy_options.py +2568 -0
  176. sqlalchemy/orm/sync.py +164 -0
  177. sqlalchemy/orm/unitofwork.py +796 -0
  178. sqlalchemy/orm/util.py +2403 -0
  179. sqlalchemy/orm/writeonly.py +674 -0
  180. sqlalchemy/pool/__init__.py +44 -0
  181. sqlalchemy/pool/base.py +1516 -0
  182. sqlalchemy/pool/events.py +372 -0
  183. sqlalchemy/pool/impl.py +588 -0
  184. sqlalchemy/py.typed +0 -0
  185. sqlalchemy/schema.py +69 -0
  186. sqlalchemy/sql/__init__.py +145 -0
  187. sqlalchemy/sql/_dml_constructors.py +132 -0
  188. sqlalchemy/sql/_elements_constructors.py +1872 -0
  189. sqlalchemy/sql/_orm_types.py +20 -0
  190. sqlalchemy/sql/_py_util.py +75 -0
  191. sqlalchemy/sql/_selectable_constructors.py +763 -0
  192. sqlalchemy/sql/_typing.py +481 -0
  193. sqlalchemy/sql/annotation.py +587 -0
  194. sqlalchemy/sql/base.py +2219 -0
  195. sqlalchemy/sql/cache_key.py +1057 -0
  196. sqlalchemy/sql/coercions.py +1404 -0
  197. sqlalchemy/sql/compiler.py +8034 -0
  198. sqlalchemy/sql/crud.py +1744 -0
  199. sqlalchemy/sql/ddl.py +1444 -0
  200. sqlalchemy/sql/default_comparator.py +551 -0
  201. sqlalchemy/sql/dml.py +1850 -0
  202. sqlalchemy/sql/elements.py +5589 -0
  203. sqlalchemy/sql/events.py +458 -0
  204. sqlalchemy/sql/expression.py +159 -0
  205. sqlalchemy/sql/functions.py +2139 -0
  206. sqlalchemy/sql/lambdas.py +1442 -0
  207. sqlalchemy/sql/naming.py +209 -0
  208. sqlalchemy/sql/operators.py +2623 -0
  209. sqlalchemy/sql/roles.py +323 -0
  210. sqlalchemy/sql/schema.py +6222 -0
  211. sqlalchemy/sql/selectable.py +7265 -0
  212. sqlalchemy/sql/sqltypes.py +3930 -0
  213. sqlalchemy/sql/traversals.py +1024 -0
  214. sqlalchemy/sql/type_api.py +2368 -0
  215. sqlalchemy/sql/util.py +1485 -0
  216. sqlalchemy/sql/visitors.py +1164 -0
  217. sqlalchemy/testing/__init__.py +96 -0
  218. sqlalchemy/testing/assertions.py +994 -0
  219. sqlalchemy/testing/assertsql.py +520 -0
  220. sqlalchemy/testing/asyncio.py +135 -0
  221. sqlalchemy/testing/config.py +434 -0
  222. sqlalchemy/testing/engines.py +478 -0
  223. sqlalchemy/testing/entities.py +117 -0
  224. sqlalchemy/testing/exclusions.py +476 -0
  225. sqlalchemy/testing/fixtures/__init__.py +28 -0
  226. sqlalchemy/testing/fixtures/base.py +366 -0
  227. sqlalchemy/testing/fixtures/mypy.py +332 -0
  228. sqlalchemy/testing/fixtures/orm.py +227 -0
  229. sqlalchemy/testing/fixtures/sql.py +482 -0
  230. sqlalchemy/testing/pickleable.py +155 -0
  231. sqlalchemy/testing/plugin/__init__.py +6 -0
  232. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  233. sqlalchemy/testing/plugin/plugin_base.py +828 -0
  234. sqlalchemy/testing/plugin/pytestplugin.py +892 -0
  235. sqlalchemy/testing/profiling.py +327 -0
  236. sqlalchemy/testing/provision.py +597 -0
  237. sqlalchemy/testing/requirements.py +1940 -0
  238. sqlalchemy/testing/schema.py +198 -0
  239. sqlalchemy/testing/suite/__init__.py +19 -0
  240. sqlalchemy/testing/suite/test_cte.py +237 -0
  241. sqlalchemy/testing/suite/test_ddl.py +389 -0
  242. sqlalchemy/testing/suite/test_deprecations.py +153 -0
  243. sqlalchemy/testing/suite/test_dialect.py +776 -0
  244. sqlalchemy/testing/suite/test_insert.py +630 -0
  245. sqlalchemy/testing/suite/test_reflection.py +3557 -0
  246. sqlalchemy/testing/suite/test_results.py +504 -0
  247. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  248. sqlalchemy/testing/suite/test_select.py +2010 -0
  249. sqlalchemy/testing/suite/test_sequence.py +317 -0
  250. sqlalchemy/testing/suite/test_types.py +2147 -0
  251. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  252. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  253. sqlalchemy/testing/util.py +535 -0
  254. sqlalchemy/testing/warnings.py +52 -0
  255. sqlalchemy/types.py +74 -0
  256. sqlalchemy/util/__init__.py +162 -0
  257. sqlalchemy/util/_collections.py +717 -0
  258. sqlalchemy/util/_concurrency_py3k.py +288 -0
  259. sqlalchemy/util/_has_cy.py +40 -0
  260. sqlalchemy/util/_py_collections.py +541 -0
  261. sqlalchemy/util/compat.py +316 -0
  262. sqlalchemy/util/concurrency.py +110 -0
  263. sqlalchemy/util/deprecations.py +401 -0
  264. sqlalchemy/util/langhelpers.py +2306 -0
  265. sqlalchemy/util/preloaded.py +150 -0
  266. sqlalchemy/util/queue.py +322 -0
  267. sqlalchemy/util/tool_support.py +201 -0
  268. sqlalchemy/util/topological.py +120 -0
  269. sqlalchemy/util/typing.py +734 -0
  270. sqlalchemy-2.0.45.dist-info/METADATA +243 -0
  271. sqlalchemy-2.0.45.dist-info/RECORD +274 -0
  272. sqlalchemy-2.0.45.dist-info/WHEEL +5 -0
  273. sqlalchemy-2.0.45.dist-info/licenses/LICENSE +19 -0
  274. sqlalchemy-2.0.45.dist-info/top_level.txt +1 -0
@@ -0,0 +1,4089 @@
1
+ # dialects/mssql/base.py
2
+ # Copyright (C) 2005-2025 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
+ # mypy: ignore-errors
8
+
9
+ """
10
+ .. dialect:: mssql
11
+ :name: Microsoft SQL Server
12
+ :normal_support: 2012+
13
+ :best_effort: 2005+
14
+
15
+ .. _mssql_external_dialects:
16
+
17
+ External Dialects
18
+ -----------------
19
+
20
+ In addition to the above DBAPI layers with native SQLAlchemy support, there
21
+ are third-party dialects for other DBAPI layers that are compatible
22
+ with SQL Server. See the "External Dialects" list on the
23
+ :ref:`dialect_toplevel` page.
24
+
25
+ .. _mssql_identity:
26
+
27
+ Auto Increment Behavior / IDENTITY Columns
28
+ ------------------------------------------
29
+
30
+ SQL Server provides so-called "auto incrementing" behavior using the
31
+ ``IDENTITY`` construct, which can be placed on any single integer column in a
32
+ table. SQLAlchemy considers ``IDENTITY`` within its default "autoincrement"
33
+ behavior for an integer primary key column, described at
34
+ :paramref:`_schema.Column.autoincrement`. This means that by default,
35
+ the first integer primary key column in a :class:`_schema.Table` will be
36
+ considered to be the identity column - unless it is associated with a
37
+ :class:`.Sequence` - and will generate DDL as such::
38
+
39
+ from sqlalchemy import Table, MetaData, Column, Integer
40
+
41
+ m = MetaData()
42
+ t = Table(
43
+ "t",
44
+ m,
45
+ Column("id", Integer, primary_key=True),
46
+ Column("x", Integer),
47
+ )
48
+ m.create_all(engine)
49
+
50
+ The above example will generate DDL as:
51
+
52
+ .. sourcecode:: sql
53
+
54
+ CREATE TABLE t (
55
+ id INTEGER NOT NULL IDENTITY,
56
+ x INTEGER NULL,
57
+ PRIMARY KEY (id)
58
+ )
59
+
60
+ For the case where this default generation of ``IDENTITY`` is not desired,
61
+ specify ``False`` for the :paramref:`_schema.Column.autoincrement` flag,
62
+ on the first integer primary key column::
63
+
64
+ m = MetaData()
65
+ t = Table(
66
+ "t",
67
+ m,
68
+ Column("id", Integer, primary_key=True, autoincrement=False),
69
+ Column("x", Integer),
70
+ )
71
+ m.create_all(engine)
72
+
73
+ To add the ``IDENTITY`` keyword to a non-primary key column, specify
74
+ ``True`` for the :paramref:`_schema.Column.autoincrement` flag on the desired
75
+ :class:`_schema.Column` object, and ensure that
76
+ :paramref:`_schema.Column.autoincrement`
77
+ is set to ``False`` on any integer primary key column::
78
+
79
+ m = MetaData()
80
+ t = Table(
81
+ "t",
82
+ m,
83
+ Column("id", Integer, primary_key=True, autoincrement=False),
84
+ Column("x", Integer, autoincrement=True),
85
+ )
86
+ m.create_all(engine)
87
+
88
+ .. versionchanged:: 1.4 Added :class:`_schema.Identity` construct
89
+ in a :class:`_schema.Column` to specify the start and increment
90
+ parameters of an IDENTITY. These replace
91
+ the use of the :class:`.Sequence` object in order to specify these values.
92
+
93
+ .. deprecated:: 1.4
94
+
95
+ The ``mssql_identity_start`` and ``mssql_identity_increment`` parameters
96
+ to :class:`_schema.Column` are deprecated and should we replaced by
97
+ an :class:`_schema.Identity` object. Specifying both ways of configuring
98
+ an IDENTITY will result in a compile error.
99
+ These options are also no longer returned as part of the
100
+ ``dialect_options`` key in :meth:`_reflection.Inspector.get_columns`.
101
+ Use the information in the ``identity`` key instead.
102
+
103
+ .. deprecated:: 1.3
104
+
105
+ The use of :class:`.Sequence` to specify IDENTITY characteristics is
106
+ deprecated and will be removed in a future release. Please use
107
+ the :class:`_schema.Identity` object parameters
108
+ :paramref:`_schema.Identity.start` and
109
+ :paramref:`_schema.Identity.increment`.
110
+
111
+ .. versionchanged:: 1.4 Removed the ability to use a :class:`.Sequence`
112
+ object to modify IDENTITY characteristics. :class:`.Sequence` objects
113
+ now only manipulate true T-SQL SEQUENCE types.
114
+
115
+ .. note::
116
+
117
+ There can only be one IDENTITY column on the table. When using
118
+ ``autoincrement=True`` to enable the IDENTITY keyword, SQLAlchemy does not
119
+ guard against multiple columns specifying the option simultaneously. The
120
+ SQL Server database will instead reject the ``CREATE TABLE`` statement.
121
+
122
+ .. note::
123
+
124
+ An INSERT statement which attempts to provide a value for a column that is
125
+ marked with IDENTITY will be rejected by SQL Server. In order for the
126
+ value to be accepted, a session-level option "SET IDENTITY_INSERT" must be
127
+ enabled. The SQLAlchemy SQL Server dialect will perform this operation
128
+ automatically when using a core :class:`_expression.Insert`
129
+ construct; if the
130
+ execution specifies a value for the IDENTITY column, the "IDENTITY_INSERT"
131
+ option will be enabled for the span of that statement's invocation.However,
132
+ this scenario is not high performing and should not be relied upon for
133
+ normal use. If a table doesn't actually require IDENTITY behavior in its
134
+ integer primary key column, the keyword should be disabled when creating
135
+ the table by ensuring that ``autoincrement=False`` is set.
136
+
137
+ Controlling "Start" and "Increment"
138
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139
+
140
+ Specific control over the "start" and "increment" values for
141
+ the ``IDENTITY`` generator are provided using the
142
+ :paramref:`_schema.Identity.start` and :paramref:`_schema.Identity.increment`
143
+ parameters passed to the :class:`_schema.Identity` object::
144
+
145
+ from sqlalchemy import Table, Integer, Column, Identity
146
+
147
+ test = Table(
148
+ "test",
149
+ metadata,
150
+ Column(
151
+ "id", Integer, primary_key=True, Identity(start=100, increment=10)
152
+ ),
153
+ Column("name", String(20)),
154
+ )
155
+
156
+ The CREATE TABLE for the above :class:`_schema.Table` object would be:
157
+
158
+ .. sourcecode:: sql
159
+
160
+ CREATE TABLE test (
161
+ id INTEGER NOT NULL IDENTITY(100,10) PRIMARY KEY,
162
+ name VARCHAR(20) NULL,
163
+ )
164
+
165
+ .. note::
166
+
167
+ The :class:`_schema.Identity` object supports many other parameter in
168
+ addition to ``start`` and ``increment``. These are not supported by
169
+ SQL Server and will be ignored when generating the CREATE TABLE ddl.
170
+
171
+ .. versionchanged:: 1.3.19 The :class:`_schema.Identity` object is
172
+ now used to affect the
173
+ ``IDENTITY`` generator for a :class:`_schema.Column` under SQL Server.
174
+ Previously, the :class:`.Sequence` object was used. As SQL Server now
175
+ supports real sequences as a separate construct, :class:`.Sequence` will be
176
+ functional in the normal way starting from SQLAlchemy version 1.4.
177
+
178
+
179
+ Using IDENTITY with Non-Integer numeric types
180
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
181
+
182
+ SQL Server also allows ``IDENTITY`` to be used with ``NUMERIC`` columns. To
183
+ implement this pattern smoothly in SQLAlchemy, the primary datatype of the
184
+ column should remain as ``Integer``, however the underlying implementation
185
+ type deployed to the SQL Server database can be specified as ``Numeric`` using
186
+ :meth:`.TypeEngine.with_variant`::
187
+
188
+ from sqlalchemy import Column
189
+ from sqlalchemy import Integer
190
+ from sqlalchemy import Numeric
191
+ from sqlalchemy import String
192
+ from sqlalchemy.ext.declarative import declarative_base
193
+
194
+ Base = declarative_base()
195
+
196
+
197
+ class TestTable(Base):
198
+ __tablename__ = "test"
199
+ id = Column(
200
+ Integer().with_variant(Numeric(10, 0), "mssql"),
201
+ primary_key=True,
202
+ autoincrement=True,
203
+ )
204
+ name = Column(String)
205
+
206
+ In the above example, ``Integer().with_variant()`` provides clear usage
207
+ information that accurately describes the intent of the code. The general
208
+ restriction that ``autoincrement`` only applies to ``Integer`` is established
209
+ at the metadata level and not at the per-dialect level.
210
+
211
+ When using the above pattern, the primary key identifier that comes back from
212
+ the insertion of a row, which is also the value that would be assigned to an
213
+ ORM object such as ``TestTable`` above, will be an instance of ``Decimal()``
214
+ and not ``int`` when using SQL Server. The numeric return type of the
215
+ :class:`_types.Numeric` type can be changed to return floats by passing False
216
+ to :paramref:`_types.Numeric.asdecimal`. To normalize the return type of the
217
+ above ``Numeric(10, 0)`` to return Python ints (which also support "long"
218
+ integer values in Python 3), use :class:`_types.TypeDecorator` as follows::
219
+
220
+ from sqlalchemy import TypeDecorator
221
+
222
+
223
+ class NumericAsInteger(TypeDecorator):
224
+ "normalize floating point return values into ints"
225
+
226
+ impl = Numeric(10, 0, asdecimal=False)
227
+ cache_ok = True
228
+
229
+ def process_result_value(self, value, dialect):
230
+ if value is not None:
231
+ value = int(value)
232
+ return value
233
+
234
+
235
+ class TestTable(Base):
236
+ __tablename__ = "test"
237
+ id = Column(
238
+ Integer().with_variant(NumericAsInteger, "mssql"),
239
+ primary_key=True,
240
+ autoincrement=True,
241
+ )
242
+ name = Column(String)
243
+
244
+ .. _mssql_insert_behavior:
245
+
246
+ INSERT behavior
247
+ ^^^^^^^^^^^^^^^^
248
+
249
+ Handling of the ``IDENTITY`` column at INSERT time involves two key
250
+ techniques. The most common is being able to fetch the "last inserted value"
251
+ for a given ``IDENTITY`` column, a process which SQLAlchemy performs
252
+ implicitly in many cases, most importantly within the ORM.
253
+
254
+ The process for fetching this value has several variants:
255
+
256
+ * In the vast majority of cases, RETURNING is used in conjunction with INSERT
257
+ statements on SQL Server in order to get newly generated primary key values:
258
+
259
+ .. sourcecode:: sql
260
+
261
+ INSERT INTO t (x) OUTPUT inserted.id VALUES (?)
262
+
263
+ As of SQLAlchemy 2.0, the :ref:`engine_insertmanyvalues` feature is also
264
+ used by default to optimize many-row INSERT statements; for SQL Server
265
+ the feature takes place for both RETURNING and-non RETURNING
266
+ INSERT statements.
267
+
268
+ .. versionchanged:: 2.0.10 The :ref:`engine_insertmanyvalues` feature for
269
+ SQL Server was temporarily disabled for SQLAlchemy version 2.0.9 due to
270
+ issues with row ordering. As of 2.0.10 the feature is re-enabled, with
271
+ special case handling for the unit of work's requirement for RETURNING to
272
+ be ordered.
273
+
274
+ * When RETURNING is not available or has been disabled via
275
+ ``implicit_returning=False``, either the ``scope_identity()`` function or
276
+ the ``@@identity`` variable is used; behavior varies by backend:
277
+
278
+ * when using PyODBC, the phrase ``; select scope_identity()`` will be
279
+ appended to the end of the INSERT statement; a second result set will be
280
+ fetched in order to receive the value. Given a table as::
281
+
282
+ t = Table(
283
+ "t",
284
+ metadata,
285
+ Column("id", Integer, primary_key=True),
286
+ Column("x", Integer),
287
+ implicit_returning=False,
288
+ )
289
+
290
+ an INSERT will look like:
291
+
292
+ .. sourcecode:: sql
293
+
294
+ INSERT INTO t (x) VALUES (?); select scope_identity()
295
+
296
+ * Other dialects such as pymssql will call upon
297
+ ``SELECT scope_identity() AS lastrowid`` subsequent to an INSERT
298
+ statement. If the flag ``use_scope_identity=False`` is passed to
299
+ :func:`_sa.create_engine`,
300
+ the statement ``SELECT @@identity AS lastrowid``
301
+ is used instead.
302
+
303
+ A table that contains an ``IDENTITY`` column will prohibit an INSERT statement
304
+ that refers to the identity column explicitly. The SQLAlchemy dialect will
305
+ detect when an INSERT construct, created using a core
306
+ :func:`_expression.insert`
307
+ construct (not a plain string SQL), refers to the identity column, and
308
+ in this case will emit ``SET IDENTITY_INSERT ON`` prior to the insert
309
+ statement proceeding, and ``SET IDENTITY_INSERT OFF`` subsequent to the
310
+ execution. Given this example::
311
+
312
+ m = MetaData()
313
+ t = Table(
314
+ "t", m, Column("id", Integer, primary_key=True), Column("x", Integer)
315
+ )
316
+ m.create_all(engine)
317
+
318
+ with engine.begin() as conn:
319
+ conn.execute(t.insert(), {"id": 1, "x": 1}, {"id": 2, "x": 2})
320
+
321
+ The above column will be created with IDENTITY, however the INSERT statement
322
+ we emit is specifying explicit values. In the echo output we can see
323
+ how SQLAlchemy handles this:
324
+
325
+ .. sourcecode:: sql
326
+
327
+ CREATE TABLE t (
328
+ id INTEGER NOT NULL IDENTITY(1,1),
329
+ x INTEGER NULL,
330
+ PRIMARY KEY (id)
331
+ )
332
+
333
+ COMMIT
334
+ SET IDENTITY_INSERT t ON
335
+ INSERT INTO t (id, x) VALUES (?, ?)
336
+ ((1, 1), (2, 2))
337
+ SET IDENTITY_INSERT t OFF
338
+ COMMIT
339
+
340
+
341
+
342
+ This is an auxiliary use case suitable for testing and bulk insert scenarios.
343
+
344
+ SEQUENCE support
345
+ ----------------
346
+
347
+ The :class:`.Sequence` object creates "real" sequences, i.e.,
348
+ ``CREATE SEQUENCE``:
349
+
350
+ .. sourcecode:: pycon+sql
351
+
352
+ >>> from sqlalchemy import Sequence
353
+ >>> from sqlalchemy.schema import CreateSequence
354
+ >>> from sqlalchemy.dialects import mssql
355
+ >>> print(
356
+ ... CreateSequence(Sequence("my_seq", start=1)).compile(
357
+ ... dialect=mssql.dialect()
358
+ ... )
359
+ ... )
360
+ {printsql}CREATE SEQUENCE my_seq START WITH 1
361
+
362
+ For integer primary key generation, SQL Server's ``IDENTITY`` construct should
363
+ generally be preferred vs. sequence.
364
+
365
+ .. tip::
366
+
367
+ The default start value for T-SQL is ``-2**63`` instead of 1 as
368
+ in most other SQL databases. Users should explicitly set the
369
+ :paramref:`.Sequence.start` to 1 if that's the expected default::
370
+
371
+ seq = Sequence("my_sequence", start=1)
372
+
373
+ .. versionadded:: 1.4 added SQL Server support for :class:`.Sequence`
374
+
375
+ .. versionchanged:: 2.0 The SQL Server dialect will no longer implicitly
376
+ render "START WITH 1" for ``CREATE SEQUENCE``, which was the behavior
377
+ first implemented in version 1.4.
378
+
379
+ MAX on VARCHAR / NVARCHAR
380
+ -------------------------
381
+
382
+ SQL Server supports the special string "MAX" within the
383
+ :class:`_types.VARCHAR` and :class:`_types.NVARCHAR` datatypes,
384
+ to indicate "maximum length possible". The dialect currently handles this as
385
+ a length of "None" in the base type, rather than supplying a
386
+ dialect-specific version of these types, so that a base type
387
+ specified such as ``VARCHAR(None)`` can assume "unlengthed" behavior on
388
+ more than one backend without using dialect-specific types.
389
+
390
+ To build a SQL Server VARCHAR or NVARCHAR with MAX length, use None::
391
+
392
+ my_table = Table(
393
+ "my_table",
394
+ metadata,
395
+ Column("my_data", VARCHAR(None)),
396
+ Column("my_n_data", NVARCHAR(None)),
397
+ )
398
+
399
+ Collation Support
400
+ -----------------
401
+
402
+ Character collations are supported by the base string types,
403
+ specified by the string argument "collation"::
404
+
405
+ from sqlalchemy import VARCHAR
406
+
407
+ Column("login", VARCHAR(32, collation="Latin1_General_CI_AS"))
408
+
409
+ When such a column is associated with a :class:`_schema.Table`, the
410
+ CREATE TABLE statement for this column will yield:
411
+
412
+ .. sourcecode:: sql
413
+
414
+ login VARCHAR(32) COLLATE Latin1_General_CI_AS NULL
415
+
416
+ LIMIT/OFFSET Support
417
+ --------------------
418
+
419
+ MSSQL has added support for LIMIT / OFFSET as of SQL Server 2012, via the
420
+ "OFFSET n ROWS" and "FETCH NEXT n ROWS" clauses. SQLAlchemy supports these
421
+ syntaxes automatically if SQL Server 2012 or greater is detected.
422
+
423
+ .. versionchanged:: 1.4 support added for SQL Server "OFFSET n ROWS" and
424
+ "FETCH NEXT n ROWS" syntax.
425
+
426
+ For statements that specify only LIMIT and no OFFSET, all versions of SQL
427
+ Server support the TOP keyword. This syntax is used for all SQL Server
428
+ versions when no OFFSET clause is present. A statement such as::
429
+
430
+ select(some_table).limit(5)
431
+
432
+ will render similarly to:
433
+
434
+ .. sourcecode:: sql
435
+
436
+ SELECT TOP 5 col1, col2.. FROM table
437
+
438
+ For versions of SQL Server prior to SQL Server 2012, a statement that uses
439
+ LIMIT and OFFSET, or just OFFSET alone, will be rendered using the
440
+ ``ROW_NUMBER()`` window function. A statement such as::
441
+
442
+ select(some_table).order_by(some_table.c.col3).limit(5).offset(10)
443
+
444
+ will render similarly to:
445
+
446
+ .. sourcecode:: sql
447
+
448
+ SELECT anon_1.col1, anon_1.col2 FROM (SELECT col1, col2,
449
+ ROW_NUMBER() OVER (ORDER BY col3) AS
450
+ mssql_rn FROM table WHERE t.x = :x_1) AS
451
+ anon_1 WHERE mssql_rn > :param_1 AND mssql_rn <= :param_2 + :param_1
452
+
453
+ Note that when using LIMIT and/or OFFSET, whether using the older
454
+ or newer SQL Server syntaxes, the statement must have an ORDER BY as well,
455
+ else a :class:`.CompileError` is raised.
456
+
457
+ .. _mssql_comment_support:
458
+
459
+ DDL Comment Support
460
+ --------------------
461
+
462
+ Comment support, which includes DDL rendering for attributes such as
463
+ :paramref:`_schema.Table.comment` and :paramref:`_schema.Column.comment`, as
464
+ well as the ability to reflect these comments, is supported assuming a
465
+ supported version of SQL Server is in use. If a non-supported version such as
466
+ Azure Synapse is detected at first-connect time (based on the presence
467
+ of the ``fn_listextendedproperty`` SQL function), comment support including
468
+ rendering and table-comment reflection is disabled, as both features rely upon
469
+ SQL Server stored procedures and functions that are not available on all
470
+ backend types.
471
+
472
+ To force comment support to be on or off, bypassing autodetection, set the
473
+ parameter ``supports_comments`` within :func:`_sa.create_engine`::
474
+
475
+ e = create_engine("mssql+pyodbc://u:p@dsn", supports_comments=False)
476
+
477
+ .. versionadded:: 2.0 Added support for table and column comments for
478
+ the SQL Server dialect, including DDL generation and reflection.
479
+
480
+ .. _mssql_isolation_level:
481
+
482
+ Transaction Isolation Level
483
+ ---------------------------
484
+
485
+ All SQL Server dialects support setting of transaction isolation level
486
+ both via a dialect-specific parameter
487
+ :paramref:`_sa.create_engine.isolation_level`
488
+ accepted by :func:`_sa.create_engine`,
489
+ as well as the :paramref:`.Connection.execution_options.isolation_level`
490
+ argument as passed to
491
+ :meth:`_engine.Connection.execution_options`.
492
+ This feature works by issuing the
493
+ command ``SET TRANSACTION ISOLATION LEVEL <level>`` for
494
+ each new connection.
495
+
496
+ To set isolation level using :func:`_sa.create_engine`::
497
+
498
+ engine = create_engine(
499
+ "mssql+pyodbc://scott:tiger@ms_2008", isolation_level="REPEATABLE READ"
500
+ )
501
+
502
+ To set using per-connection execution options::
503
+
504
+ connection = engine.connect()
505
+ connection = connection.execution_options(isolation_level="READ COMMITTED")
506
+
507
+ Valid values for ``isolation_level`` include:
508
+
509
+ * ``AUTOCOMMIT`` - pyodbc / pymssql-specific
510
+ * ``READ COMMITTED``
511
+ * ``READ UNCOMMITTED``
512
+ * ``REPEATABLE READ``
513
+ * ``SERIALIZABLE``
514
+ * ``SNAPSHOT`` - specific to SQL Server
515
+
516
+ There are also more options for isolation level configurations, such as
517
+ "sub-engine" objects linked to a main :class:`_engine.Engine` which each apply
518
+ different isolation level settings. See the discussion at
519
+ :ref:`dbapi_autocommit` for background.
520
+
521
+ .. seealso::
522
+
523
+ :ref:`dbapi_autocommit`
524
+
525
+ .. _mssql_reset_on_return:
526
+
527
+ Temporary Table / Resource Reset for Connection Pooling
528
+ -------------------------------------------------------
529
+
530
+ The :class:`.QueuePool` connection pool implementation used
531
+ by the SQLAlchemy :class:`.Engine` object includes
532
+ :ref:`reset on return <pool_reset_on_return>` behavior that will invoke
533
+ the DBAPI ``.rollback()`` method when connections are returned to the pool.
534
+ While this rollback will clear out the immediate state used by the previous
535
+ transaction, it does not cover a wider range of session-level state, including
536
+ temporary tables as well as other server state such as prepared statement
537
+ handles and statement caches. An undocumented SQL Server procedure known
538
+ as ``sp_reset_connection`` is known to be a workaround for this issue which
539
+ will reset most of the session state that builds up on a connection, including
540
+ temporary tables.
541
+
542
+ To install ``sp_reset_connection`` as the means of performing reset-on-return,
543
+ the :meth:`.PoolEvents.reset` event hook may be used, as demonstrated in the
544
+ example below. The :paramref:`_sa.create_engine.pool_reset_on_return` parameter
545
+ is set to ``None`` so that the custom scheme can replace the default behavior
546
+ completely. The custom hook implementation calls ``.rollback()`` in any case,
547
+ as it's usually important that the DBAPI's own tracking of commit/rollback
548
+ will remain consistent with the state of the transaction::
549
+
550
+ from sqlalchemy import create_engine
551
+ from sqlalchemy import event
552
+
553
+ mssql_engine = create_engine(
554
+ "mssql+pyodbc://scott:tiger^5HHH@mssql2017:1433/test?driver=ODBC+Driver+17+for+SQL+Server",
555
+ # disable default reset-on-return scheme
556
+ pool_reset_on_return=None,
557
+ )
558
+
559
+
560
+ @event.listens_for(mssql_engine, "reset")
561
+ def _reset_mssql(dbapi_connection, connection_record, reset_state):
562
+ if not reset_state.terminate_only:
563
+ dbapi_connection.execute("{call sys.sp_reset_connection}")
564
+
565
+ # so that the DBAPI itself knows that the connection has been
566
+ # reset
567
+ dbapi_connection.rollback()
568
+
569
+ .. versionchanged:: 2.0.0b3 Added additional state arguments to
570
+ the :meth:`.PoolEvents.reset` event and additionally ensured the event
571
+ is invoked for all "reset" occurrences, so that it's appropriate
572
+ as a place for custom "reset" handlers. Previous schemes which
573
+ use the :meth:`.PoolEvents.checkin` handler remain usable as well.
574
+
575
+ .. seealso::
576
+
577
+ :ref:`pool_reset_on_return` - in the :ref:`pooling_toplevel` documentation
578
+
579
+ Nullability
580
+ -----------
581
+ MSSQL has support for three levels of column nullability. The default
582
+ nullability allows nulls and is explicit in the CREATE TABLE
583
+ construct:
584
+
585
+ .. sourcecode:: sql
586
+
587
+ name VARCHAR(20) NULL
588
+
589
+ If ``nullable=None`` is specified then no specification is made. In
590
+ other words the database's configured default is used. This will
591
+ render:
592
+
593
+ .. sourcecode:: sql
594
+
595
+ name VARCHAR(20)
596
+
597
+ If ``nullable`` is ``True`` or ``False`` then the column will be
598
+ ``NULL`` or ``NOT NULL`` respectively.
599
+
600
+ Date / Time Handling
601
+ --------------------
602
+ DATE and TIME are supported. Bind parameters are converted
603
+ to datetime.datetime() objects as required by most MSSQL drivers,
604
+ and results are processed from strings if needed.
605
+ The DATE and TIME types are not available for MSSQL 2005 and
606
+ previous - if a server version below 2008 is detected, DDL
607
+ for these types will be issued as DATETIME.
608
+
609
+ .. _mssql_large_type_deprecation:
610
+
611
+ Large Text/Binary Type Deprecation
612
+ ----------------------------------
613
+
614
+ Per
615
+ `SQL Server 2012/2014 Documentation <https://technet.microsoft.com/en-us/library/ms187993.aspx>`_,
616
+ the ``NTEXT``, ``TEXT`` and ``IMAGE`` datatypes are to be removed from SQL
617
+ Server in a future release. SQLAlchemy normally relates these types to the
618
+ :class:`.UnicodeText`, :class:`_expression.TextClause` and
619
+ :class:`.LargeBinary` datatypes.
620
+
621
+ In order to accommodate this change, a new flag ``deprecate_large_types``
622
+ is added to the dialect, which will be automatically set based on detection
623
+ of the server version in use, if not otherwise set by the user. The
624
+ behavior of this flag is as follows:
625
+
626
+ * When this flag is ``True``, the :class:`.UnicodeText`,
627
+ :class:`_expression.TextClause` and
628
+ :class:`.LargeBinary` datatypes, when used to render DDL, will render the
629
+ types ``NVARCHAR(max)``, ``VARCHAR(max)``, and ``VARBINARY(max)``,
630
+ respectively. This is a new behavior as of the addition of this flag.
631
+
632
+ * When this flag is ``False``, the :class:`.UnicodeText`,
633
+ :class:`_expression.TextClause` and
634
+ :class:`.LargeBinary` datatypes, when used to render DDL, will render the
635
+ types ``NTEXT``, ``TEXT``, and ``IMAGE``,
636
+ respectively. This is the long-standing behavior of these types.
637
+
638
+ * The flag begins with the value ``None``, before a database connection is
639
+ established. If the dialect is used to render DDL without the flag being
640
+ set, it is interpreted the same as ``False``.
641
+
642
+ * On first connection, the dialect detects if SQL Server version 2012 or
643
+ greater is in use; if the flag is still at ``None``, it sets it to ``True``
644
+ or ``False`` based on whether 2012 or greater is detected.
645
+
646
+ * The flag can be set to either ``True`` or ``False`` when the dialect
647
+ is created, typically via :func:`_sa.create_engine`::
648
+
649
+ eng = create_engine(
650
+ "mssql+pymssql://user:pass@host/db", deprecate_large_types=True
651
+ )
652
+
653
+ * Complete control over whether the "old" or "new" types are rendered is
654
+ available in all SQLAlchemy versions by using the UPPERCASE type objects
655
+ instead: :class:`_types.NVARCHAR`, :class:`_types.VARCHAR`,
656
+ :class:`_types.VARBINARY`, :class:`_types.TEXT`, :class:`_mssql.NTEXT`,
657
+ :class:`_mssql.IMAGE`
658
+ will always remain fixed and always output exactly that
659
+ type.
660
+
661
+ .. _multipart_schema_names:
662
+
663
+ Multipart Schema Names
664
+ ----------------------
665
+
666
+ SQL Server schemas sometimes require multiple parts to their "schema"
667
+ qualifier, that is, including the database name and owner name as separate
668
+ tokens, such as ``mydatabase.dbo.some_table``. These multipart names can be set
669
+ at once using the :paramref:`_schema.Table.schema` argument of
670
+ :class:`_schema.Table`::
671
+
672
+ Table(
673
+ "some_table",
674
+ metadata,
675
+ Column("q", String(50)),
676
+ schema="mydatabase.dbo",
677
+ )
678
+
679
+ When performing operations such as table or component reflection, a schema
680
+ argument that contains a dot will be split into separate
681
+ "database" and "owner" components in order to correctly query the SQL
682
+ Server information schema tables, as these two values are stored separately.
683
+ Additionally, when rendering the schema name for DDL or SQL, the two
684
+ components will be quoted separately for case sensitive names and other
685
+ special characters. Given an argument as below::
686
+
687
+ Table(
688
+ "some_table",
689
+ metadata,
690
+ Column("q", String(50)),
691
+ schema="MyDataBase.dbo",
692
+ )
693
+
694
+ The above schema would be rendered as ``[MyDataBase].dbo``, and also in
695
+ reflection, would be reflected using "dbo" as the owner and "MyDataBase"
696
+ as the database name.
697
+
698
+ To control how the schema name is broken into database / owner,
699
+ specify brackets (which in SQL Server are quoting characters) in the name.
700
+ Below, the "owner" will be considered as ``MyDataBase.dbo`` and the
701
+ "database" will be None::
702
+
703
+ Table(
704
+ "some_table",
705
+ metadata,
706
+ Column("q", String(50)),
707
+ schema="[MyDataBase.dbo]",
708
+ )
709
+
710
+ To individually specify both database and owner name with special characters
711
+ or embedded dots, use two sets of brackets::
712
+
713
+ Table(
714
+ "some_table",
715
+ metadata,
716
+ Column("q", String(50)),
717
+ schema="[MyDataBase.Period].[MyOwner.Dot]",
718
+ )
719
+
720
+ .. versionchanged:: 1.2 the SQL Server dialect now treats brackets as
721
+ identifier delimiters splitting the schema into separate database
722
+ and owner tokens, to allow dots within either name itself.
723
+
724
+ .. _legacy_schema_rendering:
725
+
726
+ Legacy Schema Mode
727
+ ------------------
728
+
729
+ Very old versions of the MSSQL dialect introduced the behavior such that a
730
+ schema-qualified table would be auto-aliased when used in a
731
+ SELECT statement; given a table::
732
+
733
+ account_table = Table(
734
+ "account",
735
+ metadata,
736
+ Column("id", Integer, primary_key=True),
737
+ Column("info", String(100)),
738
+ schema="customer_schema",
739
+ )
740
+
741
+ this legacy mode of rendering would assume that "customer_schema.account"
742
+ would not be accepted by all parts of the SQL statement, as illustrated
743
+ below:
744
+
745
+ .. sourcecode:: pycon+sql
746
+
747
+ >>> eng = create_engine("mssql+pymssql://mydsn", legacy_schema_aliasing=True)
748
+ >>> print(account_table.select().compile(eng))
749
+ {printsql}SELECT account_1.id, account_1.info
750
+ FROM customer_schema.account AS account_1
751
+
752
+ This mode of behavior is now off by default, as it appears to have served
753
+ no purpose; however in the case that legacy applications rely upon it,
754
+ it is available using the ``legacy_schema_aliasing`` argument to
755
+ :func:`_sa.create_engine` as illustrated above.
756
+
757
+ .. deprecated:: 1.4
758
+
759
+ The ``legacy_schema_aliasing`` flag is now
760
+ deprecated and will be removed in a future release.
761
+
762
+ .. _mssql_indexes:
763
+
764
+ Clustered Index Support
765
+ -----------------------
766
+
767
+ The MSSQL dialect supports clustered indexes (and primary keys) via the
768
+ ``mssql_clustered`` option. This option is available to :class:`.Index`,
769
+ :class:`.UniqueConstraint`. and :class:`.PrimaryKeyConstraint`.
770
+ For indexes this option can be combined with the ``mssql_columnstore`` one
771
+ to create a clustered columnstore index.
772
+
773
+ To generate a clustered index::
774
+
775
+ Index("my_index", table.c.x, mssql_clustered=True)
776
+
777
+ which renders the index as ``CREATE CLUSTERED INDEX my_index ON table (x)``.
778
+
779
+ To generate a clustered primary key use::
780
+
781
+ Table(
782
+ "my_table",
783
+ metadata,
784
+ Column("x", ...),
785
+ Column("y", ...),
786
+ PrimaryKeyConstraint("x", "y", mssql_clustered=True),
787
+ )
788
+
789
+ which will render the table, for example, as:
790
+
791
+ .. sourcecode:: sql
792
+
793
+ CREATE TABLE my_table (
794
+ x INTEGER NOT NULL,
795
+ y INTEGER NOT NULL,
796
+ PRIMARY KEY CLUSTERED (x, y)
797
+ )
798
+
799
+ Similarly, we can generate a clustered unique constraint using::
800
+
801
+ Table(
802
+ "my_table",
803
+ metadata,
804
+ Column("x", ...),
805
+ Column("y", ...),
806
+ PrimaryKeyConstraint("x"),
807
+ UniqueConstraint("y", mssql_clustered=True),
808
+ )
809
+
810
+ To explicitly request a non-clustered primary key (for example, when
811
+ a separate clustered index is desired), use::
812
+
813
+ Table(
814
+ "my_table",
815
+ metadata,
816
+ Column("x", ...),
817
+ Column("y", ...),
818
+ PrimaryKeyConstraint("x", "y", mssql_clustered=False),
819
+ )
820
+
821
+ which will render the table, for example, as:
822
+
823
+ .. sourcecode:: sql
824
+
825
+ CREATE TABLE my_table (
826
+ x INTEGER NOT NULL,
827
+ y INTEGER NOT NULL,
828
+ PRIMARY KEY NONCLUSTERED (x, y)
829
+ )
830
+
831
+ Columnstore Index Support
832
+ -------------------------
833
+
834
+ The MSSQL dialect supports columnstore indexes via the ``mssql_columnstore``
835
+ option. This option is available to :class:`.Index`. It be combined with
836
+ the ``mssql_clustered`` option to create a clustered columnstore index.
837
+
838
+ To generate a columnstore index::
839
+
840
+ Index("my_index", table.c.x, mssql_columnstore=True)
841
+
842
+ which renders the index as ``CREATE COLUMNSTORE INDEX my_index ON table (x)``.
843
+
844
+ To generate a clustered columnstore index provide no columns::
845
+
846
+ idx = Index("my_index", mssql_clustered=True, mssql_columnstore=True)
847
+ # required to associate the index with the table
848
+ table.append_constraint(idx)
849
+
850
+ the above renders the index as
851
+ ``CREATE CLUSTERED COLUMNSTORE INDEX my_index ON table``.
852
+
853
+ .. versionadded:: 2.0.18
854
+
855
+ MSSQL-Specific Index Options
856
+ -----------------------------
857
+
858
+ In addition to clustering, the MSSQL dialect supports other special options
859
+ for :class:`.Index`.
860
+
861
+ INCLUDE
862
+ ^^^^^^^
863
+
864
+ The ``mssql_include`` option renders INCLUDE(colname) for the given string
865
+ names::
866
+
867
+ Index("my_index", table.c.x, mssql_include=["y"])
868
+
869
+ would render the index as ``CREATE INDEX my_index ON table (x) INCLUDE (y)``
870
+
871
+ .. _mssql_index_where:
872
+
873
+ Filtered Indexes
874
+ ^^^^^^^^^^^^^^^^
875
+
876
+ The ``mssql_where`` option renders WHERE(condition) for the given string
877
+ names::
878
+
879
+ Index("my_index", table.c.x, mssql_where=table.c.x > 10)
880
+
881
+ would render the index as ``CREATE INDEX my_index ON table (x) WHERE x > 10``.
882
+
883
+ .. versionadded:: 1.3.4
884
+
885
+ Index ordering
886
+ ^^^^^^^^^^^^^^
887
+
888
+ Index ordering is available via functional expressions, such as::
889
+
890
+ Index("my_index", table.c.x.desc())
891
+
892
+ would render the index as ``CREATE INDEX my_index ON table (x DESC)``
893
+
894
+ .. seealso::
895
+
896
+ :ref:`schema_indexes_functional`
897
+
898
+ Compatibility Levels
899
+ --------------------
900
+ MSSQL supports the notion of setting compatibility levels at the
901
+ database level. This allows, for instance, to run a database that
902
+ is compatible with SQL2000 while running on a SQL2005 database
903
+ server. ``server_version_info`` will always return the database
904
+ server version information (in this case SQL2005) and not the
905
+ compatibility level information. Because of this, if running under
906
+ a backwards compatibility mode SQLAlchemy may attempt to use T-SQL
907
+ statements that are unable to be parsed by the database server.
908
+
909
+ .. _mssql_triggers:
910
+
911
+ Triggers
912
+ --------
913
+
914
+ SQLAlchemy by default uses OUTPUT INSERTED to get at newly
915
+ generated primary key values via IDENTITY columns or other
916
+ server side defaults. MS-SQL does not
917
+ allow the usage of OUTPUT INSERTED on tables that have triggers.
918
+ To disable the usage of OUTPUT INSERTED on a per-table basis,
919
+ specify ``implicit_returning=False`` for each :class:`_schema.Table`
920
+ which has triggers::
921
+
922
+ Table(
923
+ "mytable",
924
+ metadata,
925
+ Column("id", Integer, primary_key=True),
926
+ # ...,
927
+ implicit_returning=False,
928
+ )
929
+
930
+ Declarative form::
931
+
932
+ class MyClass(Base):
933
+ # ...
934
+ __table_args__ = {"implicit_returning": False}
935
+
936
+ .. _mssql_rowcount_versioning:
937
+
938
+ Rowcount Support / ORM Versioning
939
+ ---------------------------------
940
+
941
+ The SQL Server drivers may have limited ability to return the number
942
+ of rows updated from an UPDATE or DELETE statement.
943
+
944
+ As of this writing, the PyODBC driver is not able to return a rowcount when
945
+ OUTPUT INSERTED is used. Previous versions of SQLAlchemy therefore had
946
+ limitations for features such as the "ORM Versioning" feature that relies upon
947
+ accurate rowcounts in order to match version numbers with matched rows.
948
+
949
+ SQLAlchemy 2.0 now retrieves the "rowcount" manually for these particular use
950
+ cases based on counting the rows that arrived back within RETURNING; so while
951
+ the driver still has this limitation, the ORM Versioning feature is no longer
952
+ impacted by it. As of SQLAlchemy 2.0.5, ORM versioning has been fully
953
+ re-enabled for the pyodbc driver.
954
+
955
+ .. versionchanged:: 2.0.5 ORM versioning support is restored for the pyodbc
956
+ driver. Previously, a warning would be emitted during ORM flush that
957
+ versioning was not supported.
958
+
959
+
960
+ Enabling Snapshot Isolation
961
+ ---------------------------
962
+
963
+ SQL Server has a default transaction
964
+ isolation mode that locks entire tables, and causes even mildly concurrent
965
+ applications to have long held locks and frequent deadlocks.
966
+ Enabling snapshot isolation for the database as a whole is recommended
967
+ for modern levels of concurrency support. This is accomplished via the
968
+ following ALTER DATABASE commands executed at the SQL prompt:
969
+
970
+ .. sourcecode:: sql
971
+
972
+ ALTER DATABASE MyDatabase SET ALLOW_SNAPSHOT_ISOLATION ON
973
+
974
+ ALTER DATABASE MyDatabase SET READ_COMMITTED_SNAPSHOT ON
975
+
976
+ Background on SQL Server snapshot isolation is available at
977
+ https://msdn.microsoft.com/en-us/library/ms175095.aspx.
978
+
979
+ """ # noqa
980
+
981
+ from __future__ import annotations
982
+
983
+ import codecs
984
+ import datetime
985
+ import operator
986
+ import re
987
+ from typing import overload
988
+ from typing import TYPE_CHECKING
989
+ from uuid import UUID as _python_UUID
990
+
991
+ from . import information_schema as ischema
992
+ from .json import JSON
993
+ from .json import JSONIndexType
994
+ from .json import JSONPathType
995
+ from ... import exc
996
+ from ... import Identity
997
+ from ... import schema as sa_schema
998
+ from ... import Sequence
999
+ from ... import sql
1000
+ from ... import text
1001
+ from ... import util
1002
+ from ...engine import cursor as _cursor
1003
+ from ...engine import default
1004
+ from ...engine import reflection
1005
+ from ...engine.reflection import ReflectionDefaults
1006
+ from ...sql import coercions
1007
+ from ...sql import compiler
1008
+ from ...sql import elements
1009
+ from ...sql import expression
1010
+ from ...sql import func
1011
+ from ...sql import quoted_name
1012
+ from ...sql import roles
1013
+ from ...sql import sqltypes
1014
+ from ...sql import try_cast as try_cast # noqa: F401
1015
+ from ...sql import util as sql_util
1016
+ from ...sql._typing import is_sql_compiler
1017
+ from ...sql.compiler import InsertmanyvaluesSentinelOpts
1018
+ from ...sql.elements import TryCast as TryCast # noqa: F401
1019
+ from ...types import BIGINT
1020
+ from ...types import BINARY
1021
+ from ...types import CHAR
1022
+ from ...types import DATE
1023
+ from ...types import DATETIME
1024
+ from ...types import DECIMAL
1025
+ from ...types import FLOAT
1026
+ from ...types import INTEGER
1027
+ from ...types import NCHAR
1028
+ from ...types import NUMERIC
1029
+ from ...types import NVARCHAR
1030
+ from ...types import SMALLINT
1031
+ from ...types import TEXT
1032
+ from ...types import VARCHAR
1033
+ from ...util import update_wrapper
1034
+ from ...util.typing import Literal
1035
+
1036
+ if TYPE_CHECKING:
1037
+ from ...sql.dml import DMLState
1038
+ from ...sql.selectable import TableClause
1039
+
1040
+ # https://sqlserverbuilds.blogspot.com/
1041
+ MS_2017_VERSION = (14,)
1042
+ MS_2016_VERSION = (13,)
1043
+ MS_2014_VERSION = (12,)
1044
+ MS_2012_VERSION = (11,)
1045
+ MS_2008_VERSION = (10,)
1046
+ MS_2005_VERSION = (9,)
1047
+ MS_2000_VERSION = (8,)
1048
+
1049
+ RESERVED_WORDS = {
1050
+ "add",
1051
+ "all",
1052
+ "alter",
1053
+ "and",
1054
+ "any",
1055
+ "as",
1056
+ "asc",
1057
+ "authorization",
1058
+ "backup",
1059
+ "begin",
1060
+ "between",
1061
+ "break",
1062
+ "browse",
1063
+ "bulk",
1064
+ "by",
1065
+ "cascade",
1066
+ "case",
1067
+ "check",
1068
+ "checkpoint",
1069
+ "close",
1070
+ "clustered",
1071
+ "coalesce",
1072
+ "collate",
1073
+ "column",
1074
+ "commit",
1075
+ "compute",
1076
+ "constraint",
1077
+ "contains",
1078
+ "containstable",
1079
+ "continue",
1080
+ "convert",
1081
+ "create",
1082
+ "cross",
1083
+ "current",
1084
+ "current_date",
1085
+ "current_time",
1086
+ "current_timestamp",
1087
+ "current_user",
1088
+ "cursor",
1089
+ "database",
1090
+ "dbcc",
1091
+ "deallocate",
1092
+ "declare",
1093
+ "default",
1094
+ "delete",
1095
+ "deny",
1096
+ "desc",
1097
+ "disk",
1098
+ "distinct",
1099
+ "distributed",
1100
+ "double",
1101
+ "drop",
1102
+ "dump",
1103
+ "else",
1104
+ "end",
1105
+ "errlvl",
1106
+ "escape",
1107
+ "except",
1108
+ "exec",
1109
+ "execute",
1110
+ "exists",
1111
+ "exit",
1112
+ "external",
1113
+ "fetch",
1114
+ "file",
1115
+ "fillfactor",
1116
+ "for",
1117
+ "foreign",
1118
+ "freetext",
1119
+ "freetexttable",
1120
+ "from",
1121
+ "full",
1122
+ "function",
1123
+ "goto",
1124
+ "grant",
1125
+ "group",
1126
+ "having",
1127
+ "holdlock",
1128
+ "identity",
1129
+ "identity_insert",
1130
+ "identitycol",
1131
+ "if",
1132
+ "in",
1133
+ "index",
1134
+ "inner",
1135
+ "insert",
1136
+ "intersect",
1137
+ "into",
1138
+ "is",
1139
+ "join",
1140
+ "key",
1141
+ "kill",
1142
+ "left",
1143
+ "like",
1144
+ "lineno",
1145
+ "load",
1146
+ "merge",
1147
+ "national",
1148
+ "nocheck",
1149
+ "nonclustered",
1150
+ "not",
1151
+ "null",
1152
+ "nullif",
1153
+ "of",
1154
+ "off",
1155
+ "offsets",
1156
+ "on",
1157
+ "open",
1158
+ "opendatasource",
1159
+ "openquery",
1160
+ "openrowset",
1161
+ "openxml",
1162
+ "option",
1163
+ "or",
1164
+ "order",
1165
+ "outer",
1166
+ "over",
1167
+ "percent",
1168
+ "pivot",
1169
+ "plan",
1170
+ "precision",
1171
+ "primary",
1172
+ "print",
1173
+ "proc",
1174
+ "procedure",
1175
+ "public",
1176
+ "raiserror",
1177
+ "read",
1178
+ "readtext",
1179
+ "reconfigure",
1180
+ "references",
1181
+ "replication",
1182
+ "restore",
1183
+ "restrict",
1184
+ "return",
1185
+ "revert",
1186
+ "revoke",
1187
+ "right",
1188
+ "rollback",
1189
+ "rowcount",
1190
+ "rowguidcol",
1191
+ "rule",
1192
+ "save",
1193
+ "schema",
1194
+ "securityaudit",
1195
+ "select",
1196
+ "session_user",
1197
+ "set",
1198
+ "setuser",
1199
+ "shutdown",
1200
+ "some",
1201
+ "statistics",
1202
+ "system_user",
1203
+ "table",
1204
+ "tablesample",
1205
+ "textsize",
1206
+ "then",
1207
+ "to",
1208
+ "top",
1209
+ "tran",
1210
+ "transaction",
1211
+ "trigger",
1212
+ "truncate",
1213
+ "tsequal",
1214
+ "union",
1215
+ "unique",
1216
+ "unpivot",
1217
+ "update",
1218
+ "updatetext",
1219
+ "use",
1220
+ "user",
1221
+ "values",
1222
+ "varying",
1223
+ "view",
1224
+ "waitfor",
1225
+ "when",
1226
+ "where",
1227
+ "while",
1228
+ "with",
1229
+ "writetext",
1230
+ }
1231
+
1232
+
1233
+ class REAL(sqltypes.REAL):
1234
+ """the SQL Server REAL datatype."""
1235
+
1236
+ def __init__(self, **kw):
1237
+ # REAL is a synonym for FLOAT(24) on SQL server.
1238
+ # it is only accepted as the word "REAL" in DDL, the numeric
1239
+ # precision value is not allowed to be present
1240
+ kw.setdefault("precision", 24)
1241
+ super().__init__(**kw)
1242
+
1243
+
1244
+ class DOUBLE_PRECISION(sqltypes.DOUBLE_PRECISION):
1245
+ """the SQL Server DOUBLE PRECISION datatype.
1246
+
1247
+ .. versionadded:: 2.0.11
1248
+
1249
+ """
1250
+
1251
+ def __init__(self, **kw):
1252
+ # DOUBLE PRECISION is a synonym for FLOAT(53) on SQL server.
1253
+ # it is only accepted as the word "DOUBLE PRECISION" in DDL,
1254
+ # the numeric precision value is not allowed to be present
1255
+ kw.setdefault("precision", 53)
1256
+ super().__init__(**kw)
1257
+
1258
+
1259
+ class TINYINT(sqltypes.Integer):
1260
+ __visit_name__ = "TINYINT"
1261
+
1262
+
1263
+ # MSSQL DATE/TIME types have varied behavior, sometimes returning
1264
+ # strings. MSDate/TIME check for everything, and always
1265
+ # filter bind parameters into datetime objects (required by pyodbc,
1266
+ # not sure about other dialects).
1267
+
1268
+
1269
+ class _MSDate(sqltypes.Date):
1270
+ def bind_processor(self, dialect):
1271
+ def process(value):
1272
+ if type(value) == datetime.date:
1273
+ return datetime.datetime(value.year, value.month, value.day)
1274
+ else:
1275
+ return value
1276
+
1277
+ return process
1278
+
1279
+ _reg = re.compile(r"(\d+)-(\d+)-(\d+)")
1280
+
1281
+ def result_processor(self, dialect, coltype):
1282
+ def process(value):
1283
+ if isinstance(value, datetime.datetime):
1284
+ return value.date()
1285
+ elif isinstance(value, str):
1286
+ m = self._reg.match(value)
1287
+ if not m:
1288
+ raise ValueError(
1289
+ "could not parse %r as a date value" % (value,)
1290
+ )
1291
+ return datetime.date(*[int(x or 0) for x in m.groups()])
1292
+ else:
1293
+ return value
1294
+
1295
+ return process
1296
+
1297
+
1298
+ class TIME(sqltypes.TIME):
1299
+ def __init__(self, precision=None, **kwargs):
1300
+ self.precision = precision
1301
+ super().__init__()
1302
+
1303
+ __zero_date = datetime.date(1900, 1, 1)
1304
+
1305
+ def bind_processor(self, dialect):
1306
+ def process(value):
1307
+ if isinstance(value, datetime.datetime):
1308
+ value = datetime.datetime.combine(
1309
+ self.__zero_date, value.time()
1310
+ )
1311
+ elif isinstance(value, datetime.time):
1312
+ """issue #5339
1313
+ per: https://github.com/mkleehammer/pyodbc/wiki/Tips-and-Tricks-by-Database-Platform#time-columns
1314
+ pass TIME value as string
1315
+ """ # noqa
1316
+ value = str(value)
1317
+ return value
1318
+
1319
+ return process
1320
+
1321
+ _reg = re.compile(r"(\d+):(\d+):(\d+)(?:\.(\d{0,6}))?")
1322
+
1323
+ def result_processor(self, dialect, coltype):
1324
+ def process(value):
1325
+ if isinstance(value, datetime.datetime):
1326
+ return value.time()
1327
+ elif isinstance(value, str):
1328
+ m = self._reg.match(value)
1329
+ if not m:
1330
+ raise ValueError(
1331
+ "could not parse %r as a time value" % (value,)
1332
+ )
1333
+ return datetime.time(*[int(x or 0) for x in m.groups()])
1334
+ else:
1335
+ return value
1336
+
1337
+ return process
1338
+
1339
+
1340
+ _MSTime = TIME
1341
+
1342
+
1343
+ class _BASETIMEIMPL(TIME):
1344
+ __visit_name__ = "_BASETIMEIMPL"
1345
+
1346
+
1347
+ class _DateTimeBase:
1348
+ def bind_processor(self, dialect):
1349
+ def process(value):
1350
+ if type(value) == datetime.date:
1351
+ return datetime.datetime(value.year, value.month, value.day)
1352
+ else:
1353
+ return value
1354
+
1355
+ return process
1356
+
1357
+
1358
+ class _MSDateTime(_DateTimeBase, sqltypes.DateTime):
1359
+ pass
1360
+
1361
+
1362
+ class SMALLDATETIME(_DateTimeBase, sqltypes.DateTime):
1363
+ __visit_name__ = "SMALLDATETIME"
1364
+
1365
+
1366
+ class DATETIME2(_DateTimeBase, sqltypes.DateTime):
1367
+ __visit_name__ = "DATETIME2"
1368
+
1369
+ def __init__(self, precision=None, **kw):
1370
+ super().__init__(**kw)
1371
+ self.precision = precision
1372
+
1373
+
1374
+ class DATETIMEOFFSET(_DateTimeBase, sqltypes.DateTime):
1375
+ __visit_name__ = "DATETIMEOFFSET"
1376
+
1377
+ def __init__(self, precision=None, **kw):
1378
+ super().__init__(**kw)
1379
+ self.precision = precision
1380
+
1381
+
1382
+ class _UnicodeLiteral:
1383
+ def literal_processor(self, dialect):
1384
+ def process(value):
1385
+ value = value.replace("'", "''")
1386
+
1387
+ if dialect.identifier_preparer._double_percents:
1388
+ value = value.replace("%", "%%")
1389
+
1390
+ return "N'%s'" % value
1391
+
1392
+ return process
1393
+
1394
+
1395
+ class _MSUnicode(_UnicodeLiteral, sqltypes.Unicode):
1396
+ pass
1397
+
1398
+
1399
+ class _MSUnicodeText(_UnicodeLiteral, sqltypes.UnicodeText):
1400
+ pass
1401
+
1402
+
1403
+ class TIMESTAMP(sqltypes._Binary):
1404
+ """Implement the SQL Server TIMESTAMP type.
1405
+
1406
+ Note this is **completely different** than the SQL Standard
1407
+ TIMESTAMP type, which is not supported by SQL Server. It
1408
+ is a read-only datatype that does not support INSERT of values.
1409
+
1410
+ .. versionadded:: 1.2
1411
+
1412
+ .. seealso::
1413
+
1414
+ :class:`_mssql.ROWVERSION`
1415
+
1416
+ """
1417
+
1418
+ __visit_name__ = "TIMESTAMP"
1419
+
1420
+ # expected by _Binary to be present
1421
+ length = None
1422
+
1423
+ def __init__(self, convert_int=False):
1424
+ """Construct a TIMESTAMP or ROWVERSION type.
1425
+
1426
+ :param convert_int: if True, binary integer values will
1427
+ be converted to integers on read.
1428
+
1429
+ .. versionadded:: 1.2
1430
+
1431
+ """
1432
+ self.convert_int = convert_int
1433
+
1434
+ def result_processor(self, dialect, coltype):
1435
+ super_ = super().result_processor(dialect, coltype)
1436
+ if self.convert_int:
1437
+
1438
+ def process(value):
1439
+ if super_:
1440
+ value = super_(value)
1441
+ if value is not None:
1442
+ # https://stackoverflow.com/a/30403242/34549
1443
+ value = int(codecs.encode(value, "hex"), 16)
1444
+ return value
1445
+
1446
+ return process
1447
+ else:
1448
+ return super_
1449
+
1450
+
1451
+ class ROWVERSION(TIMESTAMP):
1452
+ """Implement the SQL Server ROWVERSION type.
1453
+
1454
+ The ROWVERSION datatype is a SQL Server synonym for the TIMESTAMP
1455
+ datatype, however current SQL Server documentation suggests using
1456
+ ROWVERSION for new datatypes going forward.
1457
+
1458
+ The ROWVERSION datatype does **not** reflect (e.g. introspect) from the
1459
+ database as itself; the returned datatype will be
1460
+ :class:`_mssql.TIMESTAMP`.
1461
+
1462
+ This is a read-only datatype that does not support INSERT of values.
1463
+
1464
+ .. versionadded:: 1.2
1465
+
1466
+ .. seealso::
1467
+
1468
+ :class:`_mssql.TIMESTAMP`
1469
+
1470
+ """
1471
+
1472
+ __visit_name__ = "ROWVERSION"
1473
+
1474
+
1475
+ class NTEXT(sqltypes.UnicodeText):
1476
+ """MSSQL NTEXT type, for variable-length unicode text up to 2^30
1477
+ characters."""
1478
+
1479
+ __visit_name__ = "NTEXT"
1480
+
1481
+
1482
+ class VARBINARY(sqltypes.VARBINARY, sqltypes.LargeBinary):
1483
+ """The MSSQL VARBINARY type.
1484
+
1485
+ This type adds additional features to the core :class:`_types.VARBINARY`
1486
+ type, including "deprecate_large_types" mode where
1487
+ either ``VARBINARY(max)`` or IMAGE is rendered, as well as the SQL
1488
+ Server ``FILESTREAM`` option.
1489
+
1490
+ .. seealso::
1491
+
1492
+ :ref:`mssql_large_type_deprecation`
1493
+
1494
+ """
1495
+
1496
+ __visit_name__ = "VARBINARY"
1497
+
1498
+ def __init__(self, length=None, filestream=False):
1499
+ """
1500
+ Construct a VARBINARY type.
1501
+
1502
+ :param length: optional, a length for the column for use in
1503
+ DDL statements, for those binary types that accept a length,
1504
+ such as the MySQL BLOB type.
1505
+
1506
+ :param filestream=False: if True, renders the ``FILESTREAM`` keyword
1507
+ in the table definition. In this case ``length`` must be ``None``
1508
+ or ``'max'``.
1509
+
1510
+ .. versionadded:: 1.4.31
1511
+
1512
+ """
1513
+
1514
+ self.filestream = filestream
1515
+ if self.filestream and length not in (None, "max"):
1516
+ raise ValueError(
1517
+ "length must be None or 'max' when setting filestream"
1518
+ )
1519
+ super().__init__(length=length)
1520
+
1521
+
1522
+ class IMAGE(sqltypes.LargeBinary):
1523
+ __visit_name__ = "IMAGE"
1524
+
1525
+
1526
+ class XML(sqltypes.Text):
1527
+ """MSSQL XML type.
1528
+
1529
+ This is a placeholder type for reflection purposes that does not include
1530
+ any Python-side datatype support. It also does not currently support
1531
+ additional arguments, such as "CONTENT", "DOCUMENT",
1532
+ "xml_schema_collection".
1533
+
1534
+ """
1535
+
1536
+ __visit_name__ = "XML"
1537
+
1538
+
1539
+ class BIT(sqltypes.Boolean):
1540
+ """MSSQL BIT type.
1541
+
1542
+ Both pyodbc and pymssql return values from BIT columns as
1543
+ Python <class 'bool'> so just subclass Boolean.
1544
+
1545
+ """
1546
+
1547
+ __visit_name__ = "BIT"
1548
+
1549
+
1550
+ class MONEY(sqltypes.TypeEngine):
1551
+ __visit_name__ = "MONEY"
1552
+
1553
+
1554
+ class SMALLMONEY(sqltypes.TypeEngine):
1555
+ __visit_name__ = "SMALLMONEY"
1556
+
1557
+
1558
+ class MSUUid(sqltypes.Uuid):
1559
+ def bind_processor(self, dialect):
1560
+ if self.native_uuid:
1561
+ # this is currently assuming pyodbc; might not work for
1562
+ # some other mssql driver
1563
+ return None
1564
+ else:
1565
+ if self.as_uuid:
1566
+
1567
+ def process(value):
1568
+ if value is not None:
1569
+ value = value.hex
1570
+ return value
1571
+
1572
+ return process
1573
+ else:
1574
+
1575
+ def process(value):
1576
+ if value is not None:
1577
+ value = value.replace("-", "").replace("''", "'")
1578
+ return value
1579
+
1580
+ return process
1581
+
1582
+ def literal_processor(self, dialect):
1583
+ if self.native_uuid:
1584
+
1585
+ def process(value):
1586
+ return f"""'{str(value).replace("''", "'")}'"""
1587
+
1588
+ return process
1589
+ else:
1590
+ if self.as_uuid:
1591
+
1592
+ def process(value):
1593
+ return f"""'{value.hex}'"""
1594
+
1595
+ return process
1596
+ else:
1597
+
1598
+ def process(value):
1599
+ return f"""'{
1600
+ value.replace("-", "").replace("'", "''")
1601
+ }'"""
1602
+
1603
+ return process
1604
+
1605
+
1606
+ class UNIQUEIDENTIFIER(sqltypes.Uuid[sqltypes._UUID_RETURN]):
1607
+ __visit_name__ = "UNIQUEIDENTIFIER"
1608
+
1609
+ @overload
1610
+ def __init__(
1611
+ self: UNIQUEIDENTIFIER[_python_UUID], as_uuid: Literal[True] = ...
1612
+ ): ...
1613
+
1614
+ @overload
1615
+ def __init__(
1616
+ self: UNIQUEIDENTIFIER[str], as_uuid: Literal[False] = ...
1617
+ ): ...
1618
+
1619
+ def __init__(self, as_uuid: bool = True):
1620
+ """Construct a :class:`_mssql.UNIQUEIDENTIFIER` type.
1621
+
1622
+
1623
+ :param as_uuid=True: if True, values will be interpreted
1624
+ as Python uuid objects, converting to/from string via the
1625
+ DBAPI.
1626
+
1627
+ .. versionchanged: 2.0 Added direct "uuid" support to the
1628
+ :class:`_mssql.UNIQUEIDENTIFIER` datatype; uuid interpretation
1629
+ defaults to ``True``.
1630
+
1631
+ """
1632
+ self.as_uuid = as_uuid
1633
+ self.native_uuid = True
1634
+
1635
+
1636
+ class SQL_VARIANT(sqltypes.TypeEngine):
1637
+ __visit_name__ = "SQL_VARIANT"
1638
+
1639
+
1640
+ # old names.
1641
+ MSDateTime = _MSDateTime
1642
+ MSDate = _MSDate
1643
+ MSReal = REAL
1644
+ MSTinyInteger = TINYINT
1645
+ MSTime = TIME
1646
+ MSSmallDateTime = SMALLDATETIME
1647
+ MSDateTime2 = DATETIME2
1648
+ MSDateTimeOffset = DATETIMEOFFSET
1649
+ MSText = TEXT
1650
+ MSNText = NTEXT
1651
+ MSString = VARCHAR
1652
+ MSNVarchar = NVARCHAR
1653
+ MSChar = CHAR
1654
+ MSNChar = NCHAR
1655
+ MSBinary = BINARY
1656
+ MSVarBinary = VARBINARY
1657
+ MSImage = IMAGE
1658
+ MSBit = BIT
1659
+ MSMoney = MONEY
1660
+ MSSmallMoney = SMALLMONEY
1661
+ MSUniqueIdentifier = UNIQUEIDENTIFIER
1662
+ MSVariant = SQL_VARIANT
1663
+
1664
+ ischema_names = {
1665
+ "int": INTEGER,
1666
+ "bigint": BIGINT,
1667
+ "smallint": SMALLINT,
1668
+ "tinyint": TINYINT,
1669
+ "varchar": VARCHAR,
1670
+ "nvarchar": NVARCHAR,
1671
+ "char": CHAR,
1672
+ "nchar": NCHAR,
1673
+ "text": TEXT,
1674
+ "ntext": NTEXT,
1675
+ "decimal": DECIMAL,
1676
+ "numeric": NUMERIC,
1677
+ "float": FLOAT,
1678
+ "datetime": DATETIME,
1679
+ "datetime2": DATETIME2,
1680
+ "datetimeoffset": DATETIMEOFFSET,
1681
+ "date": DATE,
1682
+ "time": TIME,
1683
+ "smalldatetime": SMALLDATETIME,
1684
+ "binary": BINARY,
1685
+ "varbinary": VARBINARY,
1686
+ "bit": BIT,
1687
+ "real": REAL,
1688
+ "double precision": DOUBLE_PRECISION,
1689
+ "image": IMAGE,
1690
+ "xml": XML,
1691
+ "timestamp": TIMESTAMP,
1692
+ "money": MONEY,
1693
+ "smallmoney": SMALLMONEY,
1694
+ "uniqueidentifier": UNIQUEIDENTIFIER,
1695
+ "sql_variant": SQL_VARIANT,
1696
+ }
1697
+
1698
+
1699
+ class MSTypeCompiler(compiler.GenericTypeCompiler):
1700
+ def _extend(self, spec, type_, length=None):
1701
+ """Extend a string-type declaration with standard SQL
1702
+ COLLATE annotations.
1703
+
1704
+ """
1705
+
1706
+ if getattr(type_, "collation", None):
1707
+ collation = "COLLATE %s" % type_.collation
1708
+ else:
1709
+ collation = None
1710
+
1711
+ if not length:
1712
+ length = type_.length
1713
+
1714
+ if length:
1715
+ spec = spec + "(%s)" % length
1716
+
1717
+ return " ".join([c for c in (spec, collation) if c is not None])
1718
+
1719
+ def visit_double(self, type_, **kw):
1720
+ return self.visit_DOUBLE_PRECISION(type_, **kw)
1721
+
1722
+ def visit_FLOAT(self, type_, **kw):
1723
+ precision = getattr(type_, "precision", None)
1724
+ if precision is None:
1725
+ return "FLOAT"
1726
+ else:
1727
+ return "FLOAT(%(precision)s)" % {"precision": precision}
1728
+
1729
+ def visit_TINYINT(self, type_, **kw):
1730
+ return "TINYINT"
1731
+
1732
+ def visit_TIME(self, type_, **kw):
1733
+ precision = getattr(type_, "precision", None)
1734
+ if precision is not None:
1735
+ return "TIME(%s)" % precision
1736
+ else:
1737
+ return "TIME"
1738
+
1739
+ def visit_TIMESTAMP(self, type_, **kw):
1740
+ return "TIMESTAMP"
1741
+
1742
+ def visit_ROWVERSION(self, type_, **kw):
1743
+ return "ROWVERSION"
1744
+
1745
+ def visit_datetime(self, type_, **kw):
1746
+ if type_.timezone:
1747
+ return self.visit_DATETIMEOFFSET(type_, **kw)
1748
+ else:
1749
+ return self.visit_DATETIME(type_, **kw)
1750
+
1751
+ def visit_DATETIMEOFFSET(self, type_, **kw):
1752
+ precision = getattr(type_, "precision", None)
1753
+ if precision is not None:
1754
+ return "DATETIMEOFFSET(%s)" % type_.precision
1755
+ else:
1756
+ return "DATETIMEOFFSET"
1757
+
1758
+ def visit_DATETIME2(self, type_, **kw):
1759
+ precision = getattr(type_, "precision", None)
1760
+ if precision is not None:
1761
+ return "DATETIME2(%s)" % precision
1762
+ else:
1763
+ return "DATETIME2"
1764
+
1765
+ def visit_SMALLDATETIME(self, type_, **kw):
1766
+ return "SMALLDATETIME"
1767
+
1768
+ def visit_unicode(self, type_, **kw):
1769
+ return self.visit_NVARCHAR(type_, **kw)
1770
+
1771
+ def visit_text(self, type_, **kw):
1772
+ if self.dialect.deprecate_large_types:
1773
+ return self.visit_VARCHAR(type_, **kw)
1774
+ else:
1775
+ return self.visit_TEXT(type_, **kw)
1776
+
1777
+ def visit_unicode_text(self, type_, **kw):
1778
+ if self.dialect.deprecate_large_types:
1779
+ return self.visit_NVARCHAR(type_, **kw)
1780
+ else:
1781
+ return self.visit_NTEXT(type_, **kw)
1782
+
1783
+ def visit_NTEXT(self, type_, **kw):
1784
+ return self._extend("NTEXT", type_)
1785
+
1786
+ def visit_TEXT(self, type_, **kw):
1787
+ return self._extend("TEXT", type_)
1788
+
1789
+ def visit_VARCHAR(self, type_, **kw):
1790
+ return self._extend("VARCHAR", type_, length=type_.length or "max")
1791
+
1792
+ def visit_CHAR(self, type_, **kw):
1793
+ return self._extend("CHAR", type_)
1794
+
1795
+ def visit_NCHAR(self, type_, **kw):
1796
+ return self._extend("NCHAR", type_)
1797
+
1798
+ def visit_NVARCHAR(self, type_, **kw):
1799
+ return self._extend("NVARCHAR", type_, length=type_.length or "max")
1800
+
1801
+ def visit_date(self, type_, **kw):
1802
+ if self.dialect.server_version_info < MS_2008_VERSION:
1803
+ return self.visit_DATETIME(type_, **kw)
1804
+ else:
1805
+ return self.visit_DATE(type_, **kw)
1806
+
1807
+ def visit__BASETIMEIMPL(self, type_, **kw):
1808
+ return self.visit_time(type_, **kw)
1809
+
1810
+ def visit_time(self, type_, **kw):
1811
+ if self.dialect.server_version_info < MS_2008_VERSION:
1812
+ return self.visit_DATETIME(type_, **kw)
1813
+ else:
1814
+ return self.visit_TIME(type_, **kw)
1815
+
1816
+ def visit_large_binary(self, type_, **kw):
1817
+ if self.dialect.deprecate_large_types:
1818
+ return self.visit_VARBINARY(type_, **kw)
1819
+ else:
1820
+ return self.visit_IMAGE(type_, **kw)
1821
+
1822
+ def visit_IMAGE(self, type_, **kw):
1823
+ return "IMAGE"
1824
+
1825
+ def visit_XML(self, type_, **kw):
1826
+ return "XML"
1827
+
1828
+ def visit_VARBINARY(self, type_, **kw):
1829
+ text = self._extend("VARBINARY", type_, length=type_.length or "max")
1830
+ if getattr(type_, "filestream", False):
1831
+ text += " FILESTREAM"
1832
+ return text
1833
+
1834
+ def visit_boolean(self, type_, **kw):
1835
+ return self.visit_BIT(type_)
1836
+
1837
+ def visit_BIT(self, type_, **kw):
1838
+ return "BIT"
1839
+
1840
+ def visit_JSON(self, type_, **kw):
1841
+ # this is a bit of a break with SQLAlchemy's convention of
1842
+ # "UPPERCASE name goes to UPPERCASE type name with no modification"
1843
+ return self._extend("NVARCHAR", type_, length="max")
1844
+
1845
+ def visit_MONEY(self, type_, **kw):
1846
+ return "MONEY"
1847
+
1848
+ def visit_SMALLMONEY(self, type_, **kw):
1849
+ return "SMALLMONEY"
1850
+
1851
+ def visit_uuid(self, type_, **kw):
1852
+ if type_.native_uuid:
1853
+ return self.visit_UNIQUEIDENTIFIER(type_, **kw)
1854
+ else:
1855
+ return super().visit_uuid(type_, **kw)
1856
+
1857
+ def visit_UNIQUEIDENTIFIER(self, type_, **kw):
1858
+ return "UNIQUEIDENTIFIER"
1859
+
1860
+ def visit_SQL_VARIANT(self, type_, **kw):
1861
+ return "SQL_VARIANT"
1862
+
1863
+
1864
+ class MSExecutionContext(default.DefaultExecutionContext):
1865
+ _enable_identity_insert = False
1866
+ _select_lastrowid = False
1867
+ _lastrowid = None
1868
+
1869
+ dialect: MSDialect
1870
+
1871
+ def _opt_encode(self, statement):
1872
+ if self.compiled and self.compiled.schema_translate_map:
1873
+ rst = self.compiled.preparer._render_schema_translates
1874
+ statement = rst(statement, self.compiled.schema_translate_map)
1875
+
1876
+ return statement
1877
+
1878
+ def pre_exec(self):
1879
+ """Activate IDENTITY_INSERT if needed."""
1880
+
1881
+ if self.isinsert:
1882
+ if TYPE_CHECKING:
1883
+ assert is_sql_compiler(self.compiled)
1884
+ assert isinstance(self.compiled.compile_state, DMLState)
1885
+ assert isinstance(
1886
+ self.compiled.compile_state.dml_table, TableClause
1887
+ )
1888
+
1889
+ tbl = self.compiled.compile_state.dml_table
1890
+ id_column = tbl._autoincrement_column
1891
+
1892
+ if id_column is not None and (
1893
+ not isinstance(id_column.default, Sequence)
1894
+ ):
1895
+ insert_has_identity = True
1896
+ compile_state = self.compiled.dml_compile_state
1897
+ self._enable_identity_insert = (
1898
+ id_column.key in self.compiled_parameters[0]
1899
+ ) or (
1900
+ compile_state._dict_parameters
1901
+ and (id_column.key in compile_state._insert_col_keys)
1902
+ )
1903
+
1904
+ else:
1905
+ insert_has_identity = False
1906
+ self._enable_identity_insert = False
1907
+
1908
+ self._select_lastrowid = (
1909
+ not self.compiled.inline
1910
+ and insert_has_identity
1911
+ and not self.compiled.effective_returning
1912
+ and not self._enable_identity_insert
1913
+ and not self.executemany
1914
+ )
1915
+
1916
+ if self._enable_identity_insert:
1917
+ self.root_connection._cursor_execute(
1918
+ self.cursor,
1919
+ self._opt_encode(
1920
+ "SET IDENTITY_INSERT %s ON"
1921
+ % self.identifier_preparer.format_table(tbl)
1922
+ ),
1923
+ (),
1924
+ self,
1925
+ )
1926
+
1927
+ def post_exec(self):
1928
+ """Disable IDENTITY_INSERT if enabled."""
1929
+
1930
+ conn = self.root_connection
1931
+
1932
+ if self.isinsert or self.isupdate or self.isdelete:
1933
+ self._rowcount = self.cursor.rowcount
1934
+
1935
+ if self._select_lastrowid:
1936
+ if self.dialect.use_scope_identity:
1937
+ conn._cursor_execute(
1938
+ self.cursor,
1939
+ "SELECT scope_identity() AS lastrowid",
1940
+ (),
1941
+ self,
1942
+ )
1943
+ else:
1944
+ conn._cursor_execute(
1945
+ self.cursor, "SELECT @@identity AS lastrowid", (), self
1946
+ )
1947
+ # fetchall() ensures the cursor is consumed without closing it
1948
+ row = self.cursor.fetchall()[0]
1949
+ self._lastrowid = int(row[0])
1950
+
1951
+ self.cursor_fetch_strategy = _cursor._NO_CURSOR_DML
1952
+ elif (
1953
+ self.compiled is not None
1954
+ and is_sql_compiler(self.compiled)
1955
+ and self.compiled.effective_returning
1956
+ ):
1957
+ self.cursor_fetch_strategy = (
1958
+ _cursor.FullyBufferedCursorFetchStrategy(
1959
+ self.cursor,
1960
+ self.cursor.description,
1961
+ self.cursor.fetchall(),
1962
+ )
1963
+ )
1964
+
1965
+ if self._enable_identity_insert:
1966
+ if TYPE_CHECKING:
1967
+ assert is_sql_compiler(self.compiled)
1968
+ assert isinstance(self.compiled.compile_state, DMLState)
1969
+ assert isinstance(
1970
+ self.compiled.compile_state.dml_table, TableClause
1971
+ )
1972
+ conn._cursor_execute(
1973
+ self.cursor,
1974
+ self._opt_encode(
1975
+ "SET IDENTITY_INSERT %s OFF"
1976
+ % self.identifier_preparer.format_table(
1977
+ self.compiled.compile_state.dml_table
1978
+ )
1979
+ ),
1980
+ (),
1981
+ self,
1982
+ )
1983
+
1984
+ def get_lastrowid(self):
1985
+ return self._lastrowid
1986
+
1987
+ def handle_dbapi_exception(self, e):
1988
+ if self._enable_identity_insert:
1989
+ try:
1990
+ self.cursor.execute(
1991
+ self._opt_encode(
1992
+ "SET IDENTITY_INSERT %s OFF"
1993
+ % self.identifier_preparer.format_table(
1994
+ self.compiled.compile_state.dml_table
1995
+ )
1996
+ )
1997
+ )
1998
+ except Exception:
1999
+ pass
2000
+
2001
+ def fire_sequence(self, seq, type_):
2002
+ return self._execute_scalar(
2003
+ (
2004
+ "SELECT NEXT VALUE FOR %s"
2005
+ % self.identifier_preparer.format_sequence(seq)
2006
+ ),
2007
+ type_,
2008
+ )
2009
+
2010
+ def get_insert_default(self, column):
2011
+ if (
2012
+ isinstance(column, sa_schema.Column)
2013
+ and column is column.table._autoincrement_column
2014
+ and isinstance(column.default, sa_schema.Sequence)
2015
+ and column.default.optional
2016
+ ):
2017
+ return None
2018
+ return super().get_insert_default(column)
2019
+
2020
+
2021
+ class MSSQLCompiler(compiler.SQLCompiler):
2022
+ returning_precedes_values = True
2023
+
2024
+ extract_map = util.update_copy(
2025
+ compiler.SQLCompiler.extract_map,
2026
+ {
2027
+ "doy": "dayofyear",
2028
+ "dow": "weekday",
2029
+ "milliseconds": "millisecond",
2030
+ "microseconds": "microsecond",
2031
+ },
2032
+ )
2033
+
2034
+ def __init__(self, *args, **kwargs):
2035
+ self.tablealiases = {}
2036
+ super().__init__(*args, **kwargs)
2037
+
2038
+ def _format_frame_clause(self, range_, **kw):
2039
+ kw["literal_execute"] = True
2040
+ return super()._format_frame_clause(range_, **kw)
2041
+
2042
+ def _with_legacy_schema_aliasing(fn):
2043
+ def decorate(self, *arg, **kw):
2044
+ if self.dialect.legacy_schema_aliasing:
2045
+ return fn(self, *arg, **kw)
2046
+ else:
2047
+ super_ = getattr(super(MSSQLCompiler, self), fn.__name__)
2048
+ return super_(*arg, **kw)
2049
+
2050
+ return decorate
2051
+
2052
+ def visit_now_func(self, fn, **kw):
2053
+ return "CURRENT_TIMESTAMP"
2054
+
2055
+ def visit_current_date_func(self, fn, **kw):
2056
+ return "GETDATE()"
2057
+
2058
+ def visit_length_func(self, fn, **kw):
2059
+ return "LEN%s" % self.function_argspec(fn, **kw)
2060
+
2061
+ def visit_char_length_func(self, fn, **kw):
2062
+ return "LEN%s" % self.function_argspec(fn, **kw)
2063
+
2064
+ def visit_aggregate_strings_func(self, fn, **kw):
2065
+ expr = fn.clauses.clauses[0]._compiler_dispatch(self, **kw)
2066
+ kw["literal_execute"] = True
2067
+ delimeter = fn.clauses.clauses[1]._compiler_dispatch(self, **kw)
2068
+ return f"string_agg({expr}, {delimeter})"
2069
+
2070
+ def visit_concat_op_expression_clauselist(
2071
+ self, clauselist, operator, **kw
2072
+ ):
2073
+ return " + ".join(self.process(elem, **kw) for elem in clauselist)
2074
+
2075
+ def visit_concat_op_binary(self, binary, operator, **kw):
2076
+ return "%s + %s" % (
2077
+ self.process(binary.left, **kw),
2078
+ self.process(binary.right, **kw),
2079
+ )
2080
+
2081
+ def visit_true(self, expr, **kw):
2082
+ return "1"
2083
+
2084
+ def visit_false(self, expr, **kw):
2085
+ return "0"
2086
+
2087
+ def visit_match_op_binary(self, binary, operator, **kw):
2088
+ return "CONTAINS (%s, %s)" % (
2089
+ self.process(binary.left, **kw),
2090
+ self.process(binary.right, **kw),
2091
+ )
2092
+
2093
+ def get_select_precolumns(self, select, **kw):
2094
+ """MS-SQL puts TOP, it's version of LIMIT here"""
2095
+
2096
+ s = super().get_select_precolumns(select, **kw)
2097
+
2098
+ if select._has_row_limiting_clause and self._use_top(select):
2099
+ # ODBC drivers and possibly others
2100
+ # don't support bind params in the SELECT clause on SQL Server.
2101
+ # so have to use literal here.
2102
+ kw["literal_execute"] = True
2103
+ s += "TOP %s " % self.process(
2104
+ self._get_limit_or_fetch(select), **kw
2105
+ )
2106
+ if select._fetch_clause is not None:
2107
+ if select._fetch_clause_options["percent"]:
2108
+ s += "PERCENT "
2109
+ if select._fetch_clause_options["with_ties"]:
2110
+ s += "WITH TIES "
2111
+
2112
+ return s
2113
+
2114
+ def get_from_hint_text(self, table, text):
2115
+ return text
2116
+
2117
+ def get_crud_hint_text(self, table, text):
2118
+ return text
2119
+
2120
+ def _get_limit_or_fetch(self, select):
2121
+ if select._fetch_clause is None:
2122
+ return select._limit_clause
2123
+ else:
2124
+ return select._fetch_clause
2125
+
2126
+ def _use_top(self, select):
2127
+ return (select._offset_clause is None) and (
2128
+ select._simple_int_clause(select._limit_clause)
2129
+ or (
2130
+ # limit can use TOP with is by itself. fetch only uses TOP
2131
+ # when it needs to because of PERCENT and/or WITH TIES
2132
+ # TODO: Why? shouldn't we use TOP always ?
2133
+ select._simple_int_clause(select._fetch_clause)
2134
+ and (
2135
+ select._fetch_clause_options["percent"]
2136
+ or select._fetch_clause_options["with_ties"]
2137
+ )
2138
+ )
2139
+ )
2140
+
2141
+ def limit_clause(self, cs, **kwargs):
2142
+ return ""
2143
+
2144
+ def _check_can_use_fetch_limit(self, select):
2145
+ # to use ROW_NUMBER(), an ORDER BY is required.
2146
+ # OFFSET are FETCH are options of the ORDER BY clause
2147
+ if not select._order_by_clause.clauses:
2148
+ raise exc.CompileError(
2149
+ "MSSQL requires an order_by when "
2150
+ "using an OFFSET or a non-simple "
2151
+ "LIMIT clause"
2152
+ )
2153
+
2154
+ if select._fetch_clause_options is not None and (
2155
+ select._fetch_clause_options["percent"]
2156
+ or select._fetch_clause_options["with_ties"]
2157
+ ):
2158
+ raise exc.CompileError(
2159
+ "MSSQL needs TOP to use PERCENT and/or WITH TIES. "
2160
+ "Only simple fetch without offset can be used."
2161
+ )
2162
+
2163
+ def _row_limit_clause(self, select, **kw):
2164
+ """MSSQL 2012 supports OFFSET/FETCH operators
2165
+ Use it instead subquery with row_number
2166
+
2167
+ """
2168
+
2169
+ if self.dialect._supports_offset_fetch and not self._use_top(select):
2170
+ self._check_can_use_fetch_limit(select)
2171
+
2172
+ return self.fetch_clause(
2173
+ select,
2174
+ fetch_clause=self._get_limit_or_fetch(select),
2175
+ require_offset=True,
2176
+ **kw,
2177
+ )
2178
+
2179
+ else:
2180
+ return ""
2181
+
2182
+ def visit_try_cast(self, element, **kw):
2183
+ return "TRY_CAST (%s AS %s)" % (
2184
+ self.process(element.clause, **kw),
2185
+ self.process(element.typeclause, **kw),
2186
+ )
2187
+
2188
+ def translate_select_structure(self, select_stmt, **kwargs):
2189
+ """Look for ``LIMIT`` and OFFSET in a select statement, and if
2190
+ so tries to wrap it in a subquery with ``row_number()`` criterion.
2191
+ MSSQL 2012 and above are excluded
2192
+
2193
+ """
2194
+ select = select_stmt
2195
+
2196
+ if (
2197
+ select._has_row_limiting_clause
2198
+ and not self.dialect._supports_offset_fetch
2199
+ and not self._use_top(select)
2200
+ and not getattr(select, "_mssql_visit", None)
2201
+ ):
2202
+ self._check_can_use_fetch_limit(select)
2203
+
2204
+ _order_by_clauses = [
2205
+ sql_util.unwrap_label_reference(elem)
2206
+ for elem in select._order_by_clause.clauses
2207
+ ]
2208
+
2209
+ limit_clause = self._get_limit_or_fetch(select)
2210
+ offset_clause = select._offset_clause
2211
+
2212
+ select = select._generate()
2213
+ select._mssql_visit = True
2214
+ select = (
2215
+ select.add_columns(
2216
+ sql.func.ROW_NUMBER()
2217
+ .over(order_by=_order_by_clauses)
2218
+ .label("mssql_rn")
2219
+ )
2220
+ .order_by(None)
2221
+ .alias()
2222
+ )
2223
+
2224
+ mssql_rn = sql.column("mssql_rn")
2225
+ limitselect = sql.select(
2226
+ *[c for c in select.c if c.key != "mssql_rn"]
2227
+ )
2228
+ if offset_clause is not None:
2229
+ limitselect = limitselect.where(mssql_rn > offset_clause)
2230
+ if limit_clause is not None:
2231
+ limitselect = limitselect.where(
2232
+ mssql_rn <= (limit_clause + offset_clause)
2233
+ )
2234
+ else:
2235
+ limitselect = limitselect.where(mssql_rn <= (limit_clause))
2236
+ return limitselect
2237
+ else:
2238
+ return select
2239
+
2240
+ @_with_legacy_schema_aliasing
2241
+ def visit_table(self, table, mssql_aliased=False, iscrud=False, **kwargs):
2242
+ if mssql_aliased is table or iscrud:
2243
+ return super().visit_table(table, **kwargs)
2244
+
2245
+ # alias schema-qualified tables
2246
+ alias = self._schema_aliased_table(table)
2247
+ if alias is not None:
2248
+ return self.process(alias, mssql_aliased=table, **kwargs)
2249
+ else:
2250
+ return super().visit_table(table, **kwargs)
2251
+
2252
+ @_with_legacy_schema_aliasing
2253
+ def visit_alias(self, alias, **kw):
2254
+ # translate for schema-qualified table aliases
2255
+ kw["mssql_aliased"] = alias.element
2256
+ return super().visit_alias(alias, **kw)
2257
+
2258
+ @_with_legacy_schema_aliasing
2259
+ def visit_column(self, column, add_to_result_map=None, **kw):
2260
+ if (
2261
+ column.table is not None
2262
+ and (not self.isupdate and not self.isdelete)
2263
+ or self.is_subquery()
2264
+ ):
2265
+ # translate for schema-qualified table aliases
2266
+ t = self._schema_aliased_table(column.table)
2267
+ if t is not None:
2268
+ converted = elements._corresponding_column_or_error(t, column)
2269
+ if add_to_result_map is not None:
2270
+ add_to_result_map(
2271
+ column.name,
2272
+ column.name,
2273
+ (column, column.name, column.key),
2274
+ column.type,
2275
+ )
2276
+
2277
+ return super().visit_column(converted, **kw)
2278
+
2279
+ return super().visit_column(
2280
+ column, add_to_result_map=add_to_result_map, **kw
2281
+ )
2282
+
2283
+ def _schema_aliased_table(self, table):
2284
+ if getattr(table, "schema", None) is not None:
2285
+ if table not in self.tablealiases:
2286
+ self.tablealiases[table] = table.alias()
2287
+ return self.tablealiases[table]
2288
+ else:
2289
+ return None
2290
+
2291
+ def visit_extract(self, extract, **kw):
2292
+ field = self.extract_map.get(extract.field, extract.field)
2293
+ return "DATEPART(%s, %s)" % (field, self.process(extract.expr, **kw))
2294
+
2295
+ def visit_savepoint(self, savepoint_stmt, **kw):
2296
+ return "SAVE TRANSACTION %s" % self.preparer.format_savepoint(
2297
+ savepoint_stmt
2298
+ )
2299
+
2300
+ def visit_rollback_to_savepoint(self, savepoint_stmt, **kw):
2301
+ return "ROLLBACK TRANSACTION %s" % self.preparer.format_savepoint(
2302
+ savepoint_stmt
2303
+ )
2304
+
2305
+ def visit_binary(self, binary, **kwargs):
2306
+ """Move bind parameters to the right-hand side of an operator, where
2307
+ possible.
2308
+
2309
+ """
2310
+ if (
2311
+ isinstance(binary.left, expression.BindParameter)
2312
+ and binary.operator == operator.eq
2313
+ and not isinstance(binary.right, expression.BindParameter)
2314
+ ):
2315
+ return self.process(
2316
+ expression.BinaryExpression(
2317
+ binary.right, binary.left, binary.operator
2318
+ ),
2319
+ **kwargs,
2320
+ )
2321
+ return super().visit_binary(binary, **kwargs)
2322
+
2323
+ def returning_clause(
2324
+ self, stmt, returning_cols, *, populate_result_map, **kw
2325
+ ):
2326
+ # SQL server returning clause requires that the columns refer to
2327
+ # the virtual table names "inserted" or "deleted". Here, we make
2328
+ # a simple alias of our table with that name, and then adapt the
2329
+ # columns we have from the list of RETURNING columns to that new name
2330
+ # so that they render as "inserted.<colname>" / "deleted.<colname>".
2331
+
2332
+ if stmt.is_insert or stmt.is_update:
2333
+ target = stmt.table.alias("inserted")
2334
+ elif stmt.is_delete:
2335
+ target = stmt.table.alias("deleted")
2336
+ else:
2337
+ assert False, "expected Insert, Update or Delete statement"
2338
+
2339
+ adapter = sql_util.ClauseAdapter(target)
2340
+
2341
+ # adapter.traverse() takes a column from our target table and returns
2342
+ # the one that is linked to the "inserted" / "deleted" tables. So in
2343
+ # order to retrieve these values back from the result (e.g. like
2344
+ # row[column]), tell the compiler to also add the original unadapted
2345
+ # column to the result map. Before #4877, these were (unknowingly)
2346
+ # falling back using string name matching in the result set which
2347
+ # necessarily used an expensive KeyError in order to match.
2348
+
2349
+ columns = [
2350
+ self._label_returning_column(
2351
+ stmt,
2352
+ adapter.traverse(column),
2353
+ populate_result_map,
2354
+ {"result_map_targets": (column,)},
2355
+ fallback_label_name=fallback_label_name,
2356
+ column_is_repeated=repeated,
2357
+ name=name,
2358
+ proxy_name=proxy_name,
2359
+ **kw,
2360
+ )
2361
+ for (
2362
+ name,
2363
+ proxy_name,
2364
+ fallback_label_name,
2365
+ column,
2366
+ repeated,
2367
+ ) in stmt._generate_columns_plus_names(
2368
+ True, cols=expression._select_iterables(returning_cols)
2369
+ )
2370
+ ]
2371
+
2372
+ return "OUTPUT " + ", ".join(columns)
2373
+
2374
+ def get_cte_preamble(self, recursive):
2375
+ # SQL Server finds it too inconvenient to accept
2376
+ # an entirely optional, SQL standard specified,
2377
+ # "RECURSIVE" word with their "WITH",
2378
+ # so here we go
2379
+ return "WITH"
2380
+
2381
+ def label_select_column(self, select, column, asfrom):
2382
+ if isinstance(column, expression.Function):
2383
+ return column.label(None)
2384
+ else:
2385
+ return super().label_select_column(select, column, asfrom)
2386
+
2387
+ def for_update_clause(self, select, **kw):
2388
+ # "FOR UPDATE" is only allowed on "DECLARE CURSOR" which
2389
+ # SQLAlchemy doesn't use
2390
+ return ""
2391
+
2392
+ def order_by_clause(self, select, **kw):
2393
+ # MSSQL only allows ORDER BY in subqueries if there is a LIMIT:
2394
+ # "The ORDER BY clause is invalid in views, inline functions,
2395
+ # derived tables, subqueries, and common table expressions,
2396
+ # unless TOP, OFFSET or FOR XML is also specified."
2397
+ if (
2398
+ self.is_subquery()
2399
+ and not self._use_top(select)
2400
+ and (
2401
+ select._offset is None
2402
+ or not self.dialect._supports_offset_fetch
2403
+ )
2404
+ ):
2405
+ # avoid processing the order by clause if we won't end up
2406
+ # using it, because we don't want all the bind params tacked
2407
+ # onto the positional list if that is what the dbapi requires
2408
+ return ""
2409
+
2410
+ order_by = self.process(select._order_by_clause, **kw)
2411
+
2412
+ if order_by:
2413
+ return " ORDER BY " + order_by
2414
+ else:
2415
+ return ""
2416
+
2417
+ def update_from_clause(
2418
+ self, update_stmt, from_table, extra_froms, from_hints, **kw
2419
+ ):
2420
+ """Render the UPDATE..FROM clause specific to MSSQL.
2421
+
2422
+ In MSSQL, if the UPDATE statement involves an alias of the table to
2423
+ be updated, then the table itself must be added to the FROM list as
2424
+ well. Otherwise, it is optional. Here, we add it regardless.
2425
+
2426
+ """
2427
+ return "FROM " + ", ".join(
2428
+ t._compiler_dispatch(self, asfrom=True, fromhints=from_hints, **kw)
2429
+ for t in [from_table] + extra_froms
2430
+ )
2431
+
2432
+ def delete_table_clause(self, delete_stmt, from_table, extra_froms, **kw):
2433
+ """If we have extra froms make sure we render any alias as hint."""
2434
+ ashint = False
2435
+ if extra_froms:
2436
+ ashint = True
2437
+ return from_table._compiler_dispatch(
2438
+ self, asfrom=True, iscrud=True, ashint=ashint, **kw
2439
+ )
2440
+
2441
+ def delete_extra_from_clause(
2442
+ self, delete_stmt, from_table, extra_froms, from_hints, **kw
2443
+ ):
2444
+ """Render the DELETE .. FROM clause specific to MSSQL.
2445
+
2446
+ Yes, it has the FROM keyword twice.
2447
+
2448
+ """
2449
+ return "FROM " + ", ".join(
2450
+ t._compiler_dispatch(self, asfrom=True, fromhints=from_hints, **kw)
2451
+ for t in [from_table] + extra_froms
2452
+ )
2453
+
2454
+ def visit_empty_set_expr(self, type_, **kw):
2455
+ return "SELECT 1 WHERE 1!=1"
2456
+
2457
+ def visit_is_distinct_from_binary(self, binary, operator, **kw):
2458
+ return "NOT EXISTS (SELECT %s INTERSECT SELECT %s)" % (
2459
+ self.process(binary.left),
2460
+ self.process(binary.right),
2461
+ )
2462
+
2463
+ def visit_is_not_distinct_from_binary(self, binary, operator, **kw):
2464
+ return "EXISTS (SELECT %s INTERSECT SELECT %s)" % (
2465
+ self.process(binary.left),
2466
+ self.process(binary.right),
2467
+ )
2468
+
2469
+ def _render_json_extract_from_binary(self, binary, operator, **kw):
2470
+ # note we are intentionally calling upon the process() calls in the
2471
+ # order in which they appear in the SQL String as this is used
2472
+ # by positional parameter rendering
2473
+
2474
+ if binary.type._type_affinity is sqltypes.JSON:
2475
+ return "JSON_QUERY(%s, %s)" % (
2476
+ self.process(binary.left, **kw),
2477
+ self.process(binary.right, **kw),
2478
+ )
2479
+
2480
+ # as with other dialects, start with an explicit test for NULL
2481
+ case_expression = "CASE JSON_VALUE(%s, %s) WHEN NULL THEN NULL" % (
2482
+ self.process(binary.left, **kw),
2483
+ self.process(binary.right, **kw),
2484
+ )
2485
+
2486
+ if binary.type._type_affinity is sqltypes.Integer:
2487
+ type_expression = "ELSE CAST(JSON_VALUE(%s, %s) AS INTEGER)" % (
2488
+ self.process(binary.left, **kw),
2489
+ self.process(binary.right, **kw),
2490
+ )
2491
+ elif binary.type._type_affinity is sqltypes.Numeric:
2492
+ type_expression = "ELSE CAST(JSON_VALUE(%s, %s) AS %s)" % (
2493
+ self.process(binary.left, **kw),
2494
+ self.process(binary.right, **kw),
2495
+ (
2496
+ "FLOAT"
2497
+ if isinstance(binary.type, sqltypes.Float)
2498
+ else "NUMERIC(%s, %s)"
2499
+ % (binary.type.precision, binary.type.scale)
2500
+ ),
2501
+ )
2502
+ elif binary.type._type_affinity is sqltypes.Boolean:
2503
+ # the NULL handling is particularly weird with boolean, so
2504
+ # explicitly return numeric (BIT) constants
2505
+ type_expression = (
2506
+ "WHEN 'true' THEN 1 WHEN 'false' THEN 0 ELSE NULL"
2507
+ )
2508
+ elif binary.type._type_affinity is sqltypes.String:
2509
+ # TODO: does this comment (from mysql) apply to here, too?
2510
+ # this fails with a JSON value that's a four byte unicode
2511
+ # string. SQLite has the same problem at the moment
2512
+ type_expression = "ELSE JSON_VALUE(%s, %s)" % (
2513
+ self.process(binary.left, **kw),
2514
+ self.process(binary.right, **kw),
2515
+ )
2516
+ else:
2517
+ # other affinity....this is not expected right now
2518
+ type_expression = "ELSE JSON_QUERY(%s, %s)" % (
2519
+ self.process(binary.left, **kw),
2520
+ self.process(binary.right, **kw),
2521
+ )
2522
+
2523
+ return case_expression + " " + type_expression + " END"
2524
+
2525
+ def visit_json_getitem_op_binary(self, binary, operator, **kw):
2526
+ return self._render_json_extract_from_binary(binary, operator, **kw)
2527
+
2528
+ def visit_json_path_getitem_op_binary(self, binary, operator, **kw):
2529
+ return self._render_json_extract_from_binary(binary, operator, **kw)
2530
+
2531
+ def visit_sequence(self, seq, **kw):
2532
+ return "NEXT VALUE FOR %s" % self.preparer.format_sequence(seq)
2533
+
2534
+
2535
+ class MSSQLStrictCompiler(MSSQLCompiler):
2536
+ """A subclass of MSSQLCompiler which disables the usage of bind
2537
+ parameters where not allowed natively by MS-SQL.
2538
+
2539
+ A dialect may use this compiler on a platform where native
2540
+ binds are used.
2541
+
2542
+ """
2543
+
2544
+ ansi_bind_rules = True
2545
+
2546
+ def visit_in_op_binary(self, binary, operator, **kw):
2547
+ kw["literal_execute"] = True
2548
+ return "%s IN %s" % (
2549
+ self.process(binary.left, **kw),
2550
+ self.process(binary.right, **kw),
2551
+ )
2552
+
2553
+ def visit_not_in_op_binary(self, binary, operator, **kw):
2554
+ kw["literal_execute"] = True
2555
+ return "%s NOT IN %s" % (
2556
+ self.process(binary.left, **kw),
2557
+ self.process(binary.right, **kw),
2558
+ )
2559
+
2560
+ def render_literal_value(self, value, type_):
2561
+ """
2562
+ For date and datetime values, convert to a string
2563
+ format acceptable to MSSQL. That seems to be the
2564
+ so-called ODBC canonical date format which looks
2565
+ like this:
2566
+
2567
+ yyyy-mm-dd hh:mi:ss.mmm(24h)
2568
+
2569
+ For other data types, call the base class implementation.
2570
+ """
2571
+ # datetime and date are both subclasses of datetime.date
2572
+ if issubclass(type(value), datetime.date):
2573
+ # SQL Server wants single quotes around the date string.
2574
+ return "'" + str(value) + "'"
2575
+ else:
2576
+ return super().render_literal_value(value, type_)
2577
+
2578
+
2579
+ class MSDDLCompiler(compiler.DDLCompiler):
2580
+ def get_column_specification(self, column, **kwargs):
2581
+ colspec = self.preparer.format_column(column)
2582
+
2583
+ # type is not accepted in a computed column
2584
+ if column.computed is not None:
2585
+ colspec += " " + self.process(column.computed)
2586
+ else:
2587
+ colspec += " " + self.dialect.type_compiler_instance.process(
2588
+ column.type, type_expression=column
2589
+ )
2590
+
2591
+ if column.nullable is not None:
2592
+ if (
2593
+ not column.nullable
2594
+ or column.primary_key
2595
+ or isinstance(column.default, sa_schema.Sequence)
2596
+ or column.autoincrement is True
2597
+ or column.identity
2598
+ ):
2599
+ colspec += " NOT NULL"
2600
+ elif column.computed is None:
2601
+ # don't specify "NULL" for computed columns
2602
+ colspec += " NULL"
2603
+
2604
+ if column.table is None:
2605
+ raise exc.CompileError(
2606
+ "mssql requires Table-bound columns "
2607
+ "in order to generate DDL"
2608
+ )
2609
+
2610
+ d_opt = column.dialect_options["mssql"]
2611
+ start = d_opt["identity_start"]
2612
+ increment = d_opt["identity_increment"]
2613
+ if start is not None or increment is not None:
2614
+ if column.identity:
2615
+ raise exc.CompileError(
2616
+ "Cannot specify options 'mssql_identity_start' and/or "
2617
+ "'mssql_identity_increment' while also using the "
2618
+ "'Identity' construct."
2619
+ )
2620
+ util.warn_deprecated(
2621
+ "The dialect options 'mssql_identity_start' and "
2622
+ "'mssql_identity_increment' are deprecated. "
2623
+ "Use the 'Identity' object instead.",
2624
+ "1.4",
2625
+ )
2626
+
2627
+ if column.identity:
2628
+ colspec += self.process(column.identity, **kwargs)
2629
+ elif (
2630
+ column is column.table._autoincrement_column
2631
+ or column.autoincrement is True
2632
+ ) and (
2633
+ not isinstance(column.default, Sequence) or column.default.optional
2634
+ ):
2635
+ colspec += self.process(Identity(start=start, increment=increment))
2636
+ else:
2637
+ default = self.get_column_default_string(column)
2638
+ if default is not None:
2639
+ colspec += " DEFAULT " + default
2640
+
2641
+ return colspec
2642
+
2643
+ def visit_create_index(self, create, include_schema=False, **kw):
2644
+ index = create.element
2645
+ self._verify_index_table(index)
2646
+ preparer = self.preparer
2647
+ text = "CREATE "
2648
+ if index.unique:
2649
+ text += "UNIQUE "
2650
+
2651
+ # handle clustering option
2652
+ clustered = index.dialect_options["mssql"]["clustered"]
2653
+ if clustered is not None:
2654
+ if clustered:
2655
+ text += "CLUSTERED "
2656
+ else:
2657
+ text += "NONCLUSTERED "
2658
+
2659
+ # handle columnstore option (has no negative value)
2660
+ columnstore = index.dialect_options["mssql"]["columnstore"]
2661
+ if columnstore:
2662
+ text += "COLUMNSTORE "
2663
+
2664
+ text += "INDEX %s ON %s" % (
2665
+ self._prepared_index_name(index, include_schema=include_schema),
2666
+ preparer.format_table(index.table),
2667
+ )
2668
+
2669
+ # in some case mssql allows indexes with no columns defined
2670
+ if len(index.expressions) > 0:
2671
+ text += " (%s)" % ", ".join(
2672
+ self.sql_compiler.process(
2673
+ expr, include_table=False, literal_binds=True
2674
+ )
2675
+ for expr in index.expressions
2676
+ )
2677
+
2678
+ # handle other included columns
2679
+ if index.dialect_options["mssql"]["include"]:
2680
+ inclusions = [
2681
+ index.table.c[col] if isinstance(col, str) else col
2682
+ for col in index.dialect_options["mssql"]["include"]
2683
+ ]
2684
+
2685
+ text += " INCLUDE (%s)" % ", ".join(
2686
+ [preparer.quote(c.name) for c in inclusions]
2687
+ )
2688
+
2689
+ whereclause = index.dialect_options["mssql"]["where"]
2690
+
2691
+ if whereclause is not None:
2692
+ whereclause = coercions.expect(
2693
+ roles.DDLExpressionRole, whereclause
2694
+ )
2695
+
2696
+ where_compiled = self.sql_compiler.process(
2697
+ whereclause, include_table=False, literal_binds=True
2698
+ )
2699
+ text += " WHERE " + where_compiled
2700
+
2701
+ return text
2702
+
2703
+ def visit_drop_index(self, drop, **kw):
2704
+ return "\nDROP INDEX %s ON %s" % (
2705
+ self._prepared_index_name(drop.element, include_schema=False),
2706
+ self.preparer.format_table(drop.element.table),
2707
+ )
2708
+
2709
+ def visit_primary_key_constraint(self, constraint, **kw):
2710
+ if len(constraint) == 0:
2711
+ return ""
2712
+ text = ""
2713
+ if constraint.name is not None:
2714
+ text += "CONSTRAINT %s " % self.preparer.format_constraint(
2715
+ constraint
2716
+ )
2717
+ text += "PRIMARY KEY "
2718
+
2719
+ clustered = constraint.dialect_options["mssql"]["clustered"]
2720
+ if clustered is not None:
2721
+ if clustered:
2722
+ text += "CLUSTERED "
2723
+ else:
2724
+ text += "NONCLUSTERED "
2725
+
2726
+ text += "(%s)" % ", ".join(
2727
+ self.preparer.quote(c.name) for c in constraint
2728
+ )
2729
+ text += self.define_constraint_deferrability(constraint)
2730
+ return text
2731
+
2732
+ def visit_unique_constraint(self, constraint, **kw):
2733
+ if len(constraint) == 0:
2734
+ return ""
2735
+ text = ""
2736
+ if constraint.name is not None:
2737
+ formatted_name = self.preparer.format_constraint(constraint)
2738
+ if formatted_name is not None:
2739
+ text += "CONSTRAINT %s " % formatted_name
2740
+ text += "UNIQUE %s" % self.define_unique_constraint_distinct(
2741
+ constraint, **kw
2742
+ )
2743
+ clustered = constraint.dialect_options["mssql"]["clustered"]
2744
+ if clustered is not None:
2745
+ if clustered:
2746
+ text += "CLUSTERED "
2747
+ else:
2748
+ text += "NONCLUSTERED "
2749
+
2750
+ text += "(%s)" % ", ".join(
2751
+ self.preparer.quote(c.name) for c in constraint
2752
+ )
2753
+ text += self.define_constraint_deferrability(constraint)
2754
+ return text
2755
+
2756
+ def visit_computed_column(self, generated, **kw):
2757
+ text = "AS (%s)" % self.sql_compiler.process(
2758
+ generated.sqltext, include_table=False, literal_binds=True
2759
+ )
2760
+ # explicitly check for True|False since None means server default
2761
+ if generated.persisted is True:
2762
+ text += " PERSISTED"
2763
+ return text
2764
+
2765
+ def visit_set_table_comment(self, create, **kw):
2766
+ schema = self.preparer.schema_for_object(create.element)
2767
+ schema_name = schema if schema else self.dialect.default_schema_name
2768
+ return (
2769
+ "execute sp_addextendedproperty 'MS_Description', "
2770
+ "{}, 'schema', {}, 'table', {}".format(
2771
+ self.sql_compiler.render_literal_value(
2772
+ create.element.comment, sqltypes.NVARCHAR()
2773
+ ),
2774
+ self.preparer.quote_schema(schema_name),
2775
+ self.preparer.format_table(create.element, use_schema=False),
2776
+ )
2777
+ )
2778
+
2779
+ def visit_drop_table_comment(self, drop, **kw):
2780
+ schema = self.preparer.schema_for_object(drop.element)
2781
+ schema_name = schema if schema else self.dialect.default_schema_name
2782
+ return (
2783
+ "execute sp_dropextendedproperty 'MS_Description', 'schema', "
2784
+ "{}, 'table', {}".format(
2785
+ self.preparer.quote_schema(schema_name),
2786
+ self.preparer.format_table(drop.element, use_schema=False),
2787
+ )
2788
+ )
2789
+
2790
+ def visit_set_column_comment(self, create, **kw):
2791
+ schema = self.preparer.schema_for_object(create.element.table)
2792
+ schema_name = schema if schema else self.dialect.default_schema_name
2793
+ return (
2794
+ "execute sp_addextendedproperty 'MS_Description', "
2795
+ "{}, 'schema', {}, 'table', {}, 'column', {}".format(
2796
+ self.sql_compiler.render_literal_value(
2797
+ create.element.comment, sqltypes.NVARCHAR()
2798
+ ),
2799
+ self.preparer.quote_schema(schema_name),
2800
+ self.preparer.format_table(
2801
+ create.element.table, use_schema=False
2802
+ ),
2803
+ self.preparer.format_column(create.element),
2804
+ )
2805
+ )
2806
+
2807
+ def visit_drop_column_comment(self, drop, **kw):
2808
+ schema = self.preparer.schema_for_object(drop.element.table)
2809
+ schema_name = schema if schema else self.dialect.default_schema_name
2810
+ return (
2811
+ "execute sp_dropextendedproperty 'MS_Description', 'schema', "
2812
+ "{}, 'table', {}, 'column', {}".format(
2813
+ self.preparer.quote_schema(schema_name),
2814
+ self.preparer.format_table(
2815
+ drop.element.table, use_schema=False
2816
+ ),
2817
+ self.preparer.format_column(drop.element),
2818
+ )
2819
+ )
2820
+
2821
+ def visit_create_sequence(self, create, **kw):
2822
+ prefix = None
2823
+ if create.element.data_type is not None:
2824
+ data_type = create.element.data_type
2825
+ prefix = " AS %s" % self.type_compiler.process(data_type)
2826
+ return super().visit_create_sequence(create, prefix=prefix, **kw)
2827
+
2828
+ def visit_identity_column(self, identity, **kw):
2829
+ text = " IDENTITY"
2830
+ if identity.start is not None or identity.increment is not None:
2831
+ start = 1 if identity.start is None else identity.start
2832
+ increment = 1 if identity.increment is None else identity.increment
2833
+ text += "(%s,%s)" % (start, increment)
2834
+ return text
2835
+
2836
+
2837
+ class MSIdentifierPreparer(compiler.IdentifierPreparer):
2838
+ reserved_words = RESERVED_WORDS
2839
+
2840
+ def __init__(self, dialect):
2841
+ super().__init__(
2842
+ dialect,
2843
+ initial_quote="[",
2844
+ final_quote="]",
2845
+ quote_case_sensitive_collations=False,
2846
+ )
2847
+
2848
+ def _escape_identifier(self, value):
2849
+ return value.replace("]", "]]")
2850
+
2851
+ def _unescape_identifier(self, value):
2852
+ return value.replace("]]", "]")
2853
+
2854
+ def quote_schema(self, schema, force=None):
2855
+ """Prepare a quoted table and schema name."""
2856
+
2857
+ # need to re-implement the deprecation warning entirely
2858
+ if force is not None:
2859
+ # not using the util.deprecated_params() decorator in this
2860
+ # case because of the additional function call overhead on this
2861
+ # very performance-critical spot.
2862
+ util.warn_deprecated(
2863
+ "The IdentifierPreparer.quote_schema.force parameter is "
2864
+ "deprecated and will be removed in a future release. This "
2865
+ "flag has no effect on the behavior of the "
2866
+ "IdentifierPreparer.quote method; please refer to "
2867
+ "quoted_name().",
2868
+ version="1.3",
2869
+ )
2870
+
2871
+ dbname, owner = _schema_elements(schema)
2872
+ if dbname:
2873
+ result = "%s.%s" % (self.quote(dbname), self.quote(owner))
2874
+ elif owner:
2875
+ result = self.quote(owner)
2876
+ else:
2877
+ result = ""
2878
+ return result
2879
+
2880
+
2881
+ def _db_plus_owner_listing(fn):
2882
+ def wrap(dialect, connection, schema=None, **kw):
2883
+ dbname, owner = _owner_plus_db(dialect, schema)
2884
+ return _switch_db(
2885
+ dbname,
2886
+ connection,
2887
+ fn,
2888
+ dialect,
2889
+ connection,
2890
+ dbname,
2891
+ owner,
2892
+ schema,
2893
+ **kw,
2894
+ )
2895
+
2896
+ return update_wrapper(wrap, fn)
2897
+
2898
+
2899
+ def _db_plus_owner(fn):
2900
+ def wrap(dialect, connection, tablename, schema=None, **kw):
2901
+ dbname, owner = _owner_plus_db(dialect, schema)
2902
+ return _switch_db(
2903
+ dbname,
2904
+ connection,
2905
+ fn,
2906
+ dialect,
2907
+ connection,
2908
+ tablename,
2909
+ dbname,
2910
+ owner,
2911
+ schema,
2912
+ **kw,
2913
+ )
2914
+
2915
+ return update_wrapper(wrap, fn)
2916
+
2917
+
2918
+ def _switch_db(dbname, connection, fn, *arg, **kw):
2919
+ if dbname:
2920
+ current_db = connection.exec_driver_sql("select db_name()").scalar()
2921
+ if current_db != dbname:
2922
+ connection.exec_driver_sql(
2923
+ "use %s" % connection.dialect.identifier_preparer.quote(dbname)
2924
+ )
2925
+ try:
2926
+ return fn(*arg, **kw)
2927
+ finally:
2928
+ if dbname and current_db != dbname:
2929
+ connection.exec_driver_sql(
2930
+ "use %s"
2931
+ % connection.dialect.identifier_preparer.quote(current_db)
2932
+ )
2933
+
2934
+
2935
+ def _owner_plus_db(dialect, schema):
2936
+ if not schema:
2937
+ return None, dialect.default_schema_name
2938
+ else:
2939
+ return _schema_elements(schema)
2940
+
2941
+
2942
+ _memoized_schema = util.LRUCache()
2943
+
2944
+
2945
+ def _schema_elements(schema):
2946
+ if isinstance(schema, quoted_name) and schema.quote:
2947
+ return None, schema
2948
+
2949
+ if schema in _memoized_schema:
2950
+ return _memoized_schema[schema]
2951
+
2952
+ # tests for this function are in:
2953
+ # test/dialect/mssql/test_reflection.py ->
2954
+ # OwnerPlusDBTest.test_owner_database_pairs
2955
+ # test/dialect/mssql/test_compiler.py -> test_force_schema_*
2956
+ # test/dialect/mssql/test_compiler.py -> test_schema_many_tokens_*
2957
+ #
2958
+
2959
+ if schema.startswith("__[SCHEMA_"):
2960
+ return None, schema
2961
+
2962
+ push = []
2963
+ symbol = ""
2964
+ bracket = False
2965
+ has_brackets = False
2966
+ for token in re.split(r"(\[|\]|\.)", schema):
2967
+ if not token:
2968
+ continue
2969
+ if token == "[":
2970
+ bracket = True
2971
+ has_brackets = True
2972
+ elif token == "]":
2973
+ bracket = False
2974
+ elif not bracket and token == ".":
2975
+ if has_brackets:
2976
+ push.append("[%s]" % symbol)
2977
+ else:
2978
+ push.append(symbol)
2979
+ symbol = ""
2980
+ has_brackets = False
2981
+ else:
2982
+ symbol += token
2983
+ if symbol:
2984
+ push.append(symbol)
2985
+ if len(push) > 1:
2986
+ dbname, owner = ".".join(push[0:-1]), push[-1]
2987
+
2988
+ # test for internal brackets
2989
+ if re.match(r".*\].*\[.*", dbname[1:-1]):
2990
+ dbname = quoted_name(dbname, quote=False)
2991
+ else:
2992
+ dbname = dbname.lstrip("[").rstrip("]")
2993
+
2994
+ elif len(push):
2995
+ dbname, owner = None, push[0]
2996
+ else:
2997
+ dbname, owner = None, None
2998
+
2999
+ _memoized_schema[schema] = dbname, owner
3000
+ return dbname, owner
3001
+
3002
+
3003
+ class MSDialect(default.DefaultDialect):
3004
+ # will assume it's at least mssql2005
3005
+ name = "mssql"
3006
+ supports_statement_cache = True
3007
+ supports_default_values = True
3008
+ supports_empty_insert = False
3009
+ favor_returning_over_lastrowid = True
3010
+
3011
+ returns_native_bytes = True
3012
+
3013
+ supports_comments = True
3014
+ supports_default_metavalue = False
3015
+ """dialect supports INSERT... VALUES (DEFAULT) syntax -
3016
+ SQL Server **does** support this, but **not** for the IDENTITY column,
3017
+ so we can't turn this on.
3018
+
3019
+ """
3020
+
3021
+ # supports_native_uuid is partial here, so we implement our
3022
+ # own impl type
3023
+
3024
+ execution_ctx_cls = MSExecutionContext
3025
+ use_scope_identity = True
3026
+ max_identifier_length = 128
3027
+ schema_name = "dbo"
3028
+
3029
+ insert_returning = True
3030
+ update_returning = True
3031
+ delete_returning = True
3032
+ update_returning_multifrom = True
3033
+ delete_returning_multifrom = True
3034
+
3035
+ colspecs = {
3036
+ sqltypes.DateTime: _MSDateTime,
3037
+ sqltypes.Date: _MSDate,
3038
+ sqltypes.JSON: JSON,
3039
+ sqltypes.JSON.JSONIndexType: JSONIndexType,
3040
+ sqltypes.JSON.JSONPathType: JSONPathType,
3041
+ sqltypes.Time: _BASETIMEIMPL,
3042
+ sqltypes.Unicode: _MSUnicode,
3043
+ sqltypes.UnicodeText: _MSUnicodeText,
3044
+ DATETIMEOFFSET: DATETIMEOFFSET,
3045
+ DATETIME2: DATETIME2,
3046
+ SMALLDATETIME: SMALLDATETIME,
3047
+ DATETIME: DATETIME,
3048
+ sqltypes.Uuid: MSUUid,
3049
+ }
3050
+
3051
+ engine_config_types = default.DefaultDialect.engine_config_types.union(
3052
+ {"legacy_schema_aliasing": util.asbool}
3053
+ )
3054
+
3055
+ ischema_names = ischema_names
3056
+
3057
+ supports_sequences = True
3058
+ sequences_optional = True
3059
+ # This is actually used for autoincrement, where itentity is used that
3060
+ # starts with 1.
3061
+ # for sequences T-SQL's actual default is -9223372036854775808
3062
+ default_sequence_base = 1
3063
+
3064
+ supports_native_boolean = False
3065
+ non_native_boolean_check_constraint = False
3066
+ supports_unicode_binds = True
3067
+ postfetch_lastrowid = True
3068
+
3069
+ # may be changed at server inspection time for older SQL server versions
3070
+ supports_multivalues_insert = True
3071
+
3072
+ use_insertmanyvalues = True
3073
+
3074
+ # note pyodbc will set this to False if fast_executemany is set,
3075
+ # as of SQLAlchemy 2.0.9
3076
+ use_insertmanyvalues_wo_returning = True
3077
+
3078
+ insertmanyvalues_implicit_sentinel = (
3079
+ InsertmanyvaluesSentinelOpts.AUTOINCREMENT
3080
+ | InsertmanyvaluesSentinelOpts.IDENTITY
3081
+ | InsertmanyvaluesSentinelOpts.USE_INSERT_FROM_SELECT
3082
+ )
3083
+
3084
+ # "The incoming request has too many parameters. The server supports a "
3085
+ # "maximum of 2100 parameters."
3086
+ # in fact you can have 2099 parameters.
3087
+ insertmanyvalues_max_parameters = 2099
3088
+
3089
+ _supports_offset_fetch = False
3090
+ _supports_nvarchar_max = False
3091
+
3092
+ legacy_schema_aliasing = False
3093
+
3094
+ server_version_info = ()
3095
+
3096
+ statement_compiler = MSSQLCompiler
3097
+ ddl_compiler = MSDDLCompiler
3098
+ type_compiler_cls = MSTypeCompiler
3099
+ preparer = MSIdentifierPreparer
3100
+
3101
+ construct_arguments = [
3102
+ (sa_schema.PrimaryKeyConstraint, {"clustered": None}),
3103
+ (sa_schema.UniqueConstraint, {"clustered": None}),
3104
+ (
3105
+ sa_schema.Index,
3106
+ {
3107
+ "clustered": None,
3108
+ "include": None,
3109
+ "where": None,
3110
+ "columnstore": None,
3111
+ },
3112
+ ),
3113
+ (
3114
+ sa_schema.Column,
3115
+ {"identity_start": None, "identity_increment": None},
3116
+ ),
3117
+ ]
3118
+
3119
+ def __init__(
3120
+ self,
3121
+ query_timeout=None,
3122
+ use_scope_identity=True,
3123
+ schema_name="dbo",
3124
+ deprecate_large_types=None,
3125
+ supports_comments=None,
3126
+ json_serializer=None,
3127
+ json_deserializer=None,
3128
+ legacy_schema_aliasing=None,
3129
+ ignore_no_transaction_on_rollback=False,
3130
+ **opts,
3131
+ ):
3132
+ self.query_timeout = int(query_timeout or 0)
3133
+ self.schema_name = schema_name
3134
+
3135
+ self.use_scope_identity = use_scope_identity
3136
+ self.deprecate_large_types = deprecate_large_types
3137
+ self.ignore_no_transaction_on_rollback = (
3138
+ ignore_no_transaction_on_rollback
3139
+ )
3140
+ self._user_defined_supports_comments = uds = supports_comments
3141
+ if uds is not None:
3142
+ self.supports_comments = uds
3143
+
3144
+ if legacy_schema_aliasing is not None:
3145
+ util.warn_deprecated(
3146
+ "The legacy_schema_aliasing parameter is "
3147
+ "deprecated and will be removed in a future release.",
3148
+ "1.4",
3149
+ )
3150
+ self.legacy_schema_aliasing = legacy_schema_aliasing
3151
+
3152
+ super().__init__(**opts)
3153
+
3154
+ self._json_serializer = json_serializer
3155
+ self._json_deserializer = json_deserializer
3156
+
3157
+ def do_savepoint(self, connection, name):
3158
+ # give the DBAPI a push
3159
+ connection.exec_driver_sql("IF @@TRANCOUNT = 0 BEGIN TRANSACTION")
3160
+ super().do_savepoint(connection, name)
3161
+
3162
+ def do_release_savepoint(self, connection, name):
3163
+ # SQL Server does not support RELEASE SAVEPOINT
3164
+ pass
3165
+
3166
+ def do_rollback(self, dbapi_connection):
3167
+ try:
3168
+ super().do_rollback(dbapi_connection)
3169
+ except self.dbapi.ProgrammingError as e:
3170
+ if self.ignore_no_transaction_on_rollback and re.match(
3171
+ r".*\b111214\b", str(e)
3172
+ ):
3173
+ util.warn(
3174
+ "ProgrammingError 111214 "
3175
+ "'No corresponding transaction found.' "
3176
+ "has been suppressed via "
3177
+ "ignore_no_transaction_on_rollback=True"
3178
+ )
3179
+ else:
3180
+ raise
3181
+
3182
+ _isolation_lookup = {
3183
+ "SERIALIZABLE",
3184
+ "READ UNCOMMITTED",
3185
+ "READ COMMITTED",
3186
+ "REPEATABLE READ",
3187
+ "SNAPSHOT",
3188
+ }
3189
+
3190
+ def get_isolation_level_values(self, dbapi_connection):
3191
+ return list(self._isolation_lookup)
3192
+
3193
+ def set_isolation_level(self, dbapi_connection, level):
3194
+ cursor = dbapi_connection.cursor()
3195
+ cursor.execute(f"SET TRANSACTION ISOLATION LEVEL {level}")
3196
+ cursor.close()
3197
+ if level == "SNAPSHOT":
3198
+ dbapi_connection.commit()
3199
+
3200
+ def get_isolation_level(self, dbapi_connection):
3201
+ cursor = dbapi_connection.cursor()
3202
+ view_name = "sys.system_views"
3203
+ try:
3204
+ cursor.execute(
3205
+ (
3206
+ "SELECT name FROM {} WHERE name IN "
3207
+ "('dm_exec_sessions', 'dm_pdw_nodes_exec_sessions')"
3208
+ ).format(view_name)
3209
+ )
3210
+ row = cursor.fetchone()
3211
+ if not row:
3212
+ raise NotImplementedError(
3213
+ "Can't fetch isolation level on this particular "
3214
+ "SQL Server version."
3215
+ )
3216
+
3217
+ view_name = f"sys.{row[0]}"
3218
+
3219
+ cursor.execute(
3220
+ """
3221
+ SELECT CASE transaction_isolation_level
3222
+ WHEN 0 THEN NULL
3223
+ WHEN 1 THEN 'READ UNCOMMITTED'
3224
+ WHEN 2 THEN 'READ COMMITTED'
3225
+ WHEN 3 THEN 'REPEATABLE READ'
3226
+ WHEN 4 THEN 'SERIALIZABLE'
3227
+ WHEN 5 THEN 'SNAPSHOT' END
3228
+ AS TRANSACTION_ISOLATION_LEVEL
3229
+ FROM {}
3230
+ where session_id = @@SPID
3231
+ """.format(
3232
+ view_name
3233
+ )
3234
+ )
3235
+ except self.dbapi.Error as err:
3236
+ raise NotImplementedError(
3237
+ "Can't fetch isolation level; encountered error {} when "
3238
+ 'attempting to query the "{}" view.'.format(err, view_name)
3239
+ ) from err
3240
+ else:
3241
+ row = cursor.fetchone()
3242
+ return row[0].upper()
3243
+ finally:
3244
+ cursor.close()
3245
+
3246
+ def initialize(self, connection):
3247
+ super().initialize(connection)
3248
+ self._setup_version_attributes()
3249
+ self._setup_supports_nvarchar_max(connection)
3250
+ self._setup_supports_comments(connection)
3251
+
3252
+ def _setup_version_attributes(self):
3253
+ if self.server_version_info[0] not in list(range(8, 17)):
3254
+ util.warn(
3255
+ "Unrecognized server version info '%s'. Some SQL Server "
3256
+ "features may not function properly."
3257
+ % ".".join(str(x) for x in self.server_version_info)
3258
+ )
3259
+
3260
+ if self.server_version_info >= MS_2008_VERSION:
3261
+ self.supports_multivalues_insert = True
3262
+ else:
3263
+ self.supports_multivalues_insert = False
3264
+
3265
+ if self.deprecate_large_types is None:
3266
+ self.deprecate_large_types = (
3267
+ self.server_version_info >= MS_2012_VERSION
3268
+ )
3269
+
3270
+ self._supports_offset_fetch = (
3271
+ self.server_version_info and self.server_version_info[0] >= 11
3272
+ )
3273
+
3274
+ def _setup_supports_nvarchar_max(self, connection):
3275
+ try:
3276
+ connection.scalar(
3277
+ sql.text("SELECT CAST('test max support' AS NVARCHAR(max))")
3278
+ )
3279
+ except exc.DBAPIError:
3280
+ self._supports_nvarchar_max = False
3281
+ else:
3282
+ self._supports_nvarchar_max = True
3283
+
3284
+ def _setup_supports_comments(self, connection):
3285
+ if self._user_defined_supports_comments is not None:
3286
+ return
3287
+
3288
+ try:
3289
+ connection.scalar(
3290
+ sql.text(
3291
+ "SELECT 1 FROM fn_listextendedproperty"
3292
+ "(default, default, default, default, "
3293
+ "default, default, default)"
3294
+ )
3295
+ )
3296
+ except exc.DBAPIError:
3297
+ self.supports_comments = False
3298
+ else:
3299
+ self.supports_comments = True
3300
+
3301
+ def _get_default_schema_name(self, connection):
3302
+ query = sql.text("SELECT schema_name()")
3303
+ default_schema_name = connection.scalar(query)
3304
+ if default_schema_name is not None:
3305
+ # guard against the case where the default_schema_name is being
3306
+ # fed back into a table reflection function.
3307
+ return quoted_name(default_schema_name, quote=True)
3308
+ else:
3309
+ return self.schema_name
3310
+
3311
+ @_db_plus_owner
3312
+ def has_table(self, connection, tablename, dbname, owner, schema, **kw):
3313
+ self._ensure_has_table_connection(connection)
3314
+
3315
+ return self._internal_has_table(connection, tablename, owner, **kw)
3316
+
3317
+ @reflection.cache
3318
+ @_db_plus_owner
3319
+ def has_sequence(
3320
+ self, connection, sequencename, dbname, owner, schema, **kw
3321
+ ):
3322
+ sequences = ischema.sequences
3323
+
3324
+ s = sql.select(sequences.c.sequence_name).where(
3325
+ sequences.c.sequence_name == sequencename
3326
+ )
3327
+
3328
+ if owner:
3329
+ s = s.where(sequences.c.sequence_schema == owner)
3330
+
3331
+ c = connection.execute(s)
3332
+
3333
+ return c.first() is not None
3334
+
3335
+ @reflection.cache
3336
+ @_db_plus_owner_listing
3337
+ def get_sequence_names(self, connection, dbname, owner, schema, **kw):
3338
+ sequences = ischema.sequences
3339
+
3340
+ s = sql.select(sequences.c.sequence_name)
3341
+ if owner:
3342
+ s = s.where(sequences.c.sequence_schema == owner)
3343
+
3344
+ c = connection.execute(s)
3345
+
3346
+ return [row[0] for row in c]
3347
+
3348
+ @reflection.cache
3349
+ def get_schema_names(self, connection, **kw):
3350
+ s = sql.select(ischema.schemata.c.schema_name).order_by(
3351
+ ischema.schemata.c.schema_name
3352
+ )
3353
+ schema_names = [r[0] for r in connection.execute(s)]
3354
+ return schema_names
3355
+
3356
+ @reflection.cache
3357
+ @_db_plus_owner_listing
3358
+ def get_table_names(self, connection, dbname, owner, schema, **kw):
3359
+ tables = ischema.tables
3360
+ s = (
3361
+ sql.select(tables.c.table_name)
3362
+ .where(
3363
+ sql.and_(
3364
+ tables.c.table_schema == owner,
3365
+ tables.c.table_type == "BASE TABLE",
3366
+ )
3367
+ )
3368
+ .order_by(tables.c.table_name)
3369
+ )
3370
+ table_names = [r[0] for r in connection.execute(s)]
3371
+ return table_names
3372
+
3373
+ @reflection.cache
3374
+ @_db_plus_owner_listing
3375
+ def get_view_names(self, connection, dbname, owner, schema, **kw):
3376
+ tables = ischema.tables
3377
+ s = (
3378
+ sql.select(tables.c.table_name)
3379
+ .where(
3380
+ sql.and_(
3381
+ tables.c.table_schema == owner,
3382
+ tables.c.table_type == "VIEW",
3383
+ )
3384
+ )
3385
+ .order_by(tables.c.table_name)
3386
+ )
3387
+ view_names = [r[0] for r in connection.execute(s)]
3388
+ return view_names
3389
+
3390
+ @reflection.cache
3391
+ def _internal_has_table(self, connection, tablename, owner, **kw):
3392
+ if tablename.startswith("#"): # temporary table
3393
+ # mssql does not support temporary views
3394
+ # SQL Error [4103] [S0001]: "#v": Temporary views are not allowed
3395
+ return bool(
3396
+ connection.scalar(
3397
+ # U filters on user tables only.
3398
+ text("SELECT object_id(:table_name, 'U')"),
3399
+ {"table_name": f"tempdb.dbo.[{tablename}]"},
3400
+ )
3401
+ )
3402
+ else:
3403
+ tables = ischema.tables
3404
+
3405
+ s = sql.select(tables.c.table_name).where(
3406
+ sql.and_(
3407
+ sql.or_(
3408
+ tables.c.table_type == "BASE TABLE",
3409
+ tables.c.table_type == "VIEW",
3410
+ ),
3411
+ tables.c.table_name == tablename,
3412
+ )
3413
+ )
3414
+
3415
+ if owner:
3416
+ s = s.where(tables.c.table_schema == owner)
3417
+
3418
+ c = connection.execute(s)
3419
+
3420
+ return c.first() is not None
3421
+
3422
+ def _default_or_error(self, connection, tablename, owner, method, **kw):
3423
+ # TODO: try to avoid having to run a separate query here
3424
+ if self._internal_has_table(connection, tablename, owner, **kw):
3425
+ return method()
3426
+ else:
3427
+ raise exc.NoSuchTableError(f"{owner}.{tablename}")
3428
+
3429
+ @reflection.cache
3430
+ @_db_plus_owner
3431
+ def get_indexes(self, connection, tablename, dbname, owner, schema, **kw):
3432
+ filter_definition = (
3433
+ "ind.filter_definition"
3434
+ if self.server_version_info >= MS_2008_VERSION
3435
+ else "NULL as filter_definition"
3436
+ )
3437
+ rp = connection.execution_options(future_result=True).execute(
3438
+ sql.text(
3439
+ f"""
3440
+ select
3441
+ ind.index_id,
3442
+ ind.is_unique,
3443
+ ind.name,
3444
+ ind.type,
3445
+ {filter_definition}
3446
+ from
3447
+ sys.indexes as ind
3448
+ join sys.tables as tab on
3449
+ ind.object_id = tab.object_id
3450
+ join sys.schemas as sch on
3451
+ sch.schema_id = tab.schema_id
3452
+ where
3453
+ tab.name = :tabname
3454
+ and sch.name = :schname
3455
+ and ind.is_primary_key = 0
3456
+ and ind.type != 0
3457
+ order by
3458
+ ind.name
3459
+ """
3460
+ )
3461
+ .bindparams(
3462
+ sql.bindparam("tabname", tablename, ischema.CoerceUnicode()),
3463
+ sql.bindparam("schname", owner, ischema.CoerceUnicode()),
3464
+ )
3465
+ .columns(name=sqltypes.Unicode())
3466
+ )
3467
+ indexes = {}
3468
+ for row in rp.mappings():
3469
+ indexes[row["index_id"]] = current = {
3470
+ "name": row["name"],
3471
+ "unique": row["is_unique"] == 1,
3472
+ "column_names": [],
3473
+ "include_columns": [],
3474
+ "dialect_options": {},
3475
+ }
3476
+
3477
+ do = current["dialect_options"]
3478
+ index_type = row["type"]
3479
+ if index_type in {1, 2}:
3480
+ do["mssql_clustered"] = index_type == 1
3481
+ if index_type in {5, 6}:
3482
+ do["mssql_clustered"] = index_type == 5
3483
+ do["mssql_columnstore"] = True
3484
+ if row["filter_definition"] is not None:
3485
+ do["mssql_where"] = row["filter_definition"]
3486
+
3487
+ rp = connection.execution_options(future_result=True).execute(
3488
+ sql.text(
3489
+ """
3490
+ select
3491
+ ind_col.index_id,
3492
+ col.name,
3493
+ ind_col.is_included_column
3494
+ from
3495
+ sys.columns as col
3496
+ join sys.tables as tab on
3497
+ tab.object_id = col.object_id
3498
+ join sys.index_columns as ind_col on
3499
+ ind_col.column_id = col.column_id
3500
+ and ind_col.object_id = tab.object_id
3501
+ join sys.schemas as sch on
3502
+ sch.schema_id = tab.schema_id
3503
+ where
3504
+ tab.name = :tabname
3505
+ and sch.name = :schname
3506
+ order by
3507
+ ind_col.index_id,
3508
+ ind_col.key_ordinal
3509
+ """
3510
+ )
3511
+ .bindparams(
3512
+ sql.bindparam("tabname", tablename, ischema.CoerceUnicode()),
3513
+ sql.bindparam("schname", owner, ischema.CoerceUnicode()),
3514
+ )
3515
+ .columns(name=sqltypes.Unicode())
3516
+ )
3517
+ for row in rp.mappings():
3518
+ if row["index_id"] not in indexes:
3519
+ continue
3520
+ index_def = indexes[row["index_id"]]
3521
+ is_colstore = index_def["dialect_options"].get("mssql_columnstore")
3522
+ is_clustered = index_def["dialect_options"].get("mssql_clustered")
3523
+ if not (is_colstore and is_clustered):
3524
+ # a clustered columnstore index includes all columns but does
3525
+ # not want them in the index definition
3526
+ if row["is_included_column"] and not is_colstore:
3527
+ # a noncludsted columnstore index reports that includes
3528
+ # columns but requires that are listed as normal columns
3529
+ index_def["include_columns"].append(row["name"])
3530
+ else:
3531
+ index_def["column_names"].append(row["name"])
3532
+ for index_info in indexes.values():
3533
+ # NOTE: "root level" include_columns is legacy, now part of
3534
+ # dialect_options (issue #7382)
3535
+ index_info["dialect_options"]["mssql_include"] = index_info[
3536
+ "include_columns"
3537
+ ]
3538
+
3539
+ if indexes:
3540
+ return list(indexes.values())
3541
+ else:
3542
+ return self._default_or_error(
3543
+ connection, tablename, owner, ReflectionDefaults.indexes, **kw
3544
+ )
3545
+
3546
+ @reflection.cache
3547
+ @_db_plus_owner
3548
+ def get_view_definition(
3549
+ self, connection, viewname, dbname, owner, schema, **kw
3550
+ ):
3551
+ view_def = connection.execute(
3552
+ sql.text(
3553
+ "select mod.definition "
3554
+ "from sys.sql_modules as mod "
3555
+ "join sys.views as views on mod.object_id = views.object_id "
3556
+ "join sys.schemas as sch on views.schema_id = sch.schema_id "
3557
+ "where views.name=:viewname and sch.name=:schname"
3558
+ ).bindparams(
3559
+ sql.bindparam("viewname", viewname, ischema.CoerceUnicode()),
3560
+ sql.bindparam("schname", owner, ischema.CoerceUnicode()),
3561
+ )
3562
+ ).scalar()
3563
+ if view_def:
3564
+ return view_def
3565
+ else:
3566
+ raise exc.NoSuchTableError(f"{owner}.{viewname}")
3567
+
3568
+ @reflection.cache
3569
+ def get_table_comment(self, connection, table_name, schema=None, **kw):
3570
+ if not self.supports_comments:
3571
+ raise NotImplementedError(
3572
+ "Can't get table comments on current SQL Server version in use"
3573
+ )
3574
+
3575
+ schema_name = schema if schema else self.default_schema_name
3576
+ COMMENT_SQL = """
3577
+ SELECT cast(com.value as nvarchar(max))
3578
+ FROM fn_listextendedproperty('MS_Description',
3579
+ 'schema', :schema, 'table', :table, NULL, NULL
3580
+ ) as com;
3581
+ """
3582
+
3583
+ comment = connection.execute(
3584
+ sql.text(COMMENT_SQL).bindparams(
3585
+ sql.bindparam("schema", schema_name, ischema.CoerceUnicode()),
3586
+ sql.bindparam("table", table_name, ischema.CoerceUnicode()),
3587
+ )
3588
+ ).scalar()
3589
+ if comment:
3590
+ return {"text": comment}
3591
+ else:
3592
+ return self._default_or_error(
3593
+ connection,
3594
+ table_name,
3595
+ None,
3596
+ ReflectionDefaults.table_comment,
3597
+ **kw,
3598
+ )
3599
+
3600
+ def _temp_table_name_like_pattern(self, tablename):
3601
+ # LIKE uses '%' to match zero or more characters and '_' to match any
3602
+ # single character. We want to match literal underscores, so T-SQL
3603
+ # requires that we enclose them in square brackets.
3604
+ return tablename + (
3605
+ ("[_][_][_]%") if not tablename.startswith("##") else ""
3606
+ )
3607
+
3608
+ def _get_internal_temp_table_name(self, connection, tablename):
3609
+ # it's likely that schema is always "dbo", but since we can
3610
+ # get it here, let's get it.
3611
+ # see https://stackoverflow.com/questions/8311959/
3612
+ # specifying-schema-for-temporary-tables
3613
+
3614
+ try:
3615
+ return connection.execute(
3616
+ sql.text(
3617
+ "select table_schema, table_name "
3618
+ "from tempdb.information_schema.tables "
3619
+ "where table_name like :p1"
3620
+ ),
3621
+ {"p1": self._temp_table_name_like_pattern(tablename)},
3622
+ ).one()
3623
+ except exc.MultipleResultsFound as me:
3624
+ raise exc.UnreflectableTableError(
3625
+ "Found more than one temporary table named '%s' in tempdb "
3626
+ "at this time. Cannot reliably resolve that name to its "
3627
+ "internal table name." % tablename
3628
+ ) from me
3629
+ except exc.NoResultFound as ne:
3630
+ raise exc.NoSuchTableError(
3631
+ "Unable to find a temporary table named '%s' in tempdb."
3632
+ % tablename
3633
+ ) from ne
3634
+
3635
+ @reflection.cache
3636
+ @_db_plus_owner
3637
+ def get_columns(self, connection, tablename, dbname, owner, schema, **kw):
3638
+ sys_columns = ischema.sys_columns
3639
+ sys_types = ischema.sys_types
3640
+ sys_default_constraints = ischema.sys_default_constraints
3641
+ computed_cols = ischema.computed_columns
3642
+ identity_cols = ischema.identity_columns
3643
+ extended_properties = ischema.extended_properties
3644
+
3645
+ # to access sys tables, need an object_id.
3646
+ # object_id() can normally match to the unquoted name even if it
3647
+ # has special characters. however it also accepts quoted names,
3648
+ # which means for the special case that the name itself has
3649
+ # "quotes" (e.g. brackets for SQL Server) we need to "quote" (e.g.
3650
+ # bracket) that name anyway. Fixed as part of #12654
3651
+
3652
+ is_temp_table = tablename.startswith("#")
3653
+ if is_temp_table:
3654
+ owner, tablename = self._get_internal_temp_table_name(
3655
+ connection, tablename
3656
+ )
3657
+
3658
+ object_id_tokens = [self.identifier_preparer.quote(tablename)]
3659
+ if owner:
3660
+ object_id_tokens.insert(0, self.identifier_preparer.quote(owner))
3661
+
3662
+ if is_temp_table:
3663
+ object_id_tokens.insert(0, "tempdb")
3664
+
3665
+ object_id = func.object_id(".".join(object_id_tokens))
3666
+
3667
+ whereclause = sys_columns.c.object_id == object_id
3668
+
3669
+ if self._supports_nvarchar_max:
3670
+ computed_definition = computed_cols.c.definition
3671
+ else:
3672
+ # tds_version 4.2 does not support NVARCHAR(MAX)
3673
+ computed_definition = sql.cast(
3674
+ computed_cols.c.definition, NVARCHAR(4000)
3675
+ )
3676
+
3677
+ s = (
3678
+ sql.select(
3679
+ sys_columns.c.name,
3680
+ sys_types.c.name,
3681
+ sys_columns.c.is_nullable,
3682
+ sys_columns.c.max_length,
3683
+ sys_columns.c.precision,
3684
+ sys_columns.c.scale,
3685
+ sys_default_constraints.c.definition,
3686
+ sys_columns.c.collation_name,
3687
+ computed_definition,
3688
+ computed_cols.c.is_persisted,
3689
+ identity_cols.c.is_identity,
3690
+ identity_cols.c.seed_value,
3691
+ identity_cols.c.increment_value,
3692
+ extended_properties.c.value.label("comment"),
3693
+ )
3694
+ .select_from(sys_columns)
3695
+ .join(
3696
+ sys_types,
3697
+ onclause=sys_columns.c.user_type_id
3698
+ == sys_types.c.user_type_id,
3699
+ )
3700
+ .outerjoin(
3701
+ sys_default_constraints,
3702
+ sql.and_(
3703
+ sys_default_constraints.c.object_id
3704
+ == sys_columns.c.default_object_id,
3705
+ sys_default_constraints.c.parent_column_id
3706
+ == sys_columns.c.column_id,
3707
+ ),
3708
+ )
3709
+ .outerjoin(
3710
+ computed_cols,
3711
+ onclause=sql.and_(
3712
+ computed_cols.c.object_id == sys_columns.c.object_id,
3713
+ computed_cols.c.column_id == sys_columns.c.column_id,
3714
+ ),
3715
+ )
3716
+ .outerjoin(
3717
+ identity_cols,
3718
+ onclause=sql.and_(
3719
+ identity_cols.c.object_id == sys_columns.c.object_id,
3720
+ identity_cols.c.column_id == sys_columns.c.column_id,
3721
+ ),
3722
+ )
3723
+ .outerjoin(
3724
+ extended_properties,
3725
+ onclause=sql.and_(
3726
+ extended_properties.c["class"] == 1,
3727
+ extended_properties.c.name == "MS_Description",
3728
+ sys_columns.c.object_id == extended_properties.c.major_id,
3729
+ sys_columns.c.column_id == extended_properties.c.minor_id,
3730
+ ),
3731
+ )
3732
+ .where(whereclause)
3733
+ .order_by(sys_columns.c.column_id)
3734
+ )
3735
+
3736
+ if is_temp_table:
3737
+ exec_opts = {"schema_translate_map": {"sys": "tempdb.sys"}}
3738
+ else:
3739
+ exec_opts = {"schema_translate_map": {}}
3740
+ c = connection.execution_options(**exec_opts).execute(s)
3741
+
3742
+ cols = []
3743
+ for row in c.mappings():
3744
+ name = row[sys_columns.c.name]
3745
+ type_ = row[sys_types.c.name]
3746
+ nullable = row[sys_columns.c.is_nullable] == 1
3747
+ maxlen = row[sys_columns.c.max_length]
3748
+ numericprec = row[sys_columns.c.precision]
3749
+ numericscale = row[sys_columns.c.scale]
3750
+ default = row[sys_default_constraints.c.definition]
3751
+ collation = row[sys_columns.c.collation_name]
3752
+ definition = row[computed_definition]
3753
+ is_persisted = row[computed_cols.c.is_persisted]
3754
+ is_identity = row[identity_cols.c.is_identity]
3755
+ identity_start = row[identity_cols.c.seed_value]
3756
+ identity_increment = row[identity_cols.c.increment_value]
3757
+ comment = row[extended_properties.c.value]
3758
+
3759
+ coltype = self.ischema_names.get(type_, None)
3760
+
3761
+ kwargs = {}
3762
+
3763
+ if coltype in (
3764
+ MSBinary,
3765
+ MSVarBinary,
3766
+ sqltypes.LargeBinary,
3767
+ ):
3768
+ kwargs["length"] = maxlen if maxlen != -1 else None
3769
+ elif coltype in (
3770
+ MSString,
3771
+ MSChar,
3772
+ MSText,
3773
+ ):
3774
+ kwargs["length"] = maxlen if maxlen != -1 else None
3775
+ if collation:
3776
+ kwargs["collation"] = collation
3777
+ elif coltype in (
3778
+ MSNVarchar,
3779
+ MSNChar,
3780
+ MSNText,
3781
+ ):
3782
+ kwargs["length"] = maxlen // 2 if maxlen != -1 else None
3783
+ if collation:
3784
+ kwargs["collation"] = collation
3785
+
3786
+ if coltype is None:
3787
+ util.warn(
3788
+ "Did not recognize type '%s' of column '%s'"
3789
+ % (type_, name)
3790
+ )
3791
+ coltype = sqltypes.NULLTYPE
3792
+ else:
3793
+ if issubclass(coltype, sqltypes.Numeric):
3794
+ kwargs["precision"] = numericprec
3795
+
3796
+ if not issubclass(coltype, sqltypes.Float):
3797
+ kwargs["scale"] = numericscale
3798
+
3799
+ coltype = coltype(**kwargs)
3800
+ cdict = {
3801
+ "name": name,
3802
+ "type": coltype,
3803
+ "nullable": nullable,
3804
+ "default": default,
3805
+ "autoincrement": is_identity is not None,
3806
+ "comment": comment,
3807
+ }
3808
+
3809
+ if definition is not None and is_persisted is not None:
3810
+ cdict["computed"] = {
3811
+ "sqltext": definition,
3812
+ "persisted": is_persisted,
3813
+ }
3814
+
3815
+ if is_identity is not None:
3816
+ # identity_start and identity_increment are Decimal or None
3817
+ if identity_start is None or identity_increment is None:
3818
+ cdict["identity"] = {}
3819
+ else:
3820
+ if isinstance(coltype, sqltypes.BigInteger):
3821
+ start = int(identity_start)
3822
+ increment = int(identity_increment)
3823
+ elif isinstance(coltype, sqltypes.Integer):
3824
+ start = int(identity_start)
3825
+ increment = int(identity_increment)
3826
+ else:
3827
+ start = identity_start
3828
+ increment = identity_increment
3829
+
3830
+ cdict["identity"] = {
3831
+ "start": start,
3832
+ "increment": increment,
3833
+ }
3834
+
3835
+ cols.append(cdict)
3836
+
3837
+ if cols:
3838
+ return cols
3839
+ else:
3840
+ return self._default_or_error(
3841
+ connection, tablename, owner, ReflectionDefaults.columns, **kw
3842
+ )
3843
+
3844
+ @reflection.cache
3845
+ @_db_plus_owner
3846
+ def get_pk_constraint(
3847
+ self, connection, tablename, dbname, owner, schema, **kw
3848
+ ):
3849
+ pkeys = []
3850
+ TC = ischema.constraints
3851
+ C = ischema.key_constraints.alias("C")
3852
+
3853
+ # Primary key constraints
3854
+ s = (
3855
+ sql.select(
3856
+ C.c.column_name,
3857
+ TC.c.constraint_type,
3858
+ C.c.constraint_name,
3859
+ func.objectproperty(
3860
+ func.object_id(
3861
+ C.c.table_schema + "." + C.c.constraint_name
3862
+ ),
3863
+ "CnstIsClustKey",
3864
+ ).label("is_clustered"),
3865
+ )
3866
+ .where(
3867
+ sql.and_(
3868
+ TC.c.constraint_name == C.c.constraint_name,
3869
+ TC.c.table_schema == C.c.table_schema,
3870
+ C.c.table_name == tablename,
3871
+ C.c.table_schema == owner,
3872
+ ),
3873
+ )
3874
+ .order_by(TC.c.constraint_name, C.c.ordinal_position)
3875
+ )
3876
+ c = connection.execution_options(future_result=True).execute(s)
3877
+ constraint_name = None
3878
+ is_clustered = None
3879
+ for row in c.mappings():
3880
+ if "PRIMARY" in row[TC.c.constraint_type.name]:
3881
+ pkeys.append(row["COLUMN_NAME"])
3882
+ if constraint_name is None:
3883
+ constraint_name = row[C.c.constraint_name.name]
3884
+ if is_clustered is None:
3885
+ is_clustered = row["is_clustered"]
3886
+ if pkeys:
3887
+ return {
3888
+ "constrained_columns": pkeys,
3889
+ "name": constraint_name,
3890
+ "dialect_options": {"mssql_clustered": is_clustered},
3891
+ }
3892
+ else:
3893
+ return self._default_or_error(
3894
+ connection,
3895
+ tablename,
3896
+ owner,
3897
+ ReflectionDefaults.pk_constraint,
3898
+ **kw,
3899
+ )
3900
+
3901
+ @reflection.cache
3902
+ @_db_plus_owner
3903
+ def get_foreign_keys(
3904
+ self, connection, tablename, dbname, owner, schema, **kw
3905
+ ):
3906
+ # Foreign key constraints
3907
+ s = (
3908
+ text(
3909
+ """\
3910
+ WITH fk_info AS (
3911
+ SELECT
3912
+ ischema_ref_con.constraint_schema,
3913
+ ischema_ref_con.constraint_name,
3914
+ ischema_key_col.ordinal_position,
3915
+ ischema_key_col.table_schema,
3916
+ ischema_key_col.table_name,
3917
+ ischema_ref_con.unique_constraint_schema,
3918
+ ischema_ref_con.unique_constraint_name,
3919
+ ischema_ref_con.match_option,
3920
+ ischema_ref_con.update_rule,
3921
+ ischema_ref_con.delete_rule,
3922
+ ischema_key_col.column_name AS constrained_column
3923
+ FROM
3924
+ INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS ischema_ref_con
3925
+ INNER JOIN
3926
+ INFORMATION_SCHEMA.KEY_COLUMN_USAGE ischema_key_col ON
3927
+ ischema_key_col.table_schema = ischema_ref_con.constraint_schema
3928
+ AND ischema_key_col.constraint_name =
3929
+ ischema_ref_con.constraint_name
3930
+ WHERE ischema_key_col.table_name = :tablename
3931
+ AND ischema_key_col.table_schema = :owner
3932
+ ),
3933
+ constraint_info AS (
3934
+ SELECT
3935
+ ischema_key_col.constraint_schema,
3936
+ ischema_key_col.constraint_name,
3937
+ ischema_key_col.ordinal_position,
3938
+ ischema_key_col.table_schema,
3939
+ ischema_key_col.table_name,
3940
+ ischema_key_col.column_name
3941
+ FROM
3942
+ INFORMATION_SCHEMA.KEY_COLUMN_USAGE ischema_key_col
3943
+ ),
3944
+ index_info AS (
3945
+ SELECT
3946
+ sys.schemas.name AS index_schema,
3947
+ sys.indexes.name AS index_name,
3948
+ sys.index_columns.key_ordinal AS ordinal_position,
3949
+ sys.schemas.name AS table_schema,
3950
+ sys.objects.name AS table_name,
3951
+ sys.columns.name AS column_name
3952
+ FROM
3953
+ sys.indexes
3954
+ INNER JOIN
3955
+ sys.objects ON
3956
+ sys.objects.object_id = sys.indexes.object_id
3957
+ INNER JOIN
3958
+ sys.schemas ON
3959
+ sys.schemas.schema_id = sys.objects.schema_id
3960
+ INNER JOIN
3961
+ sys.index_columns ON
3962
+ sys.index_columns.object_id = sys.objects.object_id
3963
+ AND sys.index_columns.index_id = sys.indexes.index_id
3964
+ INNER JOIN
3965
+ sys.columns ON
3966
+ sys.columns.object_id = sys.indexes.object_id
3967
+ AND sys.columns.column_id = sys.index_columns.column_id
3968
+ )
3969
+ SELECT
3970
+ fk_info.constraint_schema,
3971
+ fk_info.constraint_name,
3972
+ fk_info.ordinal_position,
3973
+ fk_info.constrained_column,
3974
+ constraint_info.table_schema AS referred_table_schema,
3975
+ constraint_info.table_name AS referred_table_name,
3976
+ constraint_info.column_name AS referred_column,
3977
+ fk_info.match_option,
3978
+ fk_info.update_rule,
3979
+ fk_info.delete_rule
3980
+ FROM
3981
+ fk_info INNER JOIN constraint_info ON
3982
+ constraint_info.constraint_schema =
3983
+ fk_info.unique_constraint_schema
3984
+ AND constraint_info.constraint_name =
3985
+ fk_info.unique_constraint_name
3986
+ AND constraint_info.ordinal_position = fk_info.ordinal_position
3987
+ UNION
3988
+ SELECT
3989
+ fk_info.constraint_schema,
3990
+ fk_info.constraint_name,
3991
+ fk_info.ordinal_position,
3992
+ fk_info.constrained_column,
3993
+ index_info.table_schema AS referred_table_schema,
3994
+ index_info.table_name AS referred_table_name,
3995
+ index_info.column_name AS referred_column,
3996
+ fk_info.match_option,
3997
+ fk_info.update_rule,
3998
+ fk_info.delete_rule
3999
+ FROM
4000
+ fk_info INNER JOIN index_info ON
4001
+ index_info.index_schema = fk_info.unique_constraint_schema
4002
+ AND index_info.index_name = fk_info.unique_constraint_name
4003
+ AND index_info.ordinal_position = fk_info.ordinal_position
4004
+ AND NOT (index_info.table_schema = fk_info.table_schema
4005
+ AND index_info.table_name = fk_info.table_name)
4006
+
4007
+ ORDER BY fk_info.constraint_schema, fk_info.constraint_name,
4008
+ fk_info.ordinal_position
4009
+ """
4010
+ )
4011
+ .bindparams(
4012
+ sql.bindparam("tablename", tablename, ischema.CoerceUnicode()),
4013
+ sql.bindparam("owner", owner, ischema.CoerceUnicode()),
4014
+ )
4015
+ .columns(
4016
+ constraint_schema=sqltypes.Unicode(),
4017
+ constraint_name=sqltypes.Unicode(),
4018
+ table_schema=sqltypes.Unicode(),
4019
+ table_name=sqltypes.Unicode(),
4020
+ constrained_column=sqltypes.Unicode(),
4021
+ referred_table_schema=sqltypes.Unicode(),
4022
+ referred_table_name=sqltypes.Unicode(),
4023
+ referred_column=sqltypes.Unicode(),
4024
+ )
4025
+ )
4026
+
4027
+ # group rows by constraint ID, to handle multi-column FKs
4028
+ fkeys = util.defaultdict(
4029
+ lambda: {
4030
+ "name": None,
4031
+ "constrained_columns": [],
4032
+ "referred_schema": None,
4033
+ "referred_table": None,
4034
+ "referred_columns": [],
4035
+ "options": {},
4036
+ }
4037
+ )
4038
+
4039
+ for r in connection.execute(s).all():
4040
+ (
4041
+ _, # constraint schema
4042
+ rfknm,
4043
+ _, # ordinal position
4044
+ scol,
4045
+ rschema,
4046
+ rtbl,
4047
+ rcol,
4048
+ # TODO: we support match=<keyword> for foreign keys so
4049
+ # we can support this also, PG has match=FULL for example
4050
+ # but this seems to not be a valid value for SQL Server
4051
+ _, # match rule
4052
+ fkuprule,
4053
+ fkdelrule,
4054
+ ) = r
4055
+
4056
+ rec = fkeys[rfknm]
4057
+ rec["name"] = rfknm
4058
+
4059
+ if fkuprule != "NO ACTION":
4060
+ rec["options"]["onupdate"] = fkuprule
4061
+
4062
+ if fkdelrule != "NO ACTION":
4063
+ rec["options"]["ondelete"] = fkdelrule
4064
+
4065
+ if not rec["referred_table"]:
4066
+ rec["referred_table"] = rtbl
4067
+ if schema is not None or owner != rschema:
4068
+ if dbname:
4069
+ rschema = dbname + "." + rschema
4070
+ rec["referred_schema"] = rschema
4071
+
4072
+ local_cols, remote_cols = (
4073
+ rec["constrained_columns"],
4074
+ rec["referred_columns"],
4075
+ )
4076
+
4077
+ local_cols.append(scol)
4078
+ remote_cols.append(rcol)
4079
+
4080
+ if fkeys:
4081
+ return list(fkeys.values())
4082
+ else:
4083
+ return self._default_or_error(
4084
+ connection,
4085
+ tablename,
4086
+ owner,
4087
+ ReflectionDefaults.foreign_keys,
4088
+ **kw,
4089
+ )