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