Browse Source

SQL ログ追加

feature/query
みてるぞ 1 month ago
parent
commit
28471cb318
1 changed files with 46 additions and 44 deletions
  1. +46
    -44
      update_db.py

+ 46
- 44
update_db.py View File

@@ -128,6 +128,8 @@ def main (
api_data, now) api_data, now)


conn.commit () conn.commit ()
print ('Committed.')

conn.close () conn.close ()




@@ -310,7 +312,7 @@ class VideoDao:
with_relation_tables: bool, with_relation_tables: bool,
) -> VideoDto | None: ) -> VideoDto | None:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT SELECT
id, id,
code, code,
@@ -323,7 +325,7 @@ class VideoDao:
WHERE WHERE
id = %s id = %s
ORDER BY ORDER BY
id""", (video_id,))
id""", (video_id,)))
row = cast (VideoRow | None, c.fetchone ()) row = cast (VideoRow | None, c.fetchone ())
if row is None: if row is None:
return None return None
@@ -334,7 +336,7 @@ class VideoDao:
with_relation_tables: bool, with_relation_tables: bool,
) -> list[VideoDto]: ) -> list[VideoDto]:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT SELECT
id, id,
code, code,
@@ -345,7 +347,7 @@ class VideoDao:
FROM FROM
videos videos
ORDER BY ORDER BY
id""")
id"""))
videos: list[VideoDto] = [] videos: list[VideoDto] = []
for row in cast (list[VideoRow], c.fetchall ()): for row in cast (list[VideoRow], c.fetchall ()):
videos.append (self._create_dto_from_row (row, with_relation_tables)) videos.append (self._create_dto_from_row (row, with_relation_tables))
@@ -355,7 +357,7 @@ class VideoDao:
self, self,
) -> list[VideoDto]: ) -> list[VideoDto]:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT SELECT
id, id,
code, code,
@@ -366,7 +368,7 @@ class VideoDao:
FROM FROM
videos videos
WHERE WHERE
deleted_at IS NULL""")
deleted_at IS NULL"""))
videos: list[VideoDto] = [] videos: list[VideoDto] = []
for row in cast (list[VideoRow], c.fetchall ()): for row in cast (list[VideoRow], c.fetchall ()):
videos.append (self._create_dto_from_row (row, False)) videos.append (self._create_dto_from_row (row, False))
@@ -385,7 +387,7 @@ class VideoDao:
deleted_at = cast (datetime | None, deleted_at) deleted_at = cast (datetime | None, deleted_at)


