Browse Source

バグ修正

feature/query
みてるぞ 1 month ago
parent
commit
890f584010
1 changed files with 48 additions and 42 deletions
  1. +48
    -42
      update_db.py

+ 48
- 42
update_db.py View File

@@ -16,8 +16,9 @@ from typing import Any, TypedDict, cast
import mysql.connector import mysql.connector
import requests import requests


DbNull = (None,)
DbNullType = tuple[None]
# TODO: “何もしなぃ” を意味する None と区別可能にするため,NULL クラスを別途用意すること
DbNull = None
DbNullType = None




class VideoSearchParam (TypedDict): class VideoSearchParam (TypedDict):
@@ -74,7 +75,8 @@ def main (
update_tables (video_dao, tag_dao, video_tag_dao, video_history_dao, comment_dao, user_dao, update_tables (video_dao, tag_dao, video_tag_dao, video_history_dao, comment_dao, user_dao,
api_data, now) api_data, now)


# TODO: 書くこと
conn.commit ()
conn.close ()




def update_tables ( def update_tables (
@@ -254,7 +256,7 @@ class VideoDao:
video_id: int, video_id: int,
with_relation_tables: bool, with_relation_tables: bool,
) -> VideoDto | None: ) -> VideoDto | None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
SELECT SELECT
id, id,
@@ -268,7 +270,7 @@ class VideoDao:
WHERE WHERE
id = %s id = %s
ORDER BY ORDER BY
id""", video_id)
id""", (video_id,))
row = c.fetchone () row = c.fetchone ()
if row is None: if row is None:
return None return None
@@ -278,7 +280,7 @@ class VideoDao:
self, self,
with_relation_tables: bool, with_relation_tables: bool,
) -> list[VideoDto]: ) -> list[VideoDto]:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
SELECT SELECT
id, id,
@@ -299,7 +301,7 @@ class VideoDao:
def fetch_alive ( def fetch_alive (
self, self,
) -> list[VideoDto]: ) -> list[VideoDto]:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
SELECT SELECT
id, id,
@@ -322,7 +324,7 @@ class VideoDao:
video: VideoDto, video: VideoDto,
with_relation_tables: bool, with_relation_tables: bool,
) -> None: ) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
INSERT INTO INSERT INTO
videos( videos(
@@ -370,14 +372,16 @@ class VideoDao:
video_ids: list[int], video_ids: list[int],
at: datetime, at: datetime,
) -> None: ) -> None:
with self.conn.cursor () as c:
if not video_ids:
return
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
UPDATE UPDATE
videos videos
SET SET
deleted_at = %s
deleted_at = %%s
WHERE WHERE
id IN (%s)""", (at, (*video_ids,)))
id IN (%s)""" % ', '.join (['%s'] * len (video_ids)), (at, *video_ids))


def _create_dto_from_row ( def _create_dto_from_row (
self, self,
@@ -428,7 +432,7 @@ class VideoTagDao:
video_id: int, video_id: int,
with_relation_tables: bool, with_relation_tables: bool,
) -> list[VideoTagDto]: ) -> list[VideoTagDto]:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
SELECT SELECT
id, id,
@@ -441,7 +445,7 @@ class VideoTagDao:
WHERE WHERE
video_id = %s video_id = %s
ORDER BY ORDER BY
id""", video_id)
id""", (video_id,))
video_tags: list[VideoTagDto] = [] video_tags: list[VideoTagDto] = []
for row in c.fetchall (): for row in c.fetchall ():
video_tags.append (self._create_dto_from_row (row, with_relation_tables)) video_tags.append (self._create_dto_from_row (row, with_relation_tables))
@@ -452,7 +456,7 @@ class VideoTagDao:
video_id: int, video_id: int,
with_relation_tables: bool, with_relation_tables: bool,
) -> list[VideoTagDto]: ) -> list[VideoTagDto]:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
SELECT SELECT
id, id,
@@ -466,7 +470,7 @@ class VideoTagDao:
video_id = %s video_id = %s
AND (untagged_at IS NULL) AND (untagged_at IS NULL)
ORDER BY ORDER BY
id""", video_id)
id""", (video_id,))
video_tags: list[VideoTagDto] = [] video_tags: list[VideoTagDto] = []
for row in c.fetchall (): for row in c.fetchall ():
video_tags.append (self._create_dto_from_row (row, with_relation_tables)) video_tags.append (self._create_dto_from_row (row, with_relation_tables))
@@ -478,7 +482,7 @@ class VideoTagDao:
tag_id: int, tag_id: int,
with_relation_tables: bool, with_relation_tables: bool,
) -> VideoTagDto | None: ) -> VideoTagDto | None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
SELECT SELECT
id, id,
@@ -501,7 +505,7 @@ class VideoTagDao:
video_tag: VideoTagDto, video_tag: VideoTagDto,
with_relation_tables: bool, with_relation_tables: bool,
) -> None: ) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
INSERT INTO INSERT INTO
video_tags( video_tags(
@@ -528,7 +532,7 @@ class VideoTagDao:
video_tag: VideoTagDto, video_tag: VideoTagDto,
with_relation_tables: bool, with_relation_tables: bool,
) -> None: ) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
INSERT INTO INSERT INTO
video_tags( video_tags(
@@ -571,15 +575,17 @@ class VideoTagDao:
tag_ids: list[int], tag_ids: list[int],
now: datetime now: datetime
) -> None: ) -> None:
with self.conn.cursor () as c:
if not tag_ids:
return
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
UPDATE UPDATE
video_tags video_tags
SET SET
untagged_at = %s
untagged_at = %%s
WHERE WHERE
video_id = %s
AND tag_ids IN (%s)""", (now, video_id, (*tag_ids,)))
video_id = %%s
AND tag_ids IN (%s)""" % ', '.join (['%s'] * len (tag_ids)), (now, video_id, *tag_ids))


