-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommentdb.py
71 lines (61 loc) · 2.16 KB
/
commentdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import sqlite3
import comment
from typing import *
class DBConnection:
INSERT_SQL = """INSERT OR IGNORE INTO comments VALUES({comment_id},"{author}","{title}","{url}","{date}","{author}","{comment}");"""
SELECT_SQL = "SELECT * FROM comments WHERE id={comment_id};"
GET_ID_SQL = "SELECT id FROM comments;"
SELECT_ALL = "SELECT * FROM comments;"
def __init__(self, db_file: str):
self._conn = sqlite3.connect(db_file)
def __del__(self):
self._conn.close()
def insert_comment(self, comm: comment.Comment) -> None:
cmd = DBConnection.INSERT_SQL.format(**comm.json())
cursor = self._conn.cursor()
cursor.execute("BEGIN TRANSACTION;")
cursor.execute(cmd)
cursor.execute("COMMIT;")
cursor.close()
def get_comment(self, comment_id: int) -> comment.Comment:
cmd = DBConnection.SELECT_SQL.format(comment_id=comment_id)
cursor = self._conn.cursor()
cursor.execute(cmd)
reply = cursor.fetchone()
print("REPLY: {}".format(reply))
cursor.close()
config = {
"comment_id": reply[0],
"url": reply[3],
"date": reply[4],
"author": reply[1],
"title": reply[2],
"comment": reply[6]
}
return comment.Comment(config)
def get_saved_comment_ids(self) -> set:
cursor = self._conn.cursor()
cursor.execute(DBConnection.GET_ID_SQL)
reply = cursor.fetchall()
cursor.close()
saved_ids = set([x[0] for x in reply])
return saved_ids
def get_all_comments(self) -> List[comment.Comment]:
cursor = self._conn.cursor()
cursor.execute(DBConnection.SELECT_ALL)
reply = cursor.fetchall()
cursor.close()
comments = []
for row in reply:
comments.append(comment.Comment(DBConnection.row_to_dict(row)))
return comments
@staticmethod
def row_to_dict(row: tuple) -> dict:
return {
"comment_id": row[0],
"url": row[3],
"date": row[4],
"author": row[1],
"title": row[2],
"comment": row[6]
}