1
|
from ..db_objects.private_key import PrivateKey
|
2
|
from ..dao.repository import IRepository
|
3
|
from db_manager import DBManager
|
4
|
from ..constants import *
|
5
|
|
6
|
|
7
|
class PrivateKeyRepositoryImpl(IRepository):
|
8
|
|
9
|
def create(self, private_key: str, password: str) -> bool:
|
10
|
"""
|
11
|
Creates a private key
|
12
|
|
13
|
:param private_key: private key
|
14
|
:param password: passphrase for encode private key
|
15
|
|
16
|
:return: the result of whether the creation was successful
|
17
|
"""
|
18
|
|
19
|
sql = f"INSERT INTO {TAB_PRIVATE_KEYS} ({COL_PRIVATE_KEY},{COL_PASSWORD}) VALUES(?,?)"
|
20
|
result = DBManager.create(sql, [private_key, password])
|
21
|
return result
|
22
|
|
23
|
def read(self, private_key_id: int = None):
|
24
|
"""
|
25
|
Reads (selects) a private key
|
26
|
|
27
|
:param private_key_id: ID of specific private
|
28
|
|
29
|
:return: instance of PrivateKey class if ID was used otherwise list of this instances
|
30
|
"""
|
31
|
|
32
|
sql_extend = ""
|
33
|
|
34
|
if private_key_id is not None:
|
35
|
sql_extend = f" WHERE {COL_ID} = ?"
|
36
|
|
37
|
sql = f"SELECT * FROM {TAB_PRIVATE_KEYS}{sql_extend}"
|
38
|
rows = DBManager.read(sql, private_key_id)
|
39
|
|
40
|
private_keys: list = []
|
41
|
|
42
|
for row in rows:
|
43
|
private_keys.append(PrivateKey(row))
|
44
|
|
45
|
if private_key_id is not None:
|
46
|
return private_keys[0]
|
47
|
|
48
|
return private_keys
|
49
|
|
50
|
def update(self, private_key_id: int, private_key: str = None, password: str = None) -> bool:
|
51
|
"""
|
52
|
Updates a private key
|
53
|
|
54
|
:param private_key_id: ID of specific private key
|
55
|
:param private_key: private key
|
56
|
:param password: passphrase for encode private key
|
57
|
|
58
|
:return: the result of whether the updation was successful
|
59
|
"""
|
60
|
|
61
|
updated_list = []
|
62
|
values = []
|
63
|
if private_key is not None:
|
64
|
updated_list.append(f"{COL_PRIVATE_KEY} = ?")
|
65
|
values.append(private_key)
|
66
|
if password is not None:
|
67
|
updated_list.append(f"{COL_PASSWORD} = ?")
|
68
|
values.append(password)
|
69
|
|
70
|
updated_str = ", ".join(updated_list)
|
71
|
sql = f"UPDATE {TAB_PRIVATE_KEYS} SET {updated_str} WHERE {COL_ID} = ?"
|
72
|
result = DBManager.update(sql, private_key_id, values)
|
73
|
return result
|
74
|
|
75
|
def delete(self, private_key_id: int) -> bool:
|
76
|
"""
|
77
|
Deletes a private key
|
78
|
|
79
|
:param private_key_id: ID of specific private key
|
80
|
|
81
|
:return: the result of whether the deletion was successful
|
82
|
"""
|
83
|
|
84
|
sql = f"DELETE FROM {TAB_PRIVATE_KEYS} WHERE {COL_ID} = ?"
|
85
|
result = DBManager.delete(sql, private_key_id, False)
|
86
|
return result
|