Projekt

Obecné

Profil

Stáhnout (3.21 KB) Statistiky
| Větev: | Tag: | Revize:
1
from sqlite3 import Connection, Cursor, Error
2

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

    
6

    
7
class PrivateKeyRepository:
8

    
9
    def __init__(self, connection: Connection, cursor: Cursor):
10
        """
11
        Constructor of the PrivateKeyRepository object
12

    
13
        :param connection: Instance of the Connection object
14
        :param cursor: Instance of the Cursor object
15
        """
16

    
17
        self.connection = connection
18
        self.cursor = cursor
19

    
20
    def create(self, private_key: PrivateKey) -> bool:
21
        """
22
        Creates a private key.
23

    
24
        :param private_key: Instance of the PrivateKey object
25

    
26
        :return: the result of whether the creation was successful
27
        """
28

    
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
42

    
43
        return True
44

    
45
    def read(self, private_key_id: int):
46
        """
47
        Reads (selects) a private key.
48

    
49
        :param private_key_id: ID of specific private key
50

    
51
        :return: instance of the PrivateKey object
52
        """
53

    
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()
60

    
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
67

    
68
        return private_key
69

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

    
74
        :param private_key_id: ID of specific private key
75
        :param private_key: Instance of the PrivateKey object
76

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

    
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
94

    
95
    def delete(self, private_key_id: int) -> bool:
96
        """
97
        Deletes a private key
98

    
99
        :param private_key_id: ID of specific private key
100

    
101
        :return: the result of whether the deletion was successful
102
        """
103

    
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
(2-2/2)