Skip to content

Commit 77e44f5

Browse files
committed
refactor: improve database schema
1 parent a3d2567 commit 77e44f5

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

database/database.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,21 @@ def create_database(database_path):
4949
cursor = connection.cursor()
5050

5151
cursor.execute("CREATE TABLE IF NOT EXISTS 'admins' "
52-
"('userID' INTEGER NOT NULL,"
52+
"('user_id' INTEGER NOT NULL,"
5353
"'first_name' TEXT,"
5454
"'username' TEXT,"
55-
"PRIMARY KEY('userID'));")
55+
"PRIMARY KEY('user_id'));")
5656

5757
cursor.execute("CREATE TABLE IF NOT EXISTS 'users'"
58-
"('userID' INTEGER NOT NULL,"
59-
"'languageID' TEXT,"
58+
"('user_id' INTEGER NOT NULL,"
6059
"'first_name' TEXT,"
6160
"'last_name' TEXT,"
6261
"'username' TEXT,"
63-
"'gamesPlayed' INTEGER,"
64-
"'gamesWon' INTEGER,"
65-
"'gamesTie' INTEGER,"
66-
"'lastPlayed' INTEGER,"
67-
"PRIMARY KEY('userID'));")
62+
"'games_played' INTEGER,"
63+
"'games_won' INTEGER,"
64+
"'games_tie' INTEGER,"
65+
"'last_played' INTEGER,"
66+
"PRIMARY KEY('user_id'));")
6867

6968
cursor.execute("CREATE TABLE IF NOT EXISTS 'chats'"
7069
"('chat_id' INTEGER NOT NULL,"
@@ -74,7 +73,7 @@ def create_database(database_path):
7473
connection.close()
7574

7675
def get_user(self, user_id):
77-
self.cursor.execute("SELECT * FROM users WHERE userID=?;", [str(user_id)])
76+
self.cursor.execute("SELECT * FROM users WHERE user_id=?;", [str(user_id)])
7877

7978
result = self.cursor.fetchone()
8079
if not result or len(result) == 0:
@@ -84,12 +83,12 @@ def get_user(self, user_id):
8483
def get_recent_players(self):
8584
one_day_in_secs = 60 * 60 * 24
8685
current_time = int(time())
87-
self.cursor.execute("SELECT userID FROM users WHERE lastPlayed>=?;", [current_time - one_day_in_secs])
86+
self.cursor.execute("SELECT user_id FROM users WHERE last_played>=?;", [current_time - one_day_in_secs])
8887

8988
return self.cursor.fetchall()
9089

9190
def get_played_games(self, user_id):
92-
self.cursor.execute("SELECT gamesPlayed FROM users WHERE userID=?;", [str(user_id)])
91+
self.cursor.execute("SELECT games_played FROM users WHERE user_id=?;", [str(user_id)])
9392

9493
result = self.cursor.fetchone()
9594

@@ -107,7 +106,7 @@ def get_all_users(self):
107106

108107
@Cache(timeout=60)
109108
def get_admins(self):
110-
self.cursor.execute("SELECT userID from admins;")
109+
self.cursor.execute("SELECT user_id from admins;")
111110
admins = self.cursor.fetchall()
112111
admin_list = []
113112
for admin in admins:
@@ -144,19 +143,19 @@ def _add_user(self, user_id, lang_id, first_name, last_name, username):
144143
return
145144

146145
def set_games_won(self, games_won, user_id):
147-
self.cursor.execute("UPDATE users SET gamesWon = ? WHERE userID = ?;", [games_won, str(user_id)])
146+
self.cursor.execute("UPDATE users SET games_won = ? WHERE user_id = ?;", [games_won, str(user_id)])
148147
self.connection.commit()
149148

150149
def set_games_played(self, games_played, user_id):
151-
self.cursor.execute("UPDATE users SET gamesPlayed = ? WHERE userID = ?;", [games_played, str(user_id)])
150+
self.cursor.execute("UPDATE users SET games_played = ? WHERE user_id = ?;", [games_played, str(user_id)])
152151
self.connection.commit()
153152

154153
def set_last_played(self, last_played, user_id):
155-
self.cursor.execute("UPDATE users SET lastPlayed = ? WHERE userID = ?;", [last_played, str(user_id)])
154+
self.cursor.execute("UPDATE users SET last_played = ? WHERE user_id = ?;", [last_played, str(user_id)])
156155
self.connection.commit()
157156

158157
def is_user_saved(self, user_id):
159-
self.cursor.execute("SELECT rowid, * FROM users WHERE userID=?;", [str(user_id)])
158+
self.cursor.execute("SELECT rowid, * FROM users WHERE user_id=?;", [str(user_id)])
160159

161160
result = self.cursor.fetchall()
162161
if len(result) > 0:
@@ -165,7 +164,7 @@ def is_user_saved(self, user_id):
165164
return False
166165

167166
def user_data_changed(self, user_id, first_name, last_name, username):
168-
self.cursor.execute("SELECT * FROM users WHERE userID=?;", [str(user_id)])
167+
self.cursor.execute("SELECT * FROM users WHERE user_id=?;", [str(user_id)])
169168

170169
result = self.cursor.fetchone()
171170

@@ -178,11 +177,11 @@ def user_data_changed(self, user_id, first_name, last_name, username):
178177
return True
179178

180179
def update_user_data(self, user_id, first_name, last_name, username):
181-
self.cursor.execute("UPDATE users SET first_name=?, last_name=?, username=? WHERE userID=?;", [first_name, last_name, username, str(user_id)])
180+
self.cursor.execute("UPDATE users SET first_name=?, last_name=?, username=? WHERE user_id=?;", [first_name, last_name, username, str(user_id)])
182181
self.connection.commit()
183182

184183
def reset_stats(self, user_id):
185-
self.cursor.execute("UPDATE users SET gamesPlayed='0', gamesWon='0', gamesTie='0', lastPlayed='0' WHERE userID=?;", [str(user_id)])
184+
self.cursor.execute("UPDATE users SET games_played='0', games_won='0', games_tie='0', last_played='0' WHERE user_id=?;", [str(user_id)])
186185
self.connection.commit()
187186

188187
def close_conn(self):

0 commit comments

Comments
 (0)