-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.py
232 lines (189 loc) · 6.45 KB
/
queries.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
#!/usr/bin/env python3
import pymysql
import cgi
import cgitb
import json
cgitb.enable()
form = cgi.FieldStorage()
print("Content-type: text/html\n")
queryGenderCount = """
Select count(*) from `_D2DEMO` group by PERSON_GENDER;
"""
queryEthnicityCount = """
Select count(*) from `_D2DEMO` group by ETHN_GRP_CAT_TXT;
"""
queryRaceCount = """
Select count(*) from `_D2DEMO` group by RACE_CAT_TXT;
"""
queryEducationCount = """
Select count(*) from _D2PCDEMO Group by PT_COMPL_SCHOOL;
"""
queryAgeCount = """
Select AGE from _D2ELIGCHECK;
"""
question1a = """
Select count(*) from _D2ELIGCHECK where PT_HX_COPD_EMP_YN like "yes";
"""
question1b = """
Select newID from _D2ELIGCHECK where PT_HX_COPD_EMP_YN like "yes";
"""
question2a = """
Select count(*) from _D2ELIGCHECK where GRPB_SMK_STS like "Former Smoker";
"""
question2b = """
Select D2ELIGCHECKID from _D2ELIGCHECK where GRPB_SMK_STS like "Former Smoker";
"""
question3a = """
Select count(distinct newID) from _D2PHYEXAM where BLD_PRESS_SYSTOLIC >= 140 and BLD_PRESS_DIASTOLIC >= 90;
"""
question3b = """
Select distinct newID from _D2PHYEXAM where BLD_PRESS_SYSTOLIC >= 140 and BLD_PRESS_DIASTOLIC >= 90;
"""
question4a = """
Select count(*) from _D2ELIGCHECK where GRPB_SMK_STS = "Current Smoker";
"""
question4b = """
Select D2ELIGCHECKID from _D2ELIGCHECK where GRPB_SMK_STS = "Current Smoker";
"""
question5a = """
Select count(*) from _D2PCSYMPCBE where PT_COMPL_SBRTH_YN like "Yes";
"""
question5b = """
Select D2PCSYMPCBEID from _D2PCSYMPCBE where PT_COMPL_SBRTH_YN like "Yes";
"""
singlePatientQuery = """
Select newID, ETHN_GRP_CAT_TXT, PERSON_GENDER, PT_COMPL_SMOK_STAT, PT_COMPL_SMOK_HAB, PT_COMPL_CIG_DAY from _D2DEMO JOIN _D2PCSMKHX using(newID) where newID = %s
"""
multiPatientQuery = "Select newID, ETHN_GRP_CAT_TXT, PERSON_GENDER, PT_COMPL_SMOK_STAT, PT_COMPL_SMOK_HAB, PT_COMPL_CIG_DAY from _D2DEMO JOIN _D2PCSMKHX using(newID) where newID IN (%s, 3)"
listPatientQuery = "Select newID, ETHN_GRP_CAT_TXT, PERSON_GENDER, PT_COMPL_SMOK_STAT, PT_COMPL_SMOK_HAB, PT_COMPL_CIG_DAY from _D2DEMO JOIN _D2PCSMKHX using(newID) where newID IN (%s, 3, 4, 5, 6, 7)"
try:
connection = pymysql.connect(
host='bioed.bu.edu',
user='isarfraz',
password='isarfraz',
db='Team_G',
port = 4253)
except pymysql.Error as e:
print(e)
cursor = connection.cursor()
if form:
selector = form.getvalue("selector", "")
id = form.getvalue("id", "")
if(selector == "genderCount"):
try:
cursor.execute(queryGenderCount)
except pymysql.Error as e:
print(e,queryGenderCount)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "ethnicityCount"):
try:
cursor.execute(queryEthnicityCount)
except pymysql.Error as e:
print(e,queryEthnicityCount)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "ageCount"):
try:
cursor.execute(queryAgeCount)
except pymysql.Error as e:
print(e,queryAgeCount)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "raceCount"):
try:
cursor.execute(queryRaceCount)
except pymysql.Error as e:
print(e,queryRaceCount)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "educationCount"):
try:
cursor.execute(queryEducationCount)
except pymysql.Error as e:
print(e,queryEducationCount)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "q1a"):
try:
cursor.execute(question1a)
except pymysql.Error as e:
print(e,question1a)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "q1b"):
try:
cursor.execute(question1b)
except pymysql.Error as e:
print(e,question1b)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "q2a"):
try:
cursor.execute(question2a)
except pymysql.Error as e:
print(e,question2a)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "q2b"):
try:
cursor.execute(question2b)
except pymysql.Error as e:
print(e,question2b)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "q3a"):
try:
cursor.execute(question3a)
except pymysql.Error as e:
print(e,question3a)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "q3b"):
try:
cursor.execute(question3b)
except pymysql.Error as e:
print(e,question3b)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "q4a"):
try:
cursor.execute(question4a)
except pymysql.Error as e:
print(e,question4a)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "q4b"):
try:
cursor.execute(question4b)
except pymysql.Error as e:
print(e,question4b)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "q5a"):
try:
cursor.execute(question5a)
except pymysql.Error as e:
print(e,question5a)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "q5b"):
try:
cursor.execute(question5b)
except pymysql.Error as e:
print(e,question5b)
results = cursor.fetchall()
print(json.dumps(results))
elif(selector == "wholePatientSearch"):
findColumnQuery = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME = %s;"
cursor.execute(findColumnQuery, [id])
table_name = cursor.fetchone()[0]
fullAttributeQuery = "Select {} from {};".format(','.join(["newID", id]), table_name)
try:
cursor.execute(fullAttributeQuery)
except pymysql.Error as e:
print(e,fullAttributeQuery)
results = cursor.fetchall()
print(json.dumps(results))
else:
print("Content-type: text/html\n")