Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 1636aefe

Přidáno uživatelem David Friesecký před asi 4 roky(ů)

Re #8471 - Edited files for DB communication

Zobrazit rozdíly:

src/impl/certificate_repository_impl.py
1
from typing import List, Dict
2

  
1 3
from ..db_objects.certificate import Certificate
2 4
from ..dao.repository import IRepository
3 5
from db_manager import DBManager
......
7 9
class CertificateRepositoryImpl(IRepository):
8 10

  
9 11
    def create(self, common_name: str, valid_from: str, valid_to: str, pem_data: str,
10
               type_id: int, private_key_id: int, usage_id: int) -> bool:
11
        sql = (f"INSERT INTO {TAB_CERTIFICATE} ({COL_COMMON_NAME},{COL_VALID_FROM},{COL_VALID_TO},{COL_PEM_DATA},"
12
               f"{COL_TYPE_ID},{COL_PRIVATE_KEY_ID},{COL_USAGE_ID})"
12
               private_key_id: int, type_id: int, usages: Dict[int, bool]) -> bool:
13
        sql = (f"INSERT INTO {TAB_CERTIFICATES} ({COL_COMMON_NAME},{COL_VALID_FROM},{COL_VALID_TO},{COL_PEM_DATA},"
14
               f"{COL_PRIVATE_KEY_ID},{COL_TYPE_ID})"
13 15
               f"VALUES(?,?,?,?,?,?,?)")
14
        result = DBManager.create(sql, common_name, valid_from, valid_to, pem_data, type_id, private_key_id, usage_id)
16
        result = DBManager.create(sql, [common_name, valid_from, valid_to, pem_data, private_key_id, type_id], usages)
15 17
        return result
16 18

  
17 19
    def read(self, certificate_id: int = None):
......
20 22
        if certificate_id is not None:
21 23
            sql_extend = f" WHERE {COL_ID} = ?"
22 24

  
23
        sql = f"SELECT * FROM {TAB_CERTIFICATE}{sql_extend}"
24
        rows = DBManager.read(sql, certificate_id)
25
        sql = f"SELECT * FROM {TAB_CERTIFICATES}{sql_extend}"
26
        certificate_rows = DBManager.read(sql, certificate_id)
25 27

  
26 28
        certificates: list = []
27 29

  
28
        for row in rows:
29
            certificates.append(Certificate(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7]))
30
        for certificate_row in certificate_rows:
31
            sql = f"SELECT * FROM {TAB_CERTIFICATE_USAGES} WHERE {COL_CERTIFICATE_ID} = ?"
32
            usage_rows = DBManager.read(sql, certificate_row[0])
33

  
34
            usage_dict: Dict[int, bool] = {}
35
            for usage_row in usage_rows:
36
                usage_dict[usage_row[1]] = True
37

  
38
            certificates.append(Certificate(certificate_row[0],
39
                                            certificate_row[1],
40
                                            certificate_row[2],
41
                                            certificate_row[3],
42
                                            certificate_row[4],
43
                                            certificate_row[5],
44
                                            certificate_row[6],
45
                                            usage_dict))
30 46

  
31 47
        if certificate_id is not None:
32 48
            return certificates[0]
......
34 50
        return certificates
35 51

  
36 52
    def update(self, certificate_id: int, common_name: str = None, valid_from: str = None, valid_to: str = None,
37
               pem_data: str = None, type_id: int = None, private_key_id: int = None, usage_id: int = None) -> bool:
53
               pem_data: str = None, private_key_id: int = None, type_id: int = None,
54
               usages: Dict[int, bool] = None) -> bool:
38 55
        updated_list = []
39 56
        values = []
40 57
        if common_name is not None:
......
49 66
        if pem_data is not None:
50 67
            updated_list.append(f"{COL_PEM_DATA} = ?")
51 68
            values.append(pem_data)
52
        if type_id is not None:
53
            updated_list.append(f"{COL_TYPE_ID} = ?")
54
            values.append(type_id)
55 69
        if private_key_id is not None:
56 70
            updated_list.append(f"{COL_PRIVATE_KEY_ID} = ?")
57 71
            values.append(private_key_id)
58
        if usage_id is not None:
59
            updated_list.append(f"{COL_USAGE_ID} = ?")
60
            values.append(usage_id)
61

  
62
        values.append(certificate_id)
72
        if type_id is not None:
73
            updated_list.append(f"{COL_TYPE_ID} = ?")
74
            values.append(type_id)
63 75

  
64 76
        updated_str = ", ".join(updated_list)
65
        sql = f"UPDATE {TAB_CERTIFICATE} SET {updated_str} WHERE {COL_ID} = ?"
66
        result = DBManager.update(sql, values)
77
        sql = f"UPDATE {TAB_CERTIFICATES} SET {updated_str} WHERE {COL_ID} = ?"
78
        result = DBManager.update(sql, certificate_id, values, usages)
67 79
        return result
68 80

  
69 81
    def delete(self, certificate_id: int) -> bool:
70
        sql = f"DELETE FROM {TAB_CERTIFICATE} WHERE {COL_ID} = ?"
71
        result = DBManager.delete(sql, certificate_id)
82
        sql = f"DELETE FROM {TAB_CERTIFICATES} WHERE {COL_ID} = ?"
83
        result = DBManager.delete(sql, certificate_id, True)
72 84
        return result

Také k dispozici: Unified diff