-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.py
More file actions
300 lines (254 loc) · 7.13 KB
/
queries.py
File metadata and controls
300 lines (254 loc) · 7.13 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import sqlite3
from datetime import date as dt
# TODO: add validation,
# also possibly some extra functions...
def _run_query(con, query, values):
cur = con.cursor()
cur.execute(query, values)
cur.close()
def add_box(con, edge, pivot_x, pivot_y, room_id):
query = '''
INSERT INTO box (edge, pivot_x, pivot_y, room_id)
VALUES (?, ?, ?, ?)
'''
values = (edge, pivot_x, pivot_y, room_id)
_run_query(con, query, values)
def remove_box(con, id):
query = '''
DELETE FROM box
WHERE id = ?
'''
values = (id, )
_run_query(con, query, values)
def edit_box(con, id, edge=-1, pivot_x=-1, pivot_y=-1, room_id=-1):
to_update = {}
if edge != -1:
to_update['edge'] = edge
if pivot_x != -1:
to_update['pivot_x'] = pivot_x
if pivot_y != -1:
to_update['pivot_y'] = pivot_y
if room_id != -1:
to_update['room_id'] = room_id
if not to_update:
print('Nothing to update')
return
cols = ", ".join([f"{key} = ?" for key in to_update.keys()])
to_update['id'] = id
values = tuple(to_update.values())
query = (f"UPDATE box SET {cols} WHERE id = ?")
_run_query(con, query, values)
def print_box(con):
query = 'SELECT * FROM box'
cur = con.cursor()
cur.execute(query)
results = cur.fetchall()
for res in results:
print(res)
cur.close()
def add_object(con, name, box_id, available=1, date='NULL'):
query = '''
INSERT INTO object (name, available, date, box_id)
VALUES (?, ?, ?, ?)
'''
values = (name, available, date, box_id)
_run_query(con, query, values)
def remove_object(con, id):
query = '''
DELETE FROM object
WHERE id = ?
'''
values = (id, )
_run_query(con, query, values)
def edit_object(con, id, name='', box_id=-1, available=-1, date=''):
to_update = {}
if name != '':
to_update['name'] = name
if box_id != -1:
to_update['box_id'] = box_id
if available != -1:
to_update['available'] = available
if date != '':
to_update['date'] = date
if not to_update:
print('Nothing to update')
return
cols = ", ".join([f"{key} = ?" for key in to_update.keys()])
to_update['id'] = id
values = tuple(to_update.values())
query = (f"UPDATE object SET {cols} WHERE id = ?")
_run_query(con, query, values)
def find_object_by_name(con, name):
query = '''
SELECT * FROM object
WHERE name = ?
'''
values = (name, )
cur = con.cursor()
cur.execute(query, values)
results = cur.fetchall()
for res in results:
print(res)
cur.close()
def find_object_by_id(con, id):
query = '''
SELECT * FROM object
WHERE id = ?
'''
values = (id, )
cur = con.cursor()
cur.execute(query, values)
results = cur.fetchall()
for res in results:
print(res)
cur.close()
def rent_object(con, id, date):
query_object = '''
UPDATE object
SET date = ?,
available = 0
WHERE id = ?
'''
values_o = (date, id)
query_movement = '''
INSERT INTO movement (object_id, date_rent, date_return)
VALUES (?, ?, ?)
'''
today = dt.today().strftime("%Y-%m-%d")
values_m = (id, today, date)
try:
_run_query(con, query_object, values_o)
_run_query(con, query_movement, values_m)
except Exception as e:
print(f'Error: {e}')
def return_object(con, obj_id, rent_id):
query_object = '''
UPDATE object
SET date = 'NULL',
available = 1
WHERE id = ?
'''
values_o = (obj_id, )
query_movement = '''
UPDATE movement
SET finished = 1
WHERE id = ?
'''
values_m = (rent_id, )
try:
_run_query(con, query_object, values_o)
_run_query(con, query_movement, values_m)
except Exception as e:
print(f'Error: {e}')
# all movements of an object
def history_object(con, id):
query = '''
SELECT * FROM movement
WHERE object_id = ?
'''
values = (id, )
cur = con.cursor()
cur.execute(query, values)
results = cur.fetchall()
for res in results:
print(res)
cur.close()
def print_movement(con):
query = 'SELECT * FROM movement'
cur = con.cursor()
cur.execute(query)
results = cur.fetchall()
for res in results:
print(res)
cur.close()
def print_object(con):
query = 'SELECT * FROM object'
cur = con.cursor()
cur.execute(query)
results = cur.fetchall()
for res in results:
print(res)
cur.close()
def print_room(con):
query = 'SELECT * FROM room'
cur = con.cursor()
cur.execute(query)
results = cur.fetchall()
for res in results:
print(res)
cur.close()
def add_room(con, name, length, width, height):
query = '''
INSERT INTO room (name, length, width, height)
VALUES (?, ?, ?, ?)
'''
values = (name, length, width, height)
_run_query(con, query, values)
def remove_room(con, id):
query = '''
DELETE FROM room
WHERE id = ?
'''
values = (id, )
_run_query(con, query, values)
def edit_room(con, id, name='', length=0, width=0, height=0):
to_update = {}
if name != '':
to_update['name'] = name
if length != 0:
to_update['length'] = length
if width != 0:
to_update['width'] = width
if height != 0:
to_update['height'] = height
if not to_update:
print('Nothing to update')
return
cols = ", ".join([f"{key} = ?" for key in to_update.keys()])
to_update['id'] = id
values = tuple(to_update.values())
query = (f"UPDATE room SET {cols} WHERE id = ?")
_run_query(con, query, values)
def add_restricted(con, room_id, pivot_x, pivot_y, length, width):
query = '''
INSERT INTO restricted
(room_id, pivot_x, pivot_y, length, width)
VALUES (?, ?, ?, ?, ?)
'''
values = (room_id, pivot_x, pivot_y, length, width)
_run_query(con, query, values)
def remove_restricted(con, id):
query = '''
DELETE FROM restricted
WHERE id = ?
'''
values = (id, )
_run_query(con, query, values)
def edit_restricted(con, id, room_id=-1, pivot_x=-1, pivot_y=-1, length=0, width=0):
to_update = {}
if room_id != -1:
to_update['room_id'] = room_id
if pivot_x != -1:
to_update['length'] = length
if pivot_y != -1:
to_update['pivot_y'] = pivot_y
if length != 0:
to_update['length'] = length
if width != 0:
to_update['width'] = width
if not to_update:
print('Nothing to update')
return
cols = ", ".join([f"{key} = ?" for key in to_update.keys()])
to_update['id'] = id
values = tuple(to_update.values())
query = (f"UPDATE restricted SET {cols} WHERE id = ?")
_run_query(con, query, values)
def print_restricted(con):
query = 'SELECT * FROM restricted'
cur = con.cursor()
cur.execute(query)
results = cur.fetchall()
for res in results:
print(res)
cur.close()