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