1
|
import sqlite3
|
2
|
from sqlite3 import Error
|
3
|
from typing import List, Dict
|
4
|
|
5
|
from ..constants import *
|
6
|
from ..dao.repository import IRepository
|
7
|
|
8
|
|
9
|
class DBManager(IRepository):
|
10
|
|
11
|
@staticmethod
|
12
|
def create(sql: str, values: List, usages: Dict[int, bool] = None) -> bool:
|
13
|
"""
|
14
|
Creates an item in database
|
15
|
|
16
|
:param sql: SQL query for creation (parameter values are replaced by question mark)
|
17
|
:param values: list of parameter values
|
18
|
:param usages: dictionary of usages IDs which certificate use
|
19
|
Dictionary[Integer, Boolean]
|
20
|
- Key = ID (CA_ID=1, SSL_ID=2, SIGNATURE_ID=3, AUTHENTICATION_ID=4)
|
21
|
- Value = used=True/unused=False
|
22
|
|
23
|
:return: the result of whether the creation was successful
|
24
|
"""
|
25
|
|
26
|
conn = None
|
27
|
try:
|
28
|
conn = sqlite3.connect(DATABASE_FILE)
|
29
|
c = conn.cursor()
|
30
|
c.execute(sql, values)
|
31
|
|
32
|
if usages is not None:
|
33
|
for usage_id, usage_value in usages:
|
34
|
if usage_value:
|
35
|
c.execute(f"INSERT INTO {TAB_CERTIFICATE_USAGES} ({COL_CERTIFICATE_ID},{COL_USAGE_TYPE_ID}) "
|
36
|
f"VALUES (?,?)",
|
37
|
[c.lastrowid, usage_id])
|
38
|
|
39
|
conn.commit()
|
40
|
c.close()
|
41
|
except Error as e:
|
42
|
print(e)
|
43
|
return False
|
44
|
finally:
|
45
|
if conn:
|
46
|
conn.close()
|
47
|
return True
|
48
|
|
49
|
@staticmethod
|
50
|
def read(sql: str, item_id: int = None):
|
51
|
"""
|
52
|
Reads (selecs) an item or items from database
|
53
|
|
54
|
:param sql: SQL query for selection (parameter values are replaced by question mark)
|
55
|
:param item_id: ID of specific item
|
56
|
|
57
|
:return: list of rows selected from database
|
58
|
"""
|
59
|
|
60
|
conn = None
|
61
|
try:
|
62
|
conn = sqlite3.connect(DATABASE_FILE)
|
63
|
c = conn.cursor()
|
64
|
|
65
|
if item_id is not None:
|
66
|
c.execute(sql, [item_id])
|
67
|
else:
|
68
|
c.execute(sql)
|
69
|
|
70
|
records = c.fetchall()
|
71
|
c.close()
|
72
|
except Error as e:
|
73
|
print(e)
|
74
|
return []
|
75
|
finally:
|
76
|
if conn:
|
77
|
conn.close()
|
78
|
return records
|
79
|
|
80
|
@staticmethod
|
81
|
def update(sql: str, item_id: int, values: List, usages: Dict[int, bool] = None) -> bool:
|
82
|
"""
|
83
|
Updates an item in database
|
84
|
|
85
|
:param sql: SQL query for creation (parameter values are replaced by question mark)
|
86
|
:param item_id: ID of specific item
|
87
|
:param values: list of parameter values
|
88
|
:param usages: dictionary of usages IDs which certificate use
|
89
|
Dictionary[Integer, Boolean]
|
90
|
- Key = ID (CA_ID=1, SSL_ID=2, SIGNATURE_ID=3, AUTHENTICATION_ID=4)
|
91
|
- Value = used=True/unused=False
|
92
|
|
93
|
:return: the result of whether the updation was successful
|
94
|
"""
|
95
|
|
96
|
conn = None
|
97
|
try:
|
98
|
values.append(item_id)
|
99
|
|
100
|
conn = sqlite3.connect(DATABASE_FILE)
|
101
|
c = conn.cursor()
|
102
|
c.execute(sql, values)
|
103
|
|
104
|
if usages is not None:
|
105
|
for usage_id, usage_value in usages:
|
106
|
c.execute(f"DELETE FROM {TAB_CERTIFICATE_USAGES} "
|
107
|
f"WHERE {COL_CERTIFICATE_ID} = ? AND {COL_USAGE_TYPE_ID} = ?",
|
108
|
[item_id, usage_id])
|
109
|
|
110
|
if usage_value:
|
111
|
c.execute(f"INSERT INTO {TAB_CERTIFICATE_USAGES} ({COL_CERTIFICATE_ID},{COL_USAGE_TYPE_ID}) "
|
112
|
f"VALUES (?,?)",
|
113
|
[item_id, usage_id])
|
114
|
|
115
|
conn.commit()
|
116
|
c.close()
|
117
|
except Error as e:
|
118
|
print(e)
|
119
|
return False
|
120
|
finally:
|
121
|
if conn:
|
122
|
conn.close()
|
123
|
return True
|
124
|
|
125
|
@staticmethod
|
126
|
def delete(sql: str, item_id: int, delete_usages: bool) -> bool:
|
127
|
"""
|
128
|
Deletes an item from database
|
129
|
|
130
|
:param sql: SQL query for selection (parameter values are replaced by question mark)
|
131
|
:param item_id: ID of specific item
|
132
|
:param delete_usages: flag specifying whether to erase rows with specific id
|
133
|
from the table of usages (CertificateUsages)
|
134
|
|
135
|
:return: the result of whether the deletion was successful
|
136
|
"""
|
137
|
|
138
|
conn = None
|
139
|
try:
|
140
|
conn = sqlite3.connect(DATABASE_FILE)
|
141
|
c = conn.cursor()
|
142
|
|
143
|
if delete_usages:
|
144
|
c.execute(f"DELETE FROM {TAB_CERTIFICATE_USAGES} WHERE {COL_CERTIFICATE_ID} = ?", [item_id])
|
145
|
|
146
|
c.execute(sql, [item_id])
|
147
|
conn.commit()
|
148
|
c.close()
|
149
|
except Error as e:
|
150
|
print(e)
|
151
|
return False
|
152
|
finally:
|
153
|
if conn:
|
154
|
conn.close()
|
155
|
return True
|