Projekt

Obecné

Profil

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

    
4
from injector import inject
5

    
6
from src.model.private_key import PrivateKey
7
from src.constants import *
8

    
9

    
10
class PrivateKeyRepository:
11

    
12
    @inject
13
    def __init__(self, connection: Connection):
14
        """
15
        Constructor of the PrivateKeyRepository object
16

    
17
        :param connection: Instance of the Connection object
18
        :param cursor: Instance of the Cursor object
19
        """
20

    
21
        self.connection = connection
22
        self.cursor = connection.cursor()
23

    
24
    def create(self, private_key: PrivateKey):
25
        """
26
        Creates a private key.
27

    
28
        :param private_key: Instance of the PrivateKey object
29

    
30
        :return: the result of whether the creation was successful
31
        """
32

    
33
        try:
34
            sql = (f"INSERT INTO {TAB_PRIVATE_KEYS} "
35
                   f"({COL_PRIVATE_KEY},"
36
                   f"{COL_PASSWORD}) "
37
                   f"VALUES(?,?)")
38
            values = [private_key.private_key,
39
                      private_key.password]
40
            self.cursor.execute(sql, values)
41
            last_id = self.cursor.lastrowid
42
            self.connection.commit()
43
        except Error as e:
44
            print(e)
45
            return None
46

    
47
        return last_id
48

    
49
    def read(self, private_key_id: int):
50
        """
51
        Reads (selects) a private key.
52

    
53
        :param private_key_id: ID of specific private key
54

    
55
        :return: instance of the PrivateKey object
56
        """
57

    
58
        try:
59
            sql = (f"SELECT * FROM {TAB_PRIVATE_KEYS} "
60
                   f"WHERE {COL_ID} = ?")
61
            values = [private_key_id]
62
            self.cursor.execute(sql, values)
63
            private_key_row = self.cursor.fetchone()
64

    
65
            private_key: PrivateKey = PrivateKey(private_key_row[0],
66
                                                 private_key_row[1],
67
                                                 private_key_row[2])
68
        except Error as e:
69
            print(e)
70
            return None
71

    
72
        if len(private_key_row) > 0:
73
            return private_key
74
        else:
75
            return None
76

    
77
    def read_all(self):
78
        """
79
        Reads (selects) all private keys.
80

    
81
        :return: list of private keys
82
        """
83

    
84
        try:
85
            sql = f"SELECT * FROM {TAB_PRIVATE_KEYS}"
86
            self.cursor.execute(sql)
87
            private_key_rows = self.cursor.fetchall()
88

    
89
            private_keys: List[PrivateKey] = []
90
            for private_key_row in private_key_rows:
91
                private_keys.append(PrivateKey(private_key_row[0],
92
                                               private_key_row[1],
93
                                               private_key_row[2]))
94
        except Error as e:
95
            print(e)
96
            return None
97

    
98
        if len(private_keys) > 0:
99
            return private_keys
100
        else:
101
            return None
102

    
103
    def update(self, private_key_id: int, private_key: PrivateKey) -> bool:
104
        """
105
        Updates a private key.
106

    
107
        :param private_key_id: ID of specific private key
108
        :param private_key: Instance of the PrivateKey object
109

    
110
        :return: the result of whether the updation was successful
111
        """
112

    
113
        try:
114
            sql = (f"UPDATE {TAB_PRIVATE_KEYS} "
115
                   f"SET {COL_PRIVATE_KEY} = ?, "
116
                   f"{COL_PASSWORD} = ? "
117
                   f"WHERE {COL_ID} = ?")
118
            values = [private_key.private_key,
119
                      private_key.password,
120
                      private_key_id]
121
            self.cursor.execute(sql, values)
122
            self.connection.commit()
123
        except Error as e:
124
            print(e)
125
            return False
126

    
127
        return True
128

    
129
    def delete(self, private_key_id: int) -> bool:
130
        """
131
        Deletes a private key
132

    
133
        :param private_key_id: ID of specific private key
134

    
135
        :return: the result of whether the deletion was successful
136
        """
137

    
138
        try:
139
            sql = (f"DELETE FROM {TAB_PRIVATE_KEYS} "
140
                   f"WHERE {COL_ID} = ?")
141
            values = [private_key_id]
142
            self.cursor.execute(sql, values)
143
            self.connection.commit()
144
        except Error as e:
145
            print(e)
146
            return False
147

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