diff options
author | jonathan vanasco <jonathan@2xlp.com> | 2021-09-23 13:06:40 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-09-28 13:58:59 -0400 |
commit | 3a152a4ffaf3848e245ad7d88966f95ba8e09db5 (patch) | |
tree | fbcfb35a0516d8bb4ef9d3d2882de1c66dac43aa /lib/sqlalchemy/dialects/mysql/base.py | |
parent | 7023c07aa6d31b3527cb1171410dcb6e8dff3ec3 (diff) | |
download | sqlalchemy-3a152a4ffaf3848e245ad7d88966f95ba8e09db5.tar.gz |
Fixes: #2937
* docs for event listen kwargs
* docs for mysql to use `listen` for changing the sql_mode`
Change-Id: I7c1678488658edda3c5baaf0f7648108e93a4be1
Diffstat (limited to 'lib/sqlalchemy/dialects/mysql/base.py')
-rw-r--r-- | lib/sqlalchemy/dialects/mysql/base.py | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 2bba2f81a..0c24ebf5e 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -400,13 +400,47 @@ ANSI Quoting Style MySQL / MariaDB feature two varieties of identifier "quoting style", one using backticks and the other using quotes, e.g. ```some_identifier``` vs. ``"some_identifier"``. All MySQL dialects detect which version -is in use by checking the value of ``sql_mode`` when a connection is first +is in use by checking the value of :ref:`sql_mode<mysql_sql_mode>` when a connection is first established with a particular :class:`_engine.Engine`. This quoting style comes into play when rendering table and column names as well as when reflecting existing database structures. The detection is entirely automatic and no special configuration is needed to use either quoting style. + +.. _mysql_sql_mode: + +Changing the sql_mode +--------------------- + +MySQL supports operating in multiple +`Server SQL Modes <https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html>`_ for +both Servers and Clients. To change the ``sql_mode`` for a given application, a +developer can leverage SQLAlchemy's Events system. + +In the following example, the event system is used to set the ``sql_mode`` on +the ``first_connect`` and ``connect`` events:: + + from sqlalchemy import create_engine, event + + eng = create_engine("mysql://scott:tiger@localhost/test", echo='debug') + + # `insert=True` will ensure this is the very first listener to run + @event.listens_for(eng, "connect", insert=True) + def connect(dbapi_connection, connection_record): + cursor = dbapi_connection.cursor() + cursor.execute("SET sql_mode = 'STRICT_ALL_TABLES'") + + conn = eng.connect() + +In the example illustrated above, the "connect" event will invoke the "SET" +statement on the connection at the moment a particular DBAPI connection is +first created for a given Pool, before the connection is made available to the +connection pool. Additionally, because the function was registered with +``insert=True``, it will be prepended to the internal list of registered +functions. + + MySQL / MariaDB SQL Extensions ------------------------------ |