diff --git a/update_db.py b/update_db.py index 168be1e..b83127d 100644 --- a/update_db.py +++ b/update_db.py @@ -172,6 +172,42 @@ class VideoDao: videos.append (self._create_dto_from_row (row, with_relation_tables)) return videos + def upsert_all ( + self, + videos: list[VideoDto], + with_relation_tables: bool = True, + ) -> int: + with self.conn.cursor () as c: + for video in videos: + c.execute (""" + INSERT INTO + videos( + code, + title, + description, + uploaded_at, + deleted_at) + VALUES + ( + %s, + %s, + %s, + %s, + %s) + ON DUPLICATE KEY UPDATE + code = VALUES(code), + title = VALUES(title), + description = VALUES(description), + uploaded_at = VALUES(uploaded_at), + deleted_at = VALUES(deleted_at)""", ( + video.code, + video.title, + video.description, + video.uploaded_at, + video.deleted_at)) + video.id_ = c.lastrowid + # TODO: with_relation_tables が True の場合,子テーブルも UPSERT すること + def _create_dto_from_row ( self, row,