-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistence_layer.py
158 lines (117 loc) · 5.73 KB
/
persistence_layer.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import sql
import domain_controllers
class _CachedStore:
"""Retrieves elements via some function and caches them after the first retrieval"""
def __init__(self, id_func, loader_func):
"""Initializes a cached store for a certain kind of objects
Parameters
----------
id_func: Function that retrieves an unique id from an object given for storage
loader_func: Function that loads an object based on its id, if it is not already cached"""
self._storage = {}
self._get_id = id_func
self._load_by_id = loader_func
def retrieve(self, object_id):
if object_id in self._storage:
return_value = self._storage[object_id]
else:
return_value = self._load_by_id(object_id)
self._storage(return_value)
return return_value
def store(self, obj):
object_id = self._get_id(obj)
self._storage[object_id] = obj
def delete(self, obj):
obj_id = self._get_id(obj)
if obj_id in self._storage:
del self._storage[obj_id]
def retrieve_values(self):
"""Returns all values in the cache as a list"""
return self._storage.values()
def retrieve_dict(self):
"""Returns all currently cached values as a dictionary"""
return self._storage.copy()
class PersistenceController:
def __init__(self, db_path):
self._db_connection = sql.Database(db_path)
self._db_connection.create_table("CREATE TABLE IF NOT EXISTS Contracts (ContractId INTEGER PRIMARY KEY, Name TEXT, Start DATE, End DATE, Hours INTEGER)")
self._db_connection.create_table("CREATE TABLE IF NOT EXISTS Projects (ProjectId INTEGER PRIMARY KEY, Name TEXT, ContractId INTEGER REFERENCES Contracts(ContractId))")
self._db_connection.create_table("CREATE TABLE IF NOT EXISTS Categories (CategoryId INTEGER PRIMARY KEY, Name TEXT)")
self._db_connection.create_table("CREATE TABLE IF NOT EXISTS Times (TimeId INTEGER PRIMARY KEY, ProjectId INTEGER REFERENCES Projects(ProjectId), CategoryId INTEGER REFERENCES Categories(CategoryId), Start DATETIME, End DATETIME, Description TEXT)")
self._contract_controller = domain_controllers.ContractController(self._db_connection, self)
self._category_controller = domain_controllers.CategoryController(self._db_connection, self)
self._project_controller = domain_controllers.ProjectController(self._db_connection, self)
self._worktime_controller = domain_controllers.WorktimeController(self._db_connection, self)
self._contracts = self._create_and_populate_cache(self._contract_controller.retrieve_all_contracts,
lambda contract: contract.contract_id,
self._contract_controller.retrieve_contract_by_id)
self._projects = self._create_and_populate_cache(self._project_controller.retrieve_all_projects,
lambda project: project.project_id,
self._project_controller.retrieve_project_by_id)
self._categories = self._create_and_populate_cache(self._category_controller.retrieve_all_categories,
lambda category: category.category_id,
self._category_controller.retrieve_category_by_id)
self._times = self._create_and_populate_cache(self._worktime_controller.retrieve_all_worktimes,
lambda time: time.time_id,
self._worktime_controller.retrieve_worktime_by_id)
# CRUD-interface for contracts
def create_contract(self, name, start, end, hours):
contract = self._contract_controller.create_contract(name, start, end, hours)
self._contracts.store(contract)
return contract
def retrieve_all_contracts(self):
return self._contracts.retrieve_values()
def retrieve_contract_by_id(self, contract_id):
return self._contracts.retrieve(contract_id)
def update_contract(self, contract):
self._contract_controller.update_contract(contract)
def delete_contract(self, contract):
self._contract_controller.delete_contract(contract)
self._contracts.delete(contract)
# CRUD-interface for projects
def create_project(self, name, contract):
project = self._project_controller.create_project(name, contract)
self._projects.store(project)
return project
def retrieve_all_projects(self):
return self._projects.retrieve_values()
def retrieve_project_by_id(self, project_id):
return self._projects.retrieve(project_id)
def update_project(self, project):
self._project_controller.update_project(project)
def delete_project(self, project):
self._project_controller.delete_project(project)
self._projects.delete(project)
# CRUD-interface for categories
def create_category(self, name):
category = self._category_controller.create_category(name)
self._categories.store(category)
return category
def retrieve_all_categories(self):
return self._categories.retrieve_values()
def retrieve_category_by_id(self, category_id):
return self._categories.retrieve(category_id)
def update_category(self, category):
self._category_controller.update_category(category)
def delete_category(self, category):
self._category_controller.delete_category(category)
self._categories.delete(category)
# CRUD-interface for times
def create_worktime(self, project, category, start, end, description):
worktime = self._worktime_controller.create_worktime(project, category, start, end, description)
self._times.store(worktime)
return worktime
def retrieve_all_worktimes(self):
return self._times.retrieve_values()
def retrieve_worktime_by_id(self, worktime_id):
return self._times.retrieve(worktime_id)
def update_worktime(self, worktime):
self._worktime_controller.update_worktime(worktime)
def delete_worktime(self, worktime):
self._worktime_controller.delete_worktime(worktime)
self._times.delete(worktime)
def _create_and_populate_cache(self, creator_func, id_func, loader_func):
return_value = _CachedStore(id_func, loader_func)
for obj in creator_func():
return_value.store(obj)
return return_value