forked from itudb2313/itudb2313
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
1216 lines (1048 loc) · 38.4 KB
/
database.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import mysql.connector as dbapi
from flask import current_app
class Database:
def __init__(self):
self.connection = dbapi.connect(
host=current_app.config["DB_HOST"],
# port = current_app.config["DB_PORT"],
user=current_app.config["DB_USER"],
password=current_app.config["DB_PASSWORD"],
database=current_app.config["DB_DATABASE"],
)
def select_all_categories(self):
query = """SELECT * FROM category"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
categories = []
column_names = [column[0] for column in cursor.description]
# iterate over the cursor object to get each row
for row in cursor:
# create a dictionary using the column names and row values
row_dict = dict(zip(column_names, row))
# add the dictionary to the dict_array
categories.append(row_dict)
return categories
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def get_category_by_id(self, category_id):
query = """SELECT * FROM category WHERE category_id=%s"""
try:
cursor = self.connection.cursor()
cursor.execute(query, (category_id,))
category = []
column_names = [column[0] for column in cursor.description]
# iterate over the cursor object to get each row
for row in cursor:
# create a dictionary using the column names and row values
row_dict = dict(zip(column_names, row))
# add the dictionary to the dict_array
category.append(row_dict)
return category
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def insert_category(
self,
category_id,
employee_id,
category_name,
rating,
quantity_sold,
being_manufactured,
total_sold_value,
):
query = """INSERT INTO category
(category_id,
employee_id,
category_name,
rating,
quantity_sold,
being_manufactured,
total_sold_value)
VALUES (%s, %s, %s, %s, %s, %s, %s)"""
try:
cursor = self.connection.cursor()
cursor.execute(
query,
(
category_id,
employee_id,
category_name,
rating,
quantity_sold,
being_manufactured,
total_sold_value,
),
)
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def update_category_by_id(
self,
category_id,
employee_id,
category_name,
rating,
quantity_sold,
being_manufactured,
total_sold_value,
):
query = """UPDATE category SET employee_id = %s, category_name = %s, rating = %s, quantity_sold = %s, being_manufactured = %s, total_sold_value = %s
WHERE category_id = %s"""
try:
cursor = self.connection.cursor()
cursor.execute(
query,
(
employee_id,
category_name,
rating,
quantity_sold,
being_manufactured,
total_sold_value,
),
)
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def delete_category_by_id(self, category_id):
query = """DELETE FROM category WHERE category_id = %s"""
print(category_id)
try:
cursor = self.connection.cursor()
cursor.execute(query, (category_id,))
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def select_all_customers(self):
query = """SELECT * FROM customer"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
customers = []
column_names = [column[0] for column in cursor.description]
#Forwarding cursor to get row data
for row in cursor:
#We create dictionary from the row
row_dict = dict(zip(column_names, row))
#We append the created dictionary to the array
customers.append(row_dict)
return customers
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def oldest_customers_with_employee(self):
query = """select customer.customer_id AS customer_id, customer.firstname AS firstname, customer.lastname AS lastname,
customer.dob AS dob, employee.firstname AS emp_fn, employee.lastname AS emp_ln, employee.employee_id AS emp_id
from customer
join employee
on customer.employee_id=employee.employee_id
order by customer.dob ASC
limit 10;"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
customers = []
column_names = [column[0] for column in cursor.description]
#Forwarding cursor to get row data
for row in cursor:
#We create dictionary from the row
row_dict = dict(zip(column_names, row))
#We append the created dictionary to the array
customers.append(row_dict)
return customers
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def get_customer_by_id(self, customer_id):
query = """SELECT * FROM customer WHERE customer_id=%s"""
try:
cursor = self.connection.cursor()
cursor.execute(query, (customer_id,))
customer = []
column_names = [column[0] for column in cursor.description]
#Forwarding cursor to get row data
for row in cursor:
#We create dictionary from the row
row_dict = dict(zip(column_names, row))
#We append the created dictionary to the array
customer.append(row_dict)
return customer
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def insert_customer(
self,
customer_id,
employee_id,
firstname,
lastname,
dob,
email,
city,
country,
):
query = """INSERT INTO customer
(customer_id,employee_id,firstname,lastname,dob,email,city,country)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"""
try:
cursor = self.connection.cursor()
cursor.execute(
query,
(
customer_id,
employee_id,
firstname,
lastname,
dob,
email,
city,
country,
),
)
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def update_customer_by_id(
self,
customer_id,
employee_id,
firstname,
lastname,
dob,
email,
city,
country,
):
query = """UPDATE customer SET employee_id = %s, firstname = %s, lastname = %s, dob = %s, email = %s, city = %s, country = %s
WHERE customer_id = %s"""
try:
cursor = self.connection.cursor()
cursor.execute(
query,
(
employee_id,
firstname,
lastname,
dob,
email,
city,
country,
customer_id,
),
)
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def delete_customer_by_id(self, customer_id):
query = """DELETE FROM customer WHERE customer_id = %s"""
print(customer_id)
try:
cursor = self.connection.cursor()
cursor.execute(query, (customer_id,))
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def get_stores_count(self):
query = """select count(*) from store"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
store_count = cursor.fetchall()
return store_count[0][0]
except dbapi.DatabaseError:
self.connection.rollback()
return None
finally:
cursor.close()
def get_all_stores(self):
query = """SELECT * FROM store"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
stores = cursor.fetchall()
return stores
except dbapi.DatabaseError:
self.connection.rollback()
return None
finally:
cursor.close()
def get_stores_columns(self):
query = """show columns from store"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
stores_column_names = cursor.fetchall()
return stores_column_names
except dbapi.DatabaseError:
self.connection.rollback()
return None
finally:
cursor.close()
def update_store(
self,
store_id,
employee_id,
store_name,
phone,
street,
city,
country,
email,
post_code,
):
query = """UPDATE test.store SET employee_id=%s, store_name=%s, phone=%s, street=%s, city=%s, country=%s, email=%s, post_code=%s WHERE store_id=%s;"""
try:
cursor = self.connection.cursor()
cursor.execute(
query,
(
employee_id,
store_name,
phone,
street,
city,
country,
email,
post_code,
store_id,
),
)
self.connection.commit()
except dbapi.DatabaseError:
print("error")
self.connection.rollback()
finally:
cursor.close()
def delete_store(self, store_id):
query = """DELETE FROM store WHERE store_id = %s"""
try:
cursor = self.connection.cursor()
cursor.execute(query, (store_id,))
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def get_store_by_name(
self, store_name, country, phone, street, city, email, post_code
):
query = """SELECT store_id FROM store WHERE store_name=%s AND phone=%s AND street=%s AND city=%s
AND country=%s AND email=%s AND post_code=%s"""
try:
cursor = self.connection.cursor()
cursor.execute(
query, (store_name, phone, street, city, country, email, post_code)
)
store = cursor.fetchall()
return store
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def insert_store(
self, employee_id, store_name, phone, street, city, country, email, post_code
):
query = """INSERT INTO store ( employee_id, store_name, phone, street, city, country, email, post_code)
VALUES ( %s, %s, %s, %s, %s, %s, %s, %s)"""
try:
cursor = self.connection.cursor()
cursor.execute(
query,
(
employee_id,
store_name,
phone,
street,
city,
country,
email,
post_code,
),
)
self.connection.commit()
except dbapi.DatabaseError:
print("error")
self.connection.rollback()
finally:
cursor.close()
def highest_salary_manager(self):
query = """select e.firstname, e.lastname, s.store_name,e.salary from employee e
inner join store s
on e.employee_id = s.employee_id
order by salary desc
limit 3"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
employee = cursor.fetchall()
return employee
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def get_all_stores_table(self,order_opt = "store_id",page_number = 1):
query = "SELECT * FROM store order by "+ order_opt +" limit 20 offset "+ str((int(page_number)-1)*20)
try:
cursor = self.connection.cursor()
cursor.execute(
query,
)
stores = cursor.fetchall()
return stores
except dbapi.DatabaseError:
self.connection.rollback()
return None
finally:
cursor.close()
def select_all_employees(self):
query = """SELECT * FROM employee"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
employees = []
column_names = [column[0] for column in cursor.description]
#Forwarding cursor to get row data
for row in cursor:
#We create dictionary from the row
row_dict = dict(zip(column_names, row))
#We append the created dictionary to the array
employees.append(row_dict)
return employees
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def select_top_ten_employees(self):
query = """SELECT employee.employee_id AS employee_id, employee.store_id AS store_id,
employee.firstname AS firstname,
employee.lastname AS lastname, count(*) AS customer_number
FROM employee
JOIN customer
ON employee.employee_id=customer.employee_id
GROUP BY employee.employee_id
ORDER BY count(*) DESC
LIMIT 10"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
employees = []
column_names = [column[0] for column in cursor.description]
#Forwarding cursor to get row data
for row in cursor:
#We create dictionary from the row
row_dict = dict(zip(column_names, row))
#We append the created dictionary to the array
employees.append(row_dict)
return employees
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def select_most_paid_employees(self):
query = """SELECT employee_id, firstname, lastname, dob, salary
FROM employee
ORDER BY salary DESC
LIMIT 12"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
employees = []
column_names = [column[0] for column in cursor.description]
#Forwarding cursor to get row data
for row in cursor:
#We create dictionary from the row
row_dict = dict(zip(column_names, row))
#We append the created dictionary to the array
employees.append(row_dict)
return employees
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def get_employee_by_id(self, employee_id):
query = """SELECT * FROM employee WHERE employee_id=%s"""
try:
cursor = self.connection.cursor()
cursor.execute(query, (employee_id,))
employee = []
column_names = [column[0] for column in cursor.description]
#Forwarding cursor to get row data
for row in cursor:
#We create dictionary from the row
row_dict = dict(zip(column_names, row))
#We append the created dictionary to the array
employee.append(row_dict)
return employee
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def update_employee_by_id(
self,
employee_id,
store_id,
firstname,
lastname,
dob,
email,
status,
salary,
city,
country,
):
query = """UPDATE employee SET store_id=%s, firstname=%s,
lastname=%s, dob=%s, email=%s, status=%s, salary=%s, city=%s, country=%s
WHERE employee_id=%s
"""
try:
cursor = self.connection.cursor()
cursor.execute(
query,
(
store_id,
firstname,
lastname,
dob,
email,
status,
salary,
city,
country,
employee_id,
),
)
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
print(dbapi.DatabaseError)
finally:
cursor.close()
def insert_employee(
self,
employee_id,
store_id,
firstname,
lastname,
dob,
email,
status,
salary,
city,
country,
):
query = """INSERT INTO employee (employee_id, store_id, firstname,
lastname, dob, email, status, salary, city, country)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
try:
cursor = self.connection.cursor()
cursor.execute(
query,
(
employee_id,
store_id,
firstname,
lastname,
dob,
email,
status,
salary,
city,
country,
),
)
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def delete_employee_by_id(self, employee_id):
query = """DELETE FROM employee WHERE employee_id = %s"""
try:
cursor = self.connection.cursor()
cursor.execute(query, (employee_id,))
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def select_all_rises(self):
query = """SELECT * FROM rise_archive"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
rises = []
column_names = [column[0] for column in cursor.description]
#Forwarding cursor to get row data
for row in cursor:
#We create dictionary from the row
row_dict = dict(zip(column_names, row))
#We append the created dictionary to the array
rises.append(row_dict)
return rises
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def select_top_rises(self):
query = """SELECT * FROM rise_archive WHERE rise_state!='Suspended' ORDER BY amount_by_percent DESC LIMIT 5"""
try:
cursor = self.connection.cursor()
cursor.execute(query)
rises = []
column_names = [column[0] for column in cursor.description]
#Forwarding cursor to get row data
for row in cursor:
#We create dictionary from the row
row_dict = dict(zip(column_names, row))
#We append the created dictionary to the array
rises.append(row_dict)
return rises
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def get_rise_by_id(self, rise_id):
query = """SELECT * FROM rise_archive WHERE rise_id=%s"""
try:
cursor = self.connection.cursor()
cursor.execute(query, (rise_id,))
rise = []
column_names = [column[0] for column in cursor.description]
#Forwarding cursor to get row data
for row in cursor:
#We create dictionary from the row
row_dict = dict(zip(column_names, row))
#We append the created dictionary to the array
rise.append(row_dict)
return rise
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def insert_rise(self, rise_id, amount_by_percent, rise_date, rise_state):
query = """INSERT INTO rise_archive (rise_id, amount_by_percent,
rise_date, rise_state) VALUES (%s, %s,%s, %s)"""
try:
cursor = self.connection.cursor()
cursor.execute(query, (rise_id, amount_by_percent, rise_date, rise_state))
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def update_rise_by_id(self, rise_id, amount_by_percent, rise_date, rise_state):
query = """UPDATE rise_archive SET amount_by_percent=%s,
rise_date=%s, rise_state=%s
WHERE rise_id=%s
"""
try:
cursor = self.connection.cursor()
cursor.execute(query, (amount_by_percent, rise_date, rise_state, rise_id))
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def delete_rise_by_id(self, rise_id):
query = """DELETE FROM rise_archive WHERE rise_id = %s"""
try:
cursor = self.connection.cursor()
cursor.execute(query, (rise_id,))
self.connection.commit()
except dbapi.DatabaseError:
self.connection.rollback()
finally:
cursor.close()
def get_product(self, product_id):
query = """SELECT * FROM product WHERE product_id = %s"""
with self.connection.cursor() as cursor:
cursor.execute(query, (product_id,))
product = cursor.fetchone()
return product
def get_products(
self,
search="%%",
lowest_price=0,
highest_price=1000000000,
lowest_km=0,
highest_km=1000000000,
color="%%",
lowest_year=0,
highest_year=3000,
page=0,
order="product_id",
):
select_clause = """SELECT product_id, product_name, model, category_name, year, color, km, price FROM product """
join_category = """INNER JOIN category USING (category_id) """
price_filters = """WHERE (price > %s AND price < %s) AND """
search_filters = (
"""(product_name LIKE %s OR model LIKE %s OR category_name LIKE %s) """
)
color_filters = """AND color LIKE %s """
km_filters = """AND (km > %s AND km < %s) """
year_filters = """AND (year > %s AND year < %s) """
order_by = """ORDER BY """ + order + """ LIMIT 10 OFFSET %s"""
query = (
select_clause
+ join_category
+ price_filters
+ search_filters
+ color_filters
+ km_filters
+ year_filters
+ order_by
)
with self.connection.cursor() as cursor:
cursor.execute(
query,
(
lowest_price,
highest_price,
search,
search,
search,
color,
lowest_km,
highest_km,
lowest_year,
highest_year,
page * 10,
),
)
products = cursor.fetchall()
return products
def insert_product(
self, product_name, model, year, color, price, km, category_id, provider_id
):
query = """INSERT INTO product (product_name, model, year, color, price, km, category_id, provider_id)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"""
with self.connection.cursor() as cursor:
cursor.execute(
query,
(product_name, model, year, color, price, km, category_id, provider_id),
)
self.connection.commit()
return True
def delete_product(self, product_id):
query = """DELETE FROM product WHERE product_id = %s"""
with self.connection.cursor() as cursor:
cursor.execute(query, (product_id,))
self.connection.commit()
return True
def update_product(
self,
product_id,
product_name,
model,
year,
color,
price,
km,
category_id,
provider_id,
):
query = """UPDATE product SET product_name=%s, model=%s, year=%s, color=%s, price=%s, km=%s, category_id=%s, provider_id=%s WHERE product_id=%s"""
with self.connection.cursor() as cursor:
cursor.execute(
query,
(
product_name,
model,
year,
color,
price,
km,
category_id,
provider_id,
product_id,
),
)
self.connection.commit()
return True
def get_colors(self):
query = """SELECT DISTINCT(color) FROM product"""
with self.connection.cursor() as cursor:
cursor.execute(query)
unique_colors = cursor.fetchall()
return unique_colors
def get_provider_countries(self):
query = """SELECT DISTINCT(country) FROM provider ORDER BY country"""
with self.connection.cursor() as cursor:
cursor.execute(query)
unique_countries = cursor.fetchall()
return unique_countries
def get_provider_cities(self):
query = """SELECT DISTINCT(city) FROM provider ORDER BY city"""
with self.connection.cursor() as cursor:
cursor.execute(query)
unique_cities = cursor.fetchall()
return unique_cities
def get_providers(self, search="%%", start=0, to=999999, country="%%", city="%%"):
query = """SELECT provider_id, provider_name, email, country, city, debt from provider """
search_filters = """WHERE (provider_name LIKE %s OR email LIKE %s) """
debt_filters = """AND (debt > %s AND debt < %s) """
country_city_filters = """AND country LIKE %s AND city LIKE %s """
limit = """ORDER BY provider_id DESC LIMIT 10"""
query += search_filters + debt_filters + country_city_filters + limit
with self.connection.cursor() as cursor:
cursor.execute(query, (search, search, start, to, country, city))
providers = cursor.fetchall()
return providers
def get_provider(self, provider_id):
query = """SELECT provider_id, provider_name, email, country, city, debt FROM provider WHERE provider_id = %s"""
with self.connection.cursor() as cursor:
cursor.execute(query, (provider_id,))
provider = cursor.fetchone()
return provider
def update_provider(self, provider_id, provider_name, email, country, city, debt):
query = """UPDATE provider SET provider_name=%s, email=%s, country=%s, city=%s, debt=%s WHERE provider_id=%s"""
with self.connection.cursor() as cursor:
cursor.execute(
query, (provider_name, email, country, city, debt, provider_id)
)
self.connection.commit()
return True
def insert_provider(self, provider_name, email, country, city, debt):
query = """INSERT INTO provider (provider_name, email, country, city, debt) VALUES (%s, %s, %s, %s, %s)"""
with self.connection.cursor() as cursor:
cursor.execute(query, (provider_name, email, country, city, debt))
self.connection.commit()
return True
def delete_provider(self, provider_id):
query = """DELETE FROM provider WHERE provider_id = %s"""
with self.connection.cursor() as cursor:
cursor.execute(query, (provider_id,))
self.connection.commit()
return True
def get_all_orders(self):
query = """SELECT * FROM orders"""
with self.connection.cursor() as cursor:
cursor.execute(query)
orders = cursor.fetchall()
return orders
def get_orders_paged(self, limit=10, offset=0, order_by="order_id", order="ASC"):
sortable_columns = [
"order_id",
"order_date",
"ship_date",
"required_date",
"order_status",
"quantity",
]
with self.connection.cursor() as cursor:
# we need to check if the column name is valid since
# execute() will put single quotes around the column name
if order_by in sortable_columns and order in ["ASC", "DESC"]:
cursor.execute(
"""select orders.*, customer.firstname, customer.lastname, product.product_name, product.model, store.store_name, employee.firstname, employee.lastname from orders inner join customer using(customer_id) inner join product using(product_id) inner join store using(store_id) inner join employee on orders.employee_id=employee.employee_id ORDER BY """
+ "orders."
+ order_by
+ " "
+ order
+ " LIMIT %s OFFSET %s",
(limit, offset),
)
else:
return []