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 requests

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


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,
api_data, now)

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


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

def _create_dto_from_row (
self,
@@ -428,7 +432,7 @@ class VideoTagDao:
video_id: int,
with_relation_tables: bool,
) -> list[VideoTagDto]:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
SELECT
id,
@@ -441,7 +445,7 @@ class VideoTagDao:
WHERE
video_id = %s
ORDER BY
id""", video_id)
id""", (video_id,))
video_tags: list[VideoTagDto] = []
for row in c.fetchall ():
video_tags.append (self._create_dto_from_row (row, with_relation_tables))
@@ -452,7 +456,7 @@ class VideoTagDao:
video_id: int,
with_relation_tables: bool,
) -> list[VideoTagDto]:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
SELECT
id,
@@ -466,7 +470,7 @@ class VideoTagDao:
video_id = %s
AND (untagged_at IS NULL)
ORDER BY
id""", video_id)
id""", (video_id,))
video_tags: list[VideoTagDto] = []
for row in c.fetchall ():
video_tags.append (self._create_dto_from_row (row, with_relation_tables))
@@ -478,7 +482,7 @@ class VideoTagDao:
tag_id: int,
with_relation_tables: bool,
) -> VideoTagDto | None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
SELECT
id,
@@ -501,7 +505,7 @@ class VideoTagDao:
video_tag: VideoTagDto,
with_relation_tables: bool,
) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
INSERT INTO
video_tags(
@@ -528,7 +532,7 @@ class VideoTagDao:
video_tag: VideoTagDto,
with_relation_tables: bool,
) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
INSERT INTO
video_tags(
@@ -571,15 +575,17 @@ class VideoTagDao:
tag_ids: list[int],
now: datetime
) -> None:
with self.conn.cursor () as c:
if not tag_ids:
return
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
UPDATE
video_tags
SET
untagged_at = %s
untagged_at = %%s
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 (
self,
@@ -619,15 +625,15 @@ class TagDao:
self,
tag_id: int,
) -> TagDto | None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
SELECT
id,
name)
name
FROM
tags
WHERE
id = %s""", tag_id)
id = %s""", (tag_id,))
row = c.fetchone ()
if row is None:
return None
@@ -637,7 +643,7 @@ class TagDao:
self,
tag_name: str,
) -> TagDto | None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
SELECT
id,
@@ -645,7 +651,7 @@ class TagDao:
FROM
tags
WHERE
name = %s""", tag_name)
name = %s""", (tag_name,))
row = c.fetchone ()
if row is None:
return None
@@ -655,26 +661,26 @@ class TagDao:
self,
tag: TagDto,
) -> None:
with self.conn.cursor () as c:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
INSERT INTO
tags(name)
VALUES
(%s)""", tag.name)
(%s)""", (tag.name,))
tag.id_ = c.lastrowid

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

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

def _create_dto_from_row (


Loading…
Cancel
Save