cardinal_pythonlib.sqlalchemy.insert_on_duplicate
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
This file is part of cardinal_pythonlib.
Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Add “INSERT ON DUPLICATE KEY UPDATE” functionality to SQLAlchemy for MySQL.
https://www.reddit.com/r/Python/comments/p5grh/sqlalchemy_whats_the_idiomatic_way_of_writing/
https://github.com/bedwards/sqlalchemy_mysql_ext/blob/master/duplicate.py … modified
https://stackoverflow.com/questions/6611563/sqlalchemy-on-duplicate-key-update
https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
Once implemented, you can do
q = sqla_table.insert_on_duplicate().values(destvalues)
session.execute(q)
Note: superseded by SQLAlchemy v1.2:
https://docs.sqlalchemy.org/en/latest/changelog/migration_12.html
https://docs.sqlalchemy.org/en/latest/dialects/mysql.html#mysql-insert-on-duplicate-key-update
- class cardinal_pythonlib.sqlalchemy.insert_on_duplicate.InsertOnDuplicate(table, values=None, inline=False, bind=None, prefixes=None, returning=None, return_defaults=False, **dialect_kw)[source]
Class that derives from
Insert
, so we can hook in to operations involving it.Construct a new
dialects.postgresql.Insert
object.This constructor is mirrored as a public API function; see
sqlalchemy.dialects.postgresql.insert()
for a full usage and argument description.
- cardinal_pythonlib.sqlalchemy.insert_on_duplicate.compile_insert_on_duplicate_key_update(insert: Insert, compiler: SQLCompiler, **kw) str [source]
Hooks into the use of the
InsertOnDuplicate
class for the MySQL dialect. Compiles the relevant SQL for anINSERT... ON DUPLICATE KEY UPDATE
statement.Notes:
We can’t get the fieldnames directly from
insert
orcompiler
.We could rewrite the innards of the visit_insert statement (https://github.com/bedwards/sqlalchemy_mysql_ext/blob/master/duplicate.py)… but, like that, it will get outdated.
We could use a hack-in-by-hand method (https://stackoverflow.com/questions/6611563/sqlalchemy-on-duplicate-key-update) … but a little automation would be nice.
So, regex to the rescue.
NOTE THAT COLUMNS ARE ALREADY QUOTED by this stage; no need to repeat.
- cardinal_pythonlib.sqlalchemy.insert_on_duplicate.insert_on_duplicate(tablename: str, values: Any | None = None, inline: bool = False, **kwargs)[source]
Command to produce an
InsertOnDuplicate
object.- Parameters:
tablename¶ – name of the table
values¶ – values to
INSERT
inline¶ – as per https://docs.sqlalchemy.org/en/latest/core/dml.html#sqlalchemy.sql.expression.insert
kwargs¶ – additional parameters
- Returns:
an
InsertOnDuplicate
object
- cardinal_pythonlib.sqlalchemy.insert_on_duplicate.monkeypatch_TableClause() None [source]
Modifies
sqlalchemy.sql.expression.TableClause
to insert ainsert_on_duplicate
member that is ourinsert_on_duplicate()
function as above.
- cardinal_pythonlib.sqlalchemy.insert_on_duplicate.unmonkeypatch_TableClause() None [source]
Reverses the action of
monkeypatch_TableClause()
.