-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
135 lines (106 loc) · 4.32 KB
/
Copy pathdb.py
File metadata and controls
135 lines (106 loc) · 4.32 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import pickle
import sqlite3
import logging
from config import _msg
from config import _log_print
from gacha import Gacha
from typing import List, Tuple, Optional
DbUserTuple = Tuple[str, int, int, int, bool, bool]
class UserDB:
database = 'database.sqlite'
database_old = 'database.sql'
old_to_new = ['win_4', 'win_5']
def __init__(self):
self.conn = sqlite3.connect(self.database)
logging.debug(_msg('log_db_created'))
if not self._check_table():
logging.debug(_msg('log_db_table_create'))
self._create_table()
for new_column in self.old_to_new:
if not self._check_column(new_column):
logging.debug(_msg('log_db_old_update'), new_column)
self._create_column(new_column)
self._restore_old()
def _restore_old(self) -> None:
if not os.path.exists(self.database_old):
return
logging.debug(_msg('log_db_old_import'))
_log_print(_msg('db_import_old_start'))
with open(self.database_old, mode='rb') as f:
data = pickle.loads(f.read())
i_users = 0
_log_print(_msg('db_import_old_users_count'), len(data))
for user, gacha in data.items():
user = user.lower()
setattr(gacha, 'win_garant_table', {'5': 0, '4': 0})
check = self.get(user)
if check:
self.update(user, gacha)
continue
self.push(user, gacha)
i_users += 1
_log_print(_msg('db_import_old_users_total'), i_users)
os.remove(self.database_old)
_log_print(_msg('db_import_old_deleted'), )
def _create_column(self, column: str) -> None:
cur = self.conn.cursor()
payload = "ALTER TABLE users ADD COLUMN %s INTEGER DEFAULT 0;" % column
cur.execute(payload)
self.conn.commit()
cur.close()
def _check_column(self, column: str) -> bool:
cur = self.conn.cursor()
cur.execute("PRAGMA table_info('users');")
table_cols = [col[1] for col in cur.fetchall()]
cur.close()
is_found = column in table_cols
if is_found:
return True
return False
def _check_table(self) -> bool:
cur = self.conn.cursor()
cur.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='users';")
ret = cur.fetchone()
cur.close()
if ret is None:
return False
return True
def _create_table(self) -> None:
cur = self.conn.cursor()
payload = "CREATE TABLE users (username TEXT PRIMARY KEY, wish_count INTEGER, wish_4_garant INTEGER, wish_5_garant INTEGER, win_4 INTEGER, win_5 INTEGER);"
cur.execute(payload)
self.conn.commit()
cur.close()
def get_all(self) -> List[DbUserTuple]:
logging.debug(_msg('log_db_method_getall'))
cur = self.conn.cursor()
payload = "SELECT * FROM users;"
cur.execute(payload)
data = cur.fetchall()
cur.close()
return data
def get(self, username) -> Optional[DbUserTuple]:
logging.debug(_msg('log_db_method_get'), username)
cur = self.conn.cursor()
payload = "SELECT * FROM users WHERE username=?;"
cur.execute(payload, (username,))
data = cur.fetchone()
cur.close()
return data
def push(self, username: str, gacha: Gacha) -> None:
logging.debug(_msg('log_db_method_push'), username, gacha)
cur = self.conn.cursor()
payload = "INSERT INTO users VALUES(?, ?, ?, ?, ?, ?);"
win_4, win_5 = gacha.win_garant_table['4'], gacha.win_garant_table['5']
cur.execute(payload, (username, gacha.wish_count, gacha.wish_4_garant, gacha.wish_5_garant, win_4, win_5))
self.conn.commit()
cur.close()
def update(self, username: str, gacha: Gacha) -> None:
logging.debug(_msg('log_db_method_update'), username, gacha)
cur = self.conn.cursor()
payload = "UPDATE users SET wish_count=?, wish_4_garant=?, wish_5_garant=?, win_4=?, win_5=? WHERE username=?;"
win_4, win_5 = gacha.win_garant_table['4'], gacha.win_garant_table['5']
cur.execute(payload, (gacha.wish_count, gacha.wish_4_garant, gacha.wish_5_garant, win_4, win_5, username))
self.conn.commit()
cur.close()