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)

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

conn.close ()


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

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

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

with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
INSERT INTO
video_tags(
video_id,
@@ -627,7 +629,7 @@ class VideoTagDao:
untagged_at = VALUES(untagged_at)""", (video_tag.video_id,
video_tag.tag_id,
video_tag.tagged_at,
untagged_at))
untagged_at)))
video_tag.id_ = c.lastrowid
if with_relation_tables:
if video_tag.video is not None:
@@ -652,14 +654,14 @@ class VideoTagDao:
if not tag_ids:
return
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
UPDATE
video_tags
SET
untagged_at = %%s
WHERE
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 (
self,
@@ -700,14 +702,14 @@ class TagDao:
tag_id: int,
) -> TagDto | None:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT
id,
name
FROM
tags
WHERE
id = %s""", (tag_id,))
id = %s""", (tag_id,)))
row = cast (TagRow | None, c.fetchone ())
if row is None:
return None
@@ -718,14 +720,14 @@ class TagDao:
tag_name: str,
) -> TagDto | None:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT
id,
name
FROM
tags
WHERE
name = %s""", (tag_name,))
name = %s""", (tag_name,)))
row = cast (TagRow | None, c.fetchone ())
if row is None:
return None
@@ -736,11 +738,11 @@ class TagDao:
tag: TagDto,
) -> None:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
INSERT INTO
tags(name)
VALUES
(%s)""", (tag.name,))
(%s)""", (tag.name,)))
tag.id_ = c.lastrowid

def upsert (
@@ -748,13 +750,13 @@ class TagDao:
tag: TagDto,
) -> None:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (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 (
@@ -784,7 +786,7 @@ class VideoHistoryDao:
with_relation_tables: bool,
) -> list[VideoHistoryDto]:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
SELECT
id,
video_id,
@@ -793,7 +795,7 @@ class VideoHistoryDao:
FROM
video_histories
WHERE
video_id = %s""", (video_id,))
video_id = %s""", (video_id,)))
video_histories: list[VideoHistoryDto] = []
for row in cast (list[VideoHistoryRow], c.fetchall ()):
video_histories.append (self._create_dto_from_row (row, with_relation_tables))
@@ -804,7 +806,7 @@ class VideoHistoryDao:
video_history: VideoHistoryDto,
) -> None:
with self.conn.cursor (dictionary = True) as c:
c.execute ("""
print (c.execute ("""
INSERT INTO
video_histories(
video_id,
@@ -816,14 +818,14 @@ class VideoHistoryDao:
%s,
%s)""", (video_history.video_id,
video_history.fetched_at,
video_history.views_count))
video_history.views_count)))

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

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

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

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