1
|
from sqlite3 import Connection, Cursor, Error
|
2
|
|
3
|
from src.db_objects.private_key import PrivateKey
|
4
|
from src.constants import *
|
5
|
|
6
|
|
7
|
class PrivateKeyRepository:
|
8
|
|
9
|
def __init__(self, connection: Connection, cursor: Cursor):
|
10
|
"""
|
11
|
Constructor of the PrivateKeyRepository object
|
12
|
|
13
|
:param connection: Instance of the Connection object
|
14
|
:param cursor: Instance of the Cursor object
|
15
|
"""
|
16
|
|
17
|
self.connection = connection
|
18
|
self.cursor = cursor
|
19
|
|
20
|
def create(self, private_key: PrivateKey):
|
21
|
"""
|
22
|
Creates a private key.
|
23
|
|
24
|
:param private_key: Instance of the PrivateKey object
|
25
|
|
26
|
:return: the result of whether the creation was successful
|
27
|
"""
|
28
|
|
29
|
try:
|
30
|
sql = (f"INSERT INTO {TAB_PRIVATE_KEYS} "
|
31
|
f"({COL_PRIVATE_KEY},"
|
32
|
f"{COL_PASSWORD}) "
|
33
|
f"VALUES(?,?)")
|
34
|
values = [private_key.private_key,
|
35
|
private_key.password]
|
36
|
self.cursor.execute(sql, values)
|
37
|
last_id = self.cursor.lastrowid
|
38
|
self.connection.commit()
|
39
|
except Error as e:
|
40
|
print(e)
|
41
|
return None
|
42
|
|
43
|
return last_id
|
44
|
|
45
|
def read(self, private_key_id: int):
|
46
|
"""
|
47
|
Reads (selects) a private key.
|
48
|
|
49
|
:param private_key_id: ID of specific private key
|
50
|
|
51
|
:return: instance of the PrivateKey object
|
52
|
"""
|
53
|
|
54
|
try:
|
55
|
sql = (f"SELECT * FROM {TAB_PRIVATE_KEYS} "
|
56
|
f"WHERE {COL_ID} = ?")
|
57
|
values = [private_key_id]
|
58
|
self.cursor.execute(sql, values)
|
59
|
private_key_row = self.cursor.fetchall()
|
60
|
|
61
|
private_key: PrivateKey = PrivateKey(private_key_row[0],
|
62
|
private_key_row[1],
|
63
|
private_key_row[2])
|
64
|
except Error as e:
|
65
|
print(e)
|
66
|
return None
|
67
|
|
68
|
if len(private_key_row) > 0:
|
69
|
return private_key
|
70
|
else:
|
71
|
return None
|
72
|
|
73
|
def update(self, private_key_id: int, private_key: PrivateKey) -> bool:
|
74
|
"""
|
75
|
Updates a private key.
|
76
|
|
77
|
:param private_key_id: ID of specific private key
|
78
|
:param private_key: Instance of the PrivateKey object
|
79
|
|
80
|
:return: the result of whether the updation was successful
|
81
|
"""
|
82
|
|
83
|
try:
|
84
|
sql = (f"UPDATE {TAB_PRIVATE_KEYS} "
|
85
|
f"SET {COL_PRIVATE_KEY} = ?, "
|
86
|
f"{COL_PASSWORD} = ? "
|
87
|
f"WHERE {COL_ID} = ?")
|
88
|
values = [private_key.private_key,
|
89
|
private_key.password,
|
90
|
private_key_id]
|
91
|
self.cursor.execute(sql, values)
|
92
|
self.connection.commit()
|
93
|
except Error as e:
|
94
|
print(e)
|
95
|
return False
|
96
|
|
97
|
return True
|
98
|
|
99
|
def delete(self, private_key_id: int) -> bool:
|
100
|
"""
|
101
|
Deletes a private key
|
102
|
|
103
|
:param private_key_id: ID of specific private key
|
104
|
|
105
|
:return: the result of whether the deletion was successful
|
106
|
"""
|
107
|
|
108
|
try:
|
109
|
sql = (f"DELETE FROM {TAB_PRIVATE_KEYS} "
|
110
|
f"WHERE {COL_ID} = ?")
|
111
|
values = [private_key_id]
|
112
|
self.cursor.execute(sql, values)
|
113
|
self.connection.commit()
|
114
|
except Error as e:
|
115
|
print(e)
|
116
|
return False
|
117
|
|
118
|
return True
|