-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
618 lines (582 loc) · 20.7 KB
/
db.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
import psycopg2
import os
from dotenv import load_dotenv
from load_excel import *
from calculations import *
import openpyxl
import pandas as pd
from sqlalchemy import create_engine
load_dotenv()
class FirstDb:
def __init__(self):
"""initialization of connection with db"""
self.connection = psycopg2.connect(
host=os.getenv("HOST"),
user=os.getenv("USER"),
password=os.getenv("PASSWORD"),
database=os.getenv("DB_NAME"),
)
self.create_first_table()
self.create_second_table()
self.create_admin_table()
self.create_patients_login_table()
self.create_admin_full_table()
self.create_users_full_table()
def create_first_table(self):
with self.connection.cursor() as cursor:
cursor.execute(
"""CREATE TABLE IF NOT EXISTS rand (
amount INTEGER,
measure TEXT,
arithmetic double precision,
minimal double precision,
maximal double precision,
deviation double precision,
variation double precision,
error double precision);"""
)
return "Таблиця готова до роботи"
def get_user_info(self, login):
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT * FROM full_info_users WHERE login = %s;""",
(login,),
)
result = cursor.fetchall()
return result
def create_admin_full_table(self):
with self.connection.cursor() as cursor:
cursor.execute(
"""CREATE TABLE IF NOT EXISTS full_info_excel (
name TEXT,
Age integer,
AP integer,
Height double precision,
Weight double precision,
BloodGroup integer,
BloodSugar double precision,
ATS integer,
ATD integer,
PAT integer,
AtAverage double precision,
Breathlessness integer,
IndKetle double precision,
IndKerdo double precision);"""
)
return "Таблиця готова до роботи"
def insert_user(
self,
name,
age,
ap,
height,
weight,
bloodgroup,
bloodsugar,
ats,
atd,
pat,
ataverage,
breathlessness,
indketle,
indkerdo,
):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO full_info_excel (name, age, ap, height, weight, bloodgroup, bloodsugar, ats, atd, pat, ataverage, breathlessness, indketle, indkerdo) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
(
name,
age,
ap,
height,
weight,
bloodgroup,
bloodsugar,
ats,
atd,
pat,
ataverage,
breathlessness,
indketle,
indkerdo,
),
)
self.connection.commit()
def create_users_full_table(self):
with self.connection.cursor() as cursor:
cursor.execute(
"""CREATE TABLE IF NOT EXISTS full_info_users (
name TEXT,
Age integer,
AP integer,
Height double precision,
Weight double precision,
BloodGroup integer,
BloodSugar double precision,
ATS integer,
ATD integer,
PAT integer,
AtAverage double precision,
Breathlessness integer,
IndKetle double precision,
IndKerdo double precision,
login text);"""
)
return "Таблиця готова до роботи"
def insert_full_excel(self, file_path):
engine = create_engine(
f'postgresql+psycopg2://{os.getenv("USER")}:{os.getenv("PASSWORD")}@{os.getenv("HOST")}/{os.getenv("DB_NAME")}'
)
with pd.ExcelFile(f"{file_path}") as xlsx:
df = pd.read_excel(xlsx)
df.to_sql(
name="full_info_excel", con=engine, if_exists="append", index=False
)
def create_admin_table(self):
with self.connection.cursor() as cursor:
cursor.execute(
"""CREATE TABLE IF NOT EXISTS users (
login varchar,
password varchar);"""
)
return "Таблиця готова до роботи"
def create_patients_login_table(self):
with self.connection.cursor() as cursor:
cursor.execute(
"""CREATE TABLE IF NOT EXISTS users_pat (
login varchar,
password varchar);"""
)
return "Таблиця готова до роботи"
def get_list_of_colnames(self):
with self.connection.cursor() as cursor:
cursor.execute("""SELECT * FROM rand""")
colnames = [desc[0] for desc in cursor.description]
print(colnames)
def check_if_excists_user(self, login):
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT login FROM full_info_users WHERE login = %s;""",
(login,),
)
result = cursor.fetchall()
return bool(len(result))
def check_if_exists_measure(self, measure):
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT measure FROM rand WHERE measure = %s;""",
(measure,),
)
result = cursor.fetchall()
return bool(len(result))
def check_if_excists_login(self, login):
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT login FROM users_pat WHERE login = %s;""",
(login,),
)
result = cursor.fetchall()
return bool(len(result))
def check_if_excists_password(self, password):
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT password FROM users_pat WHERE password = %s;""",
(password,),
)
result = cursor.fetchall()
return bool(len(result))
def check_if_user_data_exists(self, login):
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT login FROM full_info_users WHERE login = %s;""",
(login,),
)
result = cursor.fetchall()
return bool(len(result))
def check_if_exists_login_adm(self, login):
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT login FROM users WHERE login = %s;""",
(login,),
)
result = cursor.fetchall()
return bool(len(result))
def check_if_exists_password_adm(self, password):
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT password FROM users WHERE password = %s;""",
(password,),
)
result = cursor.fetchall()
return bool(len(result))
def check_if_exists_measure1(self, measure):
with self.connection.cursor() as cursor:
cursor.execute(
"""SELECT measure FROM rand2 WHERE measure = %s;""",
(measure,),
)
result = cursor.fetchall()
return bool(len(result))
def insert_new_user(self, login, password):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO users_pat (login, password) VALUES (%s, %s)""",
(
login,
password,
),
)
self.connection.commit()
def insert_user_login(
self,
name,
age,
ap,
height,
weight,
bloodgroup,
bloodsugar,
ats,
atd,
pat,
ataverage,
breathlessness,
indketle,
indkerdo,
login,
):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO full_info_users (name, age, ap, height, weight, bloodgroup, bloodsugar, ats, atd, pat, ataverage, breathlessness, indketle, indkerdo, login) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
(
name,
age,
ap,
height,
weight,
bloodgroup,
bloodsugar,
ats,
atd,
pat,
ataverage,
breathlessness,
indketle,
indkerdo,
login,
),
)
self.connection.commit()
def insert_arithmetic(self, amount, measure, value):
if not self.check_if_exists_measure(measure):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO rand (amount, measure, arithmetic) VALUES (%s, %s, %s)""",
(
amount,
measure,
value,
),
)
self.connection.commit()
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE rand SET amount = %s, arithmetic = %s WHERE measure = %s;""",
(
amount,
value,
measure,
),
)
self.connection.commit()
def insert_minimal(self, amount, measure, value):
if not self.check_if_exists_measure(measure):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO rand (amount, measure, minimal) VALUES (%s, %s, %s)""",
(
amount,
measure,
value,
),
)
self.connection.commit()
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE rand SET amount = %s, minimal = %s WHERE measure = %s;""",
(
amount,
value,
measure,
),
)
self.connection.commit()
def insert_maximal(self, amount, measure, value):
if not self.check_if_exists_measure(measure):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO rand (amount, measure, maximal) VALUES (%s, %s, %s)""",
(
amount,
measure,
value,
),
)
self.connection.commit()
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE rand SET amount = %s, maximal = %s WHERE measure = %s;""",
(
amount,
value,
measure,
),
)
self.connection.commit()
def insert_deviation(self, amount, measure, value):
if not self.check_if_exists_measure(measure):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO rand (amount, measure, deviation) VALUES (%s, %s, %s)""",
(
amount,
measure,
value,
),
)
self.connection.commit()
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE rand SET amount = %s, deviation = %s WHERE measure = %s;""",
(
amount,
value,
measure,
),
)
self.connection.commit()
def insert_variation(self, amount, measure, value):
if not self.check_if_exists_measure(measure):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO rand (amount, measure, variation) VALUES (%s, %s, %s)""",
(
amount,
measure,
value,
),
)
self.connection.commit()
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE rand SET amount = %s, variation = %s WHERE measure = %s;""",
(
amount,
value,
measure,
),
)
self.connection.commit()
def insert_error(self, amount, measure, value):
if not self.check_if_exists_measure(measure):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO rand (amount, measure, error) VALUES (%s, %s, %s)""",
(
amount,
measure,
value,
),
)
self.connection.commit()
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE rand SET amount = %s, error = %s WHERE measure = %s;""",
(
amount,
value,
measure,
),
)
self.connection.commit()
def create_second_table(self):
with self.connection.cursor() as cursor:
cursor.execute(
"""CREATE TABLE IF NOT EXISTS rand2 (
amount INTEGER,
measure TEXT,
covariance double precision,
pearson double precision,
t_test varchar);"""
)
return "Таблиця готова до роботи"
def insert_covariance(self, amount, measure, value):
if not self.check_if_exists_measure1(measure):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO rand2 (amount, measure, covariance) VALUES (%s, %s, %s)""",
(
amount,
measure,
value,
),
)
self.connection.commit()
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE rand2 SET amount = %s, covariance = %s WHERE measure = %s;""",
(
amount,
value,
measure,
),
)
self.connection.commit()
def insert_pearson(self, amount, measure, value):
if not self.check_if_exists_measure1(measure):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO rand2 (amount, measure, pearson) VALUES (%s, %s, %s)""",
(
amount,
measure,
value,
),
)
self.connection.commit()
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE rand2 SET amount = %s, pearson = %s WHERE measure = %s;""",
(
amount,
value,
measure,
),
)
self.connection.commit()
def insert_t_test(self, amount, measure, value):
if not self.check_if_exists_measure1(measure):
with self.connection.cursor() as cursor:
cursor.execute(
"""INSERT INTO rand2 (amount, measure, t_test) VALUES (%s, %s, %s)""",
(
amount,
measure,
value,
),
)
self.connection.commit()
else:
with self.connection.cursor() as cursor:
cursor.execute(
"""UPDATE rand2 SET amount = %s, t_test = %s WHERE measure = %s;""",
(
amount,
value,
measure,
),
)
self.connection.commit()
def get_rand_excel(self):
with self.connection.cursor() as cursor:
cursor.execute("""SELECT * FROM rand""")
results = cursor.fetchall()
book = openpyxl.Workbook()
sheet = book.active
k = 1
for row in [
"Кількість елементів",
"Показник",
"Середнє арифметичне",
"Мінімальне значення",
"Максимальне значення",
"Середньоквадратичне відхилення по вибірці",
"Коефіцієнт варіації",
"Помилка середнього",
]:
cell = sheet.cell(row=1, column=k)
cell.value = row
k += 1
i = 1
for row in results:
i += 1
j = 1
for col in row:
cell = sheet.cell(row=i, column=j)
cell.value = col
j += 1
book.save("rand.xlsx")
def get_full_zvit(self):
with self.connection.cursor() as cursor:
cursor.execute("""SELECT * FROM full_info_excel""")
results = cursor.fetchall()
book = openpyxl.Workbook()
sheet = book.active
k = 1
for row in [
"name",
"age",
"ap",
"height",
"weight",
"bloodgroup",
"bloodsugar",
"ats",
"atd",
"pat",
"ataverage",
"breathlessness",
"indketle",
"indkerdo",
]:
cell = sheet.cell(row=1, column=k)
cell.value = row
k += 1
i = 1
for row in results:
i += 1
j = 1
for col in row:
cell = sheet.cell(row=i, column=j)
cell.value = col
j += 1
book.save("Повний звіт.xlsx")
def get_rand2_excel(self):
with self.connection.cursor() as cursor:
cursor.execute("""SELECT * FROM rand2""")
results = cursor.fetchall()
book = openpyxl.Workbook()
sheet = book.active
k = 1
for row in [
"Кількість елементів",
"Показник",
"Коваріація",
"Коефіцієнт кореляції Пірсона",
"Т-критерий Стюдента",
]:
cell = sheet.cell(row=1, column=k)
cell.value = row
k += 1
i = 1
for row in results:
i += 1
j = 1
for col in row:
cell = sheet.cell(row=i, column=j)
cell.value = col
j += 1
book.save("rand2.xlsx")
def get_list_all(self, value):
with self.connection.cursor() as cursor:
cursor.execute("""SELECT * FROM full_info_excel;""")
result = cursor.fetchall()
return [i[value] for i in result]