SQLAlchemy 2.0.47__cp313-cp313t-win_amd64.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 +283 -0
- sqlalchemy/connectors/__init__.py +18 -0
- sqlalchemy/connectors/aioodbc.py +184 -0
- sqlalchemy/connectors/asyncio.py +429 -0
- sqlalchemy/connectors/pyodbc.py +250 -0
- sqlalchemy/cyextension/__init__.py +6 -0
- sqlalchemy/cyextension/collections.cp313t-win_amd64.pyd +0 -0
- sqlalchemy/cyextension/collections.pyx +409 -0
- sqlalchemy/cyextension/immutabledict.cp313t-win_amd64.pyd +0 -0
- sqlalchemy/cyextension/immutabledict.pxd +8 -0
- sqlalchemy/cyextension/immutabledict.pyx +133 -0
- sqlalchemy/cyextension/processors.cp313t-win_amd64.pyd +0 -0
- sqlalchemy/cyextension/processors.pyx +68 -0
- sqlalchemy/cyextension/resultproxy.cp313t-win_amd64.pyd +0 -0
- sqlalchemy/cyextension/resultproxy.pyx +102 -0
- sqlalchemy/cyextension/util.cp313t-win_amd64.pyd +0 -0
- sqlalchemy/cyextension/util.pyx +90 -0
- sqlalchemy/dialects/__init__.py +62 -0
- sqlalchemy/dialects/_typing.py +30 -0
- sqlalchemy/dialects/mssql/__init__.py +88 -0
- sqlalchemy/dialects/mssql/aioodbc.py +63 -0
- sqlalchemy/dialects/mssql/base.py +4093 -0
- sqlalchemy/dialects/mssql/information_schema.py +285 -0
- sqlalchemy/dialects/mssql/json.py +129 -0
- sqlalchemy/dialects/mssql/provision.py +185 -0
- sqlalchemy/dialects/mssql/pymssql.py +126 -0
- sqlalchemy/dialects/mssql/pyodbc.py +760 -0
- sqlalchemy/dialects/mysql/__init__.py +104 -0
- sqlalchemy/dialects/mysql/aiomysql.py +250 -0
- sqlalchemy/dialects/mysql/asyncmy.py +231 -0
- sqlalchemy/dialects/mysql/base.py +3949 -0
- sqlalchemy/dialects/mysql/cymysql.py +106 -0
- sqlalchemy/dialects/mysql/dml.py +225 -0
- sqlalchemy/dialects/mysql/enumerated.py +282 -0
- sqlalchemy/dialects/mysql/expression.py +146 -0
- sqlalchemy/dialects/mysql/json.py +91 -0
- sqlalchemy/dialects/mysql/mariadb.py +72 -0
- sqlalchemy/dialects/mysql/mariadbconnector.py +322 -0
- sqlalchemy/dialects/mysql/mysqlconnector.py +302 -0
- sqlalchemy/dialects/mysql/mysqldb.py +314 -0
- sqlalchemy/dialects/mysql/provision.py +153 -0
- sqlalchemy/dialects/mysql/pymysql.py +158 -0
- sqlalchemy/dialects/mysql/pyodbc.py +157 -0
- sqlalchemy/dialects/mysql/reflection.py +727 -0
- sqlalchemy/dialects/mysql/reserved_words.py +570 -0
- sqlalchemy/dialects/mysql/types.py +835 -0
- sqlalchemy/dialects/oracle/__init__.py +81 -0
- sqlalchemy/dialects/oracle/base.py +3802 -0
- sqlalchemy/dialects/oracle/cx_oracle.py +1555 -0
- sqlalchemy/dialects/oracle/dictionary.py +507 -0
- sqlalchemy/dialects/oracle/oracledb.py +941 -0
- sqlalchemy/dialects/oracle/provision.py +297 -0
- sqlalchemy/dialects/oracle/types.py +316 -0
- sqlalchemy/dialects/oracle/vector.py +365 -0
- sqlalchemy/dialects/postgresql/__init__.py +167 -0
- sqlalchemy/dialects/postgresql/_psycopg_common.py +189 -0
- sqlalchemy/dialects/postgresql/array.py +519 -0
- sqlalchemy/dialects/postgresql/asyncpg.py +1284 -0
- sqlalchemy/dialects/postgresql/base.py +5378 -0
- sqlalchemy/dialects/postgresql/dml.py +339 -0
- sqlalchemy/dialects/postgresql/ext.py +540 -0
- sqlalchemy/dialects/postgresql/hstore.py +406 -0
- sqlalchemy/dialects/postgresql/json.py +404 -0
- sqlalchemy/dialects/postgresql/named_types.py +524 -0
- sqlalchemy/dialects/postgresql/operators.py +129 -0
- sqlalchemy/dialects/postgresql/pg8000.py +669 -0
- sqlalchemy/dialects/postgresql/pg_catalog.py +326 -0
- sqlalchemy/dialects/postgresql/provision.py +183 -0
- sqlalchemy/dialects/postgresql/psycopg.py +862 -0
- sqlalchemy/dialects/postgresql/psycopg2.py +892 -0
- sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
- sqlalchemy/dialects/postgresql/ranges.py +1031 -0
- sqlalchemy/dialects/postgresql/types.py +313 -0
- sqlalchemy/dialects/sqlite/__init__.py +57 -0
- sqlalchemy/dialects/sqlite/aiosqlite.py +482 -0
- sqlalchemy/dialects/sqlite/base.py +3056 -0
- sqlalchemy/dialects/sqlite/dml.py +263 -0
- sqlalchemy/dialects/sqlite/json.py +92 -0
- sqlalchemy/dialects/sqlite/provision.py +229 -0
- sqlalchemy/dialects/sqlite/pysqlcipher.py +157 -0
- sqlalchemy/dialects/sqlite/pysqlite.py +756 -0
- sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
- sqlalchemy/engine/__init__.py +62 -0
- sqlalchemy/engine/_py_processors.py +136 -0
- sqlalchemy/engine/_py_row.py +128 -0
- sqlalchemy/engine/_py_util.py +74 -0
- sqlalchemy/engine/base.py +3390 -0
- sqlalchemy/engine/characteristics.py +155 -0
- sqlalchemy/engine/create.py +893 -0
- sqlalchemy/engine/cursor.py +2298 -0
- sqlalchemy/engine/default.py +2394 -0
- sqlalchemy/engine/events.py +965 -0
- sqlalchemy/engine/interfaces.py +3471 -0
- sqlalchemy/engine/mock.py +134 -0
- sqlalchemy/engine/processors.py +61 -0
- sqlalchemy/engine/reflection.py +2102 -0
- sqlalchemy/engine/result.py +2399 -0
- sqlalchemy/engine/row.py +400 -0
- sqlalchemy/engine/strategies.py +16 -0
- sqlalchemy/engine/url.py +924 -0
- sqlalchemy/engine/util.py +167 -0
- sqlalchemy/event/__init__.py +26 -0
- sqlalchemy/event/api.py +220 -0
- sqlalchemy/event/attr.py +676 -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 +832 -0
- sqlalchemy/ext/__init__.py +11 -0
- sqlalchemy/ext/associationproxy.py +2027 -0
- sqlalchemy/ext/asyncio/__init__.py +25 -0
- sqlalchemy/ext/asyncio/base.py +281 -0
- sqlalchemy/ext/asyncio/engine.py +1471 -0
- sqlalchemy/ext/asyncio/exc.py +21 -0
- sqlalchemy/ext/asyncio/result.py +965 -0
- sqlalchemy/ext/asyncio/scoping.py +1599 -0
- sqlalchemy/ext/asyncio/session.py +1947 -0
- sqlalchemy/ext/automap.py +1701 -0
- sqlalchemy/ext/baked.py +570 -0
- sqlalchemy/ext/compiler.py +600 -0
- sqlalchemy/ext/declarative/__init__.py +65 -0
- sqlalchemy/ext/declarative/extensions.py +564 -0
- sqlalchemy/ext/horizontal_shard.py +478 -0
- sqlalchemy/ext/hybrid.py +1535 -0
- sqlalchemy/ext/indexable.py +364 -0
- sqlalchemy/ext/instrumentation.py +450 -0
- sqlalchemy/ext/mutable.py +1085 -0
- sqlalchemy/ext/mypy/__init__.py +6 -0
- sqlalchemy/ext/mypy/apply.py +324 -0
- sqlalchemy/ext/mypy/decl_class.py +515 -0
- sqlalchemy/ext/mypy/infer.py +590 -0
- sqlalchemy/ext/mypy/names.py +335 -0
- sqlalchemy/ext/mypy/plugin.py +303 -0
- sqlalchemy/ext/mypy/util.py +357 -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 +288 -0
- sqlalchemy/orm/__init__.py +171 -0
- sqlalchemy/orm/_orm_constructors.py +2661 -0
- sqlalchemy/orm/_typing.py +179 -0
- sqlalchemy/orm/attributes.py +2845 -0
- sqlalchemy/orm/base.py +971 -0
- sqlalchemy/orm/bulk_persistence.py +2135 -0
- sqlalchemy/orm/clsregistry.py +571 -0
- sqlalchemy/orm/collections.py +1627 -0
- sqlalchemy/orm/context.py +3334 -0
- sqlalchemy/orm/decl_api.py +2004 -0
- sqlalchemy/orm/decl_base.py +2192 -0
- sqlalchemy/orm/dependency.py +1302 -0
- sqlalchemy/orm/descriptor_props.py +1092 -0
- sqlalchemy/orm/dynamic.py +300 -0
- sqlalchemy/orm/evaluator.py +379 -0
- sqlalchemy/orm/events.py +3252 -0
- sqlalchemy/orm/exc.py +237 -0
- sqlalchemy/orm/identity.py +302 -0
- sqlalchemy/orm/instrumentation.py +754 -0
- sqlalchemy/orm/interfaces.py +1496 -0
- sqlalchemy/orm/loading.py +1686 -0
- sqlalchemy/orm/mapped_collection.py +557 -0
- sqlalchemy/orm/mapper.py +4444 -0
- sqlalchemy/orm/path_registry.py +809 -0
- sqlalchemy/orm/persistence.py +1788 -0
- sqlalchemy/orm/properties.py +935 -0
- sqlalchemy/orm/query.py +3459 -0
- sqlalchemy/orm/relationships.py +3508 -0
- sqlalchemy/orm/scoping.py +2148 -0
- sqlalchemy/orm/session.py +5280 -0
- sqlalchemy/orm/state.py +1168 -0
- sqlalchemy/orm/state_changes.py +196 -0
- sqlalchemy/orm/strategies.py +3470 -0
- sqlalchemy/orm/strategy_options.py +2568 -0
- sqlalchemy/orm/sync.py +164 -0
- sqlalchemy/orm/unitofwork.py +796 -0
- sqlalchemy/orm/util.py +2403 -0
- sqlalchemy/orm/writeonly.py +674 -0
- sqlalchemy/pool/__init__.py +44 -0
- sqlalchemy/pool/base.py +1524 -0
- sqlalchemy/pool/events.py +375 -0
- sqlalchemy/pool/impl.py +588 -0
- sqlalchemy/py.typed +0 -0
- sqlalchemy/schema.py +69 -0
- sqlalchemy/sql/__init__.py +145 -0
- sqlalchemy/sql/_dml_constructors.py +132 -0
- sqlalchemy/sql/_elements_constructors.py +1872 -0
- sqlalchemy/sql/_orm_types.py +20 -0
- sqlalchemy/sql/_py_util.py +75 -0
- sqlalchemy/sql/_selectable_constructors.py +763 -0
- sqlalchemy/sql/_typing.py +482 -0
- sqlalchemy/sql/annotation.py +587 -0
- sqlalchemy/sql/base.py +2293 -0
- sqlalchemy/sql/cache_key.py +1057 -0
- sqlalchemy/sql/coercions.py +1404 -0
- sqlalchemy/sql/compiler.py +8081 -0
- sqlalchemy/sql/crud.py +1752 -0
- sqlalchemy/sql/ddl.py +1444 -0
- sqlalchemy/sql/default_comparator.py +551 -0
- sqlalchemy/sql/dml.py +1850 -0
- sqlalchemy/sql/elements.py +5589 -0
- sqlalchemy/sql/events.py +458 -0
- sqlalchemy/sql/expression.py +159 -0
- sqlalchemy/sql/functions.py +2158 -0
- sqlalchemy/sql/lambdas.py +1442 -0
- sqlalchemy/sql/naming.py +209 -0
- sqlalchemy/sql/operators.py +2623 -0
- sqlalchemy/sql/roles.py +323 -0
- sqlalchemy/sql/schema.py +6222 -0
- sqlalchemy/sql/selectable.py +7265 -0
- sqlalchemy/sql/sqltypes.py +3930 -0
- sqlalchemy/sql/traversals.py +1024 -0
- sqlalchemy/sql/type_api.py +2368 -0
- sqlalchemy/sql/util.py +1485 -0
- sqlalchemy/sql/visitors.py +1164 -0
- sqlalchemy/testing/__init__.py +96 -0
- sqlalchemy/testing/assertions.py +994 -0
- sqlalchemy/testing/assertsql.py +520 -0
- sqlalchemy/testing/asyncio.py +135 -0
- sqlalchemy/testing/config.py +434 -0
- sqlalchemy/testing/engines.py +483 -0
- sqlalchemy/testing/entities.py +117 -0
- sqlalchemy/testing/exclusions.py +476 -0
- sqlalchemy/testing/fixtures/__init__.py +28 -0
- sqlalchemy/testing/fixtures/base.py +384 -0
- sqlalchemy/testing/fixtures/mypy.py +332 -0
- sqlalchemy/testing/fixtures/orm.py +227 -0
- sqlalchemy/testing/fixtures/sql.py +482 -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 +603 -0
- sqlalchemy/testing/requirements.py +1945 -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 +389 -0
- sqlalchemy/testing/suite/test_deprecations.py +153 -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 +504 -0
- sqlalchemy/testing/suite/test_rowcount.py +258 -0
- sqlalchemy/testing/suite/test_select.py +2010 -0
- sqlalchemy/testing/suite/test_sequence.py +317 -0
- sqlalchemy/testing/suite/test_types.py +2147 -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 +74 -0
- sqlalchemy/util/__init__.py +162 -0
- sqlalchemy/util/_collections.py +712 -0
- sqlalchemy/util/_concurrency_py3k.py +288 -0
- sqlalchemy/util/_has_cy.py +40 -0
- sqlalchemy/util/_py_collections.py +541 -0
- sqlalchemy/util/compat.py +421 -0
- sqlalchemy/util/concurrency.py +110 -0
- sqlalchemy/util/deprecations.py +401 -0
- sqlalchemy/util/langhelpers.py +2203 -0
- sqlalchemy/util/preloaded.py +150 -0
- sqlalchemy/util/queue.py +322 -0
- sqlalchemy/util/tool_support.py +201 -0
- sqlalchemy/util/topological.py +120 -0
- sqlalchemy/util/typing.py +734 -0
- sqlalchemy-2.0.47.dist-info/METADATA +243 -0
- sqlalchemy-2.0.47.dist-info/RECORD +274 -0
- sqlalchemy-2.0.47.dist-info/WHEEL +5 -0
- sqlalchemy-2.0.47.dist-info/licenses/LICENSE +19 -0
- sqlalchemy-2.0.47.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,3949 @@
|
|
|
1
|
+
# dialects/mysql/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
|
+
|
|
8
|
+
|
|
9
|
+
r"""
|
|
10
|
+
|
|
11
|
+
.. dialect:: mysql
|
|
12
|
+
:name: MySQL / MariaDB
|
|
13
|
+
:normal_support: 5.6+ / 10+
|
|
14
|
+
:best_effort: 5.0.2+ / 5.0.2+
|
|
15
|
+
|
|
16
|
+
Supported Versions and Features
|
|
17
|
+
-------------------------------
|
|
18
|
+
|
|
19
|
+
SQLAlchemy supports MySQL starting with version 5.0.2 through modern releases,
|
|
20
|
+
as well as all modern versions of MariaDB. See the official MySQL
|
|
21
|
+
documentation for detailed information about features supported in any given
|
|
22
|
+
server release.
|
|
23
|
+
|
|
24
|
+
.. versionchanged:: 1.4 minimum MySQL version supported is now 5.0.2.
|
|
25
|
+
|
|
26
|
+
MariaDB Support
|
|
27
|
+
~~~~~~~~~~~~~~~
|
|
28
|
+
|
|
29
|
+
The MariaDB variant of MySQL retains fundamental compatibility with MySQL's
|
|
30
|
+
protocols however the development of these two products continues to diverge.
|
|
31
|
+
Within the realm of SQLAlchemy, the two databases have a small number of
|
|
32
|
+
syntactical and behavioral differences that SQLAlchemy accommodates automatically.
|
|
33
|
+
To connect to a MariaDB database, no changes to the database URL are required::
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
engine = create_engine(
|
|
37
|
+
"mysql+pymysql://user:pass@some_mariadb/dbname?charset=utf8mb4"
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
Upon first connect, the SQLAlchemy dialect employs a
|
|
41
|
+
server version detection scheme that determines if the
|
|
42
|
+
backing database reports as MariaDB. Based on this flag, the dialect
|
|
43
|
+
can make different choices in those of areas where its behavior
|
|
44
|
+
must be different.
|
|
45
|
+
|
|
46
|
+
.. _mysql_mariadb_only_mode:
|
|
47
|
+
|
|
48
|
+
MariaDB-Only Mode
|
|
49
|
+
~~~~~~~~~~~~~~~~~
|
|
50
|
+
|
|
51
|
+
The dialect also supports an **optional** "MariaDB-only" mode of connection, which may be
|
|
52
|
+
useful for the case where an application makes use of MariaDB-specific features
|
|
53
|
+
and is not compatible with a MySQL database. To use this mode of operation,
|
|
54
|
+
replace the "mysql" token in the above URL with "mariadb"::
|
|
55
|
+
|
|
56
|
+
engine = create_engine(
|
|
57
|
+
"mariadb+pymysql://user:pass@some_mariadb/dbname?charset=utf8mb4"
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
The above engine, upon first connect, will raise an error if the server version
|
|
61
|
+
detection detects that the backing database is not MariaDB.
|
|
62
|
+
|
|
63
|
+
When using an engine with ``"mariadb"`` as the dialect name, **all mysql-specific options
|
|
64
|
+
that include the name "mysql" in them are now named with "mariadb"**. This means
|
|
65
|
+
options like ``mysql_engine`` should be named ``mariadb_engine``, etc. Both
|
|
66
|
+
"mysql" and "mariadb" options can be used simultaneously for applications that
|
|
67
|
+
use URLs with both "mysql" and "mariadb" dialects::
|
|
68
|
+
|
|
69
|
+
my_table = Table(
|
|
70
|
+
"mytable",
|
|
71
|
+
metadata,
|
|
72
|
+
Column("id", Integer, primary_key=True),
|
|
73
|
+
Column("textdata", String(50)),
|
|
74
|
+
mariadb_engine="InnoDB",
|
|
75
|
+
mysql_engine="InnoDB",
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
Index(
|
|
79
|
+
"textdata_ix",
|
|
80
|
+
my_table.c.textdata,
|
|
81
|
+
mysql_prefix="FULLTEXT",
|
|
82
|
+
mariadb_prefix="FULLTEXT",
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
Similar behavior will occur when the above structures are reflected, i.e. the
|
|
86
|
+
"mariadb" prefix will be present in the option names when the database URL
|
|
87
|
+
is based on the "mariadb" name.
|
|
88
|
+
|
|
89
|
+
.. versionadded:: 1.4 Added "mariadb" dialect name supporting "MariaDB-only mode"
|
|
90
|
+
for the MySQL dialect.
|
|
91
|
+
|
|
92
|
+
.. _mysql_connection_timeouts:
|
|
93
|
+
|
|
94
|
+
Connection Timeouts and Disconnects
|
|
95
|
+
-----------------------------------
|
|
96
|
+
|
|
97
|
+
MySQL / MariaDB feature an automatic connection close behavior, for connections that
|
|
98
|
+
have been idle for a fixed period of time, defaulting to eight hours.
|
|
99
|
+
To circumvent having this issue, use
|
|
100
|
+
the :paramref:`_sa.create_engine.pool_recycle` option which ensures that
|
|
101
|
+
a connection will be discarded and replaced with a new one if it has been
|
|
102
|
+
present in the pool for a fixed number of seconds::
|
|
103
|
+
|
|
104
|
+
engine = create_engine("mysql+mysqldb://...", pool_recycle=3600)
|
|
105
|
+
|
|
106
|
+
For more comprehensive disconnect detection of pooled connections, including
|
|
107
|
+
accommodation of server restarts and network issues, a pre-ping approach may
|
|
108
|
+
be employed. See :ref:`pool_disconnects` for current approaches.
|
|
109
|
+
|
|
110
|
+
.. seealso::
|
|
111
|
+
|
|
112
|
+
:ref:`pool_disconnects` - Background on several techniques for dealing
|
|
113
|
+
with timed out connections as well as database restarts.
|
|
114
|
+
|
|
115
|
+
.. _mysql_storage_engines:
|
|
116
|
+
|
|
117
|
+
CREATE TABLE arguments including Storage Engines
|
|
118
|
+
------------------------------------------------
|
|
119
|
+
|
|
120
|
+
Both MySQL's and MariaDB's CREATE TABLE syntax includes a wide array of special options,
|
|
121
|
+
including ``ENGINE``, ``CHARSET``, ``MAX_ROWS``, ``ROW_FORMAT``,
|
|
122
|
+
``INSERT_METHOD``, and many more.
|
|
123
|
+
To accommodate the rendering of these arguments, specify the form
|
|
124
|
+
``mysql_argument_name="value"``. For example, to specify a table with
|
|
125
|
+
``ENGINE`` of ``InnoDB``, ``CHARSET`` of ``utf8mb4``, and ``KEY_BLOCK_SIZE``
|
|
126
|
+
of ``1024``::
|
|
127
|
+
|
|
128
|
+
Table(
|
|
129
|
+
"mytable",
|
|
130
|
+
metadata,
|
|
131
|
+
Column("data", String(32)),
|
|
132
|
+
mysql_engine="InnoDB",
|
|
133
|
+
mysql_charset="utf8mb4",
|
|
134
|
+
mysql_key_block_size="1024",
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
When supporting :ref:`mysql_mariadb_only_mode` mode, similar keys against
|
|
138
|
+
the "mariadb" prefix must be included as well. The values can of course
|
|
139
|
+
vary independently so that different settings on MySQL vs. MariaDB may
|
|
140
|
+
be maintained::
|
|
141
|
+
|
|
142
|
+
# support both "mysql" and "mariadb-only" engine URLs
|
|
143
|
+
|
|
144
|
+
Table(
|
|
145
|
+
"mytable",
|
|
146
|
+
metadata,
|
|
147
|
+
Column("data", String(32)),
|
|
148
|
+
mysql_engine="InnoDB",
|
|
149
|
+
mariadb_engine="InnoDB",
|
|
150
|
+
mysql_charset="utf8mb4",
|
|
151
|
+
mariadb_charset="utf8",
|
|
152
|
+
mysql_key_block_size="1024",
|
|
153
|
+
mariadb_key_block_size="1024",
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
The MySQL / MariaDB dialects will normally transfer any keyword specified as
|
|
157
|
+
``mysql_keyword_name`` to be rendered as ``KEYWORD_NAME`` in the
|
|
158
|
+
``CREATE TABLE`` statement. A handful of these names will render with a space
|
|
159
|
+
instead of an underscore; to support this, the MySQL dialect has awareness of
|
|
160
|
+
these particular names, which include ``DATA DIRECTORY``
|
|
161
|
+
(e.g. ``mysql_data_directory``), ``CHARACTER SET`` (e.g.
|
|
162
|
+
``mysql_character_set``) and ``INDEX DIRECTORY`` (e.g.
|
|
163
|
+
``mysql_index_directory``).
|
|
164
|
+
|
|
165
|
+
The most common argument is ``mysql_engine``, which refers to the storage
|
|
166
|
+
engine for the table. Historically, MySQL server installations would default
|
|
167
|
+
to ``MyISAM`` for this value, although newer versions may be defaulting
|
|
168
|
+
to ``InnoDB``. The ``InnoDB`` engine is typically preferred for its support
|
|
169
|
+
of transactions and foreign keys.
|
|
170
|
+
|
|
171
|
+
A :class:`_schema.Table`
|
|
172
|
+
that is created in a MySQL / MariaDB database with a storage engine
|
|
173
|
+
of ``MyISAM`` will be essentially non-transactional, meaning any
|
|
174
|
+
INSERT/UPDATE/DELETE statement referring to this table will be invoked as
|
|
175
|
+
autocommit. It also will have no support for foreign key constraints; while
|
|
176
|
+
the ``CREATE TABLE`` statement accepts foreign key options, when using the
|
|
177
|
+
``MyISAM`` storage engine these arguments are discarded. Reflecting such a
|
|
178
|
+
table will also produce no foreign key constraint information.
|
|
179
|
+
|
|
180
|
+
For fully atomic transactions as well as support for foreign key
|
|
181
|
+
constraints, all participating ``CREATE TABLE`` statements must specify a
|
|
182
|
+
transactional engine, which in the vast majority of cases is ``InnoDB``.
|
|
183
|
+
|
|
184
|
+
Partitioning can similarly be specified using similar options.
|
|
185
|
+
In the example below the create table will specify ``PARTITION_BY``,
|
|
186
|
+
``PARTITIONS``, ``SUBPARTITIONS`` and ``SUBPARTITION_BY``::
|
|
187
|
+
|
|
188
|
+
# can also use mariadb_* prefix
|
|
189
|
+
Table(
|
|
190
|
+
"testtable",
|
|
191
|
+
MetaData(),
|
|
192
|
+
Column("id", Integer(), primary_key=True, autoincrement=True),
|
|
193
|
+
Column("other_id", Integer(), primary_key=True, autoincrement=False),
|
|
194
|
+
mysql_partitions="2",
|
|
195
|
+
mysql_partition_by="KEY(other_id)",
|
|
196
|
+
mysql_subpartition_by="HASH(some_expr)",
|
|
197
|
+
mysql_subpartitions="2",
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
This will render:
|
|
201
|
+
|
|
202
|
+
.. sourcecode:: sql
|
|
203
|
+
|
|
204
|
+
CREATE TABLE testtable (
|
|
205
|
+
id INTEGER NOT NULL AUTO_INCREMENT,
|
|
206
|
+
other_id INTEGER NOT NULL,
|
|
207
|
+
PRIMARY KEY (id, other_id)
|
|
208
|
+
)PARTITION BY KEY(other_id) PARTITIONS 2 SUBPARTITION BY HASH(some_expr) SUBPARTITIONS 2
|
|
209
|
+
|
|
210
|
+
Case Sensitivity and Table Reflection
|
|
211
|
+
-------------------------------------
|
|
212
|
+
|
|
213
|
+
Both MySQL and MariaDB have inconsistent support for case-sensitive identifier
|
|
214
|
+
names, basing support on specific details of the underlying
|
|
215
|
+
operating system. However, it has been observed that no matter
|
|
216
|
+
what case sensitivity behavior is present, the names of tables in
|
|
217
|
+
foreign key declarations are *always* received from the database
|
|
218
|
+
as all-lower case, making it impossible to accurately reflect a
|
|
219
|
+
schema where inter-related tables use mixed-case identifier names.
|
|
220
|
+
|
|
221
|
+
Therefore it is strongly advised that table names be declared as
|
|
222
|
+
all lower case both within SQLAlchemy as well as on the MySQL / MariaDB
|
|
223
|
+
database itself, especially if database reflection features are
|
|
224
|
+
to be used.
|
|
225
|
+
|
|
226
|
+
.. _mysql_isolation_level:
|
|
227
|
+
|
|
228
|
+
Transaction Isolation Level
|
|
229
|
+
---------------------------
|
|
230
|
+
|
|
231
|
+
All MySQL / MariaDB dialects support setting of transaction isolation level both via a
|
|
232
|
+
dialect-specific parameter :paramref:`_sa.create_engine.isolation_level`
|
|
233
|
+
accepted
|
|
234
|
+
by :func:`_sa.create_engine`, as well as the
|
|
235
|
+
:paramref:`.Connection.execution_options.isolation_level` argument as passed to
|
|
236
|
+
:meth:`_engine.Connection.execution_options`.
|
|
237
|
+
This feature works by issuing the
|
|
238
|
+
command ``SET SESSION TRANSACTION ISOLATION LEVEL <level>`` for each new
|
|
239
|
+
connection. For the special AUTOCOMMIT isolation level, DBAPI-specific
|
|
240
|
+
techniques are used.
|
|
241
|
+
|
|
242
|
+
To set isolation level using :func:`_sa.create_engine`::
|
|
243
|
+
|
|
244
|
+
engine = create_engine(
|
|
245
|
+
"mysql+mysqldb://scott:tiger@localhost/test",
|
|
246
|
+
isolation_level="READ UNCOMMITTED",
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
To set using per-connection execution options::
|
|
250
|
+
|
|
251
|
+
connection = engine.connect()
|
|
252
|
+
connection = connection.execution_options(isolation_level="READ COMMITTED")
|
|
253
|
+
|
|
254
|
+
Valid values for ``isolation_level`` include:
|
|
255
|
+
|
|
256
|
+
* ``READ COMMITTED``
|
|
257
|
+
* ``READ UNCOMMITTED``
|
|
258
|
+
* ``REPEATABLE READ``
|
|
259
|
+
* ``SERIALIZABLE``
|
|
260
|
+
* ``AUTOCOMMIT``
|
|
261
|
+
|
|
262
|
+
The special ``AUTOCOMMIT`` value makes use of the various "autocommit"
|
|
263
|
+
attributes provided by specific DBAPIs, and is currently supported by
|
|
264
|
+
MySQLdb, MySQL-Client, MySQL-Connector Python, and PyMySQL. Using it,
|
|
265
|
+
the database connection will return true for the value of
|
|
266
|
+
``SELECT @@autocommit;``.
|
|
267
|
+
|
|
268
|
+
There are also more options for isolation level configurations, such as
|
|
269
|
+
"sub-engine" objects linked to a main :class:`_engine.Engine` which each apply
|
|
270
|
+
different isolation level settings. See the discussion at
|
|
271
|
+
:ref:`dbapi_autocommit` for background.
|
|
272
|
+
|
|
273
|
+
.. seealso::
|
|
274
|
+
|
|
275
|
+
:ref:`dbapi_autocommit`
|
|
276
|
+
|
|
277
|
+
AUTO_INCREMENT Behavior
|
|
278
|
+
-----------------------
|
|
279
|
+
|
|
280
|
+
When creating tables, SQLAlchemy will automatically set ``AUTO_INCREMENT`` on
|
|
281
|
+
the first :class:`.Integer` primary key column which is not marked as a
|
|
282
|
+
foreign key::
|
|
283
|
+
|
|
284
|
+
>>> t = Table(
|
|
285
|
+
... "mytable", metadata, Column("mytable_id", Integer, primary_key=True)
|
|
286
|
+
... )
|
|
287
|
+
>>> t.create()
|
|
288
|
+
CREATE TABLE mytable (
|
|
289
|
+
id INTEGER NOT NULL AUTO_INCREMENT,
|
|
290
|
+
PRIMARY KEY (id)
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
You can disable this behavior by passing ``False`` to the
|
|
294
|
+
:paramref:`_schema.Column.autoincrement` argument of :class:`_schema.Column`.
|
|
295
|
+
This flag
|
|
296
|
+
can also be used to enable auto-increment on a secondary column in a
|
|
297
|
+
multi-column key for some storage engines::
|
|
298
|
+
|
|
299
|
+
Table(
|
|
300
|
+
"mytable",
|
|
301
|
+
metadata,
|
|
302
|
+
Column("gid", Integer, primary_key=True, autoincrement=False),
|
|
303
|
+
Column("id", Integer, primary_key=True),
|
|
304
|
+
)
|
|
305
|
+
|
|
306
|
+
.. _mysql_ss_cursors:
|
|
307
|
+
|
|
308
|
+
Server Side Cursors
|
|
309
|
+
-------------------
|
|
310
|
+
|
|
311
|
+
Server-side cursor support is available for the mysqlclient, PyMySQL,
|
|
312
|
+
mariadbconnector dialects and may also be available in others. This makes use
|
|
313
|
+
of either the "buffered=True/False" flag if available or by using a class such
|
|
314
|
+
as ``MySQLdb.cursors.SSCursor`` or ``pymysql.cursors.SSCursor`` internally.
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
Server side cursors are enabled on a per-statement basis by using the
|
|
318
|
+
:paramref:`.Connection.execution_options.stream_results` connection execution
|
|
319
|
+
option::
|
|
320
|
+
|
|
321
|
+
with engine.connect() as conn:
|
|
322
|
+
result = conn.execution_options(stream_results=True).execute(
|
|
323
|
+
text("select * from table")
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
Note that some kinds of SQL statements may not be supported with
|
|
327
|
+
server side cursors; generally, only SQL statements that return rows should be
|
|
328
|
+
used with this option.
|
|
329
|
+
|
|
330
|
+
.. deprecated:: 1.4 The dialect-level server_side_cursors flag is deprecated
|
|
331
|
+
and will be removed in a future release. Please use the
|
|
332
|
+
:paramref:`_engine.Connection.stream_results` execution option for
|
|
333
|
+
unbuffered cursor support.
|
|
334
|
+
|
|
335
|
+
.. seealso::
|
|
336
|
+
|
|
337
|
+
:ref:`engine_stream_results`
|
|
338
|
+
|
|
339
|
+
.. _mysql_unicode:
|
|
340
|
+
|
|
341
|
+
Unicode
|
|
342
|
+
-------
|
|
343
|
+
|
|
344
|
+
Charset Selection
|
|
345
|
+
~~~~~~~~~~~~~~~~~
|
|
346
|
+
|
|
347
|
+
Most MySQL / MariaDB DBAPIs offer the option to set the client character set for
|
|
348
|
+
a connection. This is typically delivered using the ``charset`` parameter
|
|
349
|
+
in the URL, such as::
|
|
350
|
+
|
|
351
|
+
e = create_engine(
|
|
352
|
+
"mysql+pymysql://scott:tiger@localhost/test?charset=utf8mb4"
|
|
353
|
+
)
|
|
354
|
+
|
|
355
|
+
This charset is the **client character set** for the connection. Some
|
|
356
|
+
MySQL DBAPIs will default this to a value such as ``latin1``, and some
|
|
357
|
+
will make use of the ``default-character-set`` setting in the ``my.cnf``
|
|
358
|
+
file as well. Documentation for the DBAPI in use should be consulted
|
|
359
|
+
for specific behavior.
|
|
360
|
+
|
|
361
|
+
The encoding used for Unicode has traditionally been ``'utf8'``. However, for
|
|
362
|
+
MySQL versions 5.5.3 and MariaDB 5.5 on forward, a new MySQL-specific encoding
|
|
363
|
+
``'utf8mb4'`` has been introduced, and as of MySQL 8.0 a warning is emitted by
|
|
364
|
+
the server if plain ``utf8`` is specified within any server-side directives,
|
|
365
|
+
replaced with ``utf8mb3``. The rationale for this new encoding is due to the
|
|
366
|
+
fact that MySQL's legacy utf-8 encoding only supports codepoints up to three
|
|
367
|
+
bytes instead of four. Therefore, when communicating with a MySQL or MariaDB
|
|
368
|
+
database that includes codepoints more than three bytes in size, this new
|
|
369
|
+
charset is preferred, if supported by both the database as well as the client
|
|
370
|
+
DBAPI, as in::
|
|
371
|
+
|
|
372
|
+
e = create_engine(
|
|
373
|
+
"mysql+pymysql://scott:tiger@localhost/test?charset=utf8mb4"
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
All modern DBAPIs should support the ``utf8mb4`` charset.
|
|
377
|
+
|
|
378
|
+
In order to use ``utf8mb4`` encoding for a schema that was created with legacy
|
|
379
|
+
``utf8``, changes to the MySQL/MariaDB schema and/or server configuration may be
|
|
380
|
+
required.
|
|
381
|
+
|
|
382
|
+
.. seealso::
|
|
383
|
+
|
|
384
|
+
`The utf8mb4 Character Set \
|
|
385
|
+
<https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html>`_ - \
|
|
386
|
+
in the MySQL documentation
|
|
387
|
+
|
|
388
|
+
.. _mysql_binary_introducer:
|
|
389
|
+
|
|
390
|
+
Dealing with Binary Data Warnings and Unicode
|
|
391
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
392
|
+
|
|
393
|
+
MySQL versions 5.6, 5.7 and later (not MariaDB at the time of this writing) now
|
|
394
|
+
emit a warning when attempting to pass binary data to the database, while a
|
|
395
|
+
character set encoding is also in place, when the binary data itself is not
|
|
396
|
+
valid for that encoding:
|
|
397
|
+
|
|
398
|
+
.. sourcecode:: text
|
|
399
|
+
|
|
400
|
+
default.py:509: Warning: (1300, "Invalid utf8mb4 character string:
|
|
401
|
+
'F9876A'")
|
|
402
|
+
cursor.execute(statement, parameters)
|
|
403
|
+
|
|
404
|
+
This warning is due to the fact that the MySQL client library is attempting to
|
|
405
|
+
interpret the binary string as a unicode object even if a datatype such
|
|
406
|
+
as :class:`.LargeBinary` is in use. To resolve this, the SQL statement requires
|
|
407
|
+
a binary "character set introducer" be present before any non-NULL value
|
|
408
|
+
that renders like this:
|
|
409
|
+
|
|
410
|
+
.. sourcecode:: sql
|
|
411
|
+
|
|
412
|
+
INSERT INTO table (data) VALUES (_binary %s)
|
|
413
|
+
|
|
414
|
+
These character set introducers are provided by the DBAPI driver, assuming the
|
|
415
|
+
use of mysqlclient or PyMySQL (both of which are recommended). Add the query
|
|
416
|
+
string parameter ``binary_prefix=true`` to the URL to repair this warning::
|
|
417
|
+
|
|
418
|
+
# mysqlclient
|
|
419
|
+
engine = create_engine(
|
|
420
|
+
"mysql+mysqldb://scott:tiger@localhost/test?charset=utf8mb4&binary_prefix=true"
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
# PyMySQL
|
|
424
|
+
engine = create_engine(
|
|
425
|
+
"mysql+pymysql://scott:tiger@localhost/test?charset=utf8mb4&binary_prefix=true"
|
|
426
|
+
)
|
|
427
|
+
|
|
428
|
+
The ``binary_prefix`` flag may or may not be supported by other MySQL drivers.
|
|
429
|
+
|
|
430
|
+
SQLAlchemy itself cannot render this ``_binary`` prefix reliably, as it does
|
|
431
|
+
not work with the NULL value, which is valid to be sent as a bound parameter.
|
|
432
|
+
As the MySQL driver renders parameters directly into the SQL string, it's the
|
|
433
|
+
most efficient place for this additional keyword to be passed.
|
|
434
|
+
|
|
435
|
+
.. seealso::
|
|
436
|
+
|
|
437
|
+
`Character set introducers <https://dev.mysql.com/doc/refman/5.7/en/charset-introducer.html>`_ - on the MySQL website
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
ANSI Quoting Style
|
|
441
|
+
------------------
|
|
442
|
+
|
|
443
|
+
MySQL / MariaDB feature two varieties of identifier "quoting style", one using
|
|
444
|
+
backticks and the other using quotes, e.g. ```some_identifier``` vs.
|
|
445
|
+
``"some_identifier"``. All MySQL dialects detect which version
|
|
446
|
+
is in use by checking the value of :ref:`sql_mode<mysql_sql_mode>` when a connection is first
|
|
447
|
+
established with a particular :class:`_engine.Engine`.
|
|
448
|
+
This quoting style comes
|
|
449
|
+
into play when rendering table and column names as well as when reflecting
|
|
450
|
+
existing database structures. The detection is entirely automatic and
|
|
451
|
+
no special configuration is needed to use either quoting style.
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
.. _mysql_sql_mode:
|
|
455
|
+
|
|
456
|
+
Changing the sql_mode
|
|
457
|
+
---------------------
|
|
458
|
+
|
|
459
|
+
MySQL supports operating in multiple
|
|
460
|
+
`Server SQL Modes <https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html>`_ for
|
|
461
|
+
both Servers and Clients. To change the ``sql_mode`` for a given application, a
|
|
462
|
+
developer can leverage SQLAlchemy's Events system.
|
|
463
|
+
|
|
464
|
+
In the following example, the event system is used to set the ``sql_mode`` on
|
|
465
|
+
the ``first_connect`` and ``connect`` events::
|
|
466
|
+
|
|
467
|
+
from sqlalchemy import create_engine, event
|
|
468
|
+
|
|
469
|
+
eng = create_engine(
|
|
470
|
+
"mysql+mysqldb://scott:tiger@localhost/test", echo="debug"
|
|
471
|
+
)
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
# `insert=True` will ensure this is the very first listener to run
|
|
475
|
+
@event.listens_for(eng, "connect", insert=True)
|
|
476
|
+
def connect(dbapi_connection, connection_record):
|
|
477
|
+
cursor = dbapi_connection.cursor()
|
|
478
|
+
cursor.execute("SET sql_mode = 'STRICT_ALL_TABLES'")
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
conn = eng.connect()
|
|
482
|
+
|
|
483
|
+
In the example illustrated above, the "connect" event will invoke the "SET"
|
|
484
|
+
statement on the connection at the moment a particular DBAPI connection is
|
|
485
|
+
first created for a given Pool, before the connection is made available to the
|
|
486
|
+
connection pool. Additionally, because the function was registered with
|
|
487
|
+
``insert=True``, it will be prepended to the internal list of registered
|
|
488
|
+
functions.
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
MySQL / MariaDB SQL Extensions
|
|
492
|
+
------------------------------
|
|
493
|
+
|
|
494
|
+
Many of the MySQL / MariaDB SQL extensions are handled through SQLAlchemy's generic
|
|
495
|
+
function and operator support::
|
|
496
|
+
|
|
497
|
+
table.select(table.c.password == func.md5("plaintext"))
|
|
498
|
+
table.select(table.c.username.op("regexp")("^[a-d]"))
|
|
499
|
+
|
|
500
|
+
And of course any valid SQL statement can be executed as a string as well.
|
|
501
|
+
|
|
502
|
+
Some limited direct support for MySQL / MariaDB extensions to SQL is currently
|
|
503
|
+
available.
|
|
504
|
+
|
|
505
|
+
* INSERT..ON DUPLICATE KEY UPDATE: See
|
|
506
|
+
:ref:`mysql_insert_on_duplicate_key_update`
|
|
507
|
+
|
|
508
|
+
* SELECT pragma, use :meth:`_expression.Select.prefix_with` and
|
|
509
|
+
:meth:`_query.Query.prefix_with`::
|
|
510
|
+
|
|
511
|
+
select(...).prefix_with(["HIGH_PRIORITY", "SQL_SMALL_RESULT"])
|
|
512
|
+
|
|
513
|
+
* UPDATE with LIMIT::
|
|
514
|
+
|
|
515
|
+
update(...).with_dialect_options(mysql_limit=10, mariadb_limit=10)
|
|
516
|
+
|
|
517
|
+
* DELETE
|
|
518
|
+
with LIMIT::
|
|
519
|
+
|
|
520
|
+
delete(...).with_dialect_options(mysql_limit=10, mariadb_limit=10)
|
|
521
|
+
|
|
522
|
+
.. versionadded:: 2.0.37 Added delete with limit
|
|
523
|
+
|
|
524
|
+
* optimizer hints, use :meth:`_expression.Select.prefix_with` and
|
|
525
|
+
:meth:`_query.Query.prefix_with`::
|
|
526
|
+
|
|
527
|
+
select(...).prefix_with("/*+ NO_RANGE_OPTIMIZATION(t4 PRIMARY) */")
|
|
528
|
+
|
|
529
|
+
* index hints, use :meth:`_expression.Select.with_hint` and
|
|
530
|
+
:meth:`_query.Query.with_hint`::
|
|
531
|
+
|
|
532
|
+
select(...).with_hint(some_table, "USE INDEX xyz")
|
|
533
|
+
|
|
534
|
+
* MATCH
|
|
535
|
+
operator support::
|
|
536
|
+
|
|
537
|
+
from sqlalchemy.dialects.mysql import match
|
|
538
|
+
|
|
539
|
+
select(...).where(match(col1, col2, against="some expr").in_boolean_mode())
|
|
540
|
+
|
|
541
|
+
.. seealso::
|
|
542
|
+
|
|
543
|
+
:class:`_mysql.match`
|
|
544
|
+
|
|
545
|
+
INSERT/DELETE...RETURNING
|
|
546
|
+
-------------------------
|
|
547
|
+
|
|
548
|
+
The MariaDB dialect supports 10.5+'s ``INSERT..RETURNING`` and
|
|
549
|
+
``DELETE..RETURNING`` (10.0+) syntaxes. ``INSERT..RETURNING`` may be used
|
|
550
|
+
automatically in some cases in order to fetch newly generated identifiers in
|
|
551
|
+
place of the traditional approach of using ``cursor.lastrowid``, however
|
|
552
|
+
``cursor.lastrowid`` is currently still preferred for simple single-statement
|
|
553
|
+
cases for its better performance.
|
|
554
|
+
|
|
555
|
+
To specify an explicit ``RETURNING`` clause, use the
|
|
556
|
+
:meth:`._UpdateBase.returning` method on a per-statement basis::
|
|
557
|
+
|
|
558
|
+
# INSERT..RETURNING
|
|
559
|
+
result = connection.execute(
|
|
560
|
+
table.insert().values(name="foo").returning(table.c.col1, table.c.col2)
|
|
561
|
+
)
|
|
562
|
+
print(result.all())
|
|
563
|
+
|
|
564
|
+
# DELETE..RETURNING
|
|
565
|
+
result = connection.execute(
|
|
566
|
+
table.delete()
|
|
567
|
+
.where(table.c.name == "foo")
|
|
568
|
+
.returning(table.c.col1, table.c.col2)
|
|
569
|
+
)
|
|
570
|
+
print(result.all())
|
|
571
|
+
|
|
572
|
+
.. versionadded:: 2.0 Added support for MariaDB RETURNING
|
|
573
|
+
|
|
574
|
+
.. _mysql_insert_on_duplicate_key_update:
|
|
575
|
+
|
|
576
|
+
INSERT...ON DUPLICATE KEY UPDATE (Upsert)
|
|
577
|
+
------------------------------------------
|
|
578
|
+
|
|
579
|
+
MySQL / MariaDB allow "upserts" (update or insert)
|
|
580
|
+
of rows into a table via the ``ON DUPLICATE KEY UPDATE`` clause of the
|
|
581
|
+
``INSERT`` statement. A candidate row will only be inserted if that row does
|
|
582
|
+
not match an existing primary or unique key in the table; otherwise, an UPDATE
|
|
583
|
+
will be performed. The statement allows for separate specification of the
|
|
584
|
+
values to INSERT versus the values for UPDATE.
|
|
585
|
+
|
|
586
|
+
SQLAlchemy provides ``ON DUPLICATE KEY UPDATE`` support via the MySQL-specific
|
|
587
|
+
:func:`.mysql.insert()` function, which provides
|
|
588
|
+
the generative method :meth:`~.mysql.Insert.on_duplicate_key_update`:
|
|
589
|
+
|
|
590
|
+
.. sourcecode:: pycon+sql
|
|
591
|
+
|
|
592
|
+
>>> from sqlalchemy.dialects.mysql import insert
|
|
593
|
+
|
|
594
|
+
>>> insert_stmt = insert(my_table).values(
|
|
595
|
+
... id="some_existing_id", data="inserted value"
|
|
596
|
+
... )
|
|
597
|
+
|
|
598
|
+
>>> on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(
|
|
599
|
+
... data=insert_stmt.inserted.data, status="U"
|
|
600
|
+
... )
|
|
601
|
+
>>> print(on_duplicate_key_stmt)
|
|
602
|
+
{printsql}INSERT INTO my_table (id, data) VALUES (%s, %s)
|
|
603
|
+
ON DUPLICATE KEY UPDATE data = VALUES(data), status = %s
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
Unlike PostgreSQL's "ON CONFLICT" phrase, the "ON DUPLICATE KEY UPDATE"
|
|
607
|
+
phrase will always match on any primary key or unique key, and will always
|
|
608
|
+
perform an UPDATE if there's a match; there are no options for it to raise
|
|
609
|
+
an error or to skip performing an UPDATE.
|
|
610
|
+
|
|
611
|
+
``ON DUPLICATE KEY UPDATE`` is used to perform an update of the already
|
|
612
|
+
existing row, using any combination of new values as well as values
|
|
613
|
+
from the proposed insertion. These values are normally specified using
|
|
614
|
+
keyword arguments passed to the
|
|
615
|
+
:meth:`_mysql.Insert.on_duplicate_key_update`
|
|
616
|
+
given column key values (usually the name of the column, unless it
|
|
617
|
+
specifies :paramref:`_schema.Column.key`
|
|
618
|
+
) as keys and literal or SQL expressions
|
|
619
|
+
as values:
|
|
620
|
+
|
|
621
|
+
.. sourcecode:: pycon+sql
|
|
622
|
+
|
|
623
|
+
>>> insert_stmt = insert(my_table).values(
|
|
624
|
+
... id="some_existing_id", data="inserted value"
|
|
625
|
+
... )
|
|
626
|
+
|
|
627
|
+
>>> on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(
|
|
628
|
+
... data="some data",
|
|
629
|
+
... updated_at=func.current_timestamp(),
|
|
630
|
+
... )
|
|
631
|
+
|
|
632
|
+
>>> print(on_duplicate_key_stmt)
|
|
633
|
+
{printsql}INSERT INTO my_table (id, data) VALUES (%s, %s)
|
|
634
|
+
ON DUPLICATE KEY UPDATE data = %s, updated_at = CURRENT_TIMESTAMP
|
|
635
|
+
|
|
636
|
+
In a manner similar to that of :meth:`.UpdateBase.values`, other parameter
|
|
637
|
+
forms are accepted, including a single dictionary:
|
|
638
|
+
|
|
639
|
+
.. sourcecode:: pycon+sql
|
|
640
|
+
|
|
641
|
+
>>> on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(
|
|
642
|
+
... {"data": "some data", "updated_at": func.current_timestamp()},
|
|
643
|
+
... )
|
|
644
|
+
|
|
645
|
+
as well as a list of 2-tuples, which will automatically provide
|
|
646
|
+
a parameter-ordered UPDATE statement in a manner similar to that described
|
|
647
|
+
at :ref:`tutorial_parameter_ordered_updates`. Unlike the :class:`_expression.Update`
|
|
648
|
+
object,
|
|
649
|
+
no special flag is needed to specify the intent since the argument form is
|
|
650
|
+
this context is unambiguous:
|
|
651
|
+
|
|
652
|
+
.. sourcecode:: pycon+sql
|
|
653
|
+
|
|
654
|
+
>>> on_duplicate_key_stmt = insert_stmt.on_duplicate_key_update(
|
|
655
|
+
... [
|
|
656
|
+
... ("data", "some data"),
|
|
657
|
+
... ("updated_at", func.current_timestamp()),
|
|
658
|
+
... ]
|
|
659
|
+
... )
|
|
660
|
+
|
|
661
|
+
>>> print(on_duplicate_key_stmt)
|
|
662
|
+
{printsql}INSERT INTO my_table (id, data) VALUES (%s, %s)
|
|
663
|
+
ON DUPLICATE KEY UPDATE data = %s, updated_at = CURRENT_TIMESTAMP
|
|
664
|
+
|
|
665
|
+
.. versionchanged:: 1.3 support for parameter-ordered UPDATE clause within
|
|
666
|
+
MySQL ON DUPLICATE KEY UPDATE
|
|
667
|
+
|
|
668
|
+
.. warning::
|
|
669
|
+
|
|
670
|
+
The :meth:`_mysql.Insert.on_duplicate_key_update`
|
|
671
|
+
method does **not** take into
|
|
672
|
+
account Python-side default UPDATE values or generation functions, e.g.
|
|
673
|
+
e.g. those specified using :paramref:`_schema.Column.onupdate`.
|
|
674
|
+
These values will not be exercised for an ON DUPLICATE KEY style of UPDATE,
|
|
675
|
+
unless they are manually specified explicitly in the parameters.
|
|
676
|
+
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
In order to refer to the proposed insertion row, the special alias
|
|
680
|
+
:attr:`_mysql.Insert.inserted` is available as an attribute on
|
|
681
|
+
the :class:`_mysql.Insert` object; this object is a
|
|
682
|
+
:class:`_expression.ColumnCollection` which contains all columns of the target
|
|
683
|
+
table:
|
|
684
|
+
|
|
685
|
+
.. sourcecode:: pycon+sql
|
|
686
|
+
|
|
687
|
+
>>> stmt = insert(my_table).values(
|
|
688
|
+
... id="some_id", data="inserted value", author="jlh"
|
|
689
|
+
... )
|
|
690
|
+
|
|
691
|
+
>>> do_update_stmt = stmt.on_duplicate_key_update(
|
|
692
|
+
... data="updated value", author=stmt.inserted.author
|
|
693
|
+
... )
|
|
694
|
+
|
|
695
|
+
>>> print(do_update_stmt)
|
|
696
|
+
{printsql}INSERT INTO my_table (id, data, author) VALUES (%s, %s, %s)
|
|
697
|
+
ON DUPLICATE KEY UPDATE data = %s, author = VALUES(author)
|
|
698
|
+
|
|
699
|
+
When rendered, the "inserted" namespace will produce the expression
|
|
700
|
+
``VALUES(<columnname>)``.
|
|
701
|
+
|
|
702
|
+
.. versionadded:: 1.2 Added support for MySQL ON DUPLICATE KEY UPDATE clause
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
rowcount Support
|
|
707
|
+
----------------
|
|
708
|
+
|
|
709
|
+
SQLAlchemy standardizes the DBAPI ``cursor.rowcount`` attribute to be the
|
|
710
|
+
usual definition of "number of rows matched by an UPDATE or DELETE" statement.
|
|
711
|
+
This is in contradiction to the default setting on most MySQL DBAPI drivers,
|
|
712
|
+
which is "number of rows actually modified/deleted". For this reason, the
|
|
713
|
+
SQLAlchemy MySQL dialects always add the ``constants.CLIENT.FOUND_ROWS``
|
|
714
|
+
flag, or whatever is equivalent for the target dialect, upon connection.
|
|
715
|
+
This setting is currently hardcoded.
|
|
716
|
+
|
|
717
|
+
.. seealso::
|
|
718
|
+
|
|
719
|
+
:attr:`_engine.CursorResult.rowcount`
|
|
720
|
+
|
|
721
|
+
|
|
722
|
+
.. _mysql_indexes:
|
|
723
|
+
|
|
724
|
+
MySQL / MariaDB- Specific Index Options
|
|
725
|
+
-----------------------------------------
|
|
726
|
+
|
|
727
|
+
MySQL and MariaDB-specific extensions to the :class:`.Index` construct are available.
|
|
728
|
+
|
|
729
|
+
Index Length
|
|
730
|
+
~~~~~~~~~~~~~
|
|
731
|
+
|
|
732
|
+
MySQL and MariaDB both provide an option to create index entries with a certain length, where
|
|
733
|
+
"length" refers to the number of characters or bytes in each value which will
|
|
734
|
+
become part of the index. SQLAlchemy provides this feature via the
|
|
735
|
+
``mysql_length`` and/or ``mariadb_length`` parameters::
|
|
736
|
+
|
|
737
|
+
Index("my_index", my_table.c.data, mysql_length=10, mariadb_length=10)
|
|
738
|
+
|
|
739
|
+
Index("a_b_idx", my_table.c.a, my_table.c.b, mysql_length={"a": 4, "b": 9})
|
|
740
|
+
|
|
741
|
+
Index(
|
|
742
|
+
"a_b_idx", my_table.c.a, my_table.c.b, mariadb_length={"a": 4, "b": 9}
|
|
743
|
+
)
|
|
744
|
+
|
|
745
|
+
Prefix lengths are given in characters for nonbinary string types and in bytes
|
|
746
|
+
for binary string types. The value passed to the keyword argument *must* be
|
|
747
|
+
either an integer (and, thus, specify the same prefix length value for all
|
|
748
|
+
columns of the index) or a dict in which keys are column names and values are
|
|
749
|
+
prefix length values for corresponding columns. MySQL and MariaDB only allow a
|
|
750
|
+
length for a column of an index if it is for a CHAR, VARCHAR, TEXT, BINARY,
|
|
751
|
+
VARBINARY and BLOB.
|
|
752
|
+
|
|
753
|
+
Index Prefixes
|
|
754
|
+
~~~~~~~~~~~~~~
|
|
755
|
+
|
|
756
|
+
MySQL storage engines permit you to specify an index prefix when creating
|
|
757
|
+
an index. SQLAlchemy provides this feature via the
|
|
758
|
+
``mysql_prefix`` parameter on :class:`.Index`::
|
|
759
|
+
|
|
760
|
+
Index("my_index", my_table.c.data, mysql_prefix="FULLTEXT")
|
|
761
|
+
|
|
762
|
+
The value passed to the keyword argument will be simply passed through to the
|
|
763
|
+
underlying CREATE INDEX, so it *must* be a valid index prefix for your MySQL
|
|
764
|
+
storage engine.
|
|
765
|
+
|
|
766
|
+
.. seealso::
|
|
767
|
+
|
|
768
|
+
`CREATE INDEX <https://dev.mysql.com/doc/refman/5.0/en/create-index.html>`_ - MySQL documentation
|
|
769
|
+
|
|
770
|
+
Index Types
|
|
771
|
+
~~~~~~~~~~~~~
|
|
772
|
+
|
|
773
|
+
Some MySQL storage engines permit you to specify an index type when creating
|
|
774
|
+
an index or primary key constraint. SQLAlchemy provides this feature via the
|
|
775
|
+
``mysql_using`` parameter on :class:`.Index`::
|
|
776
|
+
|
|
777
|
+
Index(
|
|
778
|
+
"my_index", my_table.c.data, mysql_using="hash", mariadb_using="hash"
|
|
779
|
+
)
|
|
780
|
+
|
|
781
|
+
As well as the ``mysql_using`` parameter on :class:`.PrimaryKeyConstraint`::
|
|
782
|
+
|
|
783
|
+
PrimaryKeyConstraint("data", mysql_using="hash", mariadb_using="hash")
|
|
784
|
+
|
|
785
|
+
The value passed to the keyword argument will be simply passed through to the
|
|
786
|
+
underlying CREATE INDEX or PRIMARY KEY clause, so it *must* be a valid index
|
|
787
|
+
type for your MySQL storage engine.
|
|
788
|
+
|
|
789
|
+
More information can be found at:
|
|
790
|
+
|
|
791
|
+
https://dev.mysql.com/doc/refman/5.0/en/create-index.html
|
|
792
|
+
|
|
793
|
+
https://dev.mysql.com/doc/refman/5.0/en/create-table.html
|
|
794
|
+
|
|
795
|
+
Index Parsers
|
|
796
|
+
~~~~~~~~~~~~~
|
|
797
|
+
|
|
798
|
+
CREATE FULLTEXT INDEX in MySQL also supports a "WITH PARSER" option. This
|
|
799
|
+
is available using the keyword argument ``mysql_with_parser``::
|
|
800
|
+
|
|
801
|
+
Index(
|
|
802
|
+
"my_index",
|
|
803
|
+
my_table.c.data,
|
|
804
|
+
mysql_prefix="FULLTEXT",
|
|
805
|
+
mysql_with_parser="ngram",
|
|
806
|
+
mariadb_prefix="FULLTEXT",
|
|
807
|
+
mariadb_with_parser="ngram",
|
|
808
|
+
)
|
|
809
|
+
|
|
810
|
+
.. versionadded:: 1.3
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
.. _mysql_foreign_keys:
|
|
814
|
+
|
|
815
|
+
MySQL / MariaDB Foreign Keys
|
|
816
|
+
-----------------------------
|
|
817
|
+
|
|
818
|
+
MySQL and MariaDB's behavior regarding foreign keys has some important caveats.
|
|
819
|
+
|
|
820
|
+
Foreign Key Arguments to Avoid
|
|
821
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
822
|
+
|
|
823
|
+
Neither MySQL nor MariaDB support the foreign key arguments "DEFERRABLE", "INITIALLY",
|
|
824
|
+
or "MATCH". Using the ``deferrable`` or ``initially`` keyword argument with
|
|
825
|
+
:class:`_schema.ForeignKeyConstraint` or :class:`_schema.ForeignKey`
|
|
826
|
+
will have the effect of
|
|
827
|
+
these keywords being rendered in a DDL expression, which will then raise an
|
|
828
|
+
error on MySQL or MariaDB. In order to use these keywords on a foreign key while having
|
|
829
|
+
them ignored on a MySQL / MariaDB backend, use a custom compile rule::
|
|
830
|
+
|
|
831
|
+
from sqlalchemy.ext.compiler import compiles
|
|
832
|
+
from sqlalchemy.schema import ForeignKeyConstraint
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
@compiles(ForeignKeyConstraint, "mysql", "mariadb")
|
|
836
|
+
def process(element, compiler, **kw):
|
|
837
|
+
element.deferrable = element.initially = None
|
|
838
|
+
return compiler.visit_foreign_key_constraint(element, **kw)
|
|
839
|
+
|
|
840
|
+
The "MATCH" keyword is in fact more insidious, and is explicitly disallowed
|
|
841
|
+
by SQLAlchemy in conjunction with the MySQL or MariaDB backends. This argument is
|
|
842
|
+
silently ignored by MySQL / MariaDB, but in addition has the effect of ON UPDATE and ON
|
|
843
|
+
DELETE options also being ignored by the backend. Therefore MATCH should
|
|
844
|
+
never be used with the MySQL / MariaDB backends; as is the case with DEFERRABLE and
|
|
845
|
+
INITIALLY, custom compilation rules can be used to correct a
|
|
846
|
+
ForeignKeyConstraint at DDL definition time.
|
|
847
|
+
|
|
848
|
+
Reflection of Foreign Key Constraints
|
|
849
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
850
|
+
|
|
851
|
+
Not all MySQL / MariaDB storage engines support foreign keys. When using the
|
|
852
|
+
very common ``MyISAM`` MySQL storage engine, the information loaded by table
|
|
853
|
+
reflection will not include foreign keys. For these tables, you may supply a
|
|
854
|
+
:class:`~sqlalchemy.ForeignKeyConstraint` at reflection time::
|
|
855
|
+
|
|
856
|
+
Table(
|
|
857
|
+
"mytable",
|
|
858
|
+
metadata,
|
|
859
|
+
ForeignKeyConstraint(["other_id"], ["othertable.other_id"]),
|
|
860
|
+
autoload_with=engine,
|
|
861
|
+
)
|
|
862
|
+
|
|
863
|
+
.. seealso::
|
|
864
|
+
|
|
865
|
+
:ref:`mysql_storage_engines`
|
|
866
|
+
|
|
867
|
+
.. _mysql_unique_constraints:
|
|
868
|
+
|
|
869
|
+
MySQL / MariaDB Unique Constraints and Reflection
|
|
870
|
+
----------------------------------------------------
|
|
871
|
+
|
|
872
|
+
SQLAlchemy supports both the :class:`.Index` construct with the
|
|
873
|
+
flag ``unique=True``, indicating a UNIQUE index, as well as the
|
|
874
|
+
:class:`.UniqueConstraint` construct, representing a UNIQUE constraint.
|
|
875
|
+
Both objects/syntaxes are supported by MySQL / MariaDB when emitting DDL to create
|
|
876
|
+
these constraints. However, MySQL / MariaDB does not have a unique constraint
|
|
877
|
+
construct that is separate from a unique index; that is, the "UNIQUE"
|
|
878
|
+
constraint on MySQL / MariaDB is equivalent to creating a "UNIQUE INDEX".
|
|
879
|
+
|
|
880
|
+
When reflecting these constructs, the
|
|
881
|
+
:meth:`_reflection.Inspector.get_indexes`
|
|
882
|
+
and the :meth:`_reflection.Inspector.get_unique_constraints`
|
|
883
|
+
methods will **both**
|
|
884
|
+
return an entry for a UNIQUE index in MySQL / MariaDB. However, when performing
|
|
885
|
+
full table reflection using ``Table(..., autoload_with=engine)``,
|
|
886
|
+
the :class:`.UniqueConstraint` construct is
|
|
887
|
+
**not** part of the fully reflected :class:`_schema.Table` construct under any
|
|
888
|
+
circumstances; this construct is always represented by a :class:`.Index`
|
|
889
|
+
with the ``unique=True`` setting present in the :attr:`_schema.Table.indexes`
|
|
890
|
+
collection.
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
TIMESTAMP / DATETIME issues
|
|
894
|
+
---------------------------
|
|
895
|
+
|
|
896
|
+
.. _mysql_timestamp_onupdate:
|
|
897
|
+
|
|
898
|
+
Rendering ON UPDATE CURRENT TIMESTAMP for MySQL / MariaDB's explicit_defaults_for_timestamp
|
|
899
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
900
|
+
|
|
901
|
+
MySQL / MariaDB have historically expanded the DDL for the :class:`_types.TIMESTAMP`
|
|
902
|
+
datatype into the phrase "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE
|
|
903
|
+
CURRENT_TIMESTAMP", which includes non-standard SQL that automatically updates
|
|
904
|
+
the column with the current timestamp when an UPDATE occurs, eliminating the
|
|
905
|
+
usual need to use a trigger in such a case where server-side update changes are
|
|
906
|
+
desired.
|
|
907
|
+
|
|
908
|
+
MySQL 5.6 introduced a new flag `explicit_defaults_for_timestamp
|
|
909
|
+
<https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html
|
|
910
|
+
#sysvar_explicit_defaults_for_timestamp>`_ which disables the above behavior,
|
|
911
|
+
and in MySQL 8 this flag defaults to true, meaning in order to get a MySQL
|
|
912
|
+
"on update timestamp" without changing this flag, the above DDL must be
|
|
913
|
+
rendered explicitly. Additionally, the same DDL is valid for use of the
|
|
914
|
+
``DATETIME`` datatype as well.
|
|
915
|
+
|
|
916
|
+
SQLAlchemy's MySQL dialect does not yet have an option to generate
|
|
917
|
+
MySQL's "ON UPDATE CURRENT_TIMESTAMP" clause, noting that this is not a general
|
|
918
|
+
purpose "ON UPDATE" as there is no such syntax in standard SQL. SQLAlchemy's
|
|
919
|
+
:paramref:`_schema.Column.server_onupdate` parameter is currently not related
|
|
920
|
+
to this special MySQL behavior.
|
|
921
|
+
|
|
922
|
+
To generate this DDL, make use of the :paramref:`_schema.Column.server_default`
|
|
923
|
+
parameter and pass a textual clause that also includes the ON UPDATE clause::
|
|
924
|
+
|
|
925
|
+
from sqlalchemy import Table, MetaData, Column, Integer, String, TIMESTAMP
|
|
926
|
+
from sqlalchemy import text
|
|
927
|
+
|
|
928
|
+
metadata = MetaData()
|
|
929
|
+
|
|
930
|
+
mytable = Table(
|
|
931
|
+
"mytable",
|
|
932
|
+
metadata,
|
|
933
|
+
Column("id", Integer, primary_key=True),
|
|
934
|
+
Column("data", String(50)),
|
|
935
|
+
Column(
|
|
936
|
+
"last_updated",
|
|
937
|
+
TIMESTAMP,
|
|
938
|
+
server_default=text(
|
|
939
|
+
"CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
|
|
940
|
+
),
|
|
941
|
+
),
|
|
942
|
+
)
|
|
943
|
+
|
|
944
|
+
The same instructions apply to use of the :class:`_types.DateTime` and
|
|
945
|
+
:class:`_types.DATETIME` datatypes::
|
|
946
|
+
|
|
947
|
+
from sqlalchemy import DateTime
|
|
948
|
+
|
|
949
|
+
mytable = Table(
|
|
950
|
+
"mytable",
|
|
951
|
+
metadata,
|
|
952
|
+
Column("id", Integer, primary_key=True),
|
|
953
|
+
Column("data", String(50)),
|
|
954
|
+
Column(
|
|
955
|
+
"last_updated",
|
|
956
|
+
DateTime,
|
|
957
|
+
server_default=text(
|
|
958
|
+
"CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
|
|
959
|
+
),
|
|
960
|
+
),
|
|
961
|
+
)
|
|
962
|
+
|
|
963
|
+
Even though the :paramref:`_schema.Column.server_onupdate` feature does not
|
|
964
|
+
generate this DDL, it still may be desirable to signal to the ORM that this
|
|
965
|
+
updated value should be fetched. This syntax looks like the following::
|
|
966
|
+
|
|
967
|
+
from sqlalchemy.schema import FetchedValue
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
class MyClass(Base):
|
|
971
|
+
__tablename__ = "mytable"
|
|
972
|
+
|
|
973
|
+
id = Column(Integer, primary_key=True)
|
|
974
|
+
data = Column(String(50))
|
|
975
|
+
last_updated = Column(
|
|
976
|
+
TIMESTAMP,
|
|
977
|
+
server_default=text(
|
|
978
|
+
"CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
|
|
979
|
+
),
|
|
980
|
+
server_onupdate=FetchedValue(),
|
|
981
|
+
)
|
|
982
|
+
|
|
983
|
+
.. _mysql_timestamp_null:
|
|
984
|
+
|
|
985
|
+
TIMESTAMP Columns and NULL
|
|
986
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
987
|
+
|
|
988
|
+
MySQL historically enforces that a column which specifies the
|
|
989
|
+
TIMESTAMP datatype implicitly includes a default value of
|
|
990
|
+
CURRENT_TIMESTAMP, even though this is not stated, and additionally
|
|
991
|
+
sets the column as NOT NULL, the opposite behavior vs. that of all
|
|
992
|
+
other datatypes:
|
|
993
|
+
|
|
994
|
+
.. sourcecode:: text
|
|
995
|
+
|
|
996
|
+
mysql> CREATE TABLE ts_test (
|
|
997
|
+
-> a INTEGER,
|
|
998
|
+
-> b INTEGER NOT NULL,
|
|
999
|
+
-> c TIMESTAMP,
|
|
1000
|
+
-> d TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
1001
|
+
-> e TIMESTAMP NULL);
|
|
1002
|
+
Query OK, 0 rows affected (0.03 sec)
|
|
1003
|
+
|
|
1004
|
+
mysql> SHOW CREATE TABLE ts_test;
|
|
1005
|
+
+---------+-----------------------------------------------------
|
|
1006
|
+
| Table | Create Table
|
|
1007
|
+
+---------+-----------------------------------------------------
|
|
1008
|
+
| ts_test | CREATE TABLE `ts_test` (
|
|
1009
|
+
`a` int(11) DEFAULT NULL,
|
|
1010
|
+
`b` int(11) NOT NULL,
|
|
1011
|
+
`c` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
|
1012
|
+
`d` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
1013
|
+
`e` timestamp NULL DEFAULT NULL
|
|
1014
|
+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
|
|
1015
|
+
|
|
1016
|
+
Above, we see that an INTEGER column defaults to NULL, unless it is specified
|
|
1017
|
+
with NOT NULL. But when the column is of type TIMESTAMP, an implicit
|
|
1018
|
+
default of CURRENT_TIMESTAMP is generated which also coerces the column
|
|
1019
|
+
to be a NOT NULL, even though we did not specify it as such.
|
|
1020
|
+
|
|
1021
|
+
This behavior of MySQL can be changed on the MySQL side using the
|
|
1022
|
+
`explicit_defaults_for_timestamp
|
|
1023
|
+
<https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html
|
|
1024
|
+
#sysvar_explicit_defaults_for_timestamp>`_ configuration flag introduced in
|
|
1025
|
+
MySQL 5.6. With this server setting enabled, TIMESTAMP columns behave like
|
|
1026
|
+
any other datatype on the MySQL side with regards to defaults and nullability.
|
|
1027
|
+
|
|
1028
|
+
However, to accommodate the vast majority of MySQL databases that do not
|
|
1029
|
+
specify this new flag, SQLAlchemy emits the "NULL" specifier explicitly with
|
|
1030
|
+
any TIMESTAMP column that does not specify ``nullable=False``. In order to
|
|
1031
|
+
accommodate newer databases that specify ``explicit_defaults_for_timestamp``,
|
|
1032
|
+
SQLAlchemy also emits NOT NULL for TIMESTAMP columns that do specify
|
|
1033
|
+
``nullable=False``. The following example illustrates::
|
|
1034
|
+
|
|
1035
|
+
from sqlalchemy import MetaData, Integer, Table, Column, text
|
|
1036
|
+
from sqlalchemy.dialects.mysql import TIMESTAMP
|
|
1037
|
+
|
|
1038
|
+
m = MetaData()
|
|
1039
|
+
t = Table(
|
|
1040
|
+
"ts_test",
|
|
1041
|
+
m,
|
|
1042
|
+
Column("a", Integer),
|
|
1043
|
+
Column("b", Integer, nullable=False),
|
|
1044
|
+
Column("c", TIMESTAMP),
|
|
1045
|
+
Column("d", TIMESTAMP, nullable=False),
|
|
1046
|
+
)
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
from sqlalchemy import create_engine
|
|
1050
|
+
|
|
1051
|
+
e = create_engine("mysql+mysqldb://scott:tiger@localhost/test", echo=True)
|
|
1052
|
+
m.create_all(e)
|
|
1053
|
+
|
|
1054
|
+
output:
|
|
1055
|
+
|
|
1056
|
+
.. sourcecode:: sql
|
|
1057
|
+
|
|
1058
|
+
CREATE TABLE ts_test (
|
|
1059
|
+
a INTEGER,
|
|
1060
|
+
b INTEGER NOT NULL,
|
|
1061
|
+
c TIMESTAMP NULL,
|
|
1062
|
+
d TIMESTAMP NOT NULL
|
|
1063
|
+
)
|
|
1064
|
+
|
|
1065
|
+
""" # noqa
|
|
1066
|
+
from __future__ import annotations
|
|
1067
|
+
|
|
1068
|
+
from collections import defaultdict
|
|
1069
|
+
from itertools import compress
|
|
1070
|
+
import re
|
|
1071
|
+
from typing import Any
|
|
1072
|
+
from typing import Callable
|
|
1073
|
+
from typing import cast
|
|
1074
|
+
from typing import DefaultDict
|
|
1075
|
+
from typing import Dict
|
|
1076
|
+
from typing import List
|
|
1077
|
+
from typing import NoReturn
|
|
1078
|
+
from typing import Optional
|
|
1079
|
+
from typing import overload
|
|
1080
|
+
from typing import Sequence
|
|
1081
|
+
from typing import Tuple
|
|
1082
|
+
from typing import TYPE_CHECKING
|
|
1083
|
+
from typing import Union
|
|
1084
|
+
|
|
1085
|
+
from . import reflection as _reflection
|
|
1086
|
+
from .enumerated import ENUM
|
|
1087
|
+
from .enumerated import SET
|
|
1088
|
+
from .json import JSON
|
|
1089
|
+
from .json import JSONIndexType
|
|
1090
|
+
from .json import JSONPathType
|
|
1091
|
+
from .reserved_words import RESERVED_WORDS_MARIADB
|
|
1092
|
+
from .reserved_words import RESERVED_WORDS_MYSQL
|
|
1093
|
+
from .types import _FloatType
|
|
1094
|
+
from .types import _IntegerType
|
|
1095
|
+
from .types import _MatchType
|
|
1096
|
+
from .types import _NumericType
|
|
1097
|
+
from .types import _StringType
|
|
1098
|
+
from .types import BIGINT
|
|
1099
|
+
from .types import BIT
|
|
1100
|
+
from .types import CHAR
|
|
1101
|
+
from .types import DATETIME
|
|
1102
|
+
from .types import DECIMAL
|
|
1103
|
+
from .types import DOUBLE
|
|
1104
|
+
from .types import FLOAT
|
|
1105
|
+
from .types import INTEGER
|
|
1106
|
+
from .types import LONGBLOB
|
|
1107
|
+
from .types import LONGTEXT
|
|
1108
|
+
from .types import MEDIUMBLOB
|
|
1109
|
+
from .types import MEDIUMINT
|
|
1110
|
+
from .types import MEDIUMTEXT
|
|
1111
|
+
from .types import NCHAR
|
|
1112
|
+
from .types import NUMERIC
|
|
1113
|
+
from .types import NVARCHAR
|
|
1114
|
+
from .types import REAL
|
|
1115
|
+
from .types import SMALLINT
|
|
1116
|
+
from .types import TEXT
|
|
1117
|
+
from .types import TIME
|
|
1118
|
+
from .types import TIMESTAMP
|
|
1119
|
+
from .types import TINYBLOB
|
|
1120
|
+
from .types import TINYINT
|
|
1121
|
+
from .types import TINYTEXT
|
|
1122
|
+
from .types import VARCHAR
|
|
1123
|
+
from .types import YEAR
|
|
1124
|
+
from ... import exc
|
|
1125
|
+
from ... import literal_column
|
|
1126
|
+
from ... import schema as sa_schema
|
|
1127
|
+
from ... import sql
|
|
1128
|
+
from ... import util
|
|
1129
|
+
from ...engine import cursor as _cursor
|
|
1130
|
+
from ...engine import default
|
|
1131
|
+
from ...engine import reflection
|
|
1132
|
+
from ...engine.reflection import ReflectionDefaults
|
|
1133
|
+
from ...sql import coercions
|
|
1134
|
+
from ...sql import compiler
|
|
1135
|
+
from ...sql import elements
|
|
1136
|
+
from ...sql import functions
|
|
1137
|
+
from ...sql import operators
|
|
1138
|
+
from ...sql import roles
|
|
1139
|
+
from ...sql import sqltypes
|
|
1140
|
+
from ...sql import util as sql_util
|
|
1141
|
+
from ...sql import visitors
|
|
1142
|
+
from ...sql.compiler import InsertmanyvaluesSentinelOpts
|
|
1143
|
+
from ...sql.compiler import SQLCompiler
|
|
1144
|
+
from ...sql.schema import SchemaConst
|
|
1145
|
+
from ...types import BINARY
|
|
1146
|
+
from ...types import BLOB
|
|
1147
|
+
from ...types import BOOLEAN
|
|
1148
|
+
from ...types import DATE
|
|
1149
|
+
from ...types import LargeBinary
|
|
1150
|
+
from ...types import UUID
|
|
1151
|
+
from ...types import VARBINARY
|
|
1152
|
+
from ...util import topological
|
|
1153
|
+
|
|
1154
|
+
if TYPE_CHECKING:
|
|
1155
|
+
|
|
1156
|
+
from ...dialects.mysql import expression
|
|
1157
|
+
from ...dialects.mysql.dml import OnDuplicateClause
|
|
1158
|
+
from ...engine.base import Connection
|
|
1159
|
+
from ...engine.cursor import CursorResult
|
|
1160
|
+
from ...engine.interfaces import DBAPIConnection
|
|
1161
|
+
from ...engine.interfaces import DBAPICursor
|
|
1162
|
+
from ...engine.interfaces import DBAPIModule
|
|
1163
|
+
from ...engine.interfaces import IsolationLevel
|
|
1164
|
+
from ...engine.interfaces import PoolProxiedConnection
|
|
1165
|
+
from ...engine.interfaces import ReflectedCheckConstraint
|
|
1166
|
+
from ...engine.interfaces import ReflectedColumn
|
|
1167
|
+
from ...engine.interfaces import ReflectedForeignKeyConstraint
|
|
1168
|
+
from ...engine.interfaces import ReflectedIndex
|
|
1169
|
+
from ...engine.interfaces import ReflectedPrimaryKeyConstraint
|
|
1170
|
+
from ...engine.interfaces import ReflectedTableComment
|
|
1171
|
+
from ...engine.interfaces import ReflectedUniqueConstraint
|
|
1172
|
+
from ...engine.row import Row
|
|
1173
|
+
from ...engine.url import URL
|
|
1174
|
+
from ...schema import Table
|
|
1175
|
+
from ...sql import ddl
|
|
1176
|
+
from ...sql import selectable
|
|
1177
|
+
from ...sql.dml import _DMLTableElement
|
|
1178
|
+
from ...sql.dml import Delete
|
|
1179
|
+
from ...sql.dml import Update
|
|
1180
|
+
from ...sql.dml import ValuesBase
|
|
1181
|
+
from ...sql.functions import aggregate_strings
|
|
1182
|
+
from ...sql.functions import random
|
|
1183
|
+
from ...sql.functions import rollup
|
|
1184
|
+
from ...sql.functions import sysdate
|
|
1185
|
+
from ...sql.schema import IdentityOptions
|
|
1186
|
+
from ...sql.schema import Sequence as Sequence_SchemaItem
|
|
1187
|
+
from ...sql.type_api import TypeEngine
|
|
1188
|
+
from ...sql.visitors import ExternallyTraversible
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
SET_RE = re.compile(
|
|
1192
|
+
r"\s*SET\s+(?:(?:GLOBAL|SESSION)\s+)?\w", re.I | re.UNICODE
|
|
1193
|
+
)
|
|
1194
|
+
|
|
1195
|
+
# old names
|
|
1196
|
+
MSTime = TIME
|
|
1197
|
+
MSSet = SET
|
|
1198
|
+
MSEnum = ENUM
|
|
1199
|
+
MSLongBlob = LONGBLOB
|
|
1200
|
+
MSMediumBlob = MEDIUMBLOB
|
|
1201
|
+
MSTinyBlob = TINYBLOB
|
|
1202
|
+
MSBlob = BLOB
|
|
1203
|
+
MSBinary = BINARY
|
|
1204
|
+
MSVarBinary = VARBINARY
|
|
1205
|
+
MSNChar = NCHAR
|
|
1206
|
+
MSNVarChar = NVARCHAR
|
|
1207
|
+
MSChar = CHAR
|
|
1208
|
+
MSString = VARCHAR
|
|
1209
|
+
MSLongText = LONGTEXT
|
|
1210
|
+
MSMediumText = MEDIUMTEXT
|
|
1211
|
+
MSTinyText = TINYTEXT
|
|
1212
|
+
MSText = TEXT
|
|
1213
|
+
MSYear = YEAR
|
|
1214
|
+
MSTimeStamp = TIMESTAMP
|
|
1215
|
+
MSBit = BIT
|
|
1216
|
+
MSSmallInteger = SMALLINT
|
|
1217
|
+
MSTinyInteger = TINYINT
|
|
1218
|
+
MSMediumInteger = MEDIUMINT
|
|
1219
|
+
MSBigInteger = BIGINT
|
|
1220
|
+
MSNumeric = NUMERIC
|
|
1221
|
+
MSDecimal = DECIMAL
|
|
1222
|
+
MSDouble = DOUBLE
|
|
1223
|
+
MSReal = REAL
|
|
1224
|
+
MSFloat = FLOAT
|
|
1225
|
+
MSInteger = INTEGER
|
|
1226
|
+
|
|
1227
|
+
colspecs = {
|
|
1228
|
+
_IntegerType: _IntegerType,
|
|
1229
|
+
_NumericType: _NumericType,
|
|
1230
|
+
_FloatType: _FloatType,
|
|
1231
|
+
sqltypes.Numeric: NUMERIC,
|
|
1232
|
+
sqltypes.Float: FLOAT,
|
|
1233
|
+
sqltypes.Double: DOUBLE,
|
|
1234
|
+
sqltypes.Time: TIME,
|
|
1235
|
+
sqltypes.Enum: ENUM,
|
|
1236
|
+
sqltypes.MatchType: _MatchType,
|
|
1237
|
+
sqltypes.JSON: JSON,
|
|
1238
|
+
sqltypes.JSON.JSONIndexType: JSONIndexType,
|
|
1239
|
+
sqltypes.JSON.JSONPathType: JSONPathType,
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
# Everything 3.23 through 5.1 excepting OpenGIS types.
|
|
1243
|
+
ischema_names = {
|
|
1244
|
+
"bigint": BIGINT,
|
|
1245
|
+
"binary": BINARY,
|
|
1246
|
+
"bit": BIT,
|
|
1247
|
+
"blob": BLOB,
|
|
1248
|
+
"boolean": BOOLEAN,
|
|
1249
|
+
"char": CHAR,
|
|
1250
|
+
"date": DATE,
|
|
1251
|
+
"datetime": DATETIME,
|
|
1252
|
+
"decimal": DECIMAL,
|
|
1253
|
+
"double": DOUBLE,
|
|
1254
|
+
"enum": ENUM,
|
|
1255
|
+
"fixed": DECIMAL,
|
|
1256
|
+
"float": FLOAT,
|
|
1257
|
+
"int": INTEGER,
|
|
1258
|
+
"integer": INTEGER,
|
|
1259
|
+
"json": JSON,
|
|
1260
|
+
"longblob": LONGBLOB,
|
|
1261
|
+
"longtext": LONGTEXT,
|
|
1262
|
+
"mediumblob": MEDIUMBLOB,
|
|
1263
|
+
"mediumint": MEDIUMINT,
|
|
1264
|
+
"mediumtext": MEDIUMTEXT,
|
|
1265
|
+
"nchar": NCHAR,
|
|
1266
|
+
"nvarchar": NVARCHAR,
|
|
1267
|
+
"numeric": NUMERIC,
|
|
1268
|
+
"set": SET,
|
|
1269
|
+
"smallint": SMALLINT,
|
|
1270
|
+
"text": TEXT,
|
|
1271
|
+
"time": TIME,
|
|
1272
|
+
"timestamp": TIMESTAMP,
|
|
1273
|
+
"tinyblob": TINYBLOB,
|
|
1274
|
+
"tinyint": TINYINT,
|
|
1275
|
+
"tinytext": TINYTEXT,
|
|
1276
|
+
"uuid": UUID,
|
|
1277
|
+
"varbinary": VARBINARY,
|
|
1278
|
+
"varchar": VARCHAR,
|
|
1279
|
+
"year": YEAR,
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
|
|
1283
|
+
class MySQLExecutionContext(default.DefaultExecutionContext):
|
|
1284
|
+
def post_exec(self) -> None:
|
|
1285
|
+
if (
|
|
1286
|
+
self.isdelete
|
|
1287
|
+
and cast(SQLCompiler, self.compiled).effective_returning
|
|
1288
|
+
and not self.cursor.description
|
|
1289
|
+
):
|
|
1290
|
+
# All MySQL/mariadb drivers appear to not include
|
|
1291
|
+
# cursor.description for DELETE..RETURNING with no rows if the
|
|
1292
|
+
# WHERE criteria is a straight "false" condition such as our EMPTY
|
|
1293
|
+
# IN condition. manufacture an empty result in this case (issue
|
|
1294
|
+
# #10505)
|
|
1295
|
+
#
|
|
1296
|
+
# taken from cx_Oracle implementation
|
|
1297
|
+
self.cursor_fetch_strategy = (
|
|
1298
|
+
_cursor.FullyBufferedCursorFetchStrategy(
|
|
1299
|
+
self.cursor,
|
|
1300
|
+
[
|
|
1301
|
+
(entry.keyname, None) # type: ignore[misc]
|
|
1302
|
+
for entry in cast(
|
|
1303
|
+
SQLCompiler, self.compiled
|
|
1304
|
+
)._result_columns
|
|
1305
|
+
],
|
|
1306
|
+
[],
|
|
1307
|
+
)
|
|
1308
|
+
)
|
|
1309
|
+
|
|
1310
|
+
def create_server_side_cursor(self) -> DBAPICursor:
|
|
1311
|
+
if self.dialect.supports_server_side_cursors:
|
|
1312
|
+
return self._dbapi_connection.cursor(
|
|
1313
|
+
self.dialect._sscursor # type: ignore[attr-defined]
|
|
1314
|
+
)
|
|
1315
|
+
else:
|
|
1316
|
+
raise NotImplementedError()
|
|
1317
|
+
|
|
1318
|
+
def fire_sequence(
|
|
1319
|
+
self, seq: Sequence_SchemaItem, type_: sqltypes.Integer
|
|
1320
|
+
) -> int:
|
|
1321
|
+
return self._execute_scalar( # type: ignore[no-any-return]
|
|
1322
|
+
(
|
|
1323
|
+
"select nextval(%s)"
|
|
1324
|
+
% self.identifier_preparer.format_sequence(seq)
|
|
1325
|
+
),
|
|
1326
|
+
type_,
|
|
1327
|
+
)
|
|
1328
|
+
|
|
1329
|
+
|
|
1330
|
+
class MySQLCompiler(compiler.SQLCompiler):
|
|
1331
|
+
dialect: MySQLDialect
|
|
1332
|
+
render_table_with_column_in_update_from = True
|
|
1333
|
+
"""Overridden from base SQLCompiler value"""
|
|
1334
|
+
|
|
1335
|
+
extract_map = compiler.SQLCompiler.extract_map.copy()
|
|
1336
|
+
extract_map.update({"milliseconds": "millisecond"})
|
|
1337
|
+
|
|
1338
|
+
def default_from(self) -> str:
|
|
1339
|
+
"""Called when a ``SELECT`` statement has no froms,
|
|
1340
|
+
and no ``FROM`` clause is to be appended.
|
|
1341
|
+
|
|
1342
|
+
"""
|
|
1343
|
+
if self.stack:
|
|
1344
|
+
stmt = self.stack[-1]["selectable"]
|
|
1345
|
+
if stmt._where_criteria: # type: ignore[attr-defined]
|
|
1346
|
+
return " FROM DUAL"
|
|
1347
|
+
|
|
1348
|
+
return ""
|
|
1349
|
+
|
|
1350
|
+
def visit_random_func(self, fn: random, **kw: Any) -> str:
|
|
1351
|
+
return "rand%s" % self.function_argspec(fn)
|
|
1352
|
+
|
|
1353
|
+
def visit_rollup_func(self, fn: rollup[Any], **kw: Any) -> str:
|
|
1354
|
+
clause = ", ".join(
|
|
1355
|
+
elem._compiler_dispatch(self, **kw) for elem in fn.clauses
|
|
1356
|
+
)
|
|
1357
|
+
return f"{clause} WITH ROLLUP"
|
|
1358
|
+
|
|
1359
|
+
def visit_aggregate_strings_func(
|
|
1360
|
+
self, fn: aggregate_strings, **kw: Any
|
|
1361
|
+
) -> str:
|
|
1362
|
+
expr, delimiter = (
|
|
1363
|
+
elem._compiler_dispatch(self, **kw) for elem in fn.clauses
|
|
1364
|
+
)
|
|
1365
|
+
return f"group_concat({expr} SEPARATOR {delimiter})"
|
|
1366
|
+
|
|
1367
|
+
def visit_sequence(self, sequence: sa_schema.Sequence, **kw: Any) -> str:
|
|
1368
|
+
return "nextval(%s)" % self.preparer.format_sequence(sequence)
|
|
1369
|
+
|
|
1370
|
+
def visit_sysdate_func(self, fn: sysdate, **kw: Any) -> str:
|
|
1371
|
+
return "SYSDATE()"
|
|
1372
|
+
|
|
1373
|
+
def _render_json_extract_from_binary(
|
|
1374
|
+
self, binary: elements.BinaryExpression[Any], operator: Any, **kw: Any
|
|
1375
|
+
) -> str:
|
|
1376
|
+
# note we are intentionally calling upon the process() calls in the
|
|
1377
|
+
# order in which they appear in the SQL String as this is used
|
|
1378
|
+
# by positional parameter rendering
|
|
1379
|
+
|
|
1380
|
+
if binary.type._type_affinity is sqltypes.JSON:
|
|
1381
|
+
return "JSON_EXTRACT(%s, %s)" % (
|
|
1382
|
+
self.process(binary.left, **kw),
|
|
1383
|
+
self.process(binary.right, **kw),
|
|
1384
|
+
)
|
|
1385
|
+
|
|
1386
|
+
# for non-JSON, MySQL doesn't handle JSON null at all so it has to
|
|
1387
|
+
# be explicit
|
|
1388
|
+
case_expression = "CASE JSON_EXTRACT(%s, %s) WHEN 'null' THEN NULL" % (
|
|
1389
|
+
self.process(binary.left, **kw),
|
|
1390
|
+
self.process(binary.right, **kw),
|
|
1391
|
+
)
|
|
1392
|
+
|
|
1393
|
+
if binary.type._type_affinity is sqltypes.Integer:
|
|
1394
|
+
type_expression = (
|
|
1395
|
+
"ELSE CAST(JSON_EXTRACT(%s, %s) AS SIGNED INTEGER)"
|
|
1396
|
+
% (
|
|
1397
|
+
self.process(binary.left, **kw),
|
|
1398
|
+
self.process(binary.right, **kw),
|
|
1399
|
+
)
|
|
1400
|
+
)
|
|
1401
|
+
elif binary.type._type_affinity is sqltypes.Numeric:
|
|
1402
|
+
binary_type = cast(sqltypes.Numeric[Any], binary.type)
|
|
1403
|
+
if (
|
|
1404
|
+
binary_type.scale is not None
|
|
1405
|
+
and binary_type.precision is not None
|
|
1406
|
+
):
|
|
1407
|
+
# using DECIMAL here because MySQL does not recognize NUMERIC
|
|
1408
|
+
type_expression = (
|
|
1409
|
+
"ELSE CAST(JSON_EXTRACT(%s, %s) AS DECIMAL(%s, %s))"
|
|
1410
|
+
% (
|
|
1411
|
+
self.process(binary.left, **kw),
|
|
1412
|
+
self.process(binary.right, **kw),
|
|
1413
|
+
binary_type.precision,
|
|
1414
|
+
binary_type.scale,
|
|
1415
|
+
)
|
|
1416
|
+
)
|
|
1417
|
+
else:
|
|
1418
|
+
# FLOAT / REAL not added in MySQL til 8.0.17
|
|
1419
|
+
type_expression = (
|
|
1420
|
+
"ELSE JSON_EXTRACT(%s, %s)+0.0000000000000000000000"
|
|
1421
|
+
% (
|
|
1422
|
+
self.process(binary.left, **kw),
|
|
1423
|
+
self.process(binary.right, **kw),
|
|
1424
|
+
)
|
|
1425
|
+
)
|
|
1426
|
+
elif binary.type._type_affinity is sqltypes.Boolean:
|
|
1427
|
+
# the NULL handling is particularly weird with boolean, so
|
|
1428
|
+
# explicitly return true/false constants
|
|
1429
|
+
type_expression = "WHEN true THEN true ELSE false"
|
|
1430
|
+
elif binary.type._type_affinity is sqltypes.String:
|
|
1431
|
+
# (gord): this fails with a JSON value that's a four byte unicode
|
|
1432
|
+
# string. SQLite has the same problem at the moment
|
|
1433
|
+
# (zzzeek): I'm not really sure. let's take a look at a test case
|
|
1434
|
+
# that hits each backend and maybe make a requires rule for it?
|
|
1435
|
+
type_expression = "ELSE JSON_UNQUOTE(JSON_EXTRACT(%s, %s))" % (
|
|
1436
|
+
self.process(binary.left, **kw),
|
|
1437
|
+
self.process(binary.right, **kw),
|
|
1438
|
+
)
|
|
1439
|
+
else:
|
|
1440
|
+
# other affinity....this is not expected right now
|
|
1441
|
+
type_expression = "ELSE JSON_EXTRACT(%s, %s)" % (
|
|
1442
|
+
self.process(binary.left, **kw),
|
|
1443
|
+
self.process(binary.right, **kw),
|
|
1444
|
+
)
|
|
1445
|
+
|
|
1446
|
+
return case_expression + " " + type_expression + " END"
|
|
1447
|
+
|
|
1448
|
+
def visit_json_getitem_op_binary(
|
|
1449
|
+
self, binary: elements.BinaryExpression[Any], operator: Any, **kw: Any
|
|
1450
|
+
) -> str:
|
|
1451
|
+
return self._render_json_extract_from_binary(binary, operator, **kw)
|
|
1452
|
+
|
|
1453
|
+
def visit_json_path_getitem_op_binary(
|
|
1454
|
+
self, binary: elements.BinaryExpression[Any], operator: Any, **kw: Any
|
|
1455
|
+
) -> str:
|
|
1456
|
+
return self._render_json_extract_from_binary(binary, operator, **kw)
|
|
1457
|
+
|
|
1458
|
+
def visit_on_duplicate_key_update(
|
|
1459
|
+
self, on_duplicate: OnDuplicateClause, **kw: Any
|
|
1460
|
+
) -> str:
|
|
1461
|
+
statement: ValuesBase = self.current_executable
|
|
1462
|
+
|
|
1463
|
+
cols: List[elements.KeyedColumnElement[Any]]
|
|
1464
|
+
if on_duplicate._parameter_ordering:
|
|
1465
|
+
parameter_ordering = [
|
|
1466
|
+
coercions.expect(roles.DMLColumnRole, key)
|
|
1467
|
+
for key in on_duplicate._parameter_ordering
|
|
1468
|
+
]
|
|
1469
|
+
ordered_keys = set(parameter_ordering)
|
|
1470
|
+
cols = [
|
|
1471
|
+
statement.table.c[key]
|
|
1472
|
+
for key in parameter_ordering
|
|
1473
|
+
if key in statement.table.c
|
|
1474
|
+
] + [c for c in statement.table.c if c.key not in ordered_keys]
|
|
1475
|
+
else:
|
|
1476
|
+
cols = list(statement.table.c)
|
|
1477
|
+
|
|
1478
|
+
clauses = []
|
|
1479
|
+
|
|
1480
|
+
requires_mysql8_alias = statement.select is None and (
|
|
1481
|
+
self.dialect._requires_alias_for_on_duplicate_key
|
|
1482
|
+
)
|
|
1483
|
+
|
|
1484
|
+
if requires_mysql8_alias:
|
|
1485
|
+
if statement.table.name.lower() == "new": # type: ignore[union-attr] # noqa: E501
|
|
1486
|
+
_on_dup_alias_name = "new_1"
|
|
1487
|
+
else:
|
|
1488
|
+
_on_dup_alias_name = "new"
|
|
1489
|
+
|
|
1490
|
+
on_duplicate_update = {
|
|
1491
|
+
coercions.expect_as_key(roles.DMLColumnRole, key): value
|
|
1492
|
+
for key, value in on_duplicate.update.items()
|
|
1493
|
+
}
|
|
1494
|
+
|
|
1495
|
+
# traverses through all table columns to preserve table column order
|
|
1496
|
+
for column in (col for col in cols if col.key in on_duplicate_update):
|
|
1497
|
+
val = on_duplicate_update[column.key]
|
|
1498
|
+
|
|
1499
|
+
# TODO: this coercion should be up front. we can't cache
|
|
1500
|
+
# SQL constructs with non-bound literals buried in them
|
|
1501
|
+
if coercions._is_literal(val):
|
|
1502
|
+
val = elements.BindParameter(None, val, type_=column.type)
|
|
1503
|
+
value_text = self.process(val.self_group(), use_schema=False)
|
|
1504
|
+
else:
|
|
1505
|
+
|
|
1506
|
+
def replace(
|
|
1507
|
+
element: ExternallyTraversible, **kw: Any
|
|
1508
|
+
) -> Optional[ExternallyTraversible]:
|
|
1509
|
+
if (
|
|
1510
|
+
isinstance(element, elements.BindParameter)
|
|
1511
|
+
and element.type._isnull
|
|
1512
|
+
):
|
|
1513
|
+
return element._with_binary_element_type(column.type)
|
|
1514
|
+
elif (
|
|
1515
|
+
isinstance(element, elements.ColumnClause)
|
|
1516
|
+
and element.table is on_duplicate.inserted_alias
|
|
1517
|
+
):
|
|
1518
|
+
if requires_mysql8_alias:
|
|
1519
|
+
column_literal_clause = (
|
|
1520
|
+
f"{_on_dup_alias_name}."
|
|
1521
|
+
f"{self.preparer.quote(element.name)}"
|
|
1522
|
+
)
|
|
1523
|
+
else:
|
|
1524
|
+
column_literal_clause = (
|
|
1525
|
+
f"VALUES({self.preparer.quote(element.name)})"
|
|
1526
|
+
)
|
|
1527
|
+
return literal_column(column_literal_clause)
|
|
1528
|
+
else:
|
|
1529
|
+
# element is not replaced
|
|
1530
|
+
return None
|
|
1531
|
+
|
|
1532
|
+
val = visitors.replacement_traverse(val, {}, replace)
|
|
1533
|
+
value_text = self.process(val.self_group(), use_schema=False)
|
|
1534
|
+
|
|
1535
|
+
name_text = self.preparer.quote(column.name)
|
|
1536
|
+
clauses.append("%s = %s" % (name_text, value_text))
|
|
1537
|
+
|
|
1538
|
+
non_matching = set(on_duplicate_update) - {c.key for c in cols}
|
|
1539
|
+
if non_matching:
|
|
1540
|
+
util.warn(
|
|
1541
|
+
"Additional column names not matching "
|
|
1542
|
+
"any column keys in table '%s': %s"
|
|
1543
|
+
% (
|
|
1544
|
+
self.statement.table.name, # type: ignore[union-attr]
|
|
1545
|
+
(", ".join("'%s'" % c for c in non_matching)),
|
|
1546
|
+
)
|
|
1547
|
+
)
|
|
1548
|
+
|
|
1549
|
+
if requires_mysql8_alias:
|
|
1550
|
+
return (
|
|
1551
|
+
f"AS {_on_dup_alias_name} "
|
|
1552
|
+
f"ON DUPLICATE KEY UPDATE {', '.join(clauses)}"
|
|
1553
|
+
)
|
|
1554
|
+
else:
|
|
1555
|
+
return f"ON DUPLICATE KEY UPDATE {', '.join(clauses)}"
|
|
1556
|
+
|
|
1557
|
+
def visit_concat_op_expression_clauselist(
|
|
1558
|
+
self, clauselist: elements.ClauseList, operator: Any, **kw: Any
|
|
1559
|
+
) -> str:
|
|
1560
|
+
return "concat(%s)" % (
|
|
1561
|
+
", ".join(self.process(elem, **kw) for elem in clauselist.clauses)
|
|
1562
|
+
)
|
|
1563
|
+
|
|
1564
|
+
def visit_concat_op_binary(
|
|
1565
|
+
self, binary: elements.BinaryExpression[Any], operator: Any, **kw: Any
|
|
1566
|
+
) -> str:
|
|
1567
|
+
return "concat(%s, %s)" % (
|
|
1568
|
+
self.process(binary.left, **kw),
|
|
1569
|
+
self.process(binary.right, **kw),
|
|
1570
|
+
)
|
|
1571
|
+
|
|
1572
|
+
_match_valid_flag_combinations = frozenset(
|
|
1573
|
+
(
|
|
1574
|
+
# (boolean_mode, natural_language, query_expansion)
|
|
1575
|
+
(False, False, False),
|
|
1576
|
+
(True, False, False),
|
|
1577
|
+
(False, True, False),
|
|
1578
|
+
(False, False, True),
|
|
1579
|
+
(False, True, True),
|
|
1580
|
+
)
|
|
1581
|
+
)
|
|
1582
|
+
|
|
1583
|
+
_match_flag_expressions = (
|
|
1584
|
+
"IN BOOLEAN MODE",
|
|
1585
|
+
"IN NATURAL LANGUAGE MODE",
|
|
1586
|
+
"WITH QUERY EXPANSION",
|
|
1587
|
+
)
|
|
1588
|
+
|
|
1589
|
+
def visit_mysql_match(self, element: expression.match, **kw: Any) -> str:
|
|
1590
|
+
return self.visit_match_op_binary(element, element.operator, **kw)
|
|
1591
|
+
|
|
1592
|
+
def visit_match_op_binary(
|
|
1593
|
+
self, binary: expression.match, operator: Any, **kw: Any
|
|
1594
|
+
) -> str:
|
|
1595
|
+
"""
|
|
1596
|
+
Note that `mysql_boolean_mode` is enabled by default because of
|
|
1597
|
+
backward compatibility
|
|
1598
|
+
"""
|
|
1599
|
+
|
|
1600
|
+
modifiers = binary.modifiers
|
|
1601
|
+
|
|
1602
|
+
boolean_mode = modifiers.get("mysql_boolean_mode", True)
|
|
1603
|
+
natural_language = modifiers.get("mysql_natural_language", False)
|
|
1604
|
+
query_expansion = modifiers.get("mysql_query_expansion", False)
|
|
1605
|
+
|
|
1606
|
+
flag_combination = (boolean_mode, natural_language, query_expansion)
|
|
1607
|
+
|
|
1608
|
+
if flag_combination not in self._match_valid_flag_combinations:
|
|
1609
|
+
flags = (
|
|
1610
|
+
"in_boolean_mode=%s" % boolean_mode,
|
|
1611
|
+
"in_natural_language_mode=%s" % natural_language,
|
|
1612
|
+
"with_query_expansion=%s" % query_expansion,
|
|
1613
|
+
)
|
|
1614
|
+
|
|
1615
|
+
flags_str = ", ".join(flags)
|
|
1616
|
+
|
|
1617
|
+
raise exc.CompileError("Invalid MySQL match flags: %s" % flags_str)
|
|
1618
|
+
|
|
1619
|
+
match_clause = self.process(binary.left, **kw)
|
|
1620
|
+
against_clause = self.process(binary.right, **kw)
|
|
1621
|
+
|
|
1622
|
+
if any(flag_combination):
|
|
1623
|
+
flag_expressions = compress(
|
|
1624
|
+
self._match_flag_expressions,
|
|
1625
|
+
flag_combination,
|
|
1626
|
+
)
|
|
1627
|
+
|
|
1628
|
+
against_clause = " ".join([against_clause, *flag_expressions])
|
|
1629
|
+
|
|
1630
|
+
return "MATCH (%s) AGAINST (%s)" % (match_clause, against_clause)
|
|
1631
|
+
|
|
1632
|
+
def get_from_hint_text(
|
|
1633
|
+
self, table: selectable.FromClause, text: Optional[str]
|
|
1634
|
+
) -> Optional[str]:
|
|
1635
|
+
return text
|
|
1636
|
+
|
|
1637
|
+
def visit_typeclause(
|
|
1638
|
+
self,
|
|
1639
|
+
typeclause: elements.TypeClause,
|
|
1640
|
+
type_: Optional[TypeEngine[Any]] = None,
|
|
1641
|
+
**kw: Any,
|
|
1642
|
+
) -> Optional[str]:
|
|
1643
|
+
if type_ is None:
|
|
1644
|
+
type_ = typeclause.type.dialect_impl(self.dialect)
|
|
1645
|
+
if isinstance(type_, sqltypes.TypeDecorator):
|
|
1646
|
+
return self.visit_typeclause(typeclause, type_.impl, **kw) # type: ignore[arg-type] # noqa: E501
|
|
1647
|
+
elif isinstance(type_, sqltypes.Integer):
|
|
1648
|
+
if getattr(type_, "unsigned", False):
|
|
1649
|
+
return "UNSIGNED INTEGER"
|
|
1650
|
+
else:
|
|
1651
|
+
return "SIGNED INTEGER"
|
|
1652
|
+
elif isinstance(type_, sqltypes.TIMESTAMP):
|
|
1653
|
+
return "DATETIME"
|
|
1654
|
+
elif isinstance(
|
|
1655
|
+
type_,
|
|
1656
|
+
(
|
|
1657
|
+
sqltypes.DECIMAL,
|
|
1658
|
+
sqltypes.DateTime,
|
|
1659
|
+
sqltypes.Date,
|
|
1660
|
+
sqltypes.Time,
|
|
1661
|
+
),
|
|
1662
|
+
):
|
|
1663
|
+
return self.dialect.type_compiler_instance.process(type_)
|
|
1664
|
+
elif isinstance(type_, sqltypes.String) and not isinstance(
|
|
1665
|
+
type_, (ENUM, SET)
|
|
1666
|
+
):
|
|
1667
|
+
adapted = CHAR._adapt_string_for_cast(type_)
|
|
1668
|
+
return self.dialect.type_compiler_instance.process(adapted)
|
|
1669
|
+
elif isinstance(type_, sqltypes._Binary):
|
|
1670
|
+
return "BINARY"
|
|
1671
|
+
elif isinstance(type_, sqltypes.JSON):
|
|
1672
|
+
return "JSON"
|
|
1673
|
+
elif isinstance(type_, sqltypes.NUMERIC):
|
|
1674
|
+
return self.dialect.type_compiler_instance.process(type_).replace(
|
|
1675
|
+
"NUMERIC", "DECIMAL"
|
|
1676
|
+
)
|
|
1677
|
+
elif (
|
|
1678
|
+
isinstance(type_, sqltypes.Float)
|
|
1679
|
+
and self.dialect._support_float_cast
|
|
1680
|
+
):
|
|
1681
|
+
return self.dialect.type_compiler_instance.process(type_)
|
|
1682
|
+
else:
|
|
1683
|
+
return None
|
|
1684
|
+
|
|
1685
|
+
def visit_cast(self, cast: elements.Cast[Any], **kw: Any) -> str:
|
|
1686
|
+
type_ = self.process(cast.typeclause)
|
|
1687
|
+
if type_ is None:
|
|
1688
|
+
util.warn(
|
|
1689
|
+
"Datatype %s does not support CAST on MySQL/MariaDb; "
|
|
1690
|
+
"the CAST will be skipped."
|
|
1691
|
+
% self.dialect.type_compiler_instance.process(
|
|
1692
|
+
cast.typeclause.type
|
|
1693
|
+
)
|
|
1694
|
+
)
|
|
1695
|
+
return self.process(cast.clause.self_group(), **kw)
|
|
1696
|
+
|
|
1697
|
+
return "CAST(%s AS %s)" % (self.process(cast.clause, **kw), type_)
|
|
1698
|
+
|
|
1699
|
+
def render_literal_value(
|
|
1700
|
+
self, value: Optional[str], type_: TypeEngine[Any]
|
|
1701
|
+
) -> str:
|
|
1702
|
+
value = super().render_literal_value(value, type_)
|
|
1703
|
+
if self.dialect._backslash_escapes:
|
|
1704
|
+
value = value.replace("\\", "\\\\")
|
|
1705
|
+
return value
|
|
1706
|
+
|
|
1707
|
+
# override native_boolean=False behavior here, as
|
|
1708
|
+
# MySQL still supports native boolean
|
|
1709
|
+
def visit_true(self, expr: elements.True_, **kw: Any) -> str:
|
|
1710
|
+
return "true"
|
|
1711
|
+
|
|
1712
|
+
def visit_false(self, expr: elements.False_, **kw: Any) -> str:
|
|
1713
|
+
return "false"
|
|
1714
|
+
|
|
1715
|
+
def get_select_precolumns(
|
|
1716
|
+
self, select: selectable.Select[Any], **kw: Any
|
|
1717
|
+
) -> str:
|
|
1718
|
+
"""Add special MySQL keywords in place of DISTINCT.
|
|
1719
|
+
|
|
1720
|
+
.. deprecated:: 1.4 This usage is deprecated.
|
|
1721
|
+
:meth:`_expression.Select.prefix_with` should be used for special
|
|
1722
|
+
keywords at the start of a SELECT.
|
|
1723
|
+
|
|
1724
|
+
"""
|
|
1725
|
+
if isinstance(select._distinct, str):
|
|
1726
|
+
util.warn_deprecated(
|
|
1727
|
+
"Sending string values for 'distinct' is deprecated in the "
|
|
1728
|
+
"MySQL dialect and will be removed in a future release. "
|
|
1729
|
+
"Please use :meth:`.Select.prefix_with` for special keywords "
|
|
1730
|
+
"at the start of a SELECT statement",
|
|
1731
|
+
version="1.4",
|
|
1732
|
+
)
|
|
1733
|
+
return select._distinct.upper() + " "
|
|
1734
|
+
|
|
1735
|
+
return super().get_select_precolumns(select, **kw)
|
|
1736
|
+
|
|
1737
|
+
def visit_join(
|
|
1738
|
+
self,
|
|
1739
|
+
join: selectable.Join,
|
|
1740
|
+
asfrom: bool = False,
|
|
1741
|
+
from_linter: Optional[compiler.FromLinter] = None,
|
|
1742
|
+
**kwargs: Any,
|
|
1743
|
+
) -> str:
|
|
1744
|
+
if from_linter:
|
|
1745
|
+
from_linter.edges.add((join.left, join.right))
|
|
1746
|
+
|
|
1747
|
+
if join.full:
|
|
1748
|
+
join_type = " FULL OUTER JOIN "
|
|
1749
|
+
elif join.isouter:
|
|
1750
|
+
join_type = " LEFT OUTER JOIN "
|
|
1751
|
+
else:
|
|
1752
|
+
join_type = " INNER JOIN "
|
|
1753
|
+
|
|
1754
|
+
return "".join(
|
|
1755
|
+
(
|
|
1756
|
+
self.process(
|
|
1757
|
+
join.left, asfrom=True, from_linter=from_linter, **kwargs
|
|
1758
|
+
),
|
|
1759
|
+
join_type,
|
|
1760
|
+
self.process(
|
|
1761
|
+
join.right, asfrom=True, from_linter=from_linter, **kwargs
|
|
1762
|
+
),
|
|
1763
|
+
" ON ",
|
|
1764
|
+
self.process(join.onclause, from_linter=from_linter, **kwargs), # type: ignore[arg-type] # noqa: E501
|
|
1765
|
+
)
|
|
1766
|
+
)
|
|
1767
|
+
|
|
1768
|
+
def for_update_clause(
|
|
1769
|
+
self, select: selectable.GenerativeSelect, **kw: Any
|
|
1770
|
+
) -> str:
|
|
1771
|
+
assert select._for_update_arg is not None
|
|
1772
|
+
if select._for_update_arg.read:
|
|
1773
|
+
if self.dialect.use_mysql_for_share:
|
|
1774
|
+
tmp = " FOR SHARE"
|
|
1775
|
+
else:
|
|
1776
|
+
tmp = " LOCK IN SHARE MODE"
|
|
1777
|
+
else:
|
|
1778
|
+
tmp = " FOR UPDATE"
|
|
1779
|
+
|
|
1780
|
+
if select._for_update_arg.of and self.dialect.supports_for_update_of:
|
|
1781
|
+
tables: util.OrderedSet[elements.ClauseElement] = util.OrderedSet()
|
|
1782
|
+
for c in select._for_update_arg.of:
|
|
1783
|
+
tables.update(sql_util.surface_selectables_only(c))
|
|
1784
|
+
|
|
1785
|
+
tmp += " OF " + ", ".join(
|
|
1786
|
+
self.process(table, ashint=True, use_schema=False, **kw)
|
|
1787
|
+
for table in tables
|
|
1788
|
+
)
|
|
1789
|
+
|
|
1790
|
+
if select._for_update_arg.nowait:
|
|
1791
|
+
tmp += " NOWAIT"
|
|
1792
|
+
|
|
1793
|
+
if select._for_update_arg.skip_locked:
|
|
1794
|
+
tmp += " SKIP LOCKED"
|
|
1795
|
+
|
|
1796
|
+
return tmp
|
|
1797
|
+
|
|
1798
|
+
def limit_clause(
|
|
1799
|
+
self, select: selectable.GenerativeSelect, **kw: Any
|
|
1800
|
+
) -> str:
|
|
1801
|
+
# MySQL supports:
|
|
1802
|
+
# LIMIT <limit>
|
|
1803
|
+
# LIMIT <offset>, <limit>
|
|
1804
|
+
# and in server versions > 3.3:
|
|
1805
|
+
# LIMIT <limit> OFFSET <offset>
|
|
1806
|
+
# The latter is more readable for offsets but we're stuck with the
|
|
1807
|
+
# former until we can refine dialects by server revision.
|
|
1808
|
+
|
|
1809
|
+
limit_clause, offset_clause = (
|
|
1810
|
+
select._limit_clause,
|
|
1811
|
+
select._offset_clause,
|
|
1812
|
+
)
|
|
1813
|
+
|
|
1814
|
+
if limit_clause is None and offset_clause is None:
|
|
1815
|
+
return ""
|
|
1816
|
+
elif offset_clause is not None:
|
|
1817
|
+
# As suggested by the MySQL docs, need to apply an
|
|
1818
|
+
# artificial limit if one wasn't provided
|
|
1819
|
+
# https://dev.mysql.com/doc/refman/5.0/en/select.html
|
|
1820
|
+
if limit_clause is None:
|
|
1821
|
+
# TODO: remove ??
|
|
1822
|
+
# hardwire the upper limit. Currently
|
|
1823
|
+
# needed consistent with the usage of the upper
|
|
1824
|
+
# bound as part of MySQL's "syntax" for OFFSET with
|
|
1825
|
+
# no LIMIT.
|
|
1826
|
+
return " \n LIMIT %s, %s" % (
|
|
1827
|
+
self.process(offset_clause, **kw),
|
|
1828
|
+
"18446744073709551615",
|
|
1829
|
+
)
|
|
1830
|
+
else:
|
|
1831
|
+
return " \n LIMIT %s, %s" % (
|
|
1832
|
+
self.process(offset_clause, **kw),
|
|
1833
|
+
self.process(limit_clause, **kw),
|
|
1834
|
+
)
|
|
1835
|
+
else:
|
|
1836
|
+
assert limit_clause is not None
|
|
1837
|
+
# No offset provided, so just use the limit
|
|
1838
|
+
return " \n LIMIT %s" % (self.process(limit_clause, **kw),)
|
|
1839
|
+
|
|
1840
|
+
def update_limit_clause(self, update_stmt: Update) -> Optional[str]:
|
|
1841
|
+
limit = update_stmt.kwargs.get("%s_limit" % self.dialect.name, None)
|
|
1842
|
+
if limit is not None:
|
|
1843
|
+
return f"LIMIT {int(limit)}"
|
|
1844
|
+
else:
|
|
1845
|
+
return None
|
|
1846
|
+
|
|
1847
|
+
def delete_limit_clause(self, delete_stmt: Delete) -> Optional[str]:
|
|
1848
|
+
limit = delete_stmt.kwargs.get("%s_limit" % self.dialect.name, None)
|
|
1849
|
+
if limit is not None:
|
|
1850
|
+
return f"LIMIT {int(limit)}"
|
|
1851
|
+
else:
|
|
1852
|
+
return None
|
|
1853
|
+
|
|
1854
|
+
def update_tables_clause(
|
|
1855
|
+
self,
|
|
1856
|
+
update_stmt: Update,
|
|
1857
|
+
from_table: _DMLTableElement,
|
|
1858
|
+
extra_froms: List[selectable.FromClause],
|
|
1859
|
+
**kw: Any,
|
|
1860
|
+
) -> str:
|
|
1861
|
+
kw["asfrom"] = True
|
|
1862
|
+
return ", ".join(
|
|
1863
|
+
t._compiler_dispatch(self, **kw)
|
|
1864
|
+
for t in [from_table] + list(extra_froms)
|
|
1865
|
+
)
|
|
1866
|
+
|
|
1867
|
+
def update_from_clause(
|
|
1868
|
+
self,
|
|
1869
|
+
update_stmt: Update,
|
|
1870
|
+
from_table: _DMLTableElement,
|
|
1871
|
+
extra_froms: List[selectable.FromClause],
|
|
1872
|
+
from_hints: Any,
|
|
1873
|
+
**kw: Any,
|
|
1874
|
+
) -> None:
|
|
1875
|
+
return None
|
|
1876
|
+
|
|
1877
|
+
def delete_table_clause(
|
|
1878
|
+
self,
|
|
1879
|
+
delete_stmt: Delete,
|
|
1880
|
+
from_table: _DMLTableElement,
|
|
1881
|
+
extra_froms: List[selectable.FromClause],
|
|
1882
|
+
**kw: Any,
|
|
1883
|
+
) -> str:
|
|
1884
|
+
"""If we have extra froms make sure we render any alias as hint."""
|
|
1885
|
+
ashint = False
|
|
1886
|
+
if extra_froms:
|
|
1887
|
+
ashint = True
|
|
1888
|
+
return from_table._compiler_dispatch(
|
|
1889
|
+
self, asfrom=True, iscrud=True, ashint=ashint, **kw
|
|
1890
|
+
)
|
|
1891
|
+
|
|
1892
|
+
def delete_extra_from_clause(
|
|
1893
|
+
self,
|
|
1894
|
+
delete_stmt: Delete,
|
|
1895
|
+
from_table: _DMLTableElement,
|
|
1896
|
+
extra_froms: List[selectable.FromClause],
|
|
1897
|
+
from_hints: Any,
|
|
1898
|
+
**kw: Any,
|
|
1899
|
+
) -> str:
|
|
1900
|
+
"""Render the DELETE .. USING clause specific to MySQL."""
|
|
1901
|
+
kw["asfrom"] = True
|
|
1902
|
+
return "USING " + ", ".join(
|
|
1903
|
+
t._compiler_dispatch(self, fromhints=from_hints, **kw)
|
|
1904
|
+
for t in [from_table] + extra_froms
|
|
1905
|
+
)
|
|
1906
|
+
|
|
1907
|
+
def visit_empty_set_expr(
|
|
1908
|
+
self, element_types: List[TypeEngine[Any]], **kw: Any
|
|
1909
|
+
) -> str:
|
|
1910
|
+
return (
|
|
1911
|
+
"SELECT %(outer)s FROM (SELECT %(inner)s) "
|
|
1912
|
+
"as _empty_set WHERE 1!=1"
|
|
1913
|
+
% {
|
|
1914
|
+
"inner": ", ".join(
|
|
1915
|
+
"1 AS _in_%s" % idx
|
|
1916
|
+
for idx, type_ in enumerate(element_types)
|
|
1917
|
+
),
|
|
1918
|
+
"outer": ", ".join(
|
|
1919
|
+
"_in_%s" % idx for idx, type_ in enumerate(element_types)
|
|
1920
|
+
),
|
|
1921
|
+
}
|
|
1922
|
+
)
|
|
1923
|
+
|
|
1924
|
+
def visit_is_distinct_from_binary(
|
|
1925
|
+
self, binary: elements.BinaryExpression[Any], operator: Any, **kw: Any
|
|
1926
|
+
) -> str:
|
|
1927
|
+
return "NOT (%s <=> %s)" % (
|
|
1928
|
+
self.process(binary.left),
|
|
1929
|
+
self.process(binary.right),
|
|
1930
|
+
)
|
|
1931
|
+
|
|
1932
|
+
def visit_is_not_distinct_from_binary(
|
|
1933
|
+
self, binary: elements.BinaryExpression[Any], operator: Any, **kw: Any
|
|
1934
|
+
) -> str:
|
|
1935
|
+
return "%s <=> %s" % (
|
|
1936
|
+
self.process(binary.left),
|
|
1937
|
+
self.process(binary.right),
|
|
1938
|
+
)
|
|
1939
|
+
|
|
1940
|
+
def _mariadb_regexp_flags(
|
|
1941
|
+
self, flags: str, pattern: elements.ColumnElement[Any], **kw: Any
|
|
1942
|
+
) -> str:
|
|
1943
|
+
return "CONCAT('(?', %s, ')', %s)" % (
|
|
1944
|
+
self.render_literal_value(flags, sqltypes.STRINGTYPE),
|
|
1945
|
+
self.process(pattern, **kw),
|
|
1946
|
+
)
|
|
1947
|
+
|
|
1948
|
+
def _regexp_match(
|
|
1949
|
+
self,
|
|
1950
|
+
op_string: str,
|
|
1951
|
+
binary: elements.BinaryExpression[Any],
|
|
1952
|
+
operator: Any,
|
|
1953
|
+
**kw: Any,
|
|
1954
|
+
) -> str:
|
|
1955
|
+
assert binary.modifiers is not None
|
|
1956
|
+
flags = binary.modifiers["flags"]
|
|
1957
|
+
if flags is None:
|
|
1958
|
+
return self._generate_generic_binary(binary, op_string, **kw)
|
|
1959
|
+
elif self.dialect.is_mariadb:
|
|
1960
|
+
return "%s%s%s" % (
|
|
1961
|
+
self.process(binary.left, **kw),
|
|
1962
|
+
op_string,
|
|
1963
|
+
self._mariadb_regexp_flags(flags, binary.right),
|
|
1964
|
+
)
|
|
1965
|
+
else:
|
|
1966
|
+
text = "REGEXP_LIKE(%s, %s, %s)" % (
|
|
1967
|
+
self.process(binary.left, **kw),
|
|
1968
|
+
self.process(binary.right, **kw),
|
|
1969
|
+
self.render_literal_value(flags, sqltypes.STRINGTYPE),
|
|
1970
|
+
)
|
|
1971
|
+
if op_string == " NOT REGEXP ":
|
|
1972
|
+
return "NOT %s" % text
|
|
1973
|
+
else:
|
|
1974
|
+
return text
|
|
1975
|
+
|
|
1976
|
+
def visit_regexp_match_op_binary(
|
|
1977
|
+
self, binary: elements.BinaryExpression[Any], operator: Any, **kw: Any
|
|
1978
|
+
) -> str:
|
|
1979
|
+
return self._regexp_match(" REGEXP ", binary, operator, **kw)
|
|
1980
|
+
|
|
1981
|
+
def visit_not_regexp_match_op_binary(
|
|
1982
|
+
self, binary: elements.BinaryExpression[Any], operator: Any, **kw: Any
|
|
1983
|
+
) -> str:
|
|
1984
|
+
return self._regexp_match(" NOT REGEXP ", binary, operator, **kw)
|
|
1985
|
+
|
|
1986
|
+
def visit_regexp_replace_op_binary(
|
|
1987
|
+
self, binary: elements.BinaryExpression[Any], operator: Any, **kw: Any
|
|
1988
|
+
) -> str:
|
|
1989
|
+
assert binary.modifiers is not None
|
|
1990
|
+
flags = binary.modifiers["flags"]
|
|
1991
|
+
if flags is None:
|
|
1992
|
+
return "REGEXP_REPLACE(%s, %s)" % (
|
|
1993
|
+
self.process(binary.left, **kw),
|
|
1994
|
+
self.process(binary.right, **kw),
|
|
1995
|
+
)
|
|
1996
|
+
elif self.dialect.is_mariadb:
|
|
1997
|
+
return "REGEXP_REPLACE(%s, %s, %s)" % (
|
|
1998
|
+
self.process(binary.left, **kw),
|
|
1999
|
+
self._mariadb_regexp_flags(flags, binary.right.clauses[0]),
|
|
2000
|
+
self.process(binary.right.clauses[1], **kw),
|
|
2001
|
+
)
|
|
2002
|
+
else:
|
|
2003
|
+
return "REGEXP_REPLACE(%s, %s, %s)" % (
|
|
2004
|
+
self.process(binary.left, **kw),
|
|
2005
|
+
self.process(binary.right, **kw),
|
|
2006
|
+
self.render_literal_value(flags, sqltypes.STRINGTYPE),
|
|
2007
|
+
)
|
|
2008
|
+
|
|
2009
|
+
|
|
2010
|
+
class MySQLDDLCompiler(compiler.DDLCompiler):
|
|
2011
|
+
dialect: MySQLDialect
|
|
2012
|
+
|
|
2013
|
+
def get_column_specification(
|
|
2014
|
+
self, column: sa_schema.Column[Any], **kw: Any
|
|
2015
|
+
) -> str:
|
|
2016
|
+
"""Builds column DDL."""
|
|
2017
|
+
if (
|
|
2018
|
+
self.dialect.is_mariadb is True
|
|
2019
|
+
and column.computed is not None
|
|
2020
|
+
and column._user_defined_nullable is SchemaConst.NULL_UNSPECIFIED
|
|
2021
|
+
):
|
|
2022
|
+
column.nullable = True
|
|
2023
|
+
colspec = [
|
|
2024
|
+
self.preparer.format_column(column),
|
|
2025
|
+
self.dialect.type_compiler_instance.process(
|
|
2026
|
+
column.type, type_expression=column
|
|
2027
|
+
),
|
|
2028
|
+
]
|
|
2029
|
+
|
|
2030
|
+
if column.computed is not None:
|
|
2031
|
+
colspec.append(self.process(column.computed))
|
|
2032
|
+
|
|
2033
|
+
is_timestamp = isinstance(
|
|
2034
|
+
column.type._unwrapped_dialect_impl(self.dialect),
|
|
2035
|
+
sqltypes.TIMESTAMP,
|
|
2036
|
+
)
|
|
2037
|
+
|
|
2038
|
+
if not column.nullable:
|
|
2039
|
+
colspec.append("NOT NULL")
|
|
2040
|
+
|
|
2041
|
+
# see: https://docs.sqlalchemy.org/en/latest/dialects/mysql.html#mysql_timestamp_null # noqa
|
|
2042
|
+
elif column.nullable and is_timestamp:
|
|
2043
|
+
colspec.append("NULL")
|
|
2044
|
+
|
|
2045
|
+
comment = column.comment
|
|
2046
|
+
if comment is not None:
|
|
2047
|
+
literal = self.sql_compiler.render_literal_value(
|
|
2048
|
+
comment, sqltypes.String()
|
|
2049
|
+
)
|
|
2050
|
+
colspec.append("COMMENT " + literal)
|
|
2051
|
+
|
|
2052
|
+
if (
|
|
2053
|
+
column.table is not None
|
|
2054
|
+
and column is column.table._autoincrement_column
|
|
2055
|
+
and (
|
|
2056
|
+
column.server_default is None
|
|
2057
|
+
or isinstance(column.server_default, sa_schema.Identity)
|
|
2058
|
+
)
|
|
2059
|
+
and not (
|
|
2060
|
+
self.dialect.supports_sequences
|
|
2061
|
+
and isinstance(column.default, sa_schema.Sequence)
|
|
2062
|
+
and not column.default.optional
|
|
2063
|
+
)
|
|
2064
|
+
):
|
|
2065
|
+
colspec.append("AUTO_INCREMENT")
|
|
2066
|
+
else:
|
|
2067
|
+
default = self.get_column_default_string(column)
|
|
2068
|
+
|
|
2069
|
+
if default is not None:
|
|
2070
|
+
if (
|
|
2071
|
+
self.dialect._support_default_function
|
|
2072
|
+
and not re.match(r"^\s*[\'\"\(]", default)
|
|
2073
|
+
and not re.search(r"ON +UPDATE", default, re.I)
|
|
2074
|
+
and not re.match(
|
|
2075
|
+
r"\bnow\(\d+\)|\bcurrent_timestamp\(\d+\)",
|
|
2076
|
+
default,
|
|
2077
|
+
re.I,
|
|
2078
|
+
)
|
|
2079
|
+
and re.match(r".*\W.*", default)
|
|
2080
|
+
):
|
|
2081
|
+
colspec.append(f"DEFAULT ({default})")
|
|
2082
|
+
else:
|
|
2083
|
+
colspec.append("DEFAULT " + default)
|
|
2084
|
+
return " ".join(colspec)
|
|
2085
|
+
|
|
2086
|
+
def post_create_table(self, table: sa_schema.Table) -> str:
|
|
2087
|
+
"""Build table-level CREATE options like ENGINE and COLLATE."""
|
|
2088
|
+
|
|
2089
|
+
table_opts = []
|
|
2090
|
+
|
|
2091
|
+
opts = {
|
|
2092
|
+
k[len(self.dialect.name) + 1 :].upper(): v
|
|
2093
|
+
for k, v in table.kwargs.items()
|
|
2094
|
+
if k.startswith("%s_" % self.dialect.name)
|
|
2095
|
+
}
|
|
2096
|
+
|
|
2097
|
+
if table.comment is not None:
|
|
2098
|
+
opts["COMMENT"] = table.comment
|
|
2099
|
+
|
|
2100
|
+
partition_options = [
|
|
2101
|
+
"PARTITION_BY",
|
|
2102
|
+
"PARTITIONS",
|
|
2103
|
+
"SUBPARTITIONS",
|
|
2104
|
+
"SUBPARTITION_BY",
|
|
2105
|
+
]
|
|
2106
|
+
|
|
2107
|
+
nonpart_options = set(opts).difference(partition_options)
|
|
2108
|
+
part_options = set(opts).intersection(partition_options)
|
|
2109
|
+
|
|
2110
|
+
for opt in topological.sort(
|
|
2111
|
+
[
|
|
2112
|
+
("DEFAULT_CHARSET", "COLLATE"),
|
|
2113
|
+
("DEFAULT_CHARACTER_SET", "COLLATE"),
|
|
2114
|
+
("CHARSET", "COLLATE"),
|
|
2115
|
+
("CHARACTER_SET", "COLLATE"),
|
|
2116
|
+
],
|
|
2117
|
+
nonpart_options,
|
|
2118
|
+
):
|
|
2119
|
+
arg = opts[opt]
|
|
2120
|
+
if opt in _reflection._options_of_type_string:
|
|
2121
|
+
arg = self.sql_compiler.render_literal_value(
|
|
2122
|
+
arg, sqltypes.String()
|
|
2123
|
+
)
|
|
2124
|
+
|
|
2125
|
+
if opt in (
|
|
2126
|
+
"DATA_DIRECTORY",
|
|
2127
|
+
"INDEX_DIRECTORY",
|
|
2128
|
+
"DEFAULT_CHARACTER_SET",
|
|
2129
|
+
"CHARACTER_SET",
|
|
2130
|
+
"DEFAULT_CHARSET",
|
|
2131
|
+
"DEFAULT_COLLATE",
|
|
2132
|
+
):
|
|
2133
|
+
opt = opt.replace("_", " ")
|
|
2134
|
+
|
|
2135
|
+
joiner = "="
|
|
2136
|
+
if opt in (
|
|
2137
|
+
"TABLESPACE",
|
|
2138
|
+
"DEFAULT CHARACTER SET",
|
|
2139
|
+
"CHARACTER SET",
|
|
2140
|
+
"COLLATE",
|
|
2141
|
+
):
|
|
2142
|
+
joiner = " "
|
|
2143
|
+
|
|
2144
|
+
table_opts.append(joiner.join((opt, arg)))
|
|
2145
|
+
|
|
2146
|
+
for opt in topological.sort(
|
|
2147
|
+
[
|
|
2148
|
+
("PARTITION_BY", "PARTITIONS"),
|
|
2149
|
+
("PARTITION_BY", "SUBPARTITION_BY"),
|
|
2150
|
+
("PARTITION_BY", "SUBPARTITIONS"),
|
|
2151
|
+
("PARTITIONS", "SUBPARTITIONS"),
|
|
2152
|
+
("PARTITIONS", "SUBPARTITION_BY"),
|
|
2153
|
+
("SUBPARTITION_BY", "SUBPARTITIONS"),
|
|
2154
|
+
],
|
|
2155
|
+
part_options,
|
|
2156
|
+
):
|
|
2157
|
+
arg = opts[opt]
|
|
2158
|
+
if opt in _reflection._options_of_type_string:
|
|
2159
|
+
arg = self.sql_compiler.render_literal_value(
|
|
2160
|
+
arg, sqltypes.String()
|
|
2161
|
+
)
|
|
2162
|
+
|
|
2163
|
+
opt = opt.replace("_", " ")
|
|
2164
|
+
joiner = " "
|
|
2165
|
+
|
|
2166
|
+
table_opts.append(joiner.join((opt, arg)))
|
|
2167
|
+
|
|
2168
|
+
return " ".join(table_opts)
|
|
2169
|
+
|
|
2170
|
+
def visit_create_index(self, create: ddl.CreateIndex, **kw: Any) -> str: # type: ignore[override] # noqa: E501
|
|
2171
|
+
index = create.element
|
|
2172
|
+
self._verify_index_table(index)
|
|
2173
|
+
preparer = self.preparer
|
|
2174
|
+
table = preparer.format_table(index.table) # type: ignore[arg-type]
|
|
2175
|
+
|
|
2176
|
+
columns = [
|
|
2177
|
+
self.sql_compiler.process(
|
|
2178
|
+
(
|
|
2179
|
+
elements.Grouping(expr) # type: ignore[arg-type]
|
|
2180
|
+
if (
|
|
2181
|
+
isinstance(expr, elements.BinaryExpression)
|
|
2182
|
+
or (
|
|
2183
|
+
isinstance(expr, elements.UnaryExpression)
|
|
2184
|
+
and expr.modifier
|
|
2185
|
+
not in (operators.desc_op, operators.asc_op)
|
|
2186
|
+
)
|
|
2187
|
+
or isinstance(expr, functions.FunctionElement)
|
|
2188
|
+
)
|
|
2189
|
+
else expr
|
|
2190
|
+
),
|
|
2191
|
+
include_table=False,
|
|
2192
|
+
literal_binds=True,
|
|
2193
|
+
)
|
|
2194
|
+
for expr in index.expressions
|
|
2195
|
+
]
|
|
2196
|
+
|
|
2197
|
+
name = self._prepared_index_name(index)
|
|
2198
|
+
|
|
2199
|
+
text = "CREATE "
|
|
2200
|
+
if index.unique:
|
|
2201
|
+
text += "UNIQUE "
|
|
2202
|
+
|
|
2203
|
+
index_prefix = index.get_dialect_option(self.dialect, "prefix")
|
|
2204
|
+
if index_prefix:
|
|
2205
|
+
text += index_prefix + " "
|
|
2206
|
+
|
|
2207
|
+
text += "INDEX "
|
|
2208
|
+
if create.if_not_exists:
|
|
2209
|
+
text += "IF NOT EXISTS "
|
|
2210
|
+
text += "%s ON %s " % (name, table)
|
|
2211
|
+
|
|
2212
|
+
length = index.get_dialect_option(self.dialect, "length")
|
|
2213
|
+
if length is not None:
|
|
2214
|
+
if isinstance(length, dict):
|
|
2215
|
+
# length value can be a (column_name --> integer value)
|
|
2216
|
+
# mapping specifying the prefix length for each column of the
|
|
2217
|
+
# index
|
|
2218
|
+
columns_str = ", ".join(
|
|
2219
|
+
(
|
|
2220
|
+
"%s(%d)" % (expr, length[col.name]) # type: ignore[union-attr] # noqa: E501
|
|
2221
|
+
if col.name in length # type: ignore[union-attr]
|
|
2222
|
+
else (
|
|
2223
|
+
"%s(%d)" % (expr, length[expr])
|
|
2224
|
+
if expr in length
|
|
2225
|
+
else "%s" % expr
|
|
2226
|
+
)
|
|
2227
|
+
)
|
|
2228
|
+
for col, expr in zip(index.expressions, columns)
|
|
2229
|
+
)
|
|
2230
|
+
else:
|
|
2231
|
+
# or can be an integer value specifying the same
|
|
2232
|
+
# prefix length for all columns of the index
|
|
2233
|
+
columns_str = ", ".join(
|
|
2234
|
+
"%s(%d)" % (col, length) for col in columns
|
|
2235
|
+
)
|
|
2236
|
+
else:
|
|
2237
|
+
columns_str = ", ".join(columns)
|
|
2238
|
+
text += "(%s)" % columns_str
|
|
2239
|
+
|
|
2240
|
+
parser = index.get_dialect_option(
|
|
2241
|
+
self.dialect, "with_parser", deprecated_fallback="mysql"
|
|
2242
|
+
)
|
|
2243
|
+
if parser is not None:
|
|
2244
|
+
text += " WITH PARSER %s" % (parser,)
|
|
2245
|
+
|
|
2246
|
+
using = index.get_dialect_option(
|
|
2247
|
+
self.dialect, "using", deprecated_fallback="mysql"
|
|
2248
|
+
)
|
|
2249
|
+
if using is not None:
|
|
2250
|
+
text += " USING %s" % (preparer.quote(using))
|
|
2251
|
+
|
|
2252
|
+
return text
|
|
2253
|
+
|
|
2254
|
+
def visit_primary_key_constraint(
|
|
2255
|
+
self, constraint: sa_schema.PrimaryKeyConstraint, **kw: Any
|
|
2256
|
+
) -> str:
|
|
2257
|
+
text = super().visit_primary_key_constraint(constraint)
|
|
2258
|
+
using = constraint.get_dialect_option(
|
|
2259
|
+
self.dialect, "using", deprecated_fallback="mysql"
|
|
2260
|
+
)
|
|
2261
|
+
if using:
|
|
2262
|
+
text += " USING %s" % (self.preparer.quote(using))
|
|
2263
|
+
return text
|
|
2264
|
+
|
|
2265
|
+
def visit_drop_index(self, drop: ddl.DropIndex, **kw: Any) -> str:
|
|
2266
|
+
index = drop.element
|
|
2267
|
+
text = "\nDROP INDEX "
|
|
2268
|
+
if drop.if_exists:
|
|
2269
|
+
text += "IF EXISTS "
|
|
2270
|
+
|
|
2271
|
+
return text + "%s ON %s" % (
|
|
2272
|
+
self._prepared_index_name(index, include_schema=False),
|
|
2273
|
+
self.preparer.format_table(index.table), # type: ignore[arg-type]
|
|
2274
|
+
)
|
|
2275
|
+
|
|
2276
|
+
def visit_drop_constraint(
|
|
2277
|
+
self, drop: ddl.DropConstraint, **kw: Any
|
|
2278
|
+
) -> str:
|
|
2279
|
+
constraint = drop.element
|
|
2280
|
+
if isinstance(constraint, sa_schema.ForeignKeyConstraint):
|
|
2281
|
+
qual = "FOREIGN KEY "
|
|
2282
|
+
const = self.preparer.format_constraint(constraint)
|
|
2283
|
+
elif isinstance(constraint, sa_schema.PrimaryKeyConstraint):
|
|
2284
|
+
qual = "PRIMARY KEY "
|
|
2285
|
+
const = ""
|
|
2286
|
+
elif isinstance(constraint, sa_schema.UniqueConstraint):
|
|
2287
|
+
qual = "INDEX "
|
|
2288
|
+
const = self.preparer.format_constraint(constraint)
|
|
2289
|
+
elif isinstance(constraint, sa_schema.CheckConstraint):
|
|
2290
|
+
if self.dialect.is_mariadb:
|
|
2291
|
+
qual = "CONSTRAINT "
|
|
2292
|
+
else:
|
|
2293
|
+
qual = "CHECK "
|
|
2294
|
+
const = self.preparer.format_constraint(constraint)
|
|
2295
|
+
else:
|
|
2296
|
+
qual = ""
|
|
2297
|
+
const = self.preparer.format_constraint(constraint)
|
|
2298
|
+
return "ALTER TABLE %s DROP %s%s" % (
|
|
2299
|
+
self.preparer.format_table(constraint.table),
|
|
2300
|
+
qual,
|
|
2301
|
+
const,
|
|
2302
|
+
)
|
|
2303
|
+
|
|
2304
|
+
def define_constraint_match(
|
|
2305
|
+
self, constraint: sa_schema.ForeignKeyConstraint
|
|
2306
|
+
) -> str:
|
|
2307
|
+
if constraint.match is not None:
|
|
2308
|
+
raise exc.CompileError(
|
|
2309
|
+
"MySQL ignores the 'MATCH' keyword while at the same time "
|
|
2310
|
+
"causes ON UPDATE/ON DELETE clauses to be ignored."
|
|
2311
|
+
)
|
|
2312
|
+
return ""
|
|
2313
|
+
|
|
2314
|
+
def visit_set_table_comment(
|
|
2315
|
+
self, create: ddl.SetTableComment, **kw: Any
|
|
2316
|
+
) -> str:
|
|
2317
|
+
return "ALTER TABLE %s COMMENT %s" % (
|
|
2318
|
+
self.preparer.format_table(create.element),
|
|
2319
|
+
self.sql_compiler.render_literal_value(
|
|
2320
|
+
create.element.comment, sqltypes.String()
|
|
2321
|
+
),
|
|
2322
|
+
)
|
|
2323
|
+
|
|
2324
|
+
def visit_drop_table_comment(
|
|
2325
|
+
self, drop: ddl.DropTableComment, **kw: Any
|
|
2326
|
+
) -> str:
|
|
2327
|
+
return "ALTER TABLE %s COMMENT ''" % (
|
|
2328
|
+
self.preparer.format_table(drop.element)
|
|
2329
|
+
)
|
|
2330
|
+
|
|
2331
|
+
def visit_set_column_comment(
|
|
2332
|
+
self, create: ddl.SetColumnComment, **kw: Any
|
|
2333
|
+
) -> str:
|
|
2334
|
+
return "ALTER TABLE %s CHANGE %s %s" % (
|
|
2335
|
+
self.preparer.format_table(create.element.table),
|
|
2336
|
+
self.preparer.format_column(create.element),
|
|
2337
|
+
self.get_column_specification(create.element),
|
|
2338
|
+
)
|
|
2339
|
+
|
|
2340
|
+
def get_identity_options(self, identity_options: IdentityOptions) -> str:
|
|
2341
|
+
"""mariadb-specific sequence option; this will move to a
|
|
2342
|
+
mariadb-specific module in 2.1
|
|
2343
|
+
|
|
2344
|
+
"""
|
|
2345
|
+
text = super().get_identity_options(identity_options)
|
|
2346
|
+
text = text.replace("NO CYCLE", "NOCYCLE")
|
|
2347
|
+
return text
|
|
2348
|
+
|
|
2349
|
+
|
|
2350
|
+
class MySQLTypeCompiler(compiler.GenericTypeCompiler):
|
|
2351
|
+
def _extend_numeric(self, type_: _NumericType, spec: str) -> str:
|
|
2352
|
+
"Extend a numeric-type declaration with MySQL specific extensions."
|
|
2353
|
+
|
|
2354
|
+
if not self._mysql_type(type_):
|
|
2355
|
+
return spec
|
|
2356
|
+
|
|
2357
|
+
if type_.unsigned:
|
|
2358
|
+
spec += " UNSIGNED"
|
|
2359
|
+
if type_.zerofill:
|
|
2360
|
+
spec += " ZEROFILL"
|
|
2361
|
+
return spec
|
|
2362
|
+
|
|
2363
|
+
def _extend_string(
|
|
2364
|
+
self, type_: _StringType, defaults: Dict[str, Any], spec: str
|
|
2365
|
+
) -> str:
|
|
2366
|
+
"""Extend a string-type declaration with standard SQL CHARACTER SET /
|
|
2367
|
+
COLLATE annotations and MySQL specific extensions.
|
|
2368
|
+
|
|
2369
|
+
"""
|
|
2370
|
+
|
|
2371
|
+
def attr(name: str) -> Any:
|
|
2372
|
+
return getattr(type_, name, defaults.get(name))
|
|
2373
|
+
|
|
2374
|
+
if attr("charset"):
|
|
2375
|
+
charset = "CHARACTER SET %s" % attr("charset")
|
|
2376
|
+
elif attr("ascii"):
|
|
2377
|
+
charset = "ASCII"
|
|
2378
|
+
elif attr("unicode"):
|
|
2379
|
+
charset = "UNICODE"
|
|
2380
|
+
else:
|
|
2381
|
+
|
|
2382
|
+
charset = None
|
|
2383
|
+
|
|
2384
|
+
if attr("collation"):
|
|
2385
|
+
collation = "COLLATE %s" % type_.collation
|
|
2386
|
+
elif attr("binary"):
|
|
2387
|
+
collation = "BINARY"
|
|
2388
|
+
else:
|
|
2389
|
+
collation = None
|
|
2390
|
+
|
|
2391
|
+
if attr("national"):
|
|
2392
|
+
# NATIONAL (aka NCHAR/NVARCHAR) trumps charsets.
|
|
2393
|
+
return " ".join(
|
|
2394
|
+
[c for c in ("NATIONAL", spec, collation) if c is not None]
|
|
2395
|
+
)
|
|
2396
|
+
return " ".join(
|
|
2397
|
+
[c for c in (spec, charset, collation) if c is not None]
|
|
2398
|
+
)
|
|
2399
|
+
|
|
2400
|
+
def _mysql_type(self, type_: Any) -> bool:
|
|
2401
|
+
return isinstance(type_, (_StringType, _NumericType))
|
|
2402
|
+
|
|
2403
|
+
def visit_NUMERIC(self, type_: NUMERIC, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2404
|
+
if type_.precision is None:
|
|
2405
|
+
return self._extend_numeric(type_, "NUMERIC")
|
|
2406
|
+
elif type_.scale is None:
|
|
2407
|
+
return self._extend_numeric(
|
|
2408
|
+
type_,
|
|
2409
|
+
"NUMERIC(%(precision)s)" % {"precision": type_.precision},
|
|
2410
|
+
)
|
|
2411
|
+
else:
|
|
2412
|
+
return self._extend_numeric(
|
|
2413
|
+
type_,
|
|
2414
|
+
"NUMERIC(%(precision)s, %(scale)s)"
|
|
2415
|
+
% {"precision": type_.precision, "scale": type_.scale},
|
|
2416
|
+
)
|
|
2417
|
+
|
|
2418
|
+
def visit_DECIMAL(self, type_: DECIMAL, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2419
|
+
if type_.precision is None:
|
|
2420
|
+
return self._extend_numeric(type_, "DECIMAL")
|
|
2421
|
+
elif type_.scale is None:
|
|
2422
|
+
return self._extend_numeric(
|
|
2423
|
+
type_,
|
|
2424
|
+
"DECIMAL(%(precision)s)" % {"precision": type_.precision},
|
|
2425
|
+
)
|
|
2426
|
+
else:
|
|
2427
|
+
return self._extend_numeric(
|
|
2428
|
+
type_,
|
|
2429
|
+
"DECIMAL(%(precision)s, %(scale)s)"
|
|
2430
|
+
% {"precision": type_.precision, "scale": type_.scale},
|
|
2431
|
+
)
|
|
2432
|
+
|
|
2433
|
+
def visit_DOUBLE(self, type_: DOUBLE, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2434
|
+
if type_.precision is not None and type_.scale is not None:
|
|
2435
|
+
return self._extend_numeric(
|
|
2436
|
+
type_,
|
|
2437
|
+
"DOUBLE(%(precision)s, %(scale)s)"
|
|
2438
|
+
% {"precision": type_.precision, "scale": type_.scale},
|
|
2439
|
+
)
|
|
2440
|
+
else:
|
|
2441
|
+
return self._extend_numeric(type_, "DOUBLE")
|
|
2442
|
+
|
|
2443
|
+
def visit_REAL(self, type_: REAL, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2444
|
+
if type_.precision is not None and type_.scale is not None:
|
|
2445
|
+
return self._extend_numeric(
|
|
2446
|
+
type_,
|
|
2447
|
+
"REAL(%(precision)s, %(scale)s)"
|
|
2448
|
+
% {"precision": type_.precision, "scale": type_.scale},
|
|
2449
|
+
)
|
|
2450
|
+
else:
|
|
2451
|
+
return self._extend_numeric(type_, "REAL")
|
|
2452
|
+
|
|
2453
|
+
def visit_FLOAT(self, type_: FLOAT, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2454
|
+
if (
|
|
2455
|
+
self._mysql_type(type_)
|
|
2456
|
+
and type_.scale is not None
|
|
2457
|
+
and type_.precision is not None
|
|
2458
|
+
):
|
|
2459
|
+
return self._extend_numeric(
|
|
2460
|
+
type_, "FLOAT(%s, %s)" % (type_.precision, type_.scale)
|
|
2461
|
+
)
|
|
2462
|
+
elif type_.precision is not None:
|
|
2463
|
+
return self._extend_numeric(
|
|
2464
|
+
type_, "FLOAT(%s)" % (type_.precision,)
|
|
2465
|
+
)
|
|
2466
|
+
else:
|
|
2467
|
+
return self._extend_numeric(type_, "FLOAT")
|
|
2468
|
+
|
|
2469
|
+
def visit_INTEGER(self, type_: INTEGER, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2470
|
+
if self._mysql_type(type_) and type_.display_width is not None:
|
|
2471
|
+
return self._extend_numeric(
|
|
2472
|
+
type_,
|
|
2473
|
+
"INTEGER(%(display_width)s)"
|
|
2474
|
+
% {"display_width": type_.display_width},
|
|
2475
|
+
)
|
|
2476
|
+
else:
|
|
2477
|
+
return self._extend_numeric(type_, "INTEGER")
|
|
2478
|
+
|
|
2479
|
+
def visit_BIGINT(self, type_: BIGINT, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2480
|
+
if self._mysql_type(type_) and type_.display_width is not None:
|
|
2481
|
+
return self._extend_numeric(
|
|
2482
|
+
type_,
|
|
2483
|
+
"BIGINT(%(display_width)s)"
|
|
2484
|
+
% {"display_width": type_.display_width},
|
|
2485
|
+
)
|
|
2486
|
+
else:
|
|
2487
|
+
return self._extend_numeric(type_, "BIGINT")
|
|
2488
|
+
|
|
2489
|
+
def visit_MEDIUMINT(self, type_: MEDIUMINT, **kw: Any) -> str:
|
|
2490
|
+
if self._mysql_type(type_) and type_.display_width is not None:
|
|
2491
|
+
return self._extend_numeric(
|
|
2492
|
+
type_,
|
|
2493
|
+
"MEDIUMINT(%(display_width)s)"
|
|
2494
|
+
% {"display_width": type_.display_width},
|
|
2495
|
+
)
|
|
2496
|
+
else:
|
|
2497
|
+
return self._extend_numeric(type_, "MEDIUMINT")
|
|
2498
|
+
|
|
2499
|
+
def visit_TINYINT(self, type_: TINYINT, **kw: Any) -> str:
|
|
2500
|
+
if self._mysql_type(type_) and type_.display_width is not None:
|
|
2501
|
+
return self._extend_numeric(
|
|
2502
|
+
type_, "TINYINT(%s)" % type_.display_width
|
|
2503
|
+
)
|
|
2504
|
+
else:
|
|
2505
|
+
return self._extend_numeric(type_, "TINYINT")
|
|
2506
|
+
|
|
2507
|
+
def visit_SMALLINT(self, type_: SMALLINT, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2508
|
+
if self._mysql_type(type_) and type_.display_width is not None:
|
|
2509
|
+
return self._extend_numeric(
|
|
2510
|
+
type_,
|
|
2511
|
+
"SMALLINT(%(display_width)s)"
|
|
2512
|
+
% {"display_width": type_.display_width},
|
|
2513
|
+
)
|
|
2514
|
+
else:
|
|
2515
|
+
return self._extend_numeric(type_, "SMALLINT")
|
|
2516
|
+
|
|
2517
|
+
def visit_BIT(self, type_: BIT, **kw: Any) -> str:
|
|
2518
|
+
if type_.length is not None:
|
|
2519
|
+
return "BIT(%s)" % type_.length
|
|
2520
|
+
else:
|
|
2521
|
+
return "BIT"
|
|
2522
|
+
|
|
2523
|
+
def visit_DATETIME(self, type_: DATETIME, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2524
|
+
if getattr(type_, "fsp", None):
|
|
2525
|
+
return "DATETIME(%d)" % type_.fsp # type: ignore[str-format]
|
|
2526
|
+
else:
|
|
2527
|
+
return "DATETIME"
|
|
2528
|
+
|
|
2529
|
+
def visit_DATE(self, type_: DATE, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2530
|
+
return "DATE"
|
|
2531
|
+
|
|
2532
|
+
def visit_TIME(self, type_: TIME, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2533
|
+
if getattr(type_, "fsp", None):
|
|
2534
|
+
return "TIME(%d)" % type_.fsp # type: ignore[str-format]
|
|
2535
|
+
else:
|
|
2536
|
+
return "TIME"
|
|
2537
|
+
|
|
2538
|
+
def visit_TIMESTAMP(self, type_: TIMESTAMP, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2539
|
+
if getattr(type_, "fsp", None):
|
|
2540
|
+
return "TIMESTAMP(%d)" % type_.fsp # type: ignore[str-format]
|
|
2541
|
+
else:
|
|
2542
|
+
return "TIMESTAMP"
|
|
2543
|
+
|
|
2544
|
+
def visit_YEAR(self, type_: YEAR, **kw: Any) -> str:
|
|
2545
|
+
if type_.display_width is None:
|
|
2546
|
+
return "YEAR"
|
|
2547
|
+
else:
|
|
2548
|
+
return "YEAR(%s)" % type_.display_width
|
|
2549
|
+
|
|
2550
|
+
def visit_TEXT(self, type_: TEXT, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2551
|
+
if type_.length is not None:
|
|
2552
|
+
return self._extend_string(type_, {}, "TEXT(%d)" % type_.length)
|
|
2553
|
+
else:
|
|
2554
|
+
return self._extend_string(type_, {}, "TEXT")
|
|
2555
|
+
|
|
2556
|
+
def visit_TINYTEXT(self, type_: TINYTEXT, **kw: Any) -> str:
|
|
2557
|
+
return self._extend_string(type_, {}, "TINYTEXT")
|
|
2558
|
+
|
|
2559
|
+
def visit_MEDIUMTEXT(self, type_: MEDIUMTEXT, **kw: Any) -> str:
|
|
2560
|
+
return self._extend_string(type_, {}, "MEDIUMTEXT")
|
|
2561
|
+
|
|
2562
|
+
def visit_LONGTEXT(self, type_: LONGTEXT, **kw: Any) -> str:
|
|
2563
|
+
return self._extend_string(type_, {}, "LONGTEXT")
|
|
2564
|
+
|
|
2565
|
+
def visit_VARCHAR(self, type_: VARCHAR, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2566
|
+
if type_.length is not None:
|
|
2567
|
+
return self._extend_string(type_, {}, "VARCHAR(%d)" % type_.length)
|
|
2568
|
+
else:
|
|
2569
|
+
raise exc.CompileError(
|
|
2570
|
+
"VARCHAR requires a length on dialect %s" % self.dialect.name
|
|
2571
|
+
)
|
|
2572
|
+
|
|
2573
|
+
def visit_CHAR(self, type_: CHAR, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2574
|
+
if type_.length is not None:
|
|
2575
|
+
return self._extend_string(
|
|
2576
|
+
type_, {}, "CHAR(%(length)s)" % {"length": type_.length}
|
|
2577
|
+
)
|
|
2578
|
+
else:
|
|
2579
|
+
return self._extend_string(type_, {}, "CHAR")
|
|
2580
|
+
|
|
2581
|
+
def visit_NVARCHAR(self, type_: NVARCHAR, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2582
|
+
# We'll actually generate the equiv. "NATIONAL VARCHAR" instead
|
|
2583
|
+
# of "NVARCHAR".
|
|
2584
|
+
if type_.length is not None:
|
|
2585
|
+
return self._extend_string(
|
|
2586
|
+
type_,
|
|
2587
|
+
{"national": True},
|
|
2588
|
+
"VARCHAR(%(length)s)" % {"length": type_.length},
|
|
2589
|
+
)
|
|
2590
|
+
else:
|
|
2591
|
+
raise exc.CompileError(
|
|
2592
|
+
"NVARCHAR requires a length on dialect %s" % self.dialect.name
|
|
2593
|
+
)
|
|
2594
|
+
|
|
2595
|
+
def visit_NCHAR(self, type_: NCHAR, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2596
|
+
# We'll actually generate the equiv.
|
|
2597
|
+
# "NATIONAL CHAR" instead of "NCHAR".
|
|
2598
|
+
if type_.length is not None:
|
|
2599
|
+
return self._extend_string(
|
|
2600
|
+
type_,
|
|
2601
|
+
{"national": True},
|
|
2602
|
+
"CHAR(%(length)s)" % {"length": type_.length},
|
|
2603
|
+
)
|
|
2604
|
+
else:
|
|
2605
|
+
return self._extend_string(type_, {"national": True}, "CHAR")
|
|
2606
|
+
|
|
2607
|
+
def visit_UUID(self, type_: UUID[Any], **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2608
|
+
return "UUID"
|
|
2609
|
+
|
|
2610
|
+
def visit_VARBINARY(self, type_: VARBINARY, **kw: Any) -> str:
|
|
2611
|
+
return "VARBINARY(%d)" % type_.length # type: ignore[str-format]
|
|
2612
|
+
|
|
2613
|
+
def visit_JSON(self, type_: JSON, **kw: Any) -> str:
|
|
2614
|
+
return "JSON"
|
|
2615
|
+
|
|
2616
|
+
def visit_large_binary(self, type_: LargeBinary, **kw: Any) -> str:
|
|
2617
|
+
return self.visit_BLOB(type_)
|
|
2618
|
+
|
|
2619
|
+
def visit_enum(self, type_: ENUM, **kw: Any) -> str: # type: ignore[override] # NOQA: E501
|
|
2620
|
+
if not type_.native_enum:
|
|
2621
|
+
return super().visit_enum(type_)
|
|
2622
|
+
else:
|
|
2623
|
+
return self._visit_enumerated_values("ENUM", type_, type_.enums)
|
|
2624
|
+
|
|
2625
|
+
def visit_BLOB(self, type_: LargeBinary, **kw: Any) -> str:
|
|
2626
|
+
if type_.length is not None:
|
|
2627
|
+
return "BLOB(%d)" % type_.length
|
|
2628
|
+
else:
|
|
2629
|
+
return "BLOB"
|
|
2630
|
+
|
|
2631
|
+
def visit_TINYBLOB(self, type_: TINYBLOB, **kw: Any) -> str:
|
|
2632
|
+
return "TINYBLOB"
|
|
2633
|
+
|
|
2634
|
+
def visit_MEDIUMBLOB(self, type_: MEDIUMBLOB, **kw: Any) -> str:
|
|
2635
|
+
return "MEDIUMBLOB"
|
|
2636
|
+
|
|
2637
|
+
def visit_LONGBLOB(self, type_: LONGBLOB, **kw: Any) -> str:
|
|
2638
|
+
return "LONGBLOB"
|
|
2639
|
+
|
|
2640
|
+
def _visit_enumerated_values(
|
|
2641
|
+
self, name: str, type_: _StringType, enumerated_values: Sequence[str]
|
|
2642
|
+
) -> str:
|
|
2643
|
+
quoted_enums = []
|
|
2644
|
+
for e in enumerated_values:
|
|
2645
|
+
if self.dialect.identifier_preparer._double_percents:
|
|
2646
|
+
e = e.replace("%", "%%")
|
|
2647
|
+
quoted_enums.append("'%s'" % e.replace("'", "''"))
|
|
2648
|
+
return self._extend_string(
|
|
2649
|
+
type_, {}, "%s(%s)" % (name, ",".join(quoted_enums))
|
|
2650
|
+
)
|
|
2651
|
+
|
|
2652
|
+
def visit_ENUM(self, type_: ENUM, **kw: Any) -> str:
|
|
2653
|
+
return self._visit_enumerated_values("ENUM", type_, type_.enums)
|
|
2654
|
+
|
|
2655
|
+
def visit_SET(self, type_: SET, **kw: Any) -> str:
|
|
2656
|
+
return self._visit_enumerated_values("SET", type_, type_.values)
|
|
2657
|
+
|
|
2658
|
+
def visit_BOOLEAN(self, type_: sqltypes.Boolean, **kw: Any) -> str:
|
|
2659
|
+
return "BOOL"
|
|
2660
|
+
|
|
2661
|
+
|
|
2662
|
+
class MySQLIdentifierPreparer(compiler.IdentifierPreparer):
|
|
2663
|
+
reserved_words = RESERVED_WORDS_MYSQL
|
|
2664
|
+
|
|
2665
|
+
def __init__(
|
|
2666
|
+
self,
|
|
2667
|
+
dialect: default.DefaultDialect,
|
|
2668
|
+
server_ansiquotes: bool = False,
|
|
2669
|
+
**kw: Any,
|
|
2670
|
+
):
|
|
2671
|
+
if not server_ansiquotes:
|
|
2672
|
+
quote = "`"
|
|
2673
|
+
else:
|
|
2674
|
+
quote = '"'
|
|
2675
|
+
|
|
2676
|
+
super().__init__(dialect, initial_quote=quote, escape_quote=quote)
|
|
2677
|
+
|
|
2678
|
+
def _quote_free_identifiers(self, *ids: Optional[str]) -> Tuple[str, ...]:
|
|
2679
|
+
"""Unilaterally identifier-quote any number of strings."""
|
|
2680
|
+
|
|
2681
|
+
return tuple([self.quote_identifier(i) for i in ids if i is not None])
|
|
2682
|
+
|
|
2683
|
+
|
|
2684
|
+
class MariaDBIdentifierPreparer(MySQLIdentifierPreparer):
|
|
2685
|
+
reserved_words = RESERVED_WORDS_MARIADB
|
|
2686
|
+
|
|
2687
|
+
|
|
2688
|
+
class MySQLDialect(default.DefaultDialect):
|
|
2689
|
+
"""Details of the MySQL dialect.
|
|
2690
|
+
Not used directly in application code.
|
|
2691
|
+
"""
|
|
2692
|
+
|
|
2693
|
+
name = "mysql"
|
|
2694
|
+
supports_statement_cache = True
|
|
2695
|
+
|
|
2696
|
+
supports_alter = True
|
|
2697
|
+
|
|
2698
|
+
# MySQL has no true "boolean" type; we
|
|
2699
|
+
# allow for the "true" and "false" keywords, however
|
|
2700
|
+
supports_native_boolean = False
|
|
2701
|
+
|
|
2702
|
+
# support for BIT type; mysqlconnector coerces result values automatically,
|
|
2703
|
+
# all other MySQL DBAPIs require a conversion routine
|
|
2704
|
+
supports_native_bit = False
|
|
2705
|
+
|
|
2706
|
+
# identifiers are 64, however aliases can be 255...
|
|
2707
|
+
max_identifier_length = 255
|
|
2708
|
+
max_index_name_length = 64
|
|
2709
|
+
max_constraint_name_length = 64
|
|
2710
|
+
|
|
2711
|
+
div_is_floordiv = False
|
|
2712
|
+
|
|
2713
|
+
supports_native_enum = True
|
|
2714
|
+
|
|
2715
|
+
returns_native_bytes = True
|
|
2716
|
+
|
|
2717
|
+
# ... may be updated to True for MariaDB 10.3+ in initialize()
|
|
2718
|
+
supports_sequences = False
|
|
2719
|
+
|
|
2720
|
+
sequences_optional = False
|
|
2721
|
+
|
|
2722
|
+
# ... may be updated to True for MySQL 8+ in initialize()
|
|
2723
|
+
supports_for_update_of = False
|
|
2724
|
+
|
|
2725
|
+
# mysql 8.0.1 uses this syntax
|
|
2726
|
+
use_mysql_for_share = False
|
|
2727
|
+
|
|
2728
|
+
# Only available ... ... in MySQL 8+
|
|
2729
|
+
_requires_alias_for_on_duplicate_key = False
|
|
2730
|
+
|
|
2731
|
+
# MySQL doesn't support "DEFAULT VALUES" but *does* support
|
|
2732
|
+
# "VALUES (DEFAULT)"
|
|
2733
|
+
supports_default_values = False
|
|
2734
|
+
supports_default_metavalue = True
|
|
2735
|
+
|
|
2736
|
+
use_insertmanyvalues: bool = True
|
|
2737
|
+
insertmanyvalues_implicit_sentinel = (
|
|
2738
|
+
InsertmanyvaluesSentinelOpts.ANY_AUTOINCREMENT
|
|
2739
|
+
)
|
|
2740
|
+
|
|
2741
|
+
supports_sane_rowcount = True
|
|
2742
|
+
supports_sane_multi_rowcount = False
|
|
2743
|
+
supports_multivalues_insert = True
|
|
2744
|
+
insert_null_pk_still_autoincrements = True
|
|
2745
|
+
|
|
2746
|
+
supports_comments = True
|
|
2747
|
+
inline_comments = True
|
|
2748
|
+
default_paramstyle = "format"
|
|
2749
|
+
colspecs = colspecs
|
|
2750
|
+
|
|
2751
|
+
cte_follows_insert = True
|
|
2752
|
+
|
|
2753
|
+
statement_compiler = MySQLCompiler
|
|
2754
|
+
ddl_compiler = MySQLDDLCompiler
|
|
2755
|
+
type_compiler_cls = MySQLTypeCompiler
|
|
2756
|
+
ischema_names = ischema_names
|
|
2757
|
+
preparer: type[MySQLIdentifierPreparer] = MySQLIdentifierPreparer
|
|
2758
|
+
|
|
2759
|
+
is_mariadb: bool = False
|
|
2760
|
+
_mariadb_normalized_version_info = None
|
|
2761
|
+
|
|
2762
|
+
# default SQL compilation settings -
|
|
2763
|
+
# these are modified upon initialize(),
|
|
2764
|
+
# i.e. first connect
|
|
2765
|
+
_backslash_escapes = True
|
|
2766
|
+
_server_ansiquotes = False
|
|
2767
|
+
|
|
2768
|
+
server_version_info: Tuple[int, ...]
|
|
2769
|
+
identifier_preparer: MySQLIdentifierPreparer
|
|
2770
|
+
|
|
2771
|
+
construct_arguments = [
|
|
2772
|
+
(sa_schema.Table, {"*": None}),
|
|
2773
|
+
(sql.Update, {"limit": None}),
|
|
2774
|
+
(sql.Delete, {"limit": None}),
|
|
2775
|
+
(sa_schema.PrimaryKeyConstraint, {"using": None}),
|
|
2776
|
+
(
|
|
2777
|
+
sa_schema.Index,
|
|
2778
|
+
{
|
|
2779
|
+
"using": None,
|
|
2780
|
+
"length": None,
|
|
2781
|
+
"prefix": None,
|
|
2782
|
+
"with_parser": None,
|
|
2783
|
+
},
|
|
2784
|
+
),
|
|
2785
|
+
]
|
|
2786
|
+
|
|
2787
|
+
def __init__(
|
|
2788
|
+
self,
|
|
2789
|
+
json_serializer: Optional[Callable[..., Any]] = None,
|
|
2790
|
+
json_deserializer: Optional[Callable[..., Any]] = None,
|
|
2791
|
+
is_mariadb: Optional[bool] = None,
|
|
2792
|
+
**kwargs: Any,
|
|
2793
|
+
) -> None:
|
|
2794
|
+
kwargs.pop("use_ansiquotes", None) # legacy
|
|
2795
|
+
default.DefaultDialect.__init__(self, **kwargs)
|
|
2796
|
+
self._json_serializer = json_serializer
|
|
2797
|
+
self._json_deserializer = json_deserializer
|
|
2798
|
+
self._set_mariadb(is_mariadb, ())
|
|
2799
|
+
|
|
2800
|
+
def get_isolation_level_values(
|
|
2801
|
+
self, dbapi_conn: DBAPIConnection
|
|
2802
|
+
) -> Sequence[IsolationLevel]:
|
|
2803
|
+
return (
|
|
2804
|
+
"SERIALIZABLE",
|
|
2805
|
+
"READ UNCOMMITTED",
|
|
2806
|
+
"READ COMMITTED",
|
|
2807
|
+
"REPEATABLE READ",
|
|
2808
|
+
)
|
|
2809
|
+
|
|
2810
|
+
def set_isolation_level(
|
|
2811
|
+
self, dbapi_connection: DBAPIConnection, level: IsolationLevel
|
|
2812
|
+
) -> None:
|
|
2813
|
+
cursor = dbapi_connection.cursor()
|
|
2814
|
+
cursor.execute(f"SET SESSION TRANSACTION ISOLATION LEVEL {level}")
|
|
2815
|
+
cursor.execute("COMMIT")
|
|
2816
|
+
cursor.close()
|
|
2817
|
+
|
|
2818
|
+
def get_isolation_level(
|
|
2819
|
+
self, dbapi_connection: DBAPIConnection
|
|
2820
|
+
) -> IsolationLevel:
|
|
2821
|
+
cursor = dbapi_connection.cursor()
|
|
2822
|
+
if self._is_mysql and self.server_version_info >= (5, 7, 20):
|
|
2823
|
+
cursor.execute("SELECT @@transaction_isolation")
|
|
2824
|
+
else:
|
|
2825
|
+
cursor.execute("SELECT @@tx_isolation")
|
|
2826
|
+
row = cursor.fetchone()
|
|
2827
|
+
if row is None:
|
|
2828
|
+
util.warn(
|
|
2829
|
+
"Could not retrieve transaction isolation level for MySQL "
|
|
2830
|
+
"connection."
|
|
2831
|
+
)
|
|
2832
|
+
raise NotImplementedError()
|
|
2833
|
+
val = row[0]
|
|
2834
|
+
cursor.close()
|
|
2835
|
+
if isinstance(val, bytes):
|
|
2836
|
+
val = val.decode()
|
|
2837
|
+
return val.upper().replace("-", " ") # type: ignore[no-any-return]
|
|
2838
|
+
|
|
2839
|
+
@classmethod
|
|
2840
|
+
def _is_mariadb_from_url(cls, url: URL) -> bool:
|
|
2841
|
+
dbapi = cls.import_dbapi()
|
|
2842
|
+
dialect = cls(dbapi=dbapi)
|
|
2843
|
+
|
|
2844
|
+
cargs, cparams = dialect.create_connect_args(url)
|
|
2845
|
+
conn = dialect.connect(*cargs, **cparams)
|
|
2846
|
+
try:
|
|
2847
|
+
cursor = conn.cursor()
|
|
2848
|
+
cursor.execute("SELECT VERSION() LIKE '%MariaDB%'")
|
|
2849
|
+
val = cursor.fetchone()[0] # type: ignore[index]
|
|
2850
|
+
except:
|
|
2851
|
+
raise
|
|
2852
|
+
else:
|
|
2853
|
+
return bool(val)
|
|
2854
|
+
finally:
|
|
2855
|
+
conn.close()
|
|
2856
|
+
|
|
2857
|
+
def _get_server_version_info(
|
|
2858
|
+
self, connection: Connection
|
|
2859
|
+
) -> Tuple[int, ...]:
|
|
2860
|
+
# get database server version info explicitly over the wire
|
|
2861
|
+
# to avoid proxy servers like MaxScale getting in the
|
|
2862
|
+
# way with their own values, see #4205
|
|
2863
|
+
dbapi_con = connection.connection
|
|
2864
|
+
cursor = dbapi_con.cursor()
|
|
2865
|
+
cursor.execute("SELECT VERSION()")
|
|
2866
|
+
|
|
2867
|
+
val = cursor.fetchone()[0] # type: ignore[index]
|
|
2868
|
+
cursor.close()
|
|
2869
|
+
if isinstance(val, bytes):
|
|
2870
|
+
val = val.decode()
|
|
2871
|
+
|
|
2872
|
+
return self._parse_server_version(val)
|
|
2873
|
+
|
|
2874
|
+
def _parse_server_version(self, val: str) -> Tuple[int, ...]:
|
|
2875
|
+
version: List[int] = []
|
|
2876
|
+
is_mariadb = False
|
|
2877
|
+
|
|
2878
|
+
r = re.compile(r"[.\-+]")
|
|
2879
|
+
tokens = r.split(val)
|
|
2880
|
+
for token in tokens:
|
|
2881
|
+
parsed_token = re.match(
|
|
2882
|
+
r"^(?:(\d+)(?:a|b|c)?|(MariaDB\w*))$", token
|
|
2883
|
+
)
|
|
2884
|
+
if not parsed_token:
|
|
2885
|
+
continue
|
|
2886
|
+
elif parsed_token.group(2):
|
|
2887
|
+
self._mariadb_normalized_version_info = tuple(version[-3:])
|
|
2888
|
+
is_mariadb = True
|
|
2889
|
+
else:
|
|
2890
|
+
digit = int(parsed_token.group(1))
|
|
2891
|
+
version.append(digit)
|
|
2892
|
+
|
|
2893
|
+
server_version_info = tuple(version)
|
|
2894
|
+
|
|
2895
|
+
self._set_mariadb(
|
|
2896
|
+
bool(server_version_info and is_mariadb), server_version_info
|
|
2897
|
+
)
|
|
2898
|
+
|
|
2899
|
+
if not is_mariadb:
|
|
2900
|
+
self._mariadb_normalized_version_info = server_version_info
|
|
2901
|
+
|
|
2902
|
+
if server_version_info < (5, 0, 2):
|
|
2903
|
+
raise NotImplementedError(
|
|
2904
|
+
"the MySQL/MariaDB dialect supports server "
|
|
2905
|
+
"version info 5.0.2 and above."
|
|
2906
|
+
)
|
|
2907
|
+
|
|
2908
|
+
# setting it here to help w the test suite
|
|
2909
|
+
self.server_version_info = server_version_info
|
|
2910
|
+
return server_version_info
|
|
2911
|
+
|
|
2912
|
+
def _set_mariadb(
|
|
2913
|
+
self, is_mariadb: Optional[bool], server_version_info: Tuple[int, ...]
|
|
2914
|
+
) -> None:
|
|
2915
|
+
if is_mariadb is None:
|
|
2916
|
+
return
|
|
2917
|
+
|
|
2918
|
+
if not is_mariadb and self.is_mariadb:
|
|
2919
|
+
raise exc.InvalidRequestError(
|
|
2920
|
+
"MySQL version %s is not a MariaDB variant."
|
|
2921
|
+
% (".".join(map(str, server_version_info)),)
|
|
2922
|
+
)
|
|
2923
|
+
if is_mariadb:
|
|
2924
|
+
|
|
2925
|
+
if not issubclass(self.preparer, MariaDBIdentifierPreparer):
|
|
2926
|
+
self.preparer = MariaDBIdentifierPreparer
|
|
2927
|
+
# this would have been set by the default dialect already,
|
|
2928
|
+
# so set it again
|
|
2929
|
+
self.identifier_preparer = self.preparer(self)
|
|
2930
|
+
|
|
2931
|
+
# this will be updated on first connect in initialize()
|
|
2932
|
+
# if using older mariadb version
|
|
2933
|
+
self.delete_returning = True
|
|
2934
|
+
self.insert_returning = True
|
|
2935
|
+
|
|
2936
|
+
self.is_mariadb = is_mariadb
|
|
2937
|
+
|
|
2938
|
+
def do_begin_twophase(self, connection: Connection, xid: Any) -> None:
|
|
2939
|
+
connection.execute(sql.text("XA BEGIN :xid"), dict(xid=xid))
|
|
2940
|
+
|
|
2941
|
+
def do_prepare_twophase(self, connection: Connection, xid: Any) -> None:
|
|
2942
|
+
connection.execute(sql.text("XA END :xid"), dict(xid=xid))
|
|
2943
|
+
connection.execute(sql.text("XA PREPARE :xid"), dict(xid=xid))
|
|
2944
|
+
|
|
2945
|
+
def do_rollback_twophase(
|
|
2946
|
+
self,
|
|
2947
|
+
connection: Connection,
|
|
2948
|
+
xid: Any,
|
|
2949
|
+
is_prepared: bool = True,
|
|
2950
|
+
recover: bool = False,
|
|
2951
|
+
) -> None:
|
|
2952
|
+
if not is_prepared:
|
|
2953
|
+
connection.execute(sql.text("XA END :xid"), dict(xid=xid))
|
|
2954
|
+
connection.execute(sql.text("XA ROLLBACK :xid"), dict(xid=xid))
|
|
2955
|
+
|
|
2956
|
+
def do_commit_twophase(
|
|
2957
|
+
self,
|
|
2958
|
+
connection: Connection,
|
|
2959
|
+
xid: Any,
|
|
2960
|
+
is_prepared: bool = True,
|
|
2961
|
+
recover: bool = False,
|
|
2962
|
+
) -> None:
|
|
2963
|
+
if not is_prepared:
|
|
2964
|
+
self.do_prepare_twophase(connection, xid)
|
|
2965
|
+
connection.execute(sql.text("XA COMMIT :xid"), dict(xid=xid))
|
|
2966
|
+
|
|
2967
|
+
def do_recover_twophase(self, connection: Connection) -> List[Any]:
|
|
2968
|
+
resultset = connection.exec_driver_sql("XA RECOVER")
|
|
2969
|
+
return [
|
|
2970
|
+
row["data"][0 : row["gtrid_length"]]
|
|
2971
|
+
for row in resultset.mappings()
|
|
2972
|
+
]
|
|
2973
|
+
|
|
2974
|
+
def is_disconnect(
|
|
2975
|
+
self,
|
|
2976
|
+
e: DBAPIModule.Error,
|
|
2977
|
+
connection: Optional[Union[PoolProxiedConnection, DBAPIConnection]],
|
|
2978
|
+
cursor: Optional[DBAPICursor],
|
|
2979
|
+
) -> bool:
|
|
2980
|
+
if isinstance(
|
|
2981
|
+
e,
|
|
2982
|
+
(
|
|
2983
|
+
self.dbapi.OperationalError, # type: ignore
|
|
2984
|
+
self.dbapi.ProgrammingError, # type: ignore
|
|
2985
|
+
self.dbapi.InterfaceError, # type: ignore
|
|
2986
|
+
),
|
|
2987
|
+
) and self._extract_error_code(e) in (
|
|
2988
|
+
1927,
|
|
2989
|
+
2006,
|
|
2990
|
+
2013,
|
|
2991
|
+
2014,
|
|
2992
|
+
2045,
|
|
2993
|
+
2055,
|
|
2994
|
+
4031,
|
|
2995
|
+
):
|
|
2996
|
+
return True
|
|
2997
|
+
elif isinstance(
|
|
2998
|
+
e, (self.dbapi.InterfaceError, self.dbapi.InternalError) # type: ignore # noqa: E501
|
|
2999
|
+
):
|
|
3000
|
+
# if underlying connection is closed,
|
|
3001
|
+
# this is the error you get
|
|
3002
|
+
return "(0, '')" in str(e)
|
|
3003
|
+
else:
|
|
3004
|
+
return False
|
|
3005
|
+
|
|
3006
|
+
def _compat_fetchall(
|
|
3007
|
+
self, rp: CursorResult[Any], charset: Optional[str] = None
|
|
3008
|
+
) -> Union[Sequence[Row[Any]], Sequence[_DecodingRow]]:
|
|
3009
|
+
"""Proxy result rows to smooth over MySQL-Python driver
|
|
3010
|
+
inconsistencies."""
|
|
3011
|
+
|
|
3012
|
+
return [_DecodingRow(row, charset) for row in rp.fetchall()]
|
|
3013
|
+
|
|
3014
|
+
def _compat_fetchone(
|
|
3015
|
+
self, rp: CursorResult[Any], charset: Optional[str] = None
|
|
3016
|
+
) -> Union[Row[Any], None, _DecodingRow]:
|
|
3017
|
+
"""Proxy a result row to smooth over MySQL-Python driver
|
|
3018
|
+
inconsistencies."""
|
|
3019
|
+
|
|
3020
|
+
row = rp.fetchone()
|
|
3021
|
+
if row:
|
|
3022
|
+
return _DecodingRow(row, charset)
|
|
3023
|
+
else:
|
|
3024
|
+
return None
|
|
3025
|
+
|
|
3026
|
+
def _compat_first(
|
|
3027
|
+
self, rp: CursorResult[Any], charset: Optional[str] = None
|
|
3028
|
+
) -> Optional[_DecodingRow]:
|
|
3029
|
+
"""Proxy a result row to smooth over MySQL-Python driver
|
|
3030
|
+
inconsistencies."""
|
|
3031
|
+
|
|
3032
|
+
row = rp.first()
|
|
3033
|
+
if row:
|
|
3034
|
+
return _DecodingRow(row, charset)
|
|
3035
|
+
else:
|
|
3036
|
+
return None
|
|
3037
|
+
|
|
3038
|
+
def _extract_error_code(
|
|
3039
|
+
self, exception: DBAPIModule.Error
|
|
3040
|
+
) -> Optional[int]:
|
|
3041
|
+
raise NotImplementedError()
|
|
3042
|
+
|
|
3043
|
+
def _get_default_schema_name(self, connection: Connection) -> str:
|
|
3044
|
+
return connection.exec_driver_sql("SELECT DATABASE()").scalar() # type: ignore[return-value] # noqa: E501
|
|
3045
|
+
|
|
3046
|
+
@reflection.cache
|
|
3047
|
+
def has_table(
|
|
3048
|
+
self,
|
|
3049
|
+
connection: Connection,
|
|
3050
|
+
table_name: str,
|
|
3051
|
+
schema: Optional[str] = None,
|
|
3052
|
+
**kw: Any,
|
|
3053
|
+
) -> bool:
|
|
3054
|
+
self._ensure_has_table_connection(connection)
|
|
3055
|
+
|
|
3056
|
+
if schema is None:
|
|
3057
|
+
schema = self.default_schema_name
|
|
3058
|
+
|
|
3059
|
+
assert schema is not None
|
|
3060
|
+
|
|
3061
|
+
full_name = ".".join(
|
|
3062
|
+
self.identifier_preparer._quote_free_identifiers(
|
|
3063
|
+
schema, table_name
|
|
3064
|
+
)
|
|
3065
|
+
)
|
|
3066
|
+
|
|
3067
|
+
# DESCRIBE *must* be used because there is no information schema
|
|
3068
|
+
# table that returns information on temp tables that is consistently
|
|
3069
|
+
# available on MariaDB / MySQL / engine-agnostic etc.
|
|
3070
|
+
# therefore we have no choice but to use DESCRIBE and an error catch
|
|
3071
|
+
# to detect "False". See issue #9058
|
|
3072
|
+
|
|
3073
|
+
try:
|
|
3074
|
+
with connection.exec_driver_sql(
|
|
3075
|
+
f"DESCRIBE {full_name}",
|
|
3076
|
+
execution_options={"skip_user_error_events": True},
|
|
3077
|
+
) as rs:
|
|
3078
|
+
return rs.fetchone() is not None
|
|
3079
|
+
except exc.DBAPIError as e:
|
|
3080
|
+
# https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html # noqa: E501
|
|
3081
|
+
# there are a lot of codes that *may* pop up here at some point
|
|
3082
|
+
# but we continue to be fairly conservative. We include:
|
|
3083
|
+
# 1146: Table '%s.%s' doesn't exist - what every MySQL has emitted
|
|
3084
|
+
# for decades
|
|
3085
|
+
#
|
|
3086
|
+
# mysql 8 suddenly started emitting:
|
|
3087
|
+
# 1049: Unknown database '%s' - for nonexistent schema
|
|
3088
|
+
#
|
|
3089
|
+
# also added:
|
|
3090
|
+
# 1051: Unknown table '%s' - not known to emit
|
|
3091
|
+
#
|
|
3092
|
+
# there's more "doesn't exist" kinds of messages but they are
|
|
3093
|
+
# less clear if mysql 8 would suddenly start using one of those
|
|
3094
|
+
if self._extract_error_code(e.orig) in (1146, 1049, 1051): # type: ignore # noqa: E501
|
|
3095
|
+
return False
|
|
3096
|
+
raise
|
|
3097
|
+
|
|
3098
|
+
@reflection.cache
|
|
3099
|
+
def has_sequence(
|
|
3100
|
+
self,
|
|
3101
|
+
connection: Connection,
|
|
3102
|
+
sequence_name: str,
|
|
3103
|
+
schema: Optional[str] = None,
|
|
3104
|
+
**kw: Any,
|
|
3105
|
+
) -> bool:
|
|
3106
|
+
if not self.supports_sequences:
|
|
3107
|
+
self._sequences_not_supported()
|
|
3108
|
+
if not schema:
|
|
3109
|
+
schema = self.default_schema_name
|
|
3110
|
+
# MariaDB implements sequences as a special type of table
|
|
3111
|
+
#
|
|
3112
|
+
cursor = connection.execute(
|
|
3113
|
+
sql.text(
|
|
3114
|
+
"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES "
|
|
3115
|
+
"WHERE TABLE_TYPE='SEQUENCE' and TABLE_NAME=:name AND "
|
|
3116
|
+
"TABLE_SCHEMA=:schema_name"
|
|
3117
|
+
),
|
|
3118
|
+
dict(
|
|
3119
|
+
name=str(sequence_name),
|
|
3120
|
+
schema_name=str(schema),
|
|
3121
|
+
),
|
|
3122
|
+
)
|
|
3123
|
+
return cursor.first() is not None
|
|
3124
|
+
|
|
3125
|
+
def _sequences_not_supported(self) -> NoReturn:
|
|
3126
|
+
raise NotImplementedError(
|
|
3127
|
+
"Sequences are supported only by the "
|
|
3128
|
+
"MariaDB series 10.3 or greater"
|
|
3129
|
+
)
|
|
3130
|
+
|
|
3131
|
+
@reflection.cache
|
|
3132
|
+
def get_sequence_names(
|
|
3133
|
+
self, connection: Connection, schema: Optional[str] = None, **kw: Any
|
|
3134
|
+
) -> List[str]:
|
|
3135
|
+
if not self.supports_sequences:
|
|
3136
|
+
self._sequences_not_supported()
|
|
3137
|
+
if not schema:
|
|
3138
|
+
schema = self.default_schema_name
|
|
3139
|
+
# MariaDB implements sequences as a special type of table
|
|
3140
|
+
cursor = connection.execute(
|
|
3141
|
+
sql.text(
|
|
3142
|
+
"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES "
|
|
3143
|
+
"WHERE TABLE_TYPE='SEQUENCE' and TABLE_SCHEMA=:schema_name"
|
|
3144
|
+
),
|
|
3145
|
+
dict(schema_name=schema),
|
|
3146
|
+
)
|
|
3147
|
+
return [
|
|
3148
|
+
row[0]
|
|
3149
|
+
for row in self._compat_fetchall(
|
|
3150
|
+
cursor, charset=self._connection_charset
|
|
3151
|
+
)
|
|
3152
|
+
]
|
|
3153
|
+
|
|
3154
|
+
def initialize(self, connection: Connection) -> None:
|
|
3155
|
+
# this is driver-based, does not need server version info
|
|
3156
|
+
# and is fairly critical for even basic SQL operations
|
|
3157
|
+
self._connection_charset: Optional[str] = self._detect_charset(
|
|
3158
|
+
connection
|
|
3159
|
+
)
|
|
3160
|
+
|
|
3161
|
+
# call super().initialize() because we need to have
|
|
3162
|
+
# server_version_info set up. in 1.4 under python 2 only this does the
|
|
3163
|
+
# "check unicode returns" thing, which is the one area that some
|
|
3164
|
+
# SQL gets compiled within initialize() currently
|
|
3165
|
+
default.DefaultDialect.initialize(self, connection)
|
|
3166
|
+
|
|
3167
|
+
self._detect_sql_mode(connection)
|
|
3168
|
+
self._detect_ansiquotes(connection) # depends on sql mode
|
|
3169
|
+
self._detect_casing(connection)
|
|
3170
|
+
if self._server_ansiquotes:
|
|
3171
|
+
# if ansiquotes == True, build a new IdentifierPreparer
|
|
3172
|
+
# with the new setting
|
|
3173
|
+
self.identifier_preparer = self.preparer(
|
|
3174
|
+
self, server_ansiquotes=self._server_ansiquotes
|
|
3175
|
+
)
|
|
3176
|
+
|
|
3177
|
+
self.supports_sequences = (
|
|
3178
|
+
self.is_mariadb and self.server_version_info >= (10, 3)
|
|
3179
|
+
)
|
|
3180
|
+
|
|
3181
|
+
self.supports_for_update_of = (
|
|
3182
|
+
self._is_mysql and self.server_version_info >= (8,)
|
|
3183
|
+
)
|
|
3184
|
+
|
|
3185
|
+
self.use_mysql_for_share = (
|
|
3186
|
+
self._is_mysql and self.server_version_info >= (8, 0, 1)
|
|
3187
|
+
)
|
|
3188
|
+
|
|
3189
|
+
self._needs_correct_for_88718_96365 = (
|
|
3190
|
+
not self.is_mariadb and self.server_version_info >= (8,)
|
|
3191
|
+
)
|
|
3192
|
+
|
|
3193
|
+
self.delete_returning = (
|
|
3194
|
+
self.is_mariadb and self.server_version_info >= (10, 0, 5)
|
|
3195
|
+
)
|
|
3196
|
+
|
|
3197
|
+
self.insert_returning = (
|
|
3198
|
+
self.is_mariadb and self.server_version_info >= (10, 5)
|
|
3199
|
+
)
|
|
3200
|
+
|
|
3201
|
+
self._requires_alias_for_on_duplicate_key = (
|
|
3202
|
+
self._is_mysql and self.server_version_info >= (8, 0, 20)
|
|
3203
|
+
)
|
|
3204
|
+
|
|
3205
|
+
self._warn_for_known_db_issues()
|
|
3206
|
+
|
|
3207
|
+
def _warn_for_known_db_issues(self) -> None:
|
|
3208
|
+
if self.is_mariadb:
|
|
3209
|
+
mdb_version = self._mariadb_normalized_version_info
|
|
3210
|
+
assert mdb_version is not None
|
|
3211
|
+
if mdb_version > (10, 2) and mdb_version < (10, 2, 9):
|
|
3212
|
+
util.warn(
|
|
3213
|
+
"MariaDB %r before 10.2.9 has known issues regarding "
|
|
3214
|
+
"CHECK constraints, which impact handling of NULL values "
|
|
3215
|
+
"with SQLAlchemy's boolean datatype (MDEV-13596). An "
|
|
3216
|
+
"additional issue prevents proper migrations of columns "
|
|
3217
|
+
"with CHECK constraints (MDEV-11114). Please upgrade to "
|
|
3218
|
+
"MariaDB 10.2.9 or greater, or use the MariaDB 10.1 "
|
|
3219
|
+
"series, to avoid these issues." % (mdb_version,)
|
|
3220
|
+
)
|
|
3221
|
+
|
|
3222
|
+
@property
|
|
3223
|
+
def _support_float_cast(self) -> bool:
|
|
3224
|
+
if not self.server_version_info:
|
|
3225
|
+
return False
|
|
3226
|
+
elif self.is_mariadb:
|
|
3227
|
+
# ref https://mariadb.com/kb/en/mariadb-1045-release-notes/
|
|
3228
|
+
return self.server_version_info >= (10, 4, 5)
|
|
3229
|
+
else:
|
|
3230
|
+
# ref https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-17.html#mysqld-8-0-17-feature # noqa
|
|
3231
|
+
return self.server_version_info >= (8, 0, 17)
|
|
3232
|
+
|
|
3233
|
+
@property
|
|
3234
|
+
def _support_default_function(self) -> bool:
|
|
3235
|
+
if not self.server_version_info:
|
|
3236
|
+
return False
|
|
3237
|
+
elif self.is_mariadb:
|
|
3238
|
+
# ref https://mariadb.com/kb/en/mariadb-1021-release-notes/
|
|
3239
|
+
return self.server_version_info >= (10, 2, 1)
|
|
3240
|
+
else:
|
|
3241
|
+
# ref https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html # noqa
|
|
3242
|
+
return self.server_version_info >= (8, 0, 13)
|
|
3243
|
+
|
|
3244
|
+
@property
|
|
3245
|
+
def _is_mariadb(self) -> bool:
|
|
3246
|
+
return self.is_mariadb
|
|
3247
|
+
|
|
3248
|
+
@property
|
|
3249
|
+
def _is_mysql(self) -> bool:
|
|
3250
|
+
return not self.is_mariadb
|
|
3251
|
+
|
|
3252
|
+
@property
|
|
3253
|
+
def _is_mariadb_102(self) -> bool:
|
|
3254
|
+
return (
|
|
3255
|
+
self.is_mariadb
|
|
3256
|
+
and self._mariadb_normalized_version_info # type:ignore[operator]
|
|
3257
|
+
> (
|
|
3258
|
+
10,
|
|
3259
|
+
2,
|
|
3260
|
+
)
|
|
3261
|
+
)
|
|
3262
|
+
|
|
3263
|
+
@reflection.cache
|
|
3264
|
+
def get_schema_names(self, connection: Connection, **kw: Any) -> List[str]:
|
|
3265
|
+
rp = connection.exec_driver_sql("SHOW schemas")
|
|
3266
|
+
return [r[0] for r in rp]
|
|
3267
|
+
|
|
3268
|
+
@reflection.cache
|
|
3269
|
+
def get_table_names(
|
|
3270
|
+
self, connection: Connection, schema: Optional[str] = None, **kw: Any
|
|
3271
|
+
) -> List[str]:
|
|
3272
|
+
"""Return a Unicode SHOW TABLES from a given schema."""
|
|
3273
|
+
if schema is not None:
|
|
3274
|
+
current_schema: str = schema
|
|
3275
|
+
else:
|
|
3276
|
+
current_schema = self.default_schema_name # type: ignore
|
|
3277
|
+
|
|
3278
|
+
charset = self._connection_charset
|
|
3279
|
+
|
|
3280
|
+
rp = connection.exec_driver_sql(
|
|
3281
|
+
"SHOW FULL TABLES FROM %s"
|
|
3282
|
+
% self.identifier_preparer.quote_identifier(current_schema)
|
|
3283
|
+
)
|
|
3284
|
+
|
|
3285
|
+
return [
|
|
3286
|
+
row[0]
|
|
3287
|
+
for row in self._compat_fetchall(rp, charset=charset)
|
|
3288
|
+
if row[1] == "BASE TABLE"
|
|
3289
|
+
]
|
|
3290
|
+
|
|
3291
|
+
@reflection.cache
|
|
3292
|
+
def get_view_names(
|
|
3293
|
+
self, connection: Connection, schema: Optional[str] = None, **kw: Any
|
|
3294
|
+
) -> List[str]:
|
|
3295
|
+
if schema is None:
|
|
3296
|
+
schema = self.default_schema_name
|
|
3297
|
+
assert schema is not None
|
|
3298
|
+
charset = self._connection_charset
|
|
3299
|
+
rp = connection.exec_driver_sql(
|
|
3300
|
+
"SHOW FULL TABLES FROM %s"
|
|
3301
|
+
% self.identifier_preparer.quote_identifier(schema)
|
|
3302
|
+
)
|
|
3303
|
+
return [
|
|
3304
|
+
row[0]
|
|
3305
|
+
for row in self._compat_fetchall(rp, charset=charset)
|
|
3306
|
+
if row[1] in ("VIEW", "SYSTEM VIEW")
|
|
3307
|
+
]
|
|
3308
|
+
|
|
3309
|
+
@reflection.cache
|
|
3310
|
+
def get_table_options(
|
|
3311
|
+
self,
|
|
3312
|
+
connection: Connection,
|
|
3313
|
+
table_name: str,
|
|
3314
|
+
schema: Optional[str] = None,
|
|
3315
|
+
**kw: Any,
|
|
3316
|
+
) -> Dict[str, Any]:
|
|
3317
|
+
parsed_state = self._parsed_state_or_create(
|
|
3318
|
+
connection, table_name, schema, **kw
|
|
3319
|
+
)
|
|
3320
|
+
if parsed_state.table_options:
|
|
3321
|
+
return parsed_state.table_options
|
|
3322
|
+
else:
|
|
3323
|
+
return ReflectionDefaults.table_options()
|
|
3324
|
+
|
|
3325
|
+
@reflection.cache
|
|
3326
|
+
def get_columns(
|
|
3327
|
+
self,
|
|
3328
|
+
connection: Connection,
|
|
3329
|
+
table_name: str,
|
|
3330
|
+
schema: Optional[str] = None,
|
|
3331
|
+
**kw: Any,
|
|
3332
|
+
) -> List[ReflectedColumn]:
|
|
3333
|
+
parsed_state = self._parsed_state_or_create(
|
|
3334
|
+
connection, table_name, schema, **kw
|
|
3335
|
+
)
|
|
3336
|
+
if parsed_state.columns:
|
|
3337
|
+
return parsed_state.columns
|
|
3338
|
+
else:
|
|
3339
|
+
return ReflectionDefaults.columns()
|
|
3340
|
+
|
|
3341
|
+
@reflection.cache
|
|
3342
|
+
def get_pk_constraint(
|
|
3343
|
+
self,
|
|
3344
|
+
connection: Connection,
|
|
3345
|
+
table_name: str,
|
|
3346
|
+
schema: Optional[str] = None,
|
|
3347
|
+
**kw: Any,
|
|
3348
|
+
) -> ReflectedPrimaryKeyConstraint:
|
|
3349
|
+
parsed_state = self._parsed_state_or_create(
|
|
3350
|
+
connection, table_name, schema, **kw
|
|
3351
|
+
)
|
|
3352
|
+
for key in parsed_state.keys:
|
|
3353
|
+
if key["type"] == "PRIMARY":
|
|
3354
|
+
# There can be only one.
|
|
3355
|
+
cols = [s[0] for s in key["columns"]]
|
|
3356
|
+
return {"constrained_columns": cols, "name": None}
|
|
3357
|
+
return ReflectionDefaults.pk_constraint()
|
|
3358
|
+
|
|
3359
|
+
@reflection.cache
|
|
3360
|
+
def get_foreign_keys(
|
|
3361
|
+
self,
|
|
3362
|
+
connection: Connection,
|
|
3363
|
+
table_name: str,
|
|
3364
|
+
schema: Optional[str] = None,
|
|
3365
|
+
**kw: Any,
|
|
3366
|
+
) -> List[ReflectedForeignKeyConstraint]:
|
|
3367
|
+
parsed_state = self._parsed_state_or_create(
|
|
3368
|
+
connection, table_name, schema, **kw
|
|
3369
|
+
)
|
|
3370
|
+
default_schema = None
|
|
3371
|
+
|
|
3372
|
+
fkeys: List[ReflectedForeignKeyConstraint] = []
|
|
3373
|
+
|
|
3374
|
+
for spec in parsed_state.fk_constraints:
|
|
3375
|
+
ref_name = spec["table"][-1]
|
|
3376
|
+
ref_schema = len(spec["table"]) > 1 and spec["table"][-2] or schema
|
|
3377
|
+
|
|
3378
|
+
if not ref_schema:
|
|
3379
|
+
if default_schema is None:
|
|
3380
|
+
default_schema = connection.dialect.default_schema_name
|
|
3381
|
+
if schema == default_schema:
|
|
3382
|
+
ref_schema = schema
|
|
3383
|
+
|
|
3384
|
+
loc_names = spec["local"]
|
|
3385
|
+
ref_names = spec["foreign"]
|
|
3386
|
+
|
|
3387
|
+
con_kw = {}
|
|
3388
|
+
for opt in ("onupdate", "ondelete"):
|
|
3389
|
+
if spec.get(opt, False) not in ("NO ACTION", None):
|
|
3390
|
+
con_kw[opt] = spec[opt]
|
|
3391
|
+
|
|
3392
|
+
fkey_d: ReflectedForeignKeyConstraint = {
|
|
3393
|
+
"name": spec["name"],
|
|
3394
|
+
"constrained_columns": loc_names,
|
|
3395
|
+
"referred_schema": ref_schema,
|
|
3396
|
+
"referred_table": ref_name,
|
|
3397
|
+
"referred_columns": ref_names,
|
|
3398
|
+
"options": con_kw,
|
|
3399
|
+
}
|
|
3400
|
+
fkeys.append(fkey_d)
|
|
3401
|
+
|
|
3402
|
+
if self._needs_correct_for_88718_96365:
|
|
3403
|
+
self._correct_for_mysql_bugs_88718_96365(fkeys, connection)
|
|
3404
|
+
|
|
3405
|
+
return fkeys if fkeys else ReflectionDefaults.foreign_keys()
|
|
3406
|
+
|
|
3407
|
+
def _correct_for_mysql_bugs_88718_96365(
|
|
3408
|
+
self,
|
|
3409
|
+
fkeys: List[ReflectedForeignKeyConstraint],
|
|
3410
|
+
connection: Connection,
|
|
3411
|
+
) -> None:
|
|
3412
|
+
# Foreign key is always in lower case (MySQL 8.0)
|
|
3413
|
+
# https://bugs.mysql.com/bug.php?id=88718
|
|
3414
|
+
# issue #4344 for SQLAlchemy
|
|
3415
|
+
|
|
3416
|
+
# table name also for MySQL 8.0
|
|
3417
|
+
# https://bugs.mysql.com/bug.php?id=96365
|
|
3418
|
+
# issue #4751 for SQLAlchemy
|
|
3419
|
+
|
|
3420
|
+
# for lower_case_table_names=2, information_schema.columns
|
|
3421
|
+
# preserves the original table/schema casing, but SHOW CREATE
|
|
3422
|
+
# TABLE does not. this problem is not in lower_case_table_names=1,
|
|
3423
|
+
# but use case-insensitive matching for these two modes in any case.
|
|
3424
|
+
|
|
3425
|
+
if self._casing in (1, 2):
|
|
3426
|
+
|
|
3427
|
+
def lower(s: str) -> str:
|
|
3428
|
+
return s.lower()
|
|
3429
|
+
|
|
3430
|
+
else:
|
|
3431
|
+
# if on case sensitive, there can be two tables referenced
|
|
3432
|
+
# with the same name different casing, so we need to use
|
|
3433
|
+
# case-sensitive matching.
|
|
3434
|
+
def lower(s: str) -> str:
|
|
3435
|
+
return s
|
|
3436
|
+
|
|
3437
|
+
default_schema_name: str = connection.dialect.default_schema_name # type: ignore # noqa: E501
|
|
3438
|
+
|
|
3439
|
+
# NOTE: using (table_schema, table_name, lower(column_name)) in (...)
|
|
3440
|
+
# is very slow since mysql does not seem able to properly use indexse.
|
|
3441
|
+
# Unpack the where condition instead.
|
|
3442
|
+
schema_by_table_by_column: DefaultDict[
|
|
3443
|
+
str, DefaultDict[str, List[str]]
|
|
3444
|
+
] = DefaultDict(lambda: DefaultDict(list))
|
|
3445
|
+
for rec in fkeys:
|
|
3446
|
+
sch = lower(rec["referred_schema"] or default_schema_name)
|
|
3447
|
+
tbl = lower(rec["referred_table"])
|
|
3448
|
+
for col_name in rec["referred_columns"]:
|
|
3449
|
+
schema_by_table_by_column[sch][tbl].append(col_name)
|
|
3450
|
+
|
|
3451
|
+
if schema_by_table_by_column:
|
|
3452
|
+
|
|
3453
|
+
condition = sql.or_(
|
|
3454
|
+
*(
|
|
3455
|
+
sql.and_(
|
|
3456
|
+
_info_columns.c.table_schema == schema,
|
|
3457
|
+
sql.or_(
|
|
3458
|
+
*(
|
|
3459
|
+
sql.and_(
|
|
3460
|
+
_info_columns.c.table_name == table,
|
|
3461
|
+
sql.func.lower(
|
|
3462
|
+
_info_columns.c.column_name
|
|
3463
|
+
).in_(columns),
|
|
3464
|
+
)
|
|
3465
|
+
for table, columns in tables.items()
|
|
3466
|
+
)
|
|
3467
|
+
),
|
|
3468
|
+
)
|
|
3469
|
+
for schema, tables in schema_by_table_by_column.items()
|
|
3470
|
+
)
|
|
3471
|
+
)
|
|
3472
|
+
|
|
3473
|
+
select = sql.select(
|
|
3474
|
+
_info_columns.c.table_schema,
|
|
3475
|
+
_info_columns.c.table_name,
|
|
3476
|
+
_info_columns.c.column_name,
|
|
3477
|
+
).where(condition)
|
|
3478
|
+
|
|
3479
|
+
correct_for_wrong_fk_case: CursorResult[Tuple[str, str, str]] = (
|
|
3480
|
+
connection.execute(select)
|
|
3481
|
+
)
|
|
3482
|
+
|
|
3483
|
+
# in casing=0, table name and schema name come back in their
|
|
3484
|
+
# exact case.
|
|
3485
|
+
# in casing=1, table name and schema name come back in lower
|
|
3486
|
+
# case.
|
|
3487
|
+
# in casing=2, table name and schema name come back from the
|
|
3488
|
+
# information_schema.columns view in the case
|
|
3489
|
+
# that was used in CREATE DATABASE and CREATE TABLE, but
|
|
3490
|
+
# SHOW CREATE TABLE converts them to *lower case*, therefore
|
|
3491
|
+
# not matching. So for this case, case-insensitive lookup
|
|
3492
|
+
# is necessary
|
|
3493
|
+
d: DefaultDict[Tuple[str, str], Dict[str, str]] = defaultdict(dict)
|
|
3494
|
+
for schema, tname, cname in correct_for_wrong_fk_case:
|
|
3495
|
+
d[(lower(schema), lower(tname))]["SCHEMANAME"] = schema
|
|
3496
|
+
d[(lower(schema), lower(tname))]["TABLENAME"] = tname
|
|
3497
|
+
d[(lower(schema), lower(tname))][cname.lower()] = cname
|
|
3498
|
+
|
|
3499
|
+
for fkey in fkeys:
|
|
3500
|
+
rec_b = d[
|
|
3501
|
+
(
|
|
3502
|
+
lower(fkey["referred_schema"] or default_schema_name),
|
|
3503
|
+
lower(fkey["referred_table"]),
|
|
3504
|
+
)
|
|
3505
|
+
]
|
|
3506
|
+
|
|
3507
|
+
fkey["referred_table"] = rec_b["TABLENAME"]
|
|
3508
|
+
if fkey["referred_schema"] is not None:
|
|
3509
|
+
fkey["referred_schema"] = rec_b["SCHEMANAME"]
|
|
3510
|
+
|
|
3511
|
+
fkey["referred_columns"] = [
|
|
3512
|
+
rec_b[col.lower()] for col in fkey["referred_columns"]
|
|
3513
|
+
]
|
|
3514
|
+
|
|
3515
|
+
@reflection.cache
|
|
3516
|
+
def get_check_constraints(
|
|
3517
|
+
self,
|
|
3518
|
+
connection: Connection,
|
|
3519
|
+
table_name: str,
|
|
3520
|
+
schema: Optional[str] = None,
|
|
3521
|
+
**kw: Any,
|
|
3522
|
+
) -> List[ReflectedCheckConstraint]:
|
|
3523
|
+
parsed_state = self._parsed_state_or_create(
|
|
3524
|
+
connection, table_name, schema, **kw
|
|
3525
|
+
)
|
|
3526
|
+
|
|
3527
|
+
cks: List[ReflectedCheckConstraint] = [
|
|
3528
|
+
{"name": spec["name"], "sqltext": spec["sqltext"]}
|
|
3529
|
+
for spec in parsed_state.ck_constraints
|
|
3530
|
+
]
|
|
3531
|
+
cks.sort(key=lambda d: d["name"] or "~") # sort None as last
|
|
3532
|
+
return cks if cks else ReflectionDefaults.check_constraints()
|
|
3533
|
+
|
|
3534
|
+
@reflection.cache
|
|
3535
|
+
def get_table_comment(
|
|
3536
|
+
self,
|
|
3537
|
+
connection: Connection,
|
|
3538
|
+
table_name: str,
|
|
3539
|
+
schema: Optional[str] = None,
|
|
3540
|
+
**kw: Any,
|
|
3541
|
+
) -> ReflectedTableComment:
|
|
3542
|
+
parsed_state = self._parsed_state_or_create(
|
|
3543
|
+
connection, table_name, schema, **kw
|
|
3544
|
+
)
|
|
3545
|
+
comment = parsed_state.table_options.get(f"{self.name}_comment", None)
|
|
3546
|
+
if comment is not None:
|
|
3547
|
+
return {"text": comment}
|
|
3548
|
+
else:
|
|
3549
|
+
return ReflectionDefaults.table_comment()
|
|
3550
|
+
|
|
3551
|
+
@reflection.cache
|
|
3552
|
+
def get_indexes(
|
|
3553
|
+
self,
|
|
3554
|
+
connection: Connection,
|
|
3555
|
+
table_name: str,
|
|
3556
|
+
schema: Optional[str] = None,
|
|
3557
|
+
**kw: Any,
|
|
3558
|
+
) -> List[ReflectedIndex]:
|
|
3559
|
+
parsed_state = self._parsed_state_or_create(
|
|
3560
|
+
connection, table_name, schema, **kw
|
|
3561
|
+
)
|
|
3562
|
+
|
|
3563
|
+
indexes: List[ReflectedIndex] = []
|
|
3564
|
+
|
|
3565
|
+
for spec in parsed_state.keys:
|
|
3566
|
+
dialect_options = {}
|
|
3567
|
+
unique = False
|
|
3568
|
+
flavor = spec["type"]
|
|
3569
|
+
if flavor == "PRIMARY":
|
|
3570
|
+
continue
|
|
3571
|
+
if flavor == "UNIQUE":
|
|
3572
|
+
unique = True
|
|
3573
|
+
elif flavor in ("FULLTEXT", "SPATIAL"):
|
|
3574
|
+
dialect_options["%s_prefix" % self.name] = flavor
|
|
3575
|
+
elif flavor is not None:
|
|
3576
|
+
util.warn(
|
|
3577
|
+
"Converting unknown KEY type %s to a plain KEY", flavor
|
|
3578
|
+
)
|
|
3579
|
+
|
|
3580
|
+
if spec["parser"]:
|
|
3581
|
+
dialect_options["%s_with_parser" % (self.name)] = spec[
|
|
3582
|
+
"parser"
|
|
3583
|
+
]
|
|
3584
|
+
|
|
3585
|
+
index_d: ReflectedIndex = {
|
|
3586
|
+
"name": spec["name"],
|
|
3587
|
+
"column_names": [s[0] for s in spec["columns"]],
|
|
3588
|
+
"unique": unique,
|
|
3589
|
+
}
|
|
3590
|
+
|
|
3591
|
+
mysql_length = {
|
|
3592
|
+
s[0]: s[1] for s in spec["columns"] if s[1] is not None
|
|
3593
|
+
}
|
|
3594
|
+
if mysql_length:
|
|
3595
|
+
dialect_options["%s_length" % self.name] = mysql_length
|
|
3596
|
+
|
|
3597
|
+
if flavor:
|
|
3598
|
+
index_d["type"] = flavor # type: ignore[typeddict-unknown-key]
|
|
3599
|
+
|
|
3600
|
+
if dialect_options:
|
|
3601
|
+
index_d["dialect_options"] = dialect_options
|
|
3602
|
+
|
|
3603
|
+
indexes.append(index_d)
|
|
3604
|
+
indexes.sort(key=lambda d: d["name"] or "~") # sort None as last
|
|
3605
|
+
return indexes if indexes else ReflectionDefaults.indexes()
|
|
3606
|
+
|
|
3607
|
+
@reflection.cache
|
|
3608
|
+
def get_unique_constraints(
|
|
3609
|
+
self,
|
|
3610
|
+
connection: Connection,
|
|
3611
|
+
table_name: str,
|
|
3612
|
+
schema: Optional[str] = None,
|
|
3613
|
+
**kw: Any,
|
|
3614
|
+
) -> List[ReflectedUniqueConstraint]:
|
|
3615
|
+
parsed_state = self._parsed_state_or_create(
|
|
3616
|
+
connection, table_name, schema, **kw
|
|
3617
|
+
)
|
|
3618
|
+
|
|
3619
|
+
ucs: List[ReflectedUniqueConstraint] = [
|
|
3620
|
+
{
|
|
3621
|
+
"name": key["name"],
|
|
3622
|
+
"column_names": [col[0] for col in key["columns"]],
|
|
3623
|
+
"duplicates_index": key["name"],
|
|
3624
|
+
}
|
|
3625
|
+
for key in parsed_state.keys
|
|
3626
|
+
if key["type"] == "UNIQUE"
|
|
3627
|
+
]
|
|
3628
|
+
ucs.sort(key=lambda d: d["name"] or "~") # sort None as last
|
|
3629
|
+
if ucs:
|
|
3630
|
+
return ucs
|
|
3631
|
+
else:
|
|
3632
|
+
return ReflectionDefaults.unique_constraints()
|
|
3633
|
+
|
|
3634
|
+
@reflection.cache
|
|
3635
|
+
def get_view_definition(
|
|
3636
|
+
self,
|
|
3637
|
+
connection: Connection,
|
|
3638
|
+
view_name: str,
|
|
3639
|
+
schema: Optional[str] = None,
|
|
3640
|
+
**kw: Any,
|
|
3641
|
+
) -> str:
|
|
3642
|
+
charset = self._connection_charset
|
|
3643
|
+
full_name = ".".join(
|
|
3644
|
+
self.identifier_preparer._quote_free_identifiers(schema, view_name)
|
|
3645
|
+
)
|
|
3646
|
+
sql = self._show_create_table(
|
|
3647
|
+
connection, None, charset, full_name=full_name
|
|
3648
|
+
)
|
|
3649
|
+
if sql.upper().startswith("CREATE TABLE"):
|
|
3650
|
+
# it's a table, not a view
|
|
3651
|
+
raise exc.NoSuchTableError(full_name)
|
|
3652
|
+
return sql
|
|
3653
|
+
|
|
3654
|
+
def _parsed_state_or_create(
|
|
3655
|
+
self,
|
|
3656
|
+
connection: Connection,
|
|
3657
|
+
table_name: str,
|
|
3658
|
+
schema: Optional[str] = None,
|
|
3659
|
+
**kw: Any,
|
|
3660
|
+
) -> _reflection.ReflectedState:
|
|
3661
|
+
return self._setup_parser(
|
|
3662
|
+
connection,
|
|
3663
|
+
table_name,
|
|
3664
|
+
schema,
|
|
3665
|
+
info_cache=kw.get("info_cache", None),
|
|
3666
|
+
)
|
|
3667
|
+
|
|
3668
|
+
@util.memoized_property
|
|
3669
|
+
def _tabledef_parser(self) -> _reflection.MySQLTableDefinitionParser:
|
|
3670
|
+
"""return the MySQLTableDefinitionParser, generate if needed.
|
|
3671
|
+
|
|
3672
|
+
The deferred creation ensures that the dialect has
|
|
3673
|
+
retrieved server version information first.
|
|
3674
|
+
|
|
3675
|
+
"""
|
|
3676
|
+
preparer = self.identifier_preparer
|
|
3677
|
+
return _reflection.MySQLTableDefinitionParser(self, preparer)
|
|
3678
|
+
|
|
3679
|
+
@reflection.cache
|
|
3680
|
+
def _setup_parser(
|
|
3681
|
+
self,
|
|
3682
|
+
connection: Connection,
|
|
3683
|
+
table_name: str,
|
|
3684
|
+
schema: Optional[str] = None,
|
|
3685
|
+
**kw: Any,
|
|
3686
|
+
) -> _reflection.ReflectedState:
|
|
3687
|
+
charset = self._connection_charset
|
|
3688
|
+
parser = self._tabledef_parser
|
|
3689
|
+
full_name = ".".join(
|
|
3690
|
+
self.identifier_preparer._quote_free_identifiers(
|
|
3691
|
+
schema, table_name
|
|
3692
|
+
)
|
|
3693
|
+
)
|
|
3694
|
+
sql = self._show_create_table(
|
|
3695
|
+
connection, None, charset, full_name=full_name
|
|
3696
|
+
)
|
|
3697
|
+
if parser._check_view(sql):
|
|
3698
|
+
# Adapt views to something table-like.
|
|
3699
|
+
columns = self._describe_table(
|
|
3700
|
+
connection, None, charset, full_name=full_name
|
|
3701
|
+
)
|
|
3702
|
+
sql = parser._describe_to_create(
|
|
3703
|
+
table_name, columns # type: ignore[arg-type]
|
|
3704
|
+
)
|
|
3705
|
+
return parser.parse(sql, charset)
|
|
3706
|
+
|
|
3707
|
+
def _fetch_setting(
|
|
3708
|
+
self, connection: Connection, setting_name: str
|
|
3709
|
+
) -> Optional[str]:
|
|
3710
|
+
charset = self._connection_charset
|
|
3711
|
+
|
|
3712
|
+
if self.server_version_info and self.server_version_info < (5, 6):
|
|
3713
|
+
sql = "SHOW VARIABLES LIKE '%s'" % setting_name
|
|
3714
|
+
fetch_col = 1
|
|
3715
|
+
else:
|
|
3716
|
+
sql = "SELECT @@%s" % setting_name
|
|
3717
|
+
fetch_col = 0
|
|
3718
|
+
|
|
3719
|
+
show_var = connection.exec_driver_sql(sql)
|
|
3720
|
+
row = self._compat_first(show_var, charset=charset)
|
|
3721
|
+
if not row:
|
|
3722
|
+
return None
|
|
3723
|
+
else:
|
|
3724
|
+
return cast(Optional[str], row[fetch_col])
|
|
3725
|
+
|
|
3726
|
+
def _detect_charset(self, connection: Connection) -> str:
|
|
3727
|
+
raise NotImplementedError()
|
|
3728
|
+
|
|
3729
|
+
def _detect_casing(self, connection: Connection) -> int:
|
|
3730
|
+
"""Sniff out identifier case sensitivity.
|
|
3731
|
+
|
|
3732
|
+
Cached per-connection. This value can not change without a server
|
|
3733
|
+
restart.
|
|
3734
|
+
|
|
3735
|
+
"""
|
|
3736
|
+
# https://dev.mysql.com/doc/refman/en/identifier-case-sensitivity.html
|
|
3737
|
+
|
|
3738
|
+
setting = self._fetch_setting(connection, "lower_case_table_names")
|
|
3739
|
+
if setting is None:
|
|
3740
|
+
cs = 0
|
|
3741
|
+
else:
|
|
3742
|
+
# 4.0.15 returns OFF or ON according to [ticket:489]
|
|
3743
|
+
# 3.23 doesn't, 4.0.27 doesn't..
|
|
3744
|
+
if setting == "OFF":
|
|
3745
|
+
cs = 0
|
|
3746
|
+
elif setting == "ON":
|
|
3747
|
+
cs = 1
|
|
3748
|
+
else:
|
|
3749
|
+
cs = int(setting)
|
|
3750
|
+
self._casing = cs
|
|
3751
|
+
return cs
|
|
3752
|
+
|
|
3753
|
+
def _detect_collations(self, connection: Connection) -> Dict[str, str]:
|
|
3754
|
+
"""Pull the active COLLATIONS list from the server.
|
|
3755
|
+
|
|
3756
|
+
Cached per-connection.
|
|
3757
|
+
"""
|
|
3758
|
+
|
|
3759
|
+
collations = {}
|
|
3760
|
+
charset = self._connection_charset
|
|
3761
|
+
rs = connection.exec_driver_sql("SHOW COLLATION")
|
|
3762
|
+
for row in self._compat_fetchall(rs, charset):
|
|
3763
|
+
collations[row[0]] = row[1]
|
|
3764
|
+
return collations
|
|
3765
|
+
|
|
3766
|
+
def _detect_sql_mode(self, connection: Connection) -> None:
|
|
3767
|
+
setting = self._fetch_setting(connection, "sql_mode")
|
|
3768
|
+
|
|
3769
|
+
if setting is None:
|
|
3770
|
+
util.warn(
|
|
3771
|
+
"Could not retrieve SQL_MODE; please ensure the "
|
|
3772
|
+
"MySQL user has permissions to SHOW VARIABLES"
|
|
3773
|
+
)
|
|
3774
|
+
self._sql_mode = ""
|
|
3775
|
+
else:
|
|
3776
|
+
self._sql_mode = setting or ""
|
|
3777
|
+
|
|
3778
|
+
def _detect_ansiquotes(self, connection: Connection) -> None:
|
|
3779
|
+
"""Detect and adjust for the ANSI_QUOTES sql mode."""
|
|
3780
|
+
|
|
3781
|
+
mode = self._sql_mode
|
|
3782
|
+
if not mode:
|
|
3783
|
+
mode = ""
|
|
3784
|
+
elif mode.isdigit():
|
|
3785
|
+
mode_no = int(mode)
|
|
3786
|
+
mode = (mode_no | 4 == mode_no) and "ANSI_QUOTES" or ""
|
|
3787
|
+
|
|
3788
|
+
self._server_ansiquotes = "ANSI_QUOTES" in mode
|
|
3789
|
+
|
|
3790
|
+
# as of MySQL 5.0.1
|
|
3791
|
+
self._backslash_escapes = "NO_BACKSLASH_ESCAPES" not in mode
|
|
3792
|
+
|
|
3793
|
+
@overload
|
|
3794
|
+
def _show_create_table(
|
|
3795
|
+
self,
|
|
3796
|
+
connection: Connection,
|
|
3797
|
+
table: Optional[Table],
|
|
3798
|
+
charset: Optional[str],
|
|
3799
|
+
full_name: str,
|
|
3800
|
+
) -> str: ...
|
|
3801
|
+
|
|
3802
|
+
@overload
|
|
3803
|
+
def _show_create_table(
|
|
3804
|
+
self,
|
|
3805
|
+
connection: Connection,
|
|
3806
|
+
table: Table,
|
|
3807
|
+
charset: Optional[str] = None,
|
|
3808
|
+
full_name: None = None,
|
|
3809
|
+
) -> str: ...
|
|
3810
|
+
|
|
3811
|
+
def _show_create_table(
|
|
3812
|
+
self,
|
|
3813
|
+
connection: Connection,
|
|
3814
|
+
table: Optional[Table],
|
|
3815
|
+
charset: Optional[str] = None,
|
|
3816
|
+
full_name: Optional[str] = None,
|
|
3817
|
+
) -> str:
|
|
3818
|
+
"""Run SHOW CREATE TABLE for a ``Table``."""
|
|
3819
|
+
|
|
3820
|
+
if full_name is None:
|
|
3821
|
+
assert table is not None
|
|
3822
|
+
full_name = self.identifier_preparer.format_table(table)
|
|
3823
|
+
st = "SHOW CREATE TABLE %s" % full_name
|
|
3824
|
+
|
|
3825
|
+
try:
|
|
3826
|
+
rp = connection.execution_options(
|
|
3827
|
+
skip_user_error_events=True
|
|
3828
|
+
).exec_driver_sql(st)
|
|
3829
|
+
except exc.DBAPIError as e:
|
|
3830
|
+
if self._extract_error_code(e.orig) == 1146: # type: ignore[arg-type] # noqa: E501
|
|
3831
|
+
raise exc.NoSuchTableError(full_name) from e
|
|
3832
|
+
else:
|
|
3833
|
+
raise
|
|
3834
|
+
row = self._compat_first(rp, charset=charset)
|
|
3835
|
+
if not row:
|
|
3836
|
+
raise exc.NoSuchTableError(full_name)
|
|
3837
|
+
return cast(str, row[1]).strip()
|
|
3838
|
+
|
|
3839
|
+
@overload
|
|
3840
|
+
def _describe_table(
|
|
3841
|
+
self,
|
|
3842
|
+
connection: Connection,
|
|
3843
|
+
table: Optional[Table],
|
|
3844
|
+
charset: Optional[str],
|
|
3845
|
+
full_name: str,
|
|
3846
|
+
) -> Union[Sequence[Row[Any]], Sequence[_DecodingRow]]: ...
|
|
3847
|
+
|
|
3848
|
+
@overload
|
|
3849
|
+
def _describe_table(
|
|
3850
|
+
self,
|
|
3851
|
+
connection: Connection,
|
|
3852
|
+
table: Table,
|
|
3853
|
+
charset: Optional[str] = None,
|
|
3854
|
+
full_name: None = None,
|
|
3855
|
+
) -> Union[Sequence[Row[Any]], Sequence[_DecodingRow]]: ...
|
|
3856
|
+
|
|
3857
|
+
def _describe_table(
|
|
3858
|
+
self,
|
|
3859
|
+
connection: Connection,
|
|
3860
|
+
table: Optional[Table],
|
|
3861
|
+
charset: Optional[str] = None,
|
|
3862
|
+
full_name: Optional[str] = None,
|
|
3863
|
+
) -> Union[Sequence[Row[Any]], Sequence[_DecodingRow]]:
|
|
3864
|
+
"""Run DESCRIBE for a ``Table`` and return processed rows."""
|
|
3865
|
+
|
|
3866
|
+
if full_name is None:
|
|
3867
|
+
assert table is not None
|
|
3868
|
+
full_name = self.identifier_preparer.format_table(table)
|
|
3869
|
+
st = "DESCRIBE %s" % full_name
|
|
3870
|
+
|
|
3871
|
+
rp, rows = None, None
|
|
3872
|
+
try:
|
|
3873
|
+
try:
|
|
3874
|
+
rp = connection.execution_options(
|
|
3875
|
+
skip_user_error_events=True
|
|
3876
|
+
).exec_driver_sql(st)
|
|
3877
|
+
except exc.DBAPIError as e:
|
|
3878
|
+
code = self._extract_error_code(e.orig) # type: ignore[arg-type] # noqa: E501
|
|
3879
|
+
if code == 1146:
|
|
3880
|
+
raise exc.NoSuchTableError(full_name) from e
|
|
3881
|
+
|
|
3882
|
+
elif code == 1356:
|
|
3883
|
+
raise exc.UnreflectableTableError(
|
|
3884
|
+
"Table or view named %s could not be "
|
|
3885
|
+
"reflected: %s" % (full_name, e)
|
|
3886
|
+
) from e
|
|
3887
|
+
|
|
3888
|
+
else:
|
|
3889
|
+
raise
|
|
3890
|
+
rows = self._compat_fetchall(rp, charset=charset)
|
|
3891
|
+
finally:
|
|
3892
|
+
if rp:
|
|
3893
|
+
rp.close()
|
|
3894
|
+
return rows
|
|
3895
|
+
|
|
3896
|
+
|
|
3897
|
+
class _DecodingRow:
|
|
3898
|
+
"""Return unicode-decoded values based on type inspection.
|
|
3899
|
+
|
|
3900
|
+
Smooth over data type issues (esp. with alpha driver versions) and
|
|
3901
|
+
normalize strings as Unicode regardless of user-configured driver
|
|
3902
|
+
encoding settings.
|
|
3903
|
+
|
|
3904
|
+
"""
|
|
3905
|
+
|
|
3906
|
+
# Some MySQL-python versions can return some columns as
|
|
3907
|
+
# sets.Set(['value']) (seriously) but thankfully that doesn't
|
|
3908
|
+
# seem to come up in DDL queries.
|
|
3909
|
+
|
|
3910
|
+
_encoding_compat: Dict[str, str] = {
|
|
3911
|
+
"koi8r": "koi8_r",
|
|
3912
|
+
"koi8u": "koi8_u",
|
|
3913
|
+
"utf16": "utf-16-be", # MySQL's uft16 is always bigendian
|
|
3914
|
+
"utf8mb4": "utf8", # real utf8
|
|
3915
|
+
"utf8mb3": "utf8", # real utf8; saw this happen on CI but I cannot
|
|
3916
|
+
# reproduce, possibly mariadb10.6 related
|
|
3917
|
+
"eucjpms": "ujis",
|
|
3918
|
+
}
|
|
3919
|
+
|
|
3920
|
+
def __init__(self, rowproxy: Row[Any], charset: Optional[str]):
|
|
3921
|
+
self.rowproxy = rowproxy
|
|
3922
|
+
self.charset = (
|
|
3923
|
+
self._encoding_compat.get(charset, charset)
|
|
3924
|
+
if charset is not None
|
|
3925
|
+
else None
|
|
3926
|
+
)
|
|
3927
|
+
|
|
3928
|
+
def __getitem__(self, index: int) -> Any:
|
|
3929
|
+
item = self.rowproxy[index]
|
|
3930
|
+
if self.charset and isinstance(item, bytes):
|
|
3931
|
+
return item.decode(self.charset)
|
|
3932
|
+
else:
|
|
3933
|
+
return item
|
|
3934
|
+
|
|
3935
|
+
def __getattr__(self, attr: str) -> Any:
|
|
3936
|
+
item = getattr(self.rowproxy, attr)
|
|
3937
|
+
if self.charset and isinstance(item, bytes):
|
|
3938
|
+
return item.decode(self.charset)
|
|
3939
|
+
else:
|
|
3940
|
+
return item
|
|
3941
|
+
|
|
3942
|
+
|
|
3943
|
+
_info_columns = sql.table(
|
|
3944
|
+
"columns",
|
|
3945
|
+
sql.column("table_schema", VARCHAR(64)),
|
|
3946
|
+
sql.column("table_name", VARCHAR(64)),
|
|
3947
|
+
sql.column("column_name", VARCHAR(64)),
|
|
3948
|
+
schema="information_schema",
|
|
3949
|
+
)
|