-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_handler.py
More file actions
222 lines (192 loc) · 8.1 KB
/
Copy pathdatabase_handler.py
File metadata and controls
222 lines (192 loc) · 8.1 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import psycopg
from env import DB_USER, DB_PASS, DB_HOST, DB_PORT
import datetime
class DatabaseHandler:
def __init__(self):
self.connection = self._get_connection()
self.cursor = self.connection.cursor()
def _get_connection(self):
return psycopg.connect(
user=DB_USER,
password=DB_PASS,
host=DB_HOST,
port=DB_PORT
)
def insert_user(self, username: str, username_hashed: int,
password_hashed_one: int, password_hashed_two: int,
phone_number: int, balance: float,
registration_timestamp: datetime.datetime, activation_timestamp: datetime.datetime,
ic_no: str):
"""
Insert a user into the database
"""
sql_query = """
INSERT INTO User_Table(username, username_hashed, password_hashed_one, password_hashed_two, phone_no, balance, registration_timestamp, activation_timestamp, ic_no)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
self.cursor.execute(sql_query, (username, username_hashed, password_hashed_one, password_hashed_two,
phone_number, balance, registration_timestamp, activation_timestamp, ic_no))
self.connection.commit()
def fetch_creds(self, ic_number: str):
"""
name: str, birthday: datetime, identification_num: int, address: str
"""
sql_query = """
SELECT ic_no, birthdate, address
FROM Credential_Table
WHERE ic_no = %s;
"""
self.cursor.execute(sql_query, (ic_number, ))
results = self.cursor.fetchone()
return results
def fetch_user_data(self, username: str, username_hashed: int):
"""
Fetches the user data from the database based on username
"""
sql_query = """
SELECT uid, username, username_hashed, password_hashed_one, password_hashed_two, phone_no, balance, registration_timestamp, activation_timestamp, ic_no
FROM User_Table
WHERE username_hashed = %s;
"""
self.cursor.execute(sql_query, (username_hashed, ))
results = self.cursor.fetchall()
# assuming that the username is in position 1
for result in results:
if result[1] == username:
return result
return None
def fetch_user_data_by_phone(self, phone_number: int):
"""
Fetches the user data from the database based on username
"""
sql_query = """
SELECT uid, username, username_hashed, password_hashed_one, password_hashed_two, phone_no, balance, registration_timestamp, activation_timestamp, ic_no
FROM User_Table
WHERE phone_no = %s;
"""
self.cursor.execute(sql_query, (phone_number, ))
results = self.cursor.fetchone()
return results
def fetch_user_data_uid(self, uid: int):
"""
Fetches the user data from the database based on username
"""
sql_query = """
SELECT uid, username, username_hashed, password_hashed_one, password_hashed_two, phone_no, balance, registration_timestamp, activation_timestamp, ic_no
FROM User_Table
WHERE uid = %s;
"""
self.cursor.execute(sql_query, (uid, ))
results = self.cursor.fetchone()
return results
def fetch_user_data_ic_no(self, ic_no: str):
"""
Fetches the user data from the database based on username
"""
sql_query = """
SELECT uid, username, username_hashed, password_hashed_one, password_hashed_two, phone_no, balance, registration_timestamp, activation_timestamp, ic_no
FROM User_Table
WHERE ic_no = %s;
"""
self.cursor.execute(sql_query, (ic_no, ))
results = self.cursor.fetchone()
return results
def delete(self, uid: int):
"""
Delete a user from the database by UID
"""
sql_query = """
DELETE FROM User_Table
WHERE uid = %s;
"""
self.cursor.execute(sql_query, (uid, ))
self.connection.commit()
def update_password(self, uid: int, password_hashed_one: int, password_hashed_two: int):
"""
Updates the password of a user in the database based on uid
"""
sql_query = """
UPDATE User_Table
SET password_hashed_one = %s, password_hashed_two = %s
WHERE uid = %s;
"""
self.cursor.execute(sql_query, (uid, password_hashed_one, password_hashed_two))
self.connection.commit()
def update_balance(self, uid: int, tran_amt: float):
"""
Updates the balance of a user in the database based on uid
"""
sql_query = """
UPDATE User_Table
SET balance = balance + %s
WHERE uid = %s
"""
self.cursor.execute(sql_query, (tran_amt, uid))
self.connection.commit()
def bulk_activate_user(self, entries: list[datetime.datetime, str]):
"""
Bulk activate the users
"""
sql_query = """
UPDATE User_Table
SET activation_timestamp = %s
WHERE ic_no = %s;
"""
self.cursor.executemany(sql_query, entries)
self.connection.commit()
def bulk_update_balance(self, entries: list[int, float]):
"""
Updates the balance of multiple users in the database based on uid
"""
sql_query = """
UPDATE User_Table
SET balance = balance + %s
WHERE uid = %s
"""
self.cursor.executemany(sql_query, entries)
self.connection.commit()
def insert_transaction(self, sender_uid: int, receiver_uid: int, amount: float, date: datetime.datetime):
"""
Adds a transaction to the database
"""
sql_query = """
INSERT INTO Transaction_Table(sender_id, recepient_id, tran_amt, tran_timestamp)
VALUES (%s, %s, %s, %s);
"""
self.cursor.execute(sql_query, (sender_uid, receiver_uid, amount, date))
self.connection.commit()
def bulk_insert_transactions(self, entries: list[int, int, float, datetime.datetime]):
"""
Adds multiple transactions to the database
"""
sql_query = """
INSERT INTO Transaction_Table(sender_id, recepient_id, tran_amt, tran_timestamp) VALUES (%s, %s, %s, %s)
"""
self.cursor.executemany(sql_query, entries)
self.connection.commit()
def fetch_transactions(self, uid: int, start_date: datetime.datetime, end_date: datetime.datetime):
"""
Fetches the transactions of a user from the database
Each transaction is a tuple of (sender_uid, receiver_uid, amount, date)
"""
sql_query = """
SELECT tran_id, tran_amt, recepient_id, sender_id, tran_timestamp
FROM Transaction_Table
WHERE (sender_id = %s OR recepient_id = %s) AND tran_timestamp >= %s AND tran_timestamp <= %s;
"""
self.cursor.execute(sql_query, (uid, uid, start_date, end_date))
results = self.cursor.fetchall()
return results
def rollback(self):
"""
Rolls back the database to the last commit
"""
self.connection.rollback()
def __del__(self):
self.connection.close()
# SAMPLE
# In this function, the table name is 'users'
# def insert_user(self, username, password):
# self.cursor.execute('INSERT INTO users VALUES (NULL, %s, %s)', (username, password))
# self.connection.commit()
#