Projekt

Obecné

Profil

Stáhnout (8.28 KB) Statistiky
| Větev: | Tag: | Revize:
1
from sqlite3 import Connection, Error, DatabaseError, IntegrityError, ProgrammingError, OperationalError, NotSupportedError
2
from typing import List
3

    
4
from injector import inject
5

    
6
from src.exceptions.database_exception import DatabaseException
7
from src.model.private_key import PrivateKey
8
from src.constants import *
9
from src.utils.logger import Logger
10

    
11
INTEGRITY_ERROR_MSG = "Database relational integrity corrupted."
12
PROGRAMMING_ERROR_MSG = "Exception raised for programming errors (etc. SQL statement)."
13
OPERATIONAL_ERROR_MSG = "Exception raised for errors that are related to the database’s operation."
14
NOT_SUPPORTED_ERROR_MSG = "Method or database API was used which is not supported by the database"
15
DATABASE_ERROR_MSG = "Unknown exception that are related to the database."
16
ERROR_MSG = "Unknown exception."
17

    
18

    
19
class PrivateKeyRepository:
20

    
21
    @inject
22
    def __init__(self, connection: Connection):
23
        """
24
        Constructor of the PrivateKeyRepository object
25

    
26
        :param connection: Instance of the Connection object
27
        :param cursor: Instance of the Cursor object
28
        """
29

    
30
        self.connection = connection
31
        self.cursor = connection.cursor()
32

    
33
    def create(self, private_key: PrivateKey):
34
        """
35
        Creates a private key.
36

    
37
        :param private_key: Instance of the PrivateKey object
38

    
39
        :return: the result of whether the creation was successful
40
        """
41

    
42
        try:
43
            sql = (f"INSERT INTO {TAB_PRIVATE_KEYS} "
44
                   f"({COL_PRIVATE_KEY},"
45
                   f"{COL_PASSWORD}) "
46
                   f"VALUES(?,?)")
47
            values = [private_key.private_key,
48
                      private_key.password]
49
            self.cursor.execute(sql, values)
50
            last_id = self.cursor.lastrowid
51
            self.connection.commit()
52
        except IntegrityError:
53
            Logger.error(INTEGRITY_ERROR_MSG)
54
            raise DatabaseException(INTEGRITY_ERROR_MSG)
55
        except ProgrammingError:
56
            Logger.error(PROGRAMMING_ERROR_MSG)
57
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
58
        except OperationalError:
59
            Logger.error(OPERATIONAL_ERROR_MSG)
60
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
61
        except NotSupportedError:
62
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
63
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
64
        except DatabaseError:
65
            Logger.error(DATABASE_ERROR_MSG)
66
            raise DatabaseException(DATABASE_ERROR_MSG)
67
        except Error:
68
            Logger.error(ERROR_MSG)
69
            raise DatabaseException(ERROR_MSG)
70

    
71
        return last_id
72

    
73
    def read(self, private_key_id: int):
74
        """
75
        Reads (selects) a private key.
76

    
77
        :param private_key_id: ID of specific private key
78

    
79
        :return: instance of the PrivateKey object
80
        """
81

    
82
        try:
83
            sql = (f"SELECT * FROM {TAB_PRIVATE_KEYS} "
84
                   f"WHERE {COL_ID} = ?")
85
            values = [private_key_id]
86
            self.cursor.execute(sql, values)
87
            private_key_row = self.cursor.fetchone()
88

    
89
            if private_key_row is None:
90
                return None
91

    
92
            private_key: PrivateKey = PrivateKey(private_key_row[0],
93
                                                 private_key_row[1],
94
                                                 private_key_row[2])
95
        except IntegrityError:
96
            Logger.error(INTEGRITY_ERROR_MSG)
97
            raise DatabaseException(INTEGRITY_ERROR_MSG)
98
        except ProgrammingError:
99
            Logger.error(PROGRAMMING_ERROR_MSG)
100
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
101
        except OperationalError:
102
            Logger.error(OPERATIONAL_ERROR_MSG)
103
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
104
        except NotSupportedError:
105
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
106
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
107
        except DatabaseError:
108
            Logger.error(DATABASE_ERROR_MSG)
109
            raise DatabaseException(DATABASE_ERROR_MSG)
110
        except Error:
111
            Logger.error(ERROR_MSG)
112
            raise DatabaseException(ERROR_MSG)
113

    
114
        return private_key
115

    
116
    def read_all(self):
117
        """
118
        Reads (selects) all private keys.
119

    
120
        :return: list of private keys
121
        """
122

    
123
        try:
124
            sql = f"SELECT * FROM {TAB_PRIVATE_KEYS}"
125
            self.cursor.execute(sql)
126
            private_key_rows = self.cursor.fetchall()
127

    
128
            private_keys: List[PrivateKey] = []
129
            for private_key_row in private_key_rows:
130
                private_keys.append(PrivateKey(private_key_row[0],
131
                                               private_key_row[1],
132
                                               private_key_row[2]))
133
        except IntegrityError:
134
            Logger.error(INTEGRITY_ERROR_MSG)
135
            raise DatabaseException(INTEGRITY_ERROR_MSG)
136
        except ProgrammingError:
137
            Logger.error(PROGRAMMING_ERROR_MSG)
138
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
139
        except OperationalError:
140
            Logger.error(OPERATIONAL_ERROR_MSG)
141
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
142
        except NotSupportedError:
143
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
144
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
145
        except DatabaseError:
146
            Logger.error(DATABASE_ERROR_MSG)
147
            raise DatabaseException(DATABASE_ERROR_MSG)
148
        except Error:
149
            Logger.error(ERROR_MSG)
150
            raise DatabaseException(ERROR_MSG)
151

    
152
        return private_keys
153

    
154
    def update(self, private_key_id: int, private_key: PrivateKey) -> bool:
155
        """
156
        Updates a private key.
157

    
158
        :param private_key_id: ID of specific private key
159
        :param private_key: Instance of the PrivateKey object
160

    
161
        :return: the result of whether the updation was successful
162
        """
163

    
164
        try:
165
            sql = (f"UPDATE {TAB_PRIVATE_KEYS} "
166
                   f"SET {COL_PRIVATE_KEY} = ?, "
167
                   f"{COL_PASSWORD} = ? "
168
                   f"WHERE {COL_ID} = ?")
169
            values = [private_key.private_key,
170
                      private_key.password,
171
                      private_key_id]
172
            self.cursor.execute(sql, values)
173
            self.connection.commit()
174
        except IntegrityError:
175
            Logger.error(INTEGRITY_ERROR_MSG)
176
            raise DatabaseException(INTEGRITY_ERROR_MSG)
177
        except ProgrammingError:
178
            Logger.error(PROGRAMMING_ERROR_MSG)
179
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
180
        except OperationalError:
181
            Logger.error(OPERATIONAL_ERROR_MSG)
182
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
183
        except NotSupportedError:
184
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
185
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
186
        except DatabaseError:
187
            Logger.error(DATABASE_ERROR_MSG)
188
            raise DatabaseException(DATABASE_ERROR_MSG)
189
        except Error:
190
            Logger.error(ERROR_MSG)
191
            raise DatabaseException(ERROR_MSG)
192

    
193
        return self.cursor.rowcount > 0
194

    
195
    def delete(self, private_key_id: int) -> bool:
196
        """
197
        Deletes a private key
198

    
199
        :param private_key_id: ID of specific private key
200

    
201
        :return: the result of whether the deletion was successful
202
        """
203

    
204
        try:
205
            sql = (f"DELETE FROM {TAB_PRIVATE_KEYS} "
206
                   f"WHERE {COL_ID} = ?")
207
            values = [private_key_id]
208
            self.cursor.execute(sql, values)
209
            self.connection.commit()
210
        except IntegrityError:
211
            Logger.error(INTEGRITY_ERROR_MSG)
212
            raise DatabaseException(INTEGRITY_ERROR_MSG)
213
        except ProgrammingError:
214
            Logger.error(PROGRAMMING_ERROR_MSG)
215
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
216
        except OperationalError:
217
            Logger.error(OPERATIONAL_ERROR_MSG)
218
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
219
        except NotSupportedError:
220
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
221
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
222
        except DatabaseError:
223
            Logger.error(DATABASE_ERROR_MSG)
224
            raise DatabaseException(DATABASE_ERROR_MSG)
225
        except Error:
226
            Logger.error(ERROR_MSG)
227
            raise DatabaseException(ERROR_MSG)
228

    
229
        return self.cursor.rowcount > 0
(3-3/3)