1 |
2c96b4a5
|
David Friesecký
|
import sqlite3
|
2 |
|
|
from sqlite3 import Error
|
3 |
|
|
|
4 |
|
|
from ..constants import DATABASE_FILE
|
5 |
|
|
from ..dao.repository import IRepository
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
class DBManager(IRepository):
|
9 |
|
|
|
10 |
25053504
|
David Friesecký
|
@staticmethod
|
11 |
|
|
def create(sql: str, *args) -> bool:
|
12 |
2c96b4a5
|
David Friesecký
|
conn = None
|
13 |
|
|
try:
|
14 |
|
|
sql: str = args[0]
|
15 |
|
|
values = args[1:]
|
16 |
|
|
|
17 |
|
|
conn = sqlite3.connect(DATABASE_FILE)
|
18 |
|
|
c = conn.cursor()
|
19 |
|
|
c.execute(sql, values)
|
20 |
|
|
conn.commit()
|
21 |
|
|
c.close()
|
22 |
|
|
except Error as e:
|
23 |
|
|
print(e)
|
24 |
|
|
return False
|
25 |
|
|
finally:
|
26 |
|
|
if conn:
|
27 |
|
|
conn.close()
|
28 |
|
|
return True
|
29 |
|
|
|
30 |
25053504
|
David Friesecký
|
@staticmethod
|
31 |
|
|
def read(sql: str, item_id: int = None):
|
32 |
2c96b4a5
|
David Friesecký
|
conn = None
|
33 |
|
|
try:
|
34 |
|
|
conn = sqlite3.connect(DATABASE_FILE)
|
35 |
|
|
c = conn.cursor()
|
36 |
|
|
|
37 |
|
|
if item_id is not None:
|
38 |
25053504
|
David Friesecký
|
list_item_id = [item_id]
|
39 |
|
|
c.execute(sql, list_item_id)
|
40 |
2c96b4a5
|
David Friesecký
|
else:
|
41 |
|
|
c.execute(sql)
|
42 |
|
|
|
43 |
|
|
records = c.fetchall()
|
44 |
|
|
c.close()
|
45 |
|
|
except Error as e:
|
46 |
|
|
print(e)
|
47 |
|
|
return []
|
48 |
|
|
finally:
|
49 |
|
|
if conn:
|
50 |
|
|
conn.close()
|
51 |
|
|
return records
|
52 |
|
|
|
53 |
25053504
|
David Friesecký
|
@staticmethod
|
54 |
|
|
def update(sql: str, *args) -> bool:
|
55 |
2c96b4a5
|
David Friesecký
|
conn = None
|
56 |
|
|
try:
|
57 |
|
|
conn = sqlite3.connect(DATABASE_FILE)
|
58 |
|
|
c = conn.cursor()
|
59 |
61db0634
|
David Friesecký
|
c.execute(sql, args)
|
60 |
2c96b4a5
|
David Friesecký
|
conn.commit()
|
61 |
|
|
c.close()
|
62 |
|
|
except Error as e:
|
63 |
|
|
print(e)
|
64 |
|
|
return False
|
65 |
|
|
finally:
|
66 |
|
|
if conn:
|
67 |
|
|
conn.close()
|
68 |
|
|
return True
|
69 |
|
|
|
70 |
25053504
|
David Friesecký
|
@staticmethod
|
71 |
|
|
def delete(sql: str, item_id: int) -> bool:
|
72 |
2c96b4a5
|
David Friesecký
|
conn = None
|
73 |
|
|
try:
|
74 |
|
|
conn = sqlite3.connect(DATABASE_FILE)
|
75 |
|
|
c = conn.cursor()
|
76 |
|
|
c.execute(sql, (item_id,))
|
77 |
|
|
conn.commit()
|
78 |
|
|
c.close()
|
79 |
|
|
except Error as e:
|
80 |
|
|
print(e)
|
81 |
|
|
return False
|
82 |
|
|
finally:
|
83 |
|
|
if conn:
|
84 |
|
|
conn.close()
|
85 |
|
|
return True
|