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