with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
INSERT INTO INSERT INTO
videos( videos(
code, code,
@@ -409,7 +411,7 @@ class VideoDao:
video.title, video.title,
video.description, video.description,
video.uploaded_at, video.uploaded_at,
deleted_at))
deleted_at)))
video.id_ = c.lastrowid video.id_ = c.lastrowid
if with_relation_tables: if with_relation_tables:
if video.video_tags is not None: if video.video_tags is not None:
@@ -435,13 +437,13 @@ class VideoDao:
if not video_ids: if not video_ids:
return return
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
UPDATE UPDATE
videos videos
SET SET
deleted_at = %%s deleted_at = %%s
WHERE WHERE
id IN (%s)""" % ', '.join (['%s'] * len (video_ids)), (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,
@@ -493,7 +495,7 @@ class VideoTagDao:
with_relation_tables: bool, with_relation_tables: bool,
) -> list[VideoTagDto]: ) -> list[VideoTagDto]:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT SELECT
id, id,
video_id, video_id,
@@ -505,7 +507,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 cast (list[VideoTagRow], c.fetchall ()): for row in cast (list[VideoTagRow], 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))
@@ -517,7 +519,7 @@ class VideoTagDao:
with_relation_tables: bool, with_relation_tables: bool,
) -> list[VideoTagDto]: ) -> list[VideoTagDto]:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT SELECT
id, id,
video_id, video_id,
@@ -530,7 +532,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 cast (list[VideoTagRow], c.fetchall ()): for row in cast (list[VideoTagRow], 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))
@@ -543,7 +545,7 @@ class VideoTagDao:
with_relation_tables: bool, with_relation_tables: bool,
) -> VideoTagDto | None: ) -> VideoTagDto | None:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT SELECT
id, id,
video_id, video_id,
@@ -554,7 +556,7 @@ class VideoTagDao:
video_tags video_tags
WHERE WHERE
video_id = %s video_id = %s
AND tag_id = %s""", (video_id, tag_id))
AND tag_id = %s""", (video_id, tag_id)))
row = cast (VideoTagRow, c.fetchone ()) row = cast (VideoTagRow, c.fetchone ())
if row is None: if row is None:
return None return None
@@ -573,7 +575,7 @@ class VideoTagDao:
untagged_at = cast (date | None, untagged_at) untagged_at = cast (date | None, untagged_at)


with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
INSERT INTO INSERT INTO
video_tags( video_tags(
video_id, video_id,
@@ -586,7 +588,7 @@ class VideoTagDao:
%s, %s,
%s, %s,
%s)""", (video_tag.video_id, video_tag.tag_id, %s)""", (video_tag.video_id, video_tag.tag_id,
video_tag.tagged_at, untagged_at))
video_tag.tagged_at, untagged_at)))
video_tag.id_ = c.lastrowid video_tag.id_ = c.lastrowid
if with_relation_tables: if with_relation_tables:
if video_tag.video is not None: if video_tag.video is not None:
@@ -607,7 +609,7 @@ class VideoTagDao:
untagged_at = cast (date | None, untagged_at) untagged_at = cast (date | None, untagged_at)


with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
INSERT INTO INSERT INTO
video_tags( video_tags(
video_id, video_id,
@@ -627,7 +629,7 @@ class VideoTagDao:
untagged_at = VALUES(untagged_at)""", (video_tag.video_id, untagged_at = VALUES(untagged_at)""", (video_tag.video_id,
video_tag.tag_id, video_tag.tag_id,
video_tag.tagged_at, video_tag.tagged_at,
untagged_at))
untagged_at)))
video_tag.id_ = c.lastrowid video_tag.id_ = c.lastrowid
if with_relation_tables: if with_relation_tables:
if video_tag.video is not None: if video_tag.video is not None:
@@ -652,14 +654,14 @@ class VideoTagDao:
if not tag_ids: if not tag_ids:
return return
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
UPDATE UPDATE
video_tags video_tags
SET SET
untagged_at = %%s untagged_at = %%s
WHERE WHERE
video_id = %%s video_id = %%s
AND tag_ids IN (%s)""" % ', '.join (['%s'] * len (tag_ids)), (now, video_id, *tag_ids))
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,
@@ -700,14 +702,14 @@ class TagDao:
tag_id: int, tag_id: int,
) -> TagDto | None: ) -> TagDto | None:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT SELECT
id, id,
name name
FROM FROM
tags tags
WHERE WHERE
id = %s""", (tag_id,))
id = %s""", (tag_id,)))
row = cast (TagRow | None, c.fetchone ()) row = cast (TagRow | None, c.fetchone ())
if row is None: if row is None:
return None return None
@@ -718,14 +720,14 @@ class TagDao:
tag_name: str, tag_name: str,
) -> TagDto | None: ) -> TagDto | None:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT SELECT
id, id,
name name
FROM FROM
tags tags
WHERE WHERE
name = %s""", (tag_name,))
name = %s""", (tag_name,)))
row = cast (TagRow | None, c.fetchone ()) row = cast (TagRow | None, c.fetchone ())
if row is None: if row is None:
return None return None
@@ -736,11 +738,11 @@ class TagDao:
tag: TagDto, tag: TagDto,
) -> None: ) -> None:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (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 (
@@ -748,13 +750,13 @@ class TagDao:
tag: TagDto, tag: TagDto,
) -> None: ) -> None:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (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 (
@@ -784,7 +786,7 @@ class VideoHistoryDao:
with_relation_tables: bool, with_relation_tables: bool,
) -> list[VideoHistoryDto]: ) -> list[VideoHistoryDto]:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT SELECT
id, id,
video_id, video_id,
@@ -793,7 +795,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 cast (list[VideoHistoryRow], c.fetchall ()): for row in cast (list[VideoHistoryRow], 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))
@@ -804,7 +806,7 @@ class VideoHistoryDao:
video_history: VideoHistoryDto, video_history: VideoHistoryDto,
) -> None: ) -> None:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
INSERT INTO INSERT INTO
video_histories( video_histories(
video_id, video_id,
@@ -816,14 +818,14 @@ class VideoHistoryDao:
%s, %s,
%s)""", (video_history.video_id, %s)""", (video_history.video_id,
video_history.fetched_at, video_history.fetched_at,
video_history.views_count))
video_history.views_count)))


def upsert ( def upsert (
self, self,
video_history: VideoHistoryDto, video_history: VideoHistoryDto,
) -> None: ) -> None:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
INSERT INTO INSERT INTO
video_histories( video_histories(
video_id, video_id,
@@ -839,7 +841,7 @@ class VideoHistoryDao:
fetched_at, fetched_at,
views_count""", (video_history.video_id, views_count""", (video_history.video_id,
video_history.fetched_at, video_history.fetched_at,
video_history.views_count))
video_history.views_count)))


def upsert_all ( def upsert_all (
self, self,
@@ -884,7 +886,7 @@ class CommentDao:
with_relation_tables: bool, with_relation_tables: bool,
) -> list[CommentDto]: ) -> list[CommentDto]:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT SELECT
id, id,
video_id, video_id,
@@ -897,7 +899,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 cast (list[CommentRow], c.fetchall ()): for row in cast (list[CommentRow], c.fetchall ()):
comments.append (self._create_dto_from_row (row, with_relation_tables)) comments.append (self._create_dto_from_row (row, with_relation_tables))
@@ -916,7 +918,7 @@ class CommentDao:
vpos_ms = cast (int | None, vpos_ms) vpos_ms = cast (int | None, vpos_ms)


with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
INSERT INTO INSERT INTO
comments( comments(
video_id, video_id,
@@ -948,7 +950,7 @@ class CommentDao:
comment.content, comment.content,
comment.posted_at, comment.posted_at,
comment.nico_count, comment.nico_count,
vpos_ms))
vpos_ms)))


def upsert_all ( def upsert_all (
self, self,
@@ -1002,14 +1004,14 @@ class UserDao:
user_code: str user_code: str
) -> UserDto | None: ) -> UserDto | None:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT SELECT
id, id,
code code
FROM FROM
users users
WHERE WHERE
code = %s""", (user_code,))
code = %s""", (user_code,)))
row = cast (UserRow | None, c.fetchone ()) row = cast (UserRow | None, c.fetchone ())
if row is None: if row is None:
return None return None
@@ -1020,11 +1022,11 @@ class UserDao:
user: UserDto, user: UserDto,
) -> None: ) -> None:
with self.conn.cursor (dictionary = True) as c: with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (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