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.

Once implemented, you can do

q = sqla_table.insert_on_duplicate().values(destvalues)
session.execute(q)

Note: superseded by SQLAlchemy v1.2:

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 an INSERT... ON DUPLICATE KEY UPDATE statement.

Notes:

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:
Returns:

an InsertOnDuplicate object

cardinal_pythonlib.sqlalchemy.insert_on_duplicate.monkeypatch_TableClause() None[source]

Modifies sqlalchemy.sql.expression.TableClause to insert a insert_on_duplicate member that is our insert_on_duplicate() function as above.

cardinal_pythonlib.sqlalchemy.insert_on_duplicate.unmonkeypatch_TableClause() None[source]

Reverses the action of monkeypatch_TableClause().