def _create_dto_from_row ( def _create_dto_from_row (
self, self,
@@ -619,15 +625,15 @@ class TagDao:
self, self,
tag_id: int, tag_id: int,
) -> TagDto | None: ) -> TagDto | None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
SELECT SELECT
id, id,
name)
name
FROM FROM
tags tags
WHERE WHERE
id = %s""", tag_id)
id = %s""", (tag_id,))
row = c.fetchone () row = c.fetchone ()
if row is None: if row is None:
return None return None
@@ -637,7 +643,7 @@ class TagDao:
self, self,
tag_name: str, tag_name: str,
) -> TagDto | None: ) -> TagDto | None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
SELECT SELECT
id, id,
@@ -645,7 +651,7 @@ class TagDao:
FROM FROM
tags tags
WHERE WHERE
name = %s""", tag_name)
name = %s""", (tag_name,))
row = c.fetchone () row = c.fetchone ()
if row is None: if row is None:
return None return None
@@ -655,26 +661,26 @@ class TagDao:
self, self,
tag: TagDto, tag: TagDto,
) -> None: ) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
INSERT INTO INSERT INTO
tags(name) tags(name)
VALUES VALUES
(%s)""", tag.name)
(%s)""", (tag.name,))
tag.id_ = c.lastrowid tag.id_ = c.lastrowid


def upsert ( def upsert (
self, self,
tag: TagDto, tag: TagDto,
) -> None: ) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
INSERT INTO INSERT INTO
tags(name) tags(name)
VALUES VALUES
(%s) (%s)
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE
name = VALUES(name)""", tag.name)
name = VALUES(name)""", (tag.name,))
tag.id_ = c.lastrowid tag.id_ = c.lastrowid


def _create_dto_from_row ( def _create_dto_from_row (
@@ -703,7 +709,7 @@ class VideoHistoryDao:
video_id: int, video_id: int,
with_relation_tables: bool, with_relation_tables: bool,
) -> list[VideoHistoryDto]: ) -> list[VideoHistoryDto]:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
SELECT SELECT
id, id,
@@ -713,7 +719,7 @@ class VideoHistoryDao:
FROM FROM
video_histories video_histories
WHERE WHERE
video_id = %s""", video_id)
video_id = %s""", (video_id,))
video_histories: list[VideoHistoryDto] = [] video_histories: list[VideoHistoryDto] = []
for row in c.fetchall (): for row in c.fetchall ():
video_histories.append (self._create_dto_from_row (row, with_relation_tables)) video_histories.append (self._create_dto_from_row (row, with_relation_tables))
@@ -723,7 +729,7 @@ class VideoHistoryDao:
self, self,
video_history: VideoHistoryDto, video_history: VideoHistoryDto,
) -> None: ) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
INSERT INTO INSERT INTO
video_histories( video_histories(
@@ -742,7 +748,7 @@ class VideoHistoryDao:
self, self,
video_history: VideoHistoryDto, video_history: VideoHistoryDto,
) -> None: ) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
INSERT INTO INSERT INTO
video_histories( video_histories(
@@ -803,7 +809,7 @@ class CommentDao:
video_id: int, video_id: int,
with_relation_tables: bool, with_relation_tables: bool,
) -> list[CommentDto]: ) -> list[CommentDto]:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
SELECT SELECT
id, id,
@@ -817,7 +823,7 @@ class CommentDao:
FROM FROM
comments comments
WHERE WHERE
video_id = %s""", video_id)
video_id = %s""", (video_id,))
comments: list[CommentDto] = [] comments: list[CommentDto] = []
for row in c.fetchall (): for row in c.fetchall ():
comments.append (self._create_dto_from_row (row, with_relation_tables)) comments.append (self._create_dto_from_row (row, with_relation_tables))
@@ -828,7 +834,7 @@ class CommentDao:
comment: CommentDto, comment: CommentDto,
with_relation_tables: bool, with_relation_tables: bool,
) -> None: ) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
INSERT INTO INSERT INTO
comments( comments(
@@ -914,7 +920,7 @@ class UserDao:
self, self,
user_code: str user_code: str
) -> UserDto | None: ) -> UserDto | None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
SELECT SELECT
id, id,
@@ -922,7 +928,7 @@ class UserDao:
FROM FROM
users users
WHERE WHERE
code = %s""", user_code)
code = %s""", (user_code,))
row = c.fetchone () row = c.fetchone ()
if row is None: if row is None:
return None return None
@@ -932,12 +938,12 @@ class UserDao:
self, self,
user: UserDto, user: UserDto,
) -> None: ) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute (""" c.execute ("""
INSERT INTO INSERT INTO
users(code) users(code)
VALUES VALUES
(%s)""", user.code)
(%s)""", (user.code,))
user.id_ = c.lastrowid user.id_ = c.lastrowid


def _create_dto_from_row ( def _create_dto_from_row (


Loading…
Cancel
Save