-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistence.py
More file actions
112 lines (90 loc) · 3.51 KB
/
Copy pathpersistence.py
File metadata and controls
112 lines (90 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import sqlite3
import atexit
from dbtools import Dao
# Data Transfer Objects:
class Employee(object):
def __init__(self, id: int, name: str, salary: float, branche: int):
self.id = id
self.name = name
self.salary = salary
self.branche = branche
def __str__(self):
return f"({self.id}, '{self.name}', {self.salary}, {self.branche})"
class Supplier(object):
def __init__(self, id: int, name: str, contact_information: str):
self.id = id
self.name = name
self.contact_information = contact_information
def __str__(self):
return f"({self.id}, '{self.name}', '{self.contact_information}')"
class Product(object):
def __init__(self, id: int, description: str, price: float, quantity: int):
self.id = id
self.description = description
self.price = price
self.quantity = quantity
def __str__(self):
return f"({self.id}, '{self.description}', {self.price}, {self.quantity})"
class Branche(object):
def __init__(self, id: int, location: str, number_of_employees: int):
self.id = id
self.location = location
self.number_of_employees = number_of_employees
def __str__(self):
return f"({self.id}, '{self.location}', {self.number_of_employees})"
class Activitie(object):
def __init__(self, product_id, quantity, activator_id, date):
self.product_id = product_id
self.quantity = quantity
self.activator_id = activator_id
self.date = date
def __str__(self):
return f"({self.product_id}, {self.quantity}, {self.activator_id}, '{self.date}')"
#Repository
class Repository(object):
def __init__(self):
self._conn = sqlite3.connect('bgumart.db')
self.employees = Dao(Employee, self._conn)
self.suppliers = Dao(Supplier, self._conn)
self.products = Dao(Product, self._conn)
self.branches = Dao(Branche, self._conn)
self.activities = Dao(Activitie, self._conn)
def _close(self):
self._conn.commit()
self._conn.close()
def create_tables(self):
self._conn.executescript("""
CREATE TABLE employees (
id INT PRIMARY KEY,
name TEXT NOT NULL,
salary REAL NOT NULL,
branche INT REFERENCES branches(id)
);
CREATE TABLE suppliers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
contact_information TEXT
);
CREATE TABLE products (
id INTEGER PRIMARY KEY,
description TEXT NOT NULL,
price REAL NOT NULL,
quantity INTEGER NOT NULL
);
CREATE TABLE branches (
id INTEGER PRIMARY KEY,
location TEXT NOT NULL,
number_of_employees INTEGER
);
CREATE TABLE activities (
product_id INTEGER REFERENCES products(id),
quantity INTEGER NOT NULL,
activator_id INTEGER NOT NULL,
date TEXT NOT NULL
);
""")
def execute_command(self, script: str) -> list:
return self._conn.cursor().execute(script).fetchall()
# singleton
repo = Repository()
atexit.register(repo._close)