From d8b83c86325ebf45ad65cb9fe7b48f48a9d8d725 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Wed, 25 Sep 2024 01:48:58 +0900 Subject: [PATCH] =?UTF-8?q?=E6=9C=AC=E6=97=A5=E4=BD=9C=E6=A5=AD=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- update_db.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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,