-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
107 lines (90 loc) · 3.4 KB
/
db.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import sqlite3
from typing import List
from config import DB_NAME
class Database:
def __enter__(self):
self.__connection = sqlite3.connect(self._get_path())
self.__cursor = self.__connection.cursor()
self.__init_table()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.__connection.commit()
self.__connection.close()
def _get_path(self) \
-> str:
path = os.path.realpath(__file__)
path = path.removesuffix(os.path.basename(__file__))
path = os.path.join(path, DB_NAME)
path = r"{}".format(path)
return path
def __init_table(self):
self.__cursor.executescript("""
CREATE TABLE IF NOT EXISTS users(
id INT PRIMARY KEY,
UNIQUE(id)
);
CREATE TABLE IF NOT EXISTS subscriptions(
tinkoff_token VARCHAR(100),
broker_account_id INT PRIMARY KEY,
UNIQUE(broker_account_id)
);
CREATE TABLE IF NOT EXISTS users_subscriptions(
user_id INT,
broker_id INT,
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(broker_id) REFERENCES subscriptions(broker_account_id),
UNIQUE(user_id, broker_id)
)
""")
self.__connection.commit()
def add(self, user_id: int, tinkoff_token: str, broker_account_id: int):
self.__cursor.executescript(
f"INSERT OR REPLACE INTO users (id) "
f"VALUES ({user_id}); "
f"INSERT OR REPLACE INTO subscriptions (broker_account_id, tinkoff_token) "
f"VALUES ({broker_account_id}, '{tinkoff_token}'); "
f"INSERT OR REPLACE INTO users_subscriptions (user_id, broker_id) "
f"VALUES ({user_id}, {broker_account_id}); "
)
self.__connection.commit()
def get(self, user_id: int) \
-> List[tuple]:
return self.__cursor.execute(
f"SELECT user_id, tinkoff_token, broker_id "
f"FROM users AS u "
f"INNER JOIN users_subscriptions AS us "
f"ON us.user_id = u.id "
f"INNER JOIN subscriptions AS s "
f"ON us.broker_id = s.broker_account_id "
f"WHERE id = {user_id}"
).fetchall()
def get_user_ids(self) \
-> List[int]:
users = self.__cursor.execute(
f"SELECT id "
f"FROM users"
).fetchall()
user_ids = []
for user in users:
user_ids.append(user[0])
return user_ids
def not_exists_key(self, user_id: int, broker_id: int) \
-> bool:
found = self.__cursor.execute(
f"SELECT * FROM users_subscriptions "
f"WHERE user_id = {user_id} AND broker_id = {broker_id}").fetchone()
return True if found is None else False
def delete(self, user_id: int, broker_id: int):
self.__cursor.execute(
f"DELETE FROM users_subscriptions "
f"WHERE user_id = {user_id} AND broker_id = {broker_id}"
)
self.__connection.commit()
def refresh(self) -> None:
self.__cursor.executescript(
"DROP TABLE users_subscriptions; "
"DROP TABLE users; "
"DROP TABLE subscriptions; "
)
self.__connection.commit()