-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.py
467 lines (362 loc) · 12.1 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
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
import psycopg2
import datetime
from Constants import *
from typing import Tuple
def create_connection() -> Tuple | Tuple[None, None]:
"""
create_connection() attempts to create
a connection to the postgresql server
with the db name of ai. Returns the
connection and cursor on success.
Returns None otherwise.
Paramters:
None
Returns:
A Tuple of
conn: connection (The connection object returned from `psycopg2.connect()`)
cur: cursor (The cursor object returned from `conn.cursor()`)
Or A Tuple of
None
None
"""
try:
conn=psycopg2.connect("dbname=ai")
cur=conn.cursor()
return conn, cur
except:
return None, None
def create_table(conn, cur) -> None:
"""
create_table() attempts to create the
table `conversation` with 7 attributes.
Once created, create_table() commits
the changes to the db `ai`.
Paramters:
conn: connection (The connection object returned from `psycopg2.connect()`)
cur: cursor (The cursor object returned from `conn.cursor()`)
Returns:
None
"""
cur.execute("CREATE TABLE IF NOT EXISTS conversation (\
id serial PRIMARY KEY, \
question varchar NOT NULL, \
answer varchar NOT NULL, \
count integer NOT NULL, \
day date NOT NULL, \
duration real NOT NULL, \
ai varchar NOT NULL \
);")
conn.commit()
def delete_table(conn, cur) -> None:
"""
delete_table() attempts to delete the
table `conversation` if it exists.
Once deleted, delete_table() commits
the changes to the db `ai`.
Paramters:
conn: connection (The connection object returned from `psycopg2.connect()`)
cur: cursor (The cursor object returned from `conn.cursor()`)
Returns:
None
"""
cur.execute("DROP TABLE IF EXISTS conversation;")
conn.commit()
def clear_table(conn, cur) -> None:
"""
clear_table() attempts to clear all
of the rows in the table `conversation`.
Once cleared, clear_table() commits
the changes to the db `ai`.
Paramters:
conn: connection (The connection object returned from `psycopg2.connect()`)
cur: cursor (The cursor object returned from `conn.cursor()`)
Returns:
None
"""
cur.execute("TRUNCATE conversation;")
conn.commit()
def select_table_names(cur) -> list:
"""
select_table_names() attempts to query
all of the tables names in the db `ai`.
Once queried, select_table_names()
returns the result set of table names.
Paramters:
cur: cursor (The cursor object returned from `conn.cursor()`)
Returns:
cur.fetchall(): list (The list of table names in the db `ai`)
"""
cur.execute("SELECT * FROM pg_catalog.pg_tables \
WHERE schemaname='public';\
")
return cur.fetchall()
def select_table_columns(cur) -> list:
"""
select_table_columns() attemps to
query all of the columns names in
the table `conversation`. Once
quieried, select_table_columns()
returns the result set of table columns.
Paramters:
cur: cursor (The cursor object returned from `conn.cursor()`)
Returns:
cur.fetchall(): list (The list of table names in the db `ai`)
"""
cur.execute("SELECT * FROM information_schema.columns \
WHERE table_name='conversation';\
")
return cur.fetchall()
def print_table_details(cur) -> None:
"""
print_table_details() attempts to
print all of the table names and
corresponding table columns in
the db `ai` with details about
the type.
Paramters:
cur: cursor (The cursor object returned from `conn.cursor()`)
Returns:
None
"""
print("Details about Table:\n")
print("Name: ", select_table_names(cur)[0][1])
for table in select_table_columns(cur):
print("Column: ", table[3], "( Type: ", table[7], ")")
print()
def select_all_conversations(cur) -> list:
"""
select_all_conversations() attempts
to query all of the stored conversations
in the table `conversation`. Once queried,
select_all_conversations() returns the
result set of all stored conversations.
Paramters:
cur: cursor (The cursor object returned from `conn.cursor()`)
Returns:
cur.fetchall(): list (The list of table names in the db `ai`)
"""
cur.execute("\
SELECT * FROM conversation;\
")
return cur.fetchall()
def insert_conversation(conn, cur, question: str, answer: str, seconds: int, ai: str) -> None:
"""
insert_conversation() attempts to insert
into the table `conversation` the given
strings of question, answer, ai, and
integer seconds. Once inserted,
insert_conversation() commits the
changes to the db `ai`.
Paramters:
conn: connection (The connection object returned from `psycopg2.connect()`)
cur: cursor (The cursor object returned from `conn.cursor()`)
question: string (The prompt message)
answer: string (The response from target ai)
seconds: integer (The seconds target ai responded)
ai: string (The target ai)
Returns:
None
"""
cur.execute(f"INSERT INTO conversation (\
question, answer, count, \
day, duration, ai\
) VALUES (\
E\'{question}\', E\'{answer}\', 1, \
\'{datetime.datetime.now().date()}\', \
{seconds}, \'{ai}\'\
);\
")
conn.commit()
def delete_conversation(conn, cur, question: str, answer: str) -> None:
"""
delete_conversation() attempts to delete
the conversation with the given question
and answer. Once deleted,
delete_conversation() commits the changes
to the db `ai`.
Paramters:
conn: connection (The connection object returned from `psycopg2.connect()`)
cur: cursor (The cursor object returned from `conn.cursor()`)
question: string (The prompt message)
answer: string (The response from target ai)
Returns:
None
"""
cur.execute(f"DELETE FROM conversation \
WHERE conversation.question=E\'{question}\' \
AND conversation.answer=E\'{answer}\';\
")
conn.commit()
def search_question(cur, question: str, ai: str) -> list:
"""
search_question() attempts to query
the conversations with the given
question and answer. Once queried,
search_question() returns the result
set of the matching conversations.
Paramters:
cur: cursor (The cursor object returned from `conn.cursor()`)
question: string (The prompt message)
ai: string (The target ai)
Returns:
cur.fetchall(): list (The list of table names in the db `ai`)
"""
cur.execute(f"SELECT * FROM conversation \
WHERE conversation.question=E\'{question}\' \
AND conversation.ai=\'{ai}\';\
")
return cur.fetchall()
def increase_count_of_question(conn, cur, question: str, ai: str) -> None:
"""
increase_count_of_question() attempts to
query the stored conversation that contains
the given question and target ai and increments
the times asked by 1. Once queried,
increase_count_of_question() commits the changes
to the db `ai`.
Paramters:
conn: connection (The connection object returned from `psycopg2.connect()`)
cur: cursor (The cursor object returned from `conn.cursor()`)
question: string (The prompt message)
ai: string (The target ai)
Returns:
None
"""
cur.execute(f"UPDATE conversation \
SET count=count+1 \
WHERE conversation.question=E\'{question}\' \
AND ai=\'{ai}\';\
")
conn.commit()
def select_questions_asked(cur, d: str) -> list:
"""
select_questions_asked() attempts to query
the stored conversations given the parsed
flag. Once queried, select_questions_asked()
commits the changes to the db `ai`.
Paramters:
cur: cursor (The cursor object returned from `conn.cursor()`)
d: string (The given flag to filter stored conversations)
Returns:
cur.fetchall(): list (The list of table names in the db `ai`)
"""
day=""
if d==TDY[0]:
day=datetime.datetime.now().date()
elif d==YTD[0]:
day=datetime.datetime.now().date().replace(
day=datetime.datetime.now().date().day-1
)
elif d==ALL[0]:
return select_all_conversations(cur)
cur.execute(f"SELECT * FROM conversation \
WHERE conversation.day=\'{day}\';\
")
return cur.fetchall()
def select_most_asked_question(cur) -> list:
"""
select_most_asked_question() attempts to
query the conversations with the highest
times asked. Once queried,
select_most_asked_questions() returns
the result set of the conversations with
the most times asked.
Paramters:
cur: cursor (The cursor object returned from `conn.cursor()`)
Returns:
cur.fetchall(): list (The list of table names in the db `ai`)
"""
cur.execute(f"""
SELECT * FROM conversation
WHERE count=(
SELECT MAX(count)
FROM conversation
);
""")
return cur.fetchall()
def select_longest_question_waited_for(cur) -> list:
"""
select_longest_question_waited_for() attempts to
query the conversation with the longest duration
of time waited for a target ai to respond. Once
queried, select_longest_question_waited_for()
returns the result of the conversations with the
most asked times.
Paramters:
cur: cursor (The cursor object returned from `conn.cursor()`)
Returns:
cur.fetchall(): list (The list of table names in the db `ai`)
"""
cur.execute(f"SELECT * FROM conversation \
WHERE duration=(\
SELECT MAX(duration) \
FROM conversation\
);\
")
return cur.fetchall()
def select_shortest_question_waited_for(cur) -> list:
"""
select_shortest_question_waited_for() attempts to
query the conversation with the shortest duration
of time waited for a target ai to respond. Once
queried, select_shortest_question_waited_for()
returns the result of the conversation with the
shortest duration.
Paramters:
cur: cursor (The cursor object returned from `conn.cursor()`)
Returns:
cur.fetchall(): list (The list of table names in the db `ai`)
"""
cur.execute(f"SELECT * FROM conversation \
WHERE duration=(\
SELECT MIN(duration) FROM conversation\
);")
return cur.fetchall()
def select_conversation_given_id(cur, id: int) -> list:
"""
select_conversation_given_id() attempts to query
the conversation with the given id. Once queried,
select_conversation_given_id() returns the result
set of the conversation with the given id.
Paramters:
cur: cursor (The cursor object returned from `conn.cursor()`)
id: integer (The id of a stored conversation)
Returns:
cur.fetchall(): list (The list of table names in the db `ai`)
"""
cur.execute(f"SELECT * FROM conversation \
WHERE id={id};\
")
return cur.fetchall()
def select_conversations_given_date(cur, date: str) -> list:
"""
select_conversations_given_date() attempts to query
the conversations with the given date. Once queried,
select_conversations_given_date() returns the result
set of the conversations with the given date.
Paramters:
cur: cursor (The cursor object returned from `conn.cursor()`)
date: string (A date to query all stored conversations)
Returns:
cur.fetchall(): list (The list of table names in the db `ai`)
"""
cur.execute(f"SELECT * FROM conversation \
WHERE day=\'{date}\';\
")
return cur.fetchall()
def select_conversations_given_ai(cur, ai: str) -> list:
"""
select_conversations_given_ai() attempts to query
the conversations with the given ai. Once queried,
select_conversations_given_ai() returns the result
set of the conversations with the given ai.
Parameters:
cur: cursor (The cursor object returned from `conn.cursor()`)
ai: string (The target ai)
Returns:
cur.fetchall(): list (The list of table names in the db `ai`)
"""
cur.execute(f"SELECT * FROM conversation \
WHERE ai=\'{ai}\';\
")
return cur.fetchall()