-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
151 lines (122 loc) · 5.13 KB
/
Copy pathdatabase.py
File metadata and controls
151 lines (122 loc) · 5.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
from dotenv import load_dotenv
import logging as log
import pymysql
import json
import os
load_dotenv()
log.basicConfig(level=log.INFO, format="%(asctime)s %(levelname)s | %(message)s")
class Database:
def __init__(self):
self.table_name = os.getenv("MYSQL_DATA_TABLE_NAME")
def _database_connector(self):
try:
connection = pymysql.connect(
host="localhost",
user="root",
password=f"{os.getenv('MYSQL_PASSWORD')}",
database="analyzer"
)
log.info("Connection successfully created with database. ")
return connection
except pymysql.Error as e:
log.error(f"Error connection to Database: {e}")
return False
def _check_if_table_exist(self):
query = f"""
SELECT 1
FROM information_schema.tables
WHERE table_schema = %s
AND table_name = %s
LIMIT 1
"""
try:
connection = self._database_connector()
with connection.cursor() as cursor:
cursor.execute(query, ("analyzer", self.table_name))
result = cursor.fetchone()
cursor.close()
connection.close()
if result:
return True
else:
return False
except pymysql.Error as e:
log.error(f"Error searching for table: {e}")
def insert_analytics_into_database(self, data: str, file_id: int):
try:
connection = self._database_connector()
if not self._check_if_table_exist():
log.info("table isn't exist.")
with connection.cursor() as cursor:
query = f""" CREATE TABLE {self.table_name} (
file_id INT NOT NULL,
content TEXT NOT NULL,
conversation JSON,
PRIMARY KEY (file_id)
) """
cursor.execute(query)
log.info("Table created successfully.")
with connection.cursor() as cursor:
insert_data_query = f"""
INSERT INTO {self.table_name} (file_id, content)
VALUES (%s , %s)
"""
cursor.execute(insert_data_query, (file_id, data ))
log.info("Data inserted successfully.")
connection.commit()
connection.close()
except pymysql.Error as e:
log.error(f"Error inserting data into database: {e}")
def get_content_by_id(self, id: int):
query = f"""SELECT content FROM {self.table_name} WHERE file_id = %s"""
try:
connection = self._database_connector()
with connection.cursor() as cursor:
cursor.execute(query, (id,))
result = cursor.fetchall()
log.info("Successfully fetch content from database.")
return result
except pymysql.Error as e:
log.error(f"Error fetching content from database {e}")
return None
def update_conversation_into_database(self, conversation: str, file_id: int):
previous_conversation_query = f"""
SELECT conversation FROM {self.table_name} WHERE file_id = %s
"""
query = f"""
UPDATE {self.table_name} SET conversation = %s WHERE file_id = %s
"""
try:
connection = self._database_connector()
with connection.cursor() as cursor:
cursor.execute(previous_conversation_query, (file_id,))
result = cursor.fetchone()
if result and result[0]:
prev_conversation = json.loads(result[0])
else:
prev_conversation = {"messages": []}
# ✅ FIX: ensure proper dict
current_message = json.loads(conversation) if isinstance(conversation, str) else conversation
prev_conversation["messages"].append(current_message)
final_conversation_json = json.dumps(prev_conversation)
cursor.execute(query, (final_conversation_json, file_id))
connection.commit()
connection.close()
log.info("Success: conversation updated in database")
except pymysql.Error as e:
log.error(f"Error inserting conversation into database: {e}")
def get_conversation_by_id(self, id: int):
connection = self._database_connector()
query = f""" SELECT conversation FROM {self.table_name} WHERE file_id = %s"""
try:
with connection.cursor() as cursor:
cursor.execute(query, (id,))
connection.commit()
result = cursor.fetchone()
if result and result[0]:
return json.loads(result[0])
return {"messages": []}
except pymysql.Error as e:
log.error(f"Error Fetching Conversation from database {e}")
# c = Database()
# c.get_conversation_by_id(7937341)