Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 9d3eded8

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

Re #8471 - Simplification of CRUD functions
- conversion source of private key repository to one file
- added comments

Zobrazit rozdíly:

src/dao/private_key_repository.py
1
from ..db_objects.private_key import PrivateKey
2
from ..dao.repository import IRepository
3
from db_manager import DBManager
4
from ..constants import *
1
from sqlite3 import Connection, Cursor, Error
5 2

  
3
from src.db_objects.private_key import PrivateKey
4
from src.constants import *
6 5

  
7
class PrivateKeyRepositoryImpl(IRepository):
8 6

  
9
    def create(self, private_key: str, password: str) -> bool:
10
        """
11
        Creates a private key
7
class PrivateKeyRepository:
12 8

  
13
        :param private_key: private key
14
        :param password: passphrase for encode private key
9
    def __init__(self, connection: Connection, cursor: Cursor):
10
        """
11
        Constructor of the PrivateKeyRepository object
15 12

  
16
        :return: the result of whether the creation was successful
13
        :param connection: Instance of the Connection object
14
        :param cursor: Instance of the Cursor object
17 15
        """
18 16

  
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
17
        self.connection = connection
18
        self.cursor = cursor
22 19

  
23
    def read(self, private_key_id: int = None):
20
    def create(self, private_key: PrivateKey) -> bool:
24 21
        """
25
        Reads (selects) a private key
22
        Creates a private key.
26 23

  
27
        :param private_key_id: ID of specific private
24
        :param private_key: Instance of the PrivateKey object
28 25

  
29
        :return: instance of PrivateKey class if ID was used otherwise list of this instances
26
        :return: the result of whether the creation was successful
30 27
        """
31 28

  
32
        sql_extend = ""
29
        try:
30
            sql = (f"INSERT INTO {TAB_PRIVATE_KEYS} "
31
                   f"({COL_PRIVATE_KEY},"
32
                   f"{COL_PASSWORD}) "
33
                   f"VALUES(?,?)")
34
            values = [private_key.private_key,
35
                      private_key.password]
36
            self.cursor.execute(sql, values)
37
            self.connection.commit()
38

  
39
        except Error as e:
40
            print(e)
41
            return False
33 42

  
34
        if private_key_id is not None:
35
            sql_extend = f" WHERE {COL_ID} = ?"
43
        return True
36 44

  
37
        sql = f"SELECT * FROM {TAB_PRIVATE_KEYS}{sql_extend}"
38
        rows = DBManager.read(sql, private_key_id)
45
    def read(self, private_key_id: int):
46
        """
47
        Reads (selects) a private key.
39 48

  
40
        private_keys: list = []
49
        :param private_key_id: ID of specific private key
41 50

  
42
        for row in rows:
43
            private_keys.append(PrivateKey(row))
51
        :return: instance of the PrivateKey object
52
        """
44 53

  
45
        if private_key_id is not None:
46
            return private_keys[0]
54
        try:
55
            sql = (f"SELECT * FROM {TAB_PRIVATE_KEYS} "
56
                   f"WHERE {COL_ID} = ?")
57
            values = [private_key_id]
58
            self.cursor.execute(sql, values)
59
            private_key_row = self.cursor.fetchall()
47 60

  
48
        return private_keys
61
            private_key: PrivateKey = PrivateKey(private_key_row[0],
62
                                                 private_key_row[1],
63
                                                 private_key_row[2])
64
        except Error as e:
65
            print(e)
66
            return None
49 67

  
50
    def update(self, private_key_id: int, private_key: str = None, password: str = None) -> bool:
68
        return private_key
69

  
70
    def update(self, private_key_id: int, private_key: PrivateKey) -> bool:
51 71
        """
52
        Updates a private key
72
        Updates a private key.
53 73

  
54 74
        :param private_key_id: ID of specific private key
55
        :param private_key: private key
56
        :param password: passphrase for encode private key
75
        :param private_key: Instance of the PrivateKey object
57 76

  
58 77
        :return: the result of whether the updation was successful
59 78
        """
60 79

  
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
80
        try:
81
            sql = (f"UPDATE {TAB_PRIVATE_KEYS} "
82
                   f"SET {COL_PRIVATE_KEY} = ?, "
83
                   f"{COL_PASSWORD} = ? "
84
                   f"WHERE {COL_ID} = ?")
85
            values = [private_key.private_key,
86
                      private_key.password]
87
            self.cursor.execute(sql, values)
88
            self.connection.commit()
89
        except Error as e:
90
            print(e)
91
            return False
92

  
93
        return True
74 94

  
75 95
    def delete(self, private_key_id: int) -> bool:
76 96
        """
......
81 101
        :return: the result of whether the deletion was successful
82 102
        """
83 103

  
84
        sql = f"DELETE FROM {TAB_PRIVATE_KEYS} WHERE {COL_ID} = ?"
85
        result = DBManager.delete(sql, private_key_id, False)
86
        return result
104
        try:
105
            sql = (f"DELETE FROM {TAB_PRIVATE_KEYS} "
106
                   f"WHERE {COL_ID} = ?")
107
            values = [private_key_id]
108
            self.cursor.execute(sql, values)
109
            self.connection.commit()
110
        except Error as e:
111
            print(e)
112
            return False
113

  
114
        return True

Také k dispozici: Unified diff