-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
124 lines (106 loc) · 4.03 KB
/
database.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from typing import Tuple, Dict, List
import config
import sqlite3
def allProfiles(database: Tuple[sqlite3.Connection, sqlite3.Cursor]) -> Dict:
'''
Returns all the profiles from DB
Args:
database (tuple[sqlite3.Connection, sqlite3.Cursor]): connection and cursor of DB
Returns:
profiles (List[Dict]) | Literal[False]: List of dictionaries with profiles
{service_name : (username_hash, password_hash)}
'''
try:
profiles = {}
con, cur = database
fetching_query = '''SELECT * FROM profiles'''
cur.execute(fetching_query)
data = cur.fetchall()
for profile in data:
profile_id, service, username_hash, password_hash = profile
profiles[service] = (username_hash.encode(), password_hash.encode())
return profiles
except sqlite3.Error as e:
print(e)
return False
def newProfile(service_name: str, username_hash: str, password_hash: str, database: Tuple[sqlite3.Connection, sqlite3.Cursor]) -> bool:
'''
Creates new profile in DB
Args:
service_name (str): Name of the profile's service
username_hash (str): Hashed username of the profile's service
password_hash (str): Hashed password of the profile's service
database (tuple[sqlite3.Connection, sqlite3.Cursor]): connection and cursor of DB
Returns:
result (bool): Success of operation
'''
try:
con, cur = database
insert_query = '''INSERT INTO profiles(service, username, password) VALUES(?, ?, ?)'''
cur.execute(insert_query, (service_name, username_hash, password_hash,))
con.commit()
except sqlite3.Error as e:
print(e)
return False
return True
def updateProfile(service_name: str, new_name: str, username_hash: str, password_hash: str, database: Tuple[sqlite3.Connection, sqlite3.Cursor]) -> bool:
'''
Updating username and password hashes in DB by service_name
Args:
service_name (str): Name of the service to update
new_name (str): New name of the profile
username_hash (str): Hash of new username
password_hash (str): Hash of new password
database (Tuple[sqlite3.Connection, sqlite3.Cursor]): database (tuple[sqlite3.Connection, sqlite3.Cursor]): connection and cursor of DB
Returns:
result (bool): True - success, False - fail
'''
con, cur = database
query = f'UPDATE profiles SET username = ?, password = ?, service = ? WHERE service = ?'
try:
cur.execute(query, (username_hash, password_hash, new_name, service_name,))
con.commit()
except sqlite3.Error as e:
print(e)
return False
return True
def deleteProfile(service_name: str, database: Tuple[sqlite3.Connection, sqlite3.Cursor]) -> bool:
'''
Deleting profile in DB table
Args:
service_name (str): Name of the service to update
database (Tuple[sqlite3.Connection, sqlite3.Cursor]): database (tuple[sqlite3.Connection, sqlite3.Cursor]): connection and cursor of DB
Returns:
result (bool): True - success, False - fail
'''
con, cur = database
query = 'DELETE FROM profiles WHERE service = ?'
try:
cur.execute(query, (service_name,))
con.commit()
except sqlite3.Error as e:
print(e)
return False
return True
def setUp():
'''
Setup database
Returns:
tuple[Connection, Cursor] | Literal[False]
con (sqlite3.Connection): Connection to database
cur (sqlite3.Cursor): Cursor of current connection
'''
try:
con = sqlite3.connect(database=config.DB_NAME, check_same_thread=False)
cur = con.cursor()
init_query = '''CREATE TABLE IF NOT EXISTS profiles (
id integer PRIMARY KEY,
service text NOT NULL,
username text,
password text
);'''
cur.execute(init_query)
except sqlite3.Error as e:
print(e)
return False
return con, cur