Browse Source

print で出力するのは無意味だったので取消し

feature/query
みてるぞ 1 month ago
parent
commit
988cf714f8
1 changed files with 43 additions and 45 deletions
  1. +43
    -45
      update_db.py

+ 43
- 45
update_db.py View File

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

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

conn.close ()


@@ -312,7 +310,7 @@ class VideoDao:
with_relation_tables: bool,
) -> VideoDto | None:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
SELECT
id,
code,
@@ -325,7 +323,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
@@ -336,7 +334,7 @@ class VideoDao:
with_relation_tables: bool,
) -> list[VideoDto]:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
SELECT
id,
code,
@@ -347,7 +345,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))
@@ -357,7 +355,7 @@ class VideoDao:
self,
) -> list[VideoDto]:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
SELECT
id,
code,
@@ -368,7 +366,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))
@@ -387,7 +385,7 @@ class VideoDao:
deleted_at = cast (datetime | None, deleted_at)

with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
INSERT INTO
videos(
code,
@@ -412,7 +410,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:
@@ -438,13 +436,13 @@ class VideoDao:
if not video_ids:
return
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
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,
@@ -496,7 +494,7 @@ class VideoTagDao:
with_relation_tables: bool,
) -> list[VideoTagDto]:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
SELECT
id,
video_id,
@@ -508,7 +506,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))
@@ -520,7 +518,7 @@ class VideoTagDao:
with_relation_tables: bool,
) -> list[VideoTagDto]:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
SELECT
id,
video_id,
@@ -533,7 +531,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))
@@ -546,7 +544,7 @@ class VideoTagDao:
with_relation_tables: bool,
) -> VideoTagDto | None:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
SELECT
id,
video_id,
@@ -557,7 +555,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
@@ -576,7 +574,7 @@ class VideoTagDao:
untagged_at = cast (date | None, untagged_at)

with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
INSERT INTO
video_tags(
video_id,
@@ -589,7 +587,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:
@@ -610,7 +608,7 @@ class VideoTagDao:
untagged_at = cast (date | None, untagged_at)

with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
INSERT INTO
video_tags(
video_id,
@@ -631,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:
@@ -656,7 +654,7 @@ class VideoTagDao:
if not tag_ids:
return
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
UPDATE
video_tags
SET
@@ -704,14 +702,14 @@ class TagDao:
tag_id: int,
) -> TagDto | None:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
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
@@ -722,14 +720,14 @@ class TagDao:
tag_name: str,
) -> TagDto | None:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
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
@@ -740,11 +738,11 @@ class TagDao:
tag: TagDto,
) -> None:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
INSERT INTO
tags(name)
VALUES
(%s)""", (tag.name,)))
(%s)""", (tag.name,))
tag.id_ = c.lastrowid

def upsert (
@@ -752,14 +750,14 @@ class TagDao:
tag: TagDto,
) -> None:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
INSERT INTO
tags(name)
VALUES
(%s)
ON DUPLICATE KEY UPDATE
id = LAST_INSERT_ID(id),
name = VALUES(name)""", (tag.name,)))
name = VALUES(name)""", (tag.name,))
tag.id_ = c.lastrowid

def _create_dto_from_row (
@@ -789,7 +787,7 @@ class VideoHistoryDao:
with_relation_tables: bool,
) -> list[VideoHistoryDto]:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
SELECT
id,
video_id,
@@ -798,7 +796,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))
@@ -809,7 +807,7 @@ class VideoHistoryDao:
video_history: VideoHistoryDto,
) -> None:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
INSERT INTO
video_histories(
video_id,
@@ -821,14 +819,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:
print (c.execute ("""
c.execute ("""
INSERT INTO
video_histories(
video_id,
@@ -845,7 +843,7 @@ class VideoHistoryDao:
fetched_at = VALUES(fetched_at),
views_count = VALUES(views_count)""", (video_history.video_id,
video_history.fetched_at,
video_history.views_count)))
video_history.views_count))

def upsert_all (
self,
@@ -890,7 +888,7 @@ class CommentDao:
with_relation_tables: bool,
) -> list[CommentDto]:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
c.execute ("""
SELECT
id,
video_id,
@@ -903,7 +901,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))
@@ -922,7 +920,7 @@ class CommentDao:
vpos_ms = cast (int | None, vpos_ms)

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

def upsert_all (
self,
@@ -1009,14 +1007,14 @@ class UserDao:
user_code: str
) -> UserDto | None:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
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
@@ -1027,11 +1025,11 @@ class UserDao:
user: UserDto,
) -> None:
with self.conn.cursor (dictionary = True) as c:
print (c.execute ("""
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