Projekt

Obecné

Profil

« Předchozí | Další » 

Revize b3c80ccb

Přidáno uživatelem David Friesecký před téměř 4 roky(ů)

Re #8570 - Added logs

Zobrazit rozdíly:

src/dao/certificate_repository.py
1
from sqlite3 import Connection, Error
1
from sqlite3 import Connection, Error, DatabaseError, IntegrityError, ProgrammingError, OperationalError, NotSupportedError
2 2
from typing import Dict, List
3 3

  
4 4
from src.exceptions.database_exception import DatabaseException
......
7 7
from src.model.certificate import Certificate
8 8
from src.utils.logger import Logger
9 9

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

  
10 17

  
11 18
class CertificateRepository:
12 19

  
......
30 37
        :return: the result of whether the creation was successful
31 38
        """
32 39

  
40
        Logger.debug("Function launched.")
41

  
33 42
        try:
34 43
            sql = (f"INSERT INTO {TAB_CERTIFICATES} "
35 44
                   f"({COL_COMMON_NAME},"
......
66 75
                        values = [last_id, usage_id]
67 76
                        self.cursor.execute(sql, values)
68 77
                        self.connection.commit()
69
        except Error as e:
70
            raise DatabaseException(e)
78
        except IntegrityError:
79
            Logger.error(INTEGRITY_ERROR_MSG)
80
            raise DatabaseException(INTEGRITY_ERROR_MSG)
81
        except ProgrammingError:
82
            Logger.error(PROGRAMMING_ERROR_MSG)
83
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
84
        except OperationalError:
85
            Logger.error(OPERATIONAL_ERROR_MSG)
86
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
87
        except NotSupportedError:
88
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
89
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
90
        except DatabaseError:
91
            Logger.error(DATABASE_ERROR_MSG)
92
            raise DatabaseException(DATABASE_ERROR_MSG)
93
        except Error:
94
            Logger.error(ERROR_MSG)
95
            raise DatabaseException(ERROR_MSG)
71 96

  
72 97
        return last_id
73 98

  
......
80 105
        :return: instance of the Certificate object
81 106
        """
82 107

  
108
        Logger.debug("Function launched.")
109

  
83 110
        try:
84 111
            sql = (f"SELECT * FROM {TAB_CERTIFICATES} "
85 112
                   f"WHERE {COL_ID} = ?")
......
110 137
                                                   usage_dict,
111 138
                                                   certificate_row[5],
112 139
                                                   certificate_row[6])
113
        except Error as e:
114
            raise DatabaseException(e)
140
        except IntegrityError:
141
            Logger.error(INTEGRITY_ERROR_MSG)
142
            raise DatabaseException(INTEGRITY_ERROR_MSG)
143
        except ProgrammingError:
144
            Logger.error(PROGRAMMING_ERROR_MSG)
145
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
146
        except OperationalError:
147
            Logger.error(OPERATIONAL_ERROR_MSG)
148
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
149
        except NotSupportedError:
150
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
151
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
152
        except DatabaseError:
153
            Logger.error(DATABASE_ERROR_MSG)
154
            raise DatabaseException(DATABASE_ERROR_MSG)
155
        except Error:
156
            Logger.error(ERROR_MSG)
157
            raise DatabaseException(ERROR_MSG)
115 158

  
116 159
        return certificate
117 160

  
......
124 167
        :return: list of certificates
125 168
        """
126 169

  
170
        Logger.debug("Function launched.")
171

  
127 172
        try:
128 173
            sql_extension = ""
129 174
            values = []
......
159 204
                                                usage_dict,
160 205
                                                certificate_row[5],
161 206
                                                certificate_row[6]))
162
        except Error as e:
163
            raise DatabaseException(e)
207
        except IntegrityError:
208
            Logger.error(INTEGRITY_ERROR_MSG)
209
            raise DatabaseException(INTEGRITY_ERROR_MSG)
210
        except ProgrammingError:
211
            Logger.error(PROGRAMMING_ERROR_MSG)
212
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
213
        except OperationalError:
214
            Logger.error(OPERATIONAL_ERROR_MSG)
215
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
216
        except NotSupportedError:
217
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
218
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
219
        except DatabaseError:
220
            Logger.error(DATABASE_ERROR_MSG)
221
            raise DatabaseException(DATABASE_ERROR_MSG)
222
        except Error:
223
            Logger.error(ERROR_MSG)
224
            raise DatabaseException(ERROR_MSG)
164 225

  
165 226
        return certificates
166 227

  
......
176 237
        :return: the result of whether the updation was successful
177 238
        """
178 239

  
240
        Logger.debug("Function launched.")
241

  
179 242
        try:
180 243
            sql = (f"UPDATE {TAB_CERTIFICATES} "
181 244
                   f"SET {COL_COMMON_NAME} = ?, "
......
213 276
                    values = [certificate_id, usage_id]
214 277
                    self.cursor.execute(sql, values)
215 278
                    self.connection.commit()
216
        except Error as e:
217
            raise DatabaseException(e)
279
        except IntegrityError:
280
            Logger.error(INTEGRITY_ERROR_MSG)
281
            raise DatabaseException(INTEGRITY_ERROR_MSG)
282
        except ProgrammingError:
283
            Logger.error(PROGRAMMING_ERROR_MSG)
284
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
285
        except OperationalError:
286
            Logger.error(OPERATIONAL_ERROR_MSG)
287
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
288
        except NotSupportedError:
289
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
290
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
291
        except DatabaseError:
292
            Logger.error(DATABASE_ERROR_MSG)
293
            raise DatabaseException(DATABASE_ERROR_MSG)
294
        except Error:
295
            Logger.error(ERROR_MSG)
296
            raise DatabaseException(ERROR_MSG)
218 297

  
219 298
        return self.cursor.rowcount > 0
220 299

  
......
227 306
        :return: the result of whether the deletion was successful
228 307
        """
229 308

  
309
        Logger.debug("Function launched.")
310

  
230 311
        try:
231 312
            sql = (f"DELETE FROM {TAB_CERTIFICATES} "
232 313
                   f"WHERE {COL_ID} = ?")
233 314
            values = [certificate_id]
234 315
            self.cursor.execute(sql, values)
235 316
            self.connection.commit()
236
        except Error as e:
237
            raise DatabaseException(e)
317
        except IntegrityError:
318
            Logger.error(INTEGRITY_ERROR_MSG)
319
            raise DatabaseException(INTEGRITY_ERROR_MSG)
320
        except ProgrammingError:
321
            Logger.error(PROGRAMMING_ERROR_MSG)
322
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
323
        except OperationalError:
324
            Logger.error(OPERATIONAL_ERROR_MSG)
325
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
326
        except NotSupportedError:
327
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
328
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
329
        except DatabaseError:
330
            Logger.error(DATABASE_ERROR_MSG)
331
            raise DatabaseException(DATABASE_ERROR_MSG)
332
        except Error:
333
            Logger.error(ERROR_MSG)
334
            raise DatabaseException(ERROR_MSG)
238 335

  
239 336
        return self.cursor.rowcount > 0
240 337

  
......
252 349
            sqlite3.Error if an exception is thrown
253 350
        """
254 351

  
352
        Logger.debug("Function launched.")
353

  
255 354
        try:
256 355
            if revocation_date != "" and revocation_reason == "":
257 356
                revocation_reason = REV_REASON_UNSPECIFIED
......
267 366
                      certificate_id]
268 367
            self.cursor.execute(sql, values)
269 368
            self.connection.commit()
270
        except Error as e:
271
            raise DatabaseException(e)
369
        except IntegrityError:
370
            Logger.error(INTEGRITY_ERROR_MSG)
371
            raise DatabaseException(INTEGRITY_ERROR_MSG)
372
        except ProgrammingError:
373
            Logger.error(PROGRAMMING_ERROR_MSG)
374
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
375
        except OperationalError:
376
            Logger.error(OPERATIONAL_ERROR_MSG)
377
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
378
        except NotSupportedError:
379
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
380
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
381
        except DatabaseError:
382
            Logger.error(DATABASE_ERROR_MSG)
383
            raise DatabaseException(DATABASE_ERROR_MSG)
384
        except Error:
385
            Logger.error(ERROR_MSG)
386
            raise DatabaseException(ERROR_MSG)
272 387

  
273 388
        return self.cursor.rowcount > 0
274 389

  
......
283 398
            sqlite3.Error if an exception is thrown
284 399
        """
285 400

  
401
        Logger.debug("Function launched.")
402

  
286 403
        try:
287 404
            sql = (f"UPDATE {TAB_CERTIFICATES} "
288 405
                   f"SET {COL_REVOCATION_DATE} = '', "
......
291 408
            values = [certificate_id]
292 409
            self.cursor.execute(sql, values)
293 410
            self.connection.commit()
294
        except Error as e:
295
            raise DatabaseException(e)
411
        except IntegrityError:
412
            Logger.error(INTEGRITY_ERROR_MSG)
413
            raise DatabaseException(INTEGRITY_ERROR_MSG)
414
        except ProgrammingError:
415
            Logger.error(PROGRAMMING_ERROR_MSG)
416
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
417
        except OperationalError:
418
            Logger.error(OPERATIONAL_ERROR_MSG)
419
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
420
        except NotSupportedError:
421
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
422
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
423
        except DatabaseError:
424
            Logger.error(DATABASE_ERROR_MSG)
425
            raise DatabaseException(DATABASE_ERROR_MSG)
426
        except Error:
427
            Logger.error(ERROR_MSG)
428
            raise DatabaseException(ERROR_MSG)
296 429

  
297 430
        return self.cursor.rowcount > 0
298 431

  
......
308 441
            sqlite3.Error if an exception is thrown
309 442
        """
310 443

  
444
        Logger.debug("Function launched.")
445

  
311 446
        try:
312 447
            sql = (f"SELECT * FROM {TAB_CERTIFICATES} "
313 448
                   f"WHERE {COL_PARENT_ID} = ? AND {COL_REVOCATION_DATE} IS NOT NULL AND {COL_REVOCATION_DATE} != ''")
......
338 473
                                                usage_dict,
339 474
                                                certificate_row[5],
340 475
                                                certificate_row[6]))
341
        except Error as e:
342
            raise DatabaseException(e)
476
        except IntegrityError:
477
            Logger.error(INTEGRITY_ERROR_MSG)
478
            raise DatabaseException(INTEGRITY_ERROR_MSG)
479
        except ProgrammingError:
480
            Logger.error(PROGRAMMING_ERROR_MSG)
481
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
482
        except OperationalError:
483
            Logger.error(OPERATIONAL_ERROR_MSG)
484
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
485
        except NotSupportedError:
486
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
487
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
488
        except DatabaseError:
489
            Logger.error(DATABASE_ERROR_MSG)
490
            raise DatabaseException(DATABASE_ERROR_MSG)
491
        except Error:
492
            Logger.error(ERROR_MSG)
493
            raise DatabaseException(ERROR_MSG)
343 494

  
344 495
        return certificates
345 496

  
......
355 506
            sqlite3.Error if an exception is thrown
356 507
        """
357 508

  
509
        Logger.debug("Function launched.")
510

  
358 511
        try:
359 512
            sql = (f"SELECT * FROM {TAB_CERTIFICATES} "
360 513
                   f"WHERE {COL_PARENT_ID} = ? AND {COL_ID} != ?")
......
385 538
                                                usage_dict,
386 539
                                                certificate_row[5],
387 540
                                                certificate_row[6]))
388
        except Error as e:
389
            raise DatabaseException(e)
541
        except IntegrityError:
542
            Logger.error(INTEGRITY_ERROR_MSG)
543
            raise DatabaseException(INTEGRITY_ERROR_MSG)
544
        except ProgrammingError:
545
            Logger.error(PROGRAMMING_ERROR_MSG)
546
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
547
        except OperationalError:
548
            Logger.error(OPERATIONAL_ERROR_MSG)
549
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
550
        except NotSupportedError:
551
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
552
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
553
        except DatabaseError:
554
            Logger.error(DATABASE_ERROR_MSG)
555
            raise DatabaseException(DATABASE_ERROR_MSG)
556
        except Error:
557
            Logger.error(ERROR_MSG)
558
            raise DatabaseException(ERROR_MSG)
390 559

  
391 560
        return certificates
392 561

  
......
397 566
        :param certificate_id: target certificate ID
398 567
        :return: list of all descendants
399 568
        """
400
        def dfs(children_of, this, collection: list):
401
            for child in children_of(this.certificate_id):
402
                dfs(children_of, child, collection)
403
            collection.append(this)
404 569

  
405
        subtree_root = self.read(certificate_id)
406
        if subtree_root is None:
407
            return None
570
        Logger.debug("Function launched.")
571

  
572
        try:
573
            def dfs(children_of, this, collection: list):
574
                for child in children_of(this.certificate_id):
575
                    dfs(children_of, child, collection)
576
                collection.append(this)
577

  
578
            subtree_root = self.read(certificate_id)
579
            if subtree_root is None:
580
                return None
581

  
582
            all_certs = []
583
            dfs(self.get_all_issued_by, subtree_root, all_certs)
584
        except IntegrityError:
585
            Logger.error(INTEGRITY_ERROR_MSG)
586
            raise DatabaseException(INTEGRITY_ERROR_MSG)
587
        except ProgrammingError:
588
            Logger.error(PROGRAMMING_ERROR_MSG)
589
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
590
        except OperationalError:
591
            Logger.error(OPERATIONAL_ERROR_MSG)
592
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
593
        except NotSupportedError:
594
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
595
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
596
        except DatabaseError:
597
            Logger.error(DATABASE_ERROR_MSG)
598
            raise DatabaseException(DATABASE_ERROR_MSG)
599
        except Error:
600
            Logger.error(ERROR_MSG)
601
            raise DatabaseException(ERROR_MSG)
408 602

  
409
        all_certs = []
410
        dfs(self.get_all_issued_by, subtree_root, all_certs)
411 603
        return all_certs
412 604

  
413 605
    def get_next_id(self) -> int:
......
415 607
        Get identifier of the next certificate that will be inserted into the database
416 608
        :return: identifier of the next certificate that will be added into the database
417 609
        """
418
        # get next IDs of all tables
419
        self.cursor.execute("SELECT * FROM SQLITE_SEQUENCE")
420
        results = self.cursor.fetchall()
421

  
422
        # search for next ID in Certificates table and return it
423
        for result in results:
424
            if result[0] == TAB_CERTIFICATES:
425
                return result[1] + 1  # current last id + 1
426
        # if certificates table is not present in the query results, return 1
610

  
611
        Logger.debug("Function launched.")
612

  
613
        try:
614
            # get next IDs of all tables
615
            self.cursor.execute("SELECT * FROM SQLITE_SEQUENCE")
616
            results = self.cursor.fetchall()
617

  
618
            # search for next ID in Certificates table and return it
619
            for result in results:
620
                if result[0] == TAB_CERTIFICATES:
621
                    return result[1] + 1  # current last id + 1
622
            # if certificates table is not present in the query results, return 1
623
        except IntegrityError:
624
            Logger.error(INTEGRITY_ERROR_MSG)
625
            raise DatabaseException(INTEGRITY_ERROR_MSG)
626
        except ProgrammingError:
627
            Logger.error(PROGRAMMING_ERROR_MSG)
628
            raise DatabaseException(PROGRAMMING_ERROR_MSG)
629
        except OperationalError:
630
            Logger.error(OPERATIONAL_ERROR_MSG)
631
            raise DatabaseException(OPERATIONAL_ERROR_MSG)
632
        except NotSupportedError:
633
            Logger.error(NOT_SUPPORTED_ERROR_MSG)
634
            raise DatabaseException(NOT_SUPPORTED_ERROR_MSG)
635
        except DatabaseError:
636
            Logger.error(DATABASE_ERROR_MSG)
637
            raise DatabaseException(DATABASE_ERROR_MSG)
638
        except Error:
639
            Logger.error(ERROR_MSG)
640
            raise DatabaseException(ERROR_MSG)
641

  
427 642
        return 1

Také k dispozici: Unified diff