oceansweep
commited on
Commit
•
cce77c2
1
Parent(s):
968f616
Update App_Function_Libraries/DB/DB_Manager.py
Browse files- App_Function_Libraries/DB/DB_Manager.py +535 -535
App_Function_Libraries/DB/DB_Manager.py
CHANGED
@@ -1,535 +1,535 @@
|
|
1 |
-
import configparser
|
2 |
-
import logging
|
3 |
-
import os
|
4 |
-
from contextlib import contextmanager
|
5 |
-
from time import sleep
|
6 |
-
from typing import Tuple
|
7 |
-
import sqlite3
|
8 |
-
# 3rd-Party Libraries
|
9 |
-
from elasticsearch import Elasticsearch
|
10 |
-
|
11 |
-
############################################################################################################
|
12 |
-
#
|
13 |
-
# This file contains the DatabaseManager class, which is responsible for managing the database connection, i.e. either SQLite or Elasticsearch.
|
14 |
-
|
15 |
-
####
|
16 |
-
# The DatabaseManager class provides the following methods:
|
17 |
-
# - add_media: Add a new media item to the database
|
18 |
-
# - fetch_items_by_keyword: Fetch media items from the database based on a keyword
|
19 |
-
# - fetch_item_details: Fetch details of a specific media item from the database
|
20 |
-
# - update_media_content: Update the content of a specific media item in the database
|
21 |
-
# - search_and_display_items: Search for media items in the database and display the results
|
22 |
-
# - close_connection: Close the database connection
|
23 |
-
####
|
24 |
-
|
25 |
-
# Import your existing SQLite functions
|
26 |
-
from SQLite_DB
|
27 |
-
update_media_content as sqlite_update_media_content,
|
28 |
-
list_prompts as sqlite_list_prompts,
|
29 |
-
search_and_display as sqlite_search_and_display,
|
30 |
-
fetch_prompt_details as sqlite_fetch_prompt_details,
|
31 |
-
keywords_browser_interface as sqlite_keywords_browser_interface,
|
32 |
-
add_keyword as sqlite_add_keyword,
|
33 |
-
delete_keyword as sqlite_delete_keyword,
|
34 |
-
export_keywords_to_csv as sqlite_export_keywords_to_csv,
|
35 |
-
ingest_article_to_db as sqlite_ingest_article_to_db,
|
36 |
-
add_media_to_database as sqlite_add_media_to_database,
|
37 |
-
import_obsidian_note_to_db as sqlite_import_obsidian_note_to_db,
|
38 |
-
add_prompt as sqlite_add_prompt,
|
39 |
-
delete_chat_message as sqlite_delete_chat_message,
|
40 |
-
update_chat_message as sqlite_update_chat_message,
|
41 |
-
add_chat_message as sqlite_add_chat_message,
|
42 |
-
get_chat_messages as sqlite_get_chat_messages,
|
43 |
-
search_chat_conversations as sqlite_search_chat_conversations,
|
44 |
-
create_chat_conversation as sqlite_create_chat_conversation,
|
45 |
-
save_chat_history_to_database as sqlite_save_chat_history_to_database,
|
46 |
-
view_database as sqlite_view_database,
|
47 |
-
get_transcripts as sqlite_get_transcripts,
|
48 |
-
get_trashed_items as sqlite_get_trashed_items,
|
49 |
-
user_delete_item as sqlite_user_delete_item,
|
50 |
-
empty_trash as sqlite_empty_trash,
|
51 |
-
create_automated_backup as sqlite_create_automated_backup,
|
52 |
-
add_or_update_prompt as sqlite_add_or_update_prompt,
|
53 |
-
load_prompt_details as sqlite_load_prompt_details,
|
54 |
-
load_preset_prompts as sqlite_load_preset_prompts,
|
55 |
-
insert_prompt_to_db as sqlite_insert_prompt_to_db,
|
56 |
-
delete_prompt as sqlite_delete_prompt,
|
57 |
-
search_and_display_items as sqlite_search_and_display_items,
|
58 |
-
get_conversation_name as sqlite_get_conversation_name,
|
59 |
-
add_media_with_keywords as sqlite_add_media_with_keywords,
|
60 |
-
check_media_and_whisper_model as sqlite_check_media_and_whisper_model,
|
61 |
-
DatabaseError, create_document_version as sqlite_create_document_version,
|
62 |
-
get_document_version as sqlite_get_document_version
|
63 |
-
)
|
64 |
-
|
65 |
-
class Database:
|
66 |
-
def __init__(self, db_path=None):
|
67 |
-
self.db_path = db_path or os.getenv('DB_NAME', 'media_summary.db')
|
68 |
-
self.pool = []
|
69 |
-
self.pool_size = 10
|
70 |
-
|
71 |
-
@contextmanager
|
72 |
-
def get_connection(self):
|
73 |
-
retry_count = 5
|
74 |
-
retry_delay = 1
|
75 |
-
conn = None
|
76 |
-
while retry_count > 0:
|
77 |
-
try:
|
78 |
-
conn = self.pool.pop() if self.pool else sqlite3.connect(self.db_path, check_same_thread=False)
|
79 |
-
yield conn
|
80 |
-
self.pool.append(conn)
|
81 |
-
return
|
82 |
-
except sqlite3.OperationalError as e:
|
83 |
-
if 'database is locked' in str(e):
|
84 |
-
logging.warning(f"Database is locked, retrying in {retry_delay} seconds...")
|
85 |
-
retry_count -= 1
|
86 |
-
sleep(retry_delay)
|
87 |
-
else:
|
88 |
-
raise DatabaseError(f"Database error: {e}")
|
89 |
-
except Exception as e:
|
90 |
-
raise DatabaseError(f"Unexpected error: {e}")
|
91 |
-
finally:
|
92 |
-
# Ensure the connection is returned to the pool even on failure
|
93 |
-
if conn and conn not in self.pool:
|
94 |
-
self.pool.append(conn)
|
95 |
-
raise DatabaseError("Database is locked and retries have been exhausted")
|
96 |
-
|
97 |
-
def execute_query(self, query: str, params: Tuple = ()) -> None:
|
98 |
-
with self.get_connection() as conn:
|
99 |
-
try:
|
100 |
-
cursor = conn.cursor()
|
101 |
-
cursor.execute(query, params)
|
102 |
-
conn.commit()
|
103 |
-
except sqlite3.Error as e:
|
104 |
-
raise DatabaseError(f"Database error: {e}, Query: {query}")
|
105 |
-
|
106 |
-
def close_all_connections(self):
|
107 |
-
for conn in self.pool:
|
108 |
-
conn.close()
|
109 |
-
self.pool.clear()
|
110 |
-
|
111 |
-
def get_db_config():
|
112 |
-
config = configparser.ConfigParser()
|
113 |
-
config.read('config.txt')
|
114 |
-
return {
|
115 |
-
'type': config['Database']['type'],
|
116 |
-
'sqlite_path': config.get('Database', 'sqlite_path', fallback='media_summary.db'),
|
117 |
-
'elasticsearch_host': config.get('Database', 'elasticsearch_host', fallback='localhost'),
|
118 |
-
'elasticsearch_port': config.getint('Database', 'elasticsearch_port', fallback=9200)
|
119 |
-
}
|
120 |
-
|
121 |
-
db_config = get_db_config()
|
122 |
-
db_type = db_config['type']
|
123 |
-
|
124 |
-
if db_type == 'sqlite':
|
125 |
-
# Use the config path if provided, otherwise fall back to default
|
126 |
-
db = Database(db_config.get('sqlite_path'))
|
127 |
-
elif db_type == 'elasticsearch':
|
128 |
-
es = Elasticsearch([{
|
129 |
-
'host': db_config['elasticsearch_host'],
|
130 |
-
'port': db_config['elasticsearch_port']
|
131 |
-
}])
|
132 |
-
else:
|
133 |
-
raise ValueError(f"Unsupported database type: {db_type}")
|
134 |
-
|
135 |
-
db_path = db_config['sqlite_path']
|
136 |
-
|
137 |
-
# Update this path to the directory where you want to store the database backups
|
138 |
-
backup_dir = os.environ.get('DB_BACKUP_DIR', 'path/to/backup/directory')
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
if db_type == 'sqlite':
|
144 |
-
conn = sqlite3.connect(db_config['sqlite_path'])
|
145 |
-
cursor = conn.cursor()
|
146 |
-
elif db_type == 'elasticsearch':
|
147 |
-
es = Elasticsearch([{
|
148 |
-
'host': db_config['elasticsearch_host'],
|
149 |
-
'port': db_config['elasticsearch_port']
|
150 |
-
}])
|
151 |
-
else:
|
152 |
-
raise ValueError(f"Unsupported database type: {db_type}")
|
153 |
-
|
154 |
-
############################################################################################################
|
155 |
-
#
|
156 |
-
# DB-Searching functions
|
157 |
-
|
158 |
-
def view_database(*args, **kwargs):
|
159 |
-
if db_type == 'sqlite':
|
160 |
-
return sqlite_view_database(*args, **kwargs)
|
161 |
-
elif db_type == 'elasticsearch':
|
162 |
-
# Implement Elasticsearch version
|
163 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
164 |
-
|
165 |
-
def search_and_display_items(*args, **kwargs):
|
166 |
-
if db_type == 'sqlite':
|
167 |
-
return sqlite_search_and_display_items(*args, **kwargs)
|
168 |
-
elif db_type == 'elasticsearch':
|
169 |
-
# Implement Elasticsearch version
|
170 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
171 |
-
|
172 |
-
def search_and_display(*args, **kwargs):
|
173 |
-
if db_type == 'sqlite':
|
174 |
-
return sqlite_search_and_display(*args, **kwargs)
|
175 |
-
elif db_type == 'elasticsearch':
|
176 |
-
# Implement Elasticsearch version
|
177 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
178 |
-
|
179 |
-
#
|
180 |
-
# End of DB-Searching functions
|
181 |
-
############################################################################################################
|
182 |
-
|
183 |
-
############################################################################################################
|
184 |
-
#
|
185 |
-
# Transcript-related Functions
|
186 |
-
|
187 |
-
def get_transcripts(*args, **kwargs):
|
188 |
-
if db_type == 'sqlite':
|
189 |
-
return sqlite_get_transcripts(*args, **kwargs)
|
190 |
-
elif db_type == 'elasticsearch':
|
191 |
-
# Implement Elasticsearch version
|
192 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
193 |
-
|
194 |
-
#
|
195 |
-
# End of Transcript-related Functions
|
196 |
-
############################################################################################################
|
197 |
-
|
198 |
-
############################################################################################################
|
199 |
-
#
|
200 |
-
# DB-Ingestion functions
|
201 |
-
|
202 |
-
def add_media_to_database(*args, **kwargs):
|
203 |
-
if db_type == 'sqlite':
|
204 |
-
result = sqlite_add_media_to_database(*args, **kwargs)
|
205 |
-
|
206 |
-
# Extract content
|
207 |
-
segments = args[2]
|
208 |
-
if isinstance(segments, list):
|
209 |
-
content = ' '.join([segment.get('Text', '') for segment in segments if 'Text' in segment])
|
210 |
-
elif isinstance(segments, dict):
|
211 |
-
content = segments.get('text', '') or segments.get('content', '')
|
212 |
-
else:
|
213 |
-
content = str(segments)
|
214 |
-
|
215 |
-
# Extract media_id from the result
|
216 |
-
# Assuming the result is in the format "Media 'Title' added/updated successfully with ID: {media_id}"
|
217 |
-
import re
|
218 |
-
match = re.search(r"with ID: (\d+)", result)
|
219 |
-
if match:
|
220 |
-
media_id = int(match.group(1))
|
221 |
-
|
222 |
-
# Create initial document version
|
223 |
-
sqlite_create_document_version(media_id, content)
|
224 |
-
|
225 |
-
return result
|
226 |
-
elif db_type == 'elasticsearch':
|
227 |
-
# Implement Elasticsearch version
|
228 |
-
raise NotImplementedError("Elasticsearch version of add_media_to_database not yet implemented")
|
229 |
-
|
230 |
-
|
231 |
-
def import_obsidian_note_to_db(*args, **kwargs):
|
232 |
-
if db_type == 'sqlite':
|
233 |
-
return sqlite_import_obsidian_note_to_db(*args, **kwargs)
|
234 |
-
elif db_type == 'elasticsearch':
|
235 |
-
# Implement Elasticsearch version
|
236 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
237 |
-
|
238 |
-
|
239 |
-
def update_media_content(*args, **kwargs):
|
240 |
-
if db_type == 'sqlite':
|
241 |
-
result = sqlite_update_media_content(*args, **kwargs)
|
242 |
-
|
243 |
-
# Extract media_id and content
|
244 |
-
selected_item = args[0]
|
245 |
-
item_mapping = args[1]
|
246 |
-
content_input = args[2]
|
247 |
-
|
248 |
-
if selected_item and item_mapping and selected_item in item_mapping:
|
249 |
-
media_id = item_mapping[selected_item]
|
250 |
-
|
251 |
-
# Create new document version
|
252 |
-
sqlite_create_document_version(media_id, content_input)
|
253 |
-
|
254 |
-
return result
|
255 |
-
elif db_type == 'elasticsearch':
|
256 |
-
# Implement Elasticsearch version
|
257 |
-
raise NotImplementedError("Elasticsearch version of update_media_content not yet implemented")
|
258 |
-
|
259 |
-
|
260 |
-
def add_media_with_keywords(*args, **kwargs):
|
261 |
-
if db_type == 'sqlite':
|
262 |
-
return sqlite_add_media_with_keywords(*args, **kwargs)
|
263 |
-
elif db_type == 'elasticsearch':
|
264 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
265 |
-
|
266 |
-
def check_media_and_whisper_model(*args, **kwargs):
|
267 |
-
if db_type == 'sqlite':
|
268 |
-
return sqlite_check_media_and_whisper_model(*args, **kwargs)
|
269 |
-
elif db_type == 'elasticsearch':
|
270 |
-
raise NotImplementedError("Elasticsearch version of check_media_and_whisper_model not yet implemented")
|
271 |
-
|
272 |
-
def ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt):
|
273 |
-
if db_type == 'sqlite':
|
274 |
-
return sqlite_ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt)
|
275 |
-
elif db_type == 'elasticsearch':
|
276 |
-
# Implement Elasticsearch version
|
277 |
-
raise NotImplementedError("Elasticsearch version of ingest_article_to_db not yet implemented")
|
278 |
-
else:
|
279 |
-
raise ValueError(f"Unsupported database type: {db_type}")
|
280 |
-
|
281 |
-
#
|
282 |
-
# End of DB-Ingestion functions
|
283 |
-
############################################################################################################
|
284 |
-
|
285 |
-
|
286 |
-
############################################################################################################
|
287 |
-
#
|
288 |
-
# Prompt-related functions
|
289 |
-
|
290 |
-
def list_prompts(*args, **kwargs):
|
291 |
-
if db_type == 'sqlite':
|
292 |
-
return sqlite_list_prompts(*args, **kwargs)
|
293 |
-
elif db_type == 'elasticsearch':
|
294 |
-
# Implement Elasticsearch version
|
295 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
296 |
-
|
297 |
-
|
298 |
-
def fetch_prompt_details(*args, **kwargs):
|
299 |
-
if db_type == 'sqlite':
|
300 |
-
return sqlite_fetch_prompt_details(*args, **kwargs)
|
301 |
-
elif db_type == 'elasticsearch':
|
302 |
-
# Implement Elasticsearch version
|
303 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
304 |
-
|
305 |
-
def add_prompt(*args, **kwargs):
|
306 |
-
if db_type == 'sqlite':
|
307 |
-
return sqlite_add_prompt(*args, **kwargs)
|
308 |
-
elif db_type == 'elasticsearch':
|
309 |
-
# Implement Elasticsearch version
|
310 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
311 |
-
|
312 |
-
|
313 |
-
def add_or_update_prompt(*args, **kwargs):
|
314 |
-
if db_type == 'sqlite':
|
315 |
-
return sqlite_add_or_update_prompt(*args, **kwargs)
|
316 |
-
elif db_type == 'elasticsearch':
|
317 |
-
# Implement Elasticsearch version
|
318 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
319 |
-
|
320 |
-
def load_prompt_details(*args, **kwargs):
|
321 |
-
if db_type == 'sqlite':
|
322 |
-
return sqlite_load_prompt_details(*args, **kwargs)
|
323 |
-
elif db_type == 'elasticsearch':
|
324 |
-
# Implement Elasticsearch version
|
325 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
326 |
-
|
327 |
-
def load_preset_prompts(*args, **kwargs):
|
328 |
-
if db_type == 'sqlite':
|
329 |
-
return sqlite_load_preset_prompts(*args, **kwargs)
|
330 |
-
elif db_type == 'elasticsearch':
|
331 |
-
# Implement Elasticsearch version
|
332 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
333 |
-
|
334 |
-
def insert_prompt_to_db(*args, **kwargs):
|
335 |
-
if db_type == 'sqlite':
|
336 |
-
return sqlite_insert_prompt_to_db(*args, **kwargs)
|
337 |
-
elif db_type == 'elasticsearch':
|
338 |
-
# Implement Elasticsearch version
|
339 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
340 |
-
|
341 |
-
def delete_prompt(*args, **kwargs):
|
342 |
-
if db_type == 'sqlite':
|
343 |
-
return sqlite_delete_prompt(*args, **kwargs)
|
344 |
-
elif db_type == 'elasticsearch':
|
345 |
-
# Implement Elasticsearch version
|
346 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
347 |
-
|
348 |
-
#
|
349 |
-
# End of Prompt-related functions
|
350 |
-
############################################################################################################
|
351 |
-
|
352 |
-
############################################################################################################
|
353 |
-
#
|
354 |
-
# Keywords-related Functions
|
355 |
-
|
356 |
-
def keywords_browser_interface(*args, **kwargs):
|
357 |
-
if db_type == 'sqlite':
|
358 |
-
return sqlite_keywords_browser_interface(*args, **kwargs)
|
359 |
-
elif db_type == 'elasticsearch':
|
360 |
-
# Implement Elasticsearch version
|
361 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
362 |
-
|
363 |
-
def add_keyword(*args, **kwargs):
|
364 |
-
if db_type == 'sqlite':
|
365 |
-
with db.get_connection() as conn:
|
366 |
-
cursor = conn.cursor()
|
367 |
-
return sqlite_add_keyword(*args, **kwargs)
|
368 |
-
elif db_type == 'elasticsearch':
|
369 |
-
# Implement Elasticsearch version
|
370 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
371 |
-
|
372 |
-
def delete_keyword(*args, **kwargs):
|
373 |
-
if db_type == 'sqlite':
|
374 |
-
return sqlite_delete_keyword(*args, **kwargs)
|
375 |
-
elif db_type == 'elasticsearch':
|
376 |
-
# Implement Elasticsearch version
|
377 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
378 |
-
|
379 |
-
def export_keywords_to_csv(*args, **kwargs):
|
380 |
-
if db_type == 'sqlite':
|
381 |
-
return sqlite_export_keywords_to_csv(*args, **kwargs)
|
382 |
-
elif db_type == 'elasticsearch':
|
383 |
-
# Implement Elasticsearch version
|
384 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
385 |
-
|
386 |
-
#
|
387 |
-
# End of Keywords-related Functions
|
388 |
-
############################################################################################################
|
389 |
-
|
390 |
-
############################################################################################################
|
391 |
-
#
|
392 |
-
# Chat-related Functions
|
393 |
-
|
394 |
-
def delete_chat_message(*args, **kwargs):
|
395 |
-
if db_type == 'sqlite':
|
396 |
-
return sqlite_delete_chat_message(*args, **kwargs)
|
397 |
-
elif db_type == 'elasticsearch':
|
398 |
-
# Implement Elasticsearch version
|
399 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
400 |
-
|
401 |
-
def update_chat_message(*args, **kwargs):
|
402 |
-
if db_type == 'sqlite':
|
403 |
-
return sqlite_update_chat_message(*args, **kwargs)
|
404 |
-
elif db_type == 'elasticsearch':
|
405 |
-
# Implement Elasticsearch version
|
406 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
407 |
-
|
408 |
-
def add_chat_message(*args, **kwargs):
|
409 |
-
if db_type == 'sqlite':
|
410 |
-
return sqlite_add_chat_message(*args, **kwargs)
|
411 |
-
elif db_type == 'elasticsearch':
|
412 |
-
# Implement Elasticsearch version
|
413 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
414 |
-
|
415 |
-
def get_chat_messages(*args, **kwargs):
|
416 |
-
if db_type == 'sqlite':
|
417 |
-
return sqlite_get_chat_messages(*args, **kwargs)
|
418 |
-
elif db_type == 'elasticsearch':
|
419 |
-
# Implement Elasticsearch version
|
420 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
421 |
-
|
422 |
-
def search_chat_conversations(*args, **kwargs):
|
423 |
-
if db_type == 'sqlite':
|
424 |
-
return sqlite_search_chat_conversations(*args, **kwargs)
|
425 |
-
elif db_type == 'elasticsearch':
|
426 |
-
# Implement Elasticsearch version
|
427 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
428 |
-
|
429 |
-
def create_chat_conversation(*args, **kwargs):
|
430 |
-
if db_type == 'sqlite':
|
431 |
-
return sqlite_create_chat_conversation(*args, **kwargs)
|
432 |
-
elif db_type == 'elasticsearch':
|
433 |
-
# Implement Elasticsearch version
|
434 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
435 |
-
|
436 |
-
def save_chat_history_to_database(*args, **kwargs):
|
437 |
-
if db_type == 'sqlite':
|
438 |
-
return sqlite_save_chat_history_to_database(*args, **kwargs)
|
439 |
-
elif db_type == 'elasticsearch':
|
440 |
-
# Implement Elasticsearch version
|
441 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
442 |
-
|
443 |
-
def get_conversation_name(*args, **kwargs):
|
444 |
-
if db_type == 'sqlite':
|
445 |
-
return sqlite_get_conversation_name(*args, **kwargs)
|
446 |
-
elif db_type == 'elasticsearch':
|
447 |
-
# Implement Elasticsearch version
|
448 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
449 |
-
|
450 |
-
#
|
451 |
-
# End of Chat-related Functions
|
452 |
-
############################################################################################################
|
453 |
-
|
454 |
-
############################################################################################################
|
455 |
-
#
|
456 |
-
# Trash-related Functions
|
457 |
-
|
458 |
-
def get_trashed_items(*args, **kwargs):
|
459 |
-
if db_type == 'sqlite':
|
460 |
-
return sqlite_get_trashed_items(*args, **kwargs)
|
461 |
-
elif db_type == 'elasticsearch':
|
462 |
-
# Implement Elasticsearch version
|
463 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
464 |
-
|
465 |
-
def user_delete_item(*args, **kwargs):
|
466 |
-
if db_type == 'sqlite':
|
467 |
-
return sqlite_user_delete_item(*args, **kwargs)
|
468 |
-
elif db_type == 'elasticsearch':
|
469 |
-
# Implement Elasticsearch version
|
470 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
471 |
-
|
472 |
-
def empty_trash(*args, **kwargs):
|
473 |
-
if db_type == 'sqlite':
|
474 |
-
return sqlite_empty_trash(*args, **kwargs)
|
475 |
-
elif db_type == 'elasticsearch':
|
476 |
-
# Implement Elasticsearch version
|
477 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
478 |
-
|
479 |
-
#
|
480 |
-
# End of Trash-related Functions
|
481 |
-
############################################################################################################
|
482 |
-
|
483 |
-
|
484 |
-
############################################################################################################
|
485 |
-
#
|
486 |
-
# DB-Backup Functions
|
487 |
-
|
488 |
-
def create_automated_backup(*args, **kwargs):
|
489 |
-
if db_type == 'sqlite':
|
490 |
-
return sqlite_create_automated_backup(*args, **kwargs)
|
491 |
-
elif db_type == 'elasticsearch':
|
492 |
-
# Implement Elasticsearch version
|
493 |
-
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
494 |
-
|
495 |
-
#
|
496 |
-
# End of DB-Backup Functions
|
497 |
-
############################################################################################################
|
498 |
-
|
499 |
-
|
500 |
-
############################################################################################################
|
501 |
-
#
|
502 |
-
# Document Versioning Functions
|
503 |
-
|
504 |
-
def create_document_version(*args, **kwargs):
|
505 |
-
if db_type == 'sqlite':
|
506 |
-
return sqlite_create_document_version(*args, **kwargs)
|
507 |
-
elif db_type == 'elasticsearch':
|
508 |
-
# Implement Elasticsearch version
|
509 |
-
raise NotImplementedError("Elasticsearch version of create_document_version not yet implemented")
|
510 |
-
|
511 |
-
def get_document_version(*args, **kwargs):
|
512 |
-
if db_type == 'sqlite':
|
513 |
-
return sqlite_get_document_version(*args, **kwargs)
|
514 |
-
elif db_type == 'elasticsearch':
|
515 |
-
# Implement Elasticsearch version
|
516 |
-
raise NotImplementedError("Elasticsearch version of get_document_version not yet implemented")
|
517 |
-
|
518 |
-
#
|
519 |
-
# End of Document Versioning Functions
|
520 |
-
############################################################################################################
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
############################################################################################################
|
525 |
-
#
|
526 |
-
# Function to close the database connection for SQLite
|
527 |
-
|
528 |
-
def close_connection():
|
529 |
-
if db_type == 'sqlite':
|
530 |
-
db.close_all_connections()
|
531 |
-
# Elasticsearch doesn't need explicit closing
|
532 |
-
|
533 |
-
#
|
534 |
-
# End of file
|
535 |
-
############################################################################################################
|
|
|
1 |
+
import configparser
|
2 |
+
import logging
|
3 |
+
import os
|
4 |
+
from contextlib import contextmanager
|
5 |
+
from time import sleep
|
6 |
+
from typing import Tuple
|
7 |
+
import sqlite3
|
8 |
+
# 3rd-Party Libraries
|
9 |
+
from elasticsearch import Elasticsearch
|
10 |
+
|
11 |
+
############################################################################################################
|
12 |
+
#
|
13 |
+
# This file contains the DatabaseManager class, which is responsible for managing the database connection, i.e. either SQLite or Elasticsearch.
|
14 |
+
|
15 |
+
####
|
16 |
+
# The DatabaseManager class provides the following methods:
|
17 |
+
# - add_media: Add a new media item to the database
|
18 |
+
# - fetch_items_by_keyword: Fetch media items from the database based on a keyword
|
19 |
+
# - fetch_item_details: Fetch details of a specific media item from the database
|
20 |
+
# - update_media_content: Update the content of a specific media item in the database
|
21 |
+
# - search_and_display_items: Search for media items in the database and display the results
|
22 |
+
# - close_connection: Close the database connection
|
23 |
+
####
|
24 |
+
|
25 |
+
# Import your existing SQLite functions
|
26 |
+
from App_Function_Libraries.DB.SQLite_DB import (
|
27 |
+
update_media_content as sqlite_update_media_content,
|
28 |
+
list_prompts as sqlite_list_prompts,
|
29 |
+
search_and_display as sqlite_search_and_display,
|
30 |
+
fetch_prompt_details as sqlite_fetch_prompt_details,
|
31 |
+
keywords_browser_interface as sqlite_keywords_browser_interface,
|
32 |
+
add_keyword as sqlite_add_keyword,
|
33 |
+
delete_keyword as sqlite_delete_keyword,
|
34 |
+
export_keywords_to_csv as sqlite_export_keywords_to_csv,
|
35 |
+
ingest_article_to_db as sqlite_ingest_article_to_db,
|
36 |
+
add_media_to_database as sqlite_add_media_to_database,
|
37 |
+
import_obsidian_note_to_db as sqlite_import_obsidian_note_to_db,
|
38 |
+
add_prompt as sqlite_add_prompt,
|
39 |
+
delete_chat_message as sqlite_delete_chat_message,
|
40 |
+
update_chat_message as sqlite_update_chat_message,
|
41 |
+
add_chat_message as sqlite_add_chat_message,
|
42 |
+
get_chat_messages as sqlite_get_chat_messages,
|
43 |
+
search_chat_conversations as sqlite_search_chat_conversations,
|
44 |
+
create_chat_conversation as sqlite_create_chat_conversation,
|
45 |
+
save_chat_history_to_database as sqlite_save_chat_history_to_database,
|
46 |
+
view_database as sqlite_view_database,
|
47 |
+
get_transcripts as sqlite_get_transcripts,
|
48 |
+
get_trashed_items as sqlite_get_trashed_items,
|
49 |
+
user_delete_item as sqlite_user_delete_item,
|
50 |
+
empty_trash as sqlite_empty_trash,
|
51 |
+
create_automated_backup as sqlite_create_automated_backup,
|
52 |
+
add_or_update_prompt as sqlite_add_or_update_prompt,
|
53 |
+
load_prompt_details as sqlite_load_prompt_details,
|
54 |
+
load_preset_prompts as sqlite_load_preset_prompts,
|
55 |
+
insert_prompt_to_db as sqlite_insert_prompt_to_db,
|
56 |
+
delete_prompt as sqlite_delete_prompt,
|
57 |
+
search_and_display_items as sqlite_search_and_display_items,
|
58 |
+
get_conversation_name as sqlite_get_conversation_name,
|
59 |
+
add_media_with_keywords as sqlite_add_media_with_keywords,
|
60 |
+
check_media_and_whisper_model as sqlite_check_media_and_whisper_model,
|
61 |
+
DatabaseError, create_document_version as sqlite_create_document_version,
|
62 |
+
get_document_version as sqlite_get_document_version
|
63 |
+
)
|
64 |
+
|
65 |
+
class Database:
|
66 |
+
def __init__(self, db_path=None):
|
67 |
+
self.db_path = db_path or os.getenv('DB_NAME', 'media_summary.db')
|
68 |
+
self.pool = []
|
69 |
+
self.pool_size = 10
|
70 |
+
|
71 |
+
@contextmanager
|
72 |
+
def get_connection(self):
|
73 |
+
retry_count = 5
|
74 |
+
retry_delay = 1
|
75 |
+
conn = None
|
76 |
+
while retry_count > 0:
|
77 |
+
try:
|
78 |
+
conn = self.pool.pop() if self.pool else sqlite3.connect(self.db_path, check_same_thread=False)
|
79 |
+
yield conn
|
80 |
+
self.pool.append(conn)
|
81 |
+
return
|
82 |
+
except sqlite3.OperationalError as e:
|
83 |
+
if 'database is locked' in str(e):
|
84 |
+
logging.warning(f"Database is locked, retrying in {retry_delay} seconds...")
|
85 |
+
retry_count -= 1
|
86 |
+
sleep(retry_delay)
|
87 |
+
else:
|
88 |
+
raise DatabaseError(f"Database error: {e}")
|
89 |
+
except Exception as e:
|
90 |
+
raise DatabaseError(f"Unexpected error: {e}")
|
91 |
+
finally:
|
92 |
+
# Ensure the connection is returned to the pool even on failure
|
93 |
+
if conn and conn not in self.pool:
|
94 |
+
self.pool.append(conn)
|
95 |
+
raise DatabaseError("Database is locked and retries have been exhausted")
|
96 |
+
|
97 |
+
def execute_query(self, query: str, params: Tuple = ()) -> None:
|
98 |
+
with self.get_connection() as conn:
|
99 |
+
try:
|
100 |
+
cursor = conn.cursor()
|
101 |
+
cursor.execute(query, params)
|
102 |
+
conn.commit()
|
103 |
+
except sqlite3.Error as e:
|
104 |
+
raise DatabaseError(f"Database error: {e}, Query: {query}")
|
105 |
+
|
106 |
+
def close_all_connections(self):
|
107 |
+
for conn in self.pool:
|
108 |
+
conn.close()
|
109 |
+
self.pool.clear()
|
110 |
+
|
111 |
+
def get_db_config():
|
112 |
+
config = configparser.ConfigParser()
|
113 |
+
config.read('config.txt')
|
114 |
+
return {
|
115 |
+
'type': config['Database']['type'],
|
116 |
+
'sqlite_path': config.get('Database', 'sqlite_path', fallback='media_summary.db'),
|
117 |
+
'elasticsearch_host': config.get('Database', 'elasticsearch_host', fallback='localhost'),
|
118 |
+
'elasticsearch_port': config.getint('Database', 'elasticsearch_port', fallback=9200)
|
119 |
+
}
|
120 |
+
|
121 |
+
db_config = get_db_config()
|
122 |
+
db_type = db_config['type']
|
123 |
+
|
124 |
+
if db_type == 'sqlite':
|
125 |
+
# Use the config path if provided, otherwise fall back to default
|
126 |
+
db = Database(db_config.get('sqlite_path'))
|
127 |
+
elif db_type == 'elasticsearch':
|
128 |
+
es = Elasticsearch([{
|
129 |
+
'host': db_config['elasticsearch_host'],
|
130 |
+
'port': db_config['elasticsearch_port']
|
131 |
+
}])
|
132 |
+
else:
|
133 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
134 |
+
|
135 |
+
db_path = db_config['sqlite_path']
|
136 |
+
|
137 |
+
# Update this path to the directory where you want to store the database backups
|
138 |
+
backup_dir = os.environ.get('DB_BACKUP_DIR', 'path/to/backup/directory')
|
139 |
+
|
140 |
+
|
141 |
+
|
142 |
+
|
143 |
+
if db_type == 'sqlite':
|
144 |
+
conn = sqlite3.connect(db_config['sqlite_path'])
|
145 |
+
cursor = conn.cursor()
|
146 |
+
elif db_type == 'elasticsearch':
|
147 |
+
es = Elasticsearch([{
|
148 |
+
'host': db_config['elasticsearch_host'],
|
149 |
+
'port': db_config['elasticsearch_port']
|
150 |
+
}])
|
151 |
+
else:
|
152 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
153 |
+
|
154 |
+
############################################################################################################
|
155 |
+
#
|
156 |
+
# DB-Searching functions
|
157 |
+
|
158 |
+
def view_database(*args, **kwargs):
|
159 |
+
if db_type == 'sqlite':
|
160 |
+
return sqlite_view_database(*args, **kwargs)
|
161 |
+
elif db_type == 'elasticsearch':
|
162 |
+
# Implement Elasticsearch version
|
163 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
164 |
+
|
165 |
+
def search_and_display_items(*args, **kwargs):
|
166 |
+
if db_type == 'sqlite':
|
167 |
+
return sqlite_search_and_display_items(*args, **kwargs)
|
168 |
+
elif db_type == 'elasticsearch':
|
169 |
+
# Implement Elasticsearch version
|
170 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
171 |
+
|
172 |
+
def search_and_display(*args, **kwargs):
|
173 |
+
if db_type == 'sqlite':
|
174 |
+
return sqlite_search_and_display(*args, **kwargs)
|
175 |
+
elif db_type == 'elasticsearch':
|
176 |
+
# Implement Elasticsearch version
|
177 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
178 |
+
|
179 |
+
#
|
180 |
+
# End of DB-Searching functions
|
181 |
+
############################################################################################################
|
182 |
+
|
183 |
+
############################################################################################################
|
184 |
+
#
|
185 |
+
# Transcript-related Functions
|
186 |
+
|
187 |
+
def get_transcripts(*args, **kwargs):
|
188 |
+
if db_type == 'sqlite':
|
189 |
+
return sqlite_get_transcripts(*args, **kwargs)
|
190 |
+
elif db_type == 'elasticsearch':
|
191 |
+
# Implement Elasticsearch version
|
192 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
193 |
+
|
194 |
+
#
|
195 |
+
# End of Transcript-related Functions
|
196 |
+
############################################################################################################
|
197 |
+
|
198 |
+
############################################################################################################
|
199 |
+
#
|
200 |
+
# DB-Ingestion functions
|
201 |
+
|
202 |
+
def add_media_to_database(*args, **kwargs):
|
203 |
+
if db_type == 'sqlite':
|
204 |
+
result = sqlite_add_media_to_database(*args, **kwargs)
|
205 |
+
|
206 |
+
# Extract content
|
207 |
+
segments = args[2]
|
208 |
+
if isinstance(segments, list):
|
209 |
+
content = ' '.join([segment.get('Text', '') for segment in segments if 'Text' in segment])
|
210 |
+
elif isinstance(segments, dict):
|
211 |
+
content = segments.get('text', '') or segments.get('content', '')
|
212 |
+
else:
|
213 |
+
content = str(segments)
|
214 |
+
|
215 |
+
# Extract media_id from the result
|
216 |
+
# Assuming the result is in the format "Media 'Title' added/updated successfully with ID: {media_id}"
|
217 |
+
import re
|
218 |
+
match = re.search(r"with ID: (\d+)", result)
|
219 |
+
if match:
|
220 |
+
media_id = int(match.group(1))
|
221 |
+
|
222 |
+
# Create initial document version
|
223 |
+
sqlite_create_document_version(media_id, content)
|
224 |
+
|
225 |
+
return result
|
226 |
+
elif db_type == 'elasticsearch':
|
227 |
+
# Implement Elasticsearch version
|
228 |
+
raise NotImplementedError("Elasticsearch version of add_media_to_database not yet implemented")
|
229 |
+
|
230 |
+
|
231 |
+
def import_obsidian_note_to_db(*args, **kwargs):
|
232 |
+
if db_type == 'sqlite':
|
233 |
+
return sqlite_import_obsidian_note_to_db(*args, **kwargs)
|
234 |
+
elif db_type == 'elasticsearch':
|
235 |
+
# Implement Elasticsearch version
|
236 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
237 |
+
|
238 |
+
|
239 |
+
def update_media_content(*args, **kwargs):
|
240 |
+
if db_type == 'sqlite':
|
241 |
+
result = sqlite_update_media_content(*args, **kwargs)
|
242 |
+
|
243 |
+
# Extract media_id and content
|
244 |
+
selected_item = args[0]
|
245 |
+
item_mapping = args[1]
|
246 |
+
content_input = args[2]
|
247 |
+
|
248 |
+
if selected_item and item_mapping and selected_item in item_mapping:
|
249 |
+
media_id = item_mapping[selected_item]
|
250 |
+
|
251 |
+
# Create new document version
|
252 |
+
sqlite_create_document_version(media_id, content_input)
|
253 |
+
|
254 |
+
return result
|
255 |
+
elif db_type == 'elasticsearch':
|
256 |
+
# Implement Elasticsearch version
|
257 |
+
raise NotImplementedError("Elasticsearch version of update_media_content not yet implemented")
|
258 |
+
|
259 |
+
|
260 |
+
def add_media_with_keywords(*args, **kwargs):
|
261 |
+
if db_type == 'sqlite':
|
262 |
+
return sqlite_add_media_with_keywords(*args, **kwargs)
|
263 |
+
elif db_type == 'elasticsearch':
|
264 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
265 |
+
|
266 |
+
def check_media_and_whisper_model(*args, **kwargs):
|
267 |
+
if db_type == 'sqlite':
|
268 |
+
return sqlite_check_media_and_whisper_model(*args, **kwargs)
|
269 |
+
elif db_type == 'elasticsearch':
|
270 |
+
raise NotImplementedError("Elasticsearch version of check_media_and_whisper_model not yet implemented")
|
271 |
+
|
272 |
+
def ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt):
|
273 |
+
if db_type == 'sqlite':
|
274 |
+
return sqlite_ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt)
|
275 |
+
elif db_type == 'elasticsearch':
|
276 |
+
# Implement Elasticsearch version
|
277 |
+
raise NotImplementedError("Elasticsearch version of ingest_article_to_db not yet implemented")
|
278 |
+
else:
|
279 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
280 |
+
|
281 |
+
#
|
282 |
+
# End of DB-Ingestion functions
|
283 |
+
############################################################################################################
|
284 |
+
|
285 |
+
|
286 |
+
############################################################################################################
|
287 |
+
#
|
288 |
+
# Prompt-related functions
|
289 |
+
|
290 |
+
def list_prompts(*args, **kwargs):
|
291 |
+
if db_type == 'sqlite':
|
292 |
+
return sqlite_list_prompts(*args, **kwargs)
|
293 |
+
elif db_type == 'elasticsearch':
|
294 |
+
# Implement Elasticsearch version
|
295 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
296 |
+
|
297 |
+
|
298 |
+
def fetch_prompt_details(*args, **kwargs):
|
299 |
+
if db_type == 'sqlite':
|
300 |
+
return sqlite_fetch_prompt_details(*args, **kwargs)
|
301 |
+
elif db_type == 'elasticsearch':
|
302 |
+
# Implement Elasticsearch version
|
303 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
304 |
+
|
305 |
+
def add_prompt(*args, **kwargs):
|
306 |
+
if db_type == 'sqlite':
|
307 |
+
return sqlite_add_prompt(*args, **kwargs)
|
308 |
+
elif db_type == 'elasticsearch':
|
309 |
+
# Implement Elasticsearch version
|
310 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
311 |
+
|
312 |
+
|
313 |
+
def add_or_update_prompt(*args, **kwargs):
|
314 |
+
if db_type == 'sqlite':
|
315 |
+
return sqlite_add_or_update_prompt(*args, **kwargs)
|
316 |
+
elif db_type == 'elasticsearch':
|
317 |
+
# Implement Elasticsearch version
|
318 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
319 |
+
|
320 |
+
def load_prompt_details(*args, **kwargs):
|
321 |
+
if db_type == 'sqlite':
|
322 |
+
return sqlite_load_prompt_details(*args, **kwargs)
|
323 |
+
elif db_type == 'elasticsearch':
|
324 |
+
# Implement Elasticsearch version
|
325 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
326 |
+
|
327 |
+
def load_preset_prompts(*args, **kwargs):
|
328 |
+
if db_type == 'sqlite':
|
329 |
+
return sqlite_load_preset_prompts(*args, **kwargs)
|
330 |
+
elif db_type == 'elasticsearch':
|
331 |
+
# Implement Elasticsearch version
|
332 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
333 |
+
|
334 |
+
def insert_prompt_to_db(*args, **kwargs):
|
335 |
+
if db_type == 'sqlite':
|
336 |
+
return sqlite_insert_prompt_to_db(*args, **kwargs)
|
337 |
+
elif db_type == 'elasticsearch':
|
338 |
+
# Implement Elasticsearch version
|
339 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
340 |
+
|
341 |
+
def delete_prompt(*args, **kwargs):
|
342 |
+
if db_type == 'sqlite':
|
343 |
+
return sqlite_delete_prompt(*args, **kwargs)
|
344 |
+
elif db_type == 'elasticsearch':
|
345 |
+
# Implement Elasticsearch version
|
346 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
347 |
+
|
348 |
+
#
|
349 |
+
# End of Prompt-related functions
|
350 |
+
############################################################################################################
|
351 |
+
|
352 |
+
############################################################################################################
|
353 |
+
#
|
354 |
+
# Keywords-related Functions
|
355 |
+
|
356 |
+
def keywords_browser_interface(*args, **kwargs):
|
357 |
+
if db_type == 'sqlite':
|
358 |
+
return sqlite_keywords_browser_interface(*args, **kwargs)
|
359 |
+
elif db_type == 'elasticsearch':
|
360 |
+
# Implement Elasticsearch version
|
361 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
362 |
+
|
363 |
+
def add_keyword(*args, **kwargs):
|
364 |
+
if db_type == 'sqlite':
|
365 |
+
with db.get_connection() as conn:
|
366 |
+
cursor = conn.cursor()
|
367 |
+
return sqlite_add_keyword(*args, **kwargs)
|
368 |
+
elif db_type == 'elasticsearch':
|
369 |
+
# Implement Elasticsearch version
|
370 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
371 |
+
|
372 |
+
def delete_keyword(*args, **kwargs):
|
373 |
+
if db_type == 'sqlite':
|
374 |
+
return sqlite_delete_keyword(*args, **kwargs)
|
375 |
+
elif db_type == 'elasticsearch':
|
376 |
+
# Implement Elasticsearch version
|
377 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
378 |
+
|
379 |
+
def export_keywords_to_csv(*args, **kwargs):
|
380 |
+
if db_type == 'sqlite':
|
381 |
+
return sqlite_export_keywords_to_csv(*args, **kwargs)
|
382 |
+
elif db_type == 'elasticsearch':
|
383 |
+
# Implement Elasticsearch version
|
384 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
385 |
+
|
386 |
+
#
|
387 |
+
# End of Keywords-related Functions
|
388 |
+
############################################################################################################
|
389 |
+
|
390 |
+
############################################################################################################
|
391 |
+
#
|
392 |
+
# Chat-related Functions
|
393 |
+
|
394 |
+
def delete_chat_message(*args, **kwargs):
|
395 |
+
if db_type == 'sqlite':
|
396 |
+
return sqlite_delete_chat_message(*args, **kwargs)
|
397 |
+
elif db_type == 'elasticsearch':
|
398 |
+
# Implement Elasticsearch version
|
399 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
400 |
+
|
401 |
+
def update_chat_message(*args, **kwargs):
|
402 |
+
if db_type == 'sqlite':
|
403 |
+
return sqlite_update_chat_message(*args, **kwargs)
|
404 |
+
elif db_type == 'elasticsearch':
|
405 |
+
# Implement Elasticsearch version
|
406 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
407 |
+
|
408 |
+
def add_chat_message(*args, **kwargs):
|
409 |
+
if db_type == 'sqlite':
|
410 |
+
return sqlite_add_chat_message(*args, **kwargs)
|
411 |
+
elif db_type == 'elasticsearch':
|
412 |
+
# Implement Elasticsearch version
|
413 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
414 |
+
|
415 |
+
def get_chat_messages(*args, **kwargs):
|
416 |
+
if db_type == 'sqlite':
|
417 |
+
return sqlite_get_chat_messages(*args, **kwargs)
|
418 |
+
elif db_type == 'elasticsearch':
|
419 |
+
# Implement Elasticsearch version
|
420 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
421 |
+
|
422 |
+
def search_chat_conversations(*args, **kwargs):
|
423 |
+
if db_type == 'sqlite':
|
424 |
+
return sqlite_search_chat_conversations(*args, **kwargs)
|
425 |
+
elif db_type == 'elasticsearch':
|
426 |
+
# Implement Elasticsearch version
|
427 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
428 |
+
|
429 |
+
def create_chat_conversation(*args, **kwargs):
|
430 |
+
if db_type == 'sqlite':
|
431 |
+
return sqlite_create_chat_conversation(*args, **kwargs)
|
432 |
+
elif db_type == 'elasticsearch':
|
433 |
+
# Implement Elasticsearch version
|
434 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
435 |
+
|
436 |
+
def save_chat_history_to_database(*args, **kwargs):
|
437 |
+
if db_type == 'sqlite':
|
438 |
+
return sqlite_save_chat_history_to_database(*args, **kwargs)
|
439 |
+
elif db_type == 'elasticsearch':
|
440 |
+
# Implement Elasticsearch version
|
441 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
442 |
+
|
443 |
+
def get_conversation_name(*args, **kwargs):
|
444 |
+
if db_type == 'sqlite':
|
445 |
+
return sqlite_get_conversation_name(*args, **kwargs)
|
446 |
+
elif db_type == 'elasticsearch':
|
447 |
+
# Implement Elasticsearch version
|
448 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
449 |
+
|
450 |
+
#
|
451 |
+
# End of Chat-related Functions
|
452 |
+
############################################################################################################
|
453 |
+
|
454 |
+
############################################################################################################
|
455 |
+
#
|
456 |
+
# Trash-related Functions
|
457 |
+
|
458 |
+
def get_trashed_items(*args, **kwargs):
|
459 |
+
if db_type == 'sqlite':
|
460 |
+
return sqlite_get_trashed_items(*args, **kwargs)
|
461 |
+
elif db_type == 'elasticsearch':
|
462 |
+
# Implement Elasticsearch version
|
463 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
464 |
+
|
465 |
+
def user_delete_item(*args, **kwargs):
|
466 |
+
if db_type == 'sqlite':
|
467 |
+
return sqlite_user_delete_item(*args, **kwargs)
|
468 |
+
elif db_type == 'elasticsearch':
|
469 |
+
# Implement Elasticsearch version
|
470 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
471 |
+
|
472 |
+
def empty_trash(*args, **kwargs):
|
473 |
+
if db_type == 'sqlite':
|
474 |
+
return sqlite_empty_trash(*args, **kwargs)
|
475 |
+
elif db_type == 'elasticsearch':
|
476 |
+
# Implement Elasticsearch version
|
477 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
478 |
+
|
479 |
+
#
|
480 |
+
# End of Trash-related Functions
|
481 |
+
############################################################################################################
|
482 |
+
|
483 |
+
|
484 |
+
############################################################################################################
|
485 |
+
#
|
486 |
+
# DB-Backup Functions
|
487 |
+
|
488 |
+
def create_automated_backup(*args, **kwargs):
|
489 |
+
if db_type == 'sqlite':
|
490 |
+
return sqlite_create_automated_backup(*args, **kwargs)
|
491 |
+
elif db_type == 'elasticsearch':
|
492 |
+
# Implement Elasticsearch version
|
493 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
494 |
+
|
495 |
+
#
|
496 |
+
# End of DB-Backup Functions
|
497 |
+
############################################################################################################
|
498 |
+
|
499 |
+
|
500 |
+
############################################################################################################
|
501 |
+
#
|
502 |
+
# Document Versioning Functions
|
503 |
+
|
504 |
+
def create_document_version(*args, **kwargs):
|
505 |
+
if db_type == 'sqlite':
|
506 |
+
return sqlite_create_document_version(*args, **kwargs)
|
507 |
+
elif db_type == 'elasticsearch':
|
508 |
+
# Implement Elasticsearch version
|
509 |
+
raise NotImplementedError("Elasticsearch version of create_document_version not yet implemented")
|
510 |
+
|
511 |
+
def get_document_version(*args, **kwargs):
|
512 |
+
if db_type == 'sqlite':
|
513 |
+
return sqlite_get_document_version(*args, **kwargs)
|
514 |
+
elif db_type == 'elasticsearch':
|
515 |
+
# Implement Elasticsearch version
|
516 |
+
raise NotImplementedError("Elasticsearch version of get_document_version not yet implemented")
|
517 |
+
|
518 |
+
#
|
519 |
+
# End of Document Versioning Functions
|
520 |
+
############################################################################################################
|
521 |
+
|
522 |
+
|
523 |
+
|
524 |
+
############################################################################################################
|
525 |
+
#
|
526 |
+
# Function to close the database connection for SQLite
|
527 |
+
|
528 |
+
def close_connection():
|
529 |
+
if db_type == 'sqlite':
|
530 |
+
db.close_all_connections()
|
531 |
+
# Elasticsearch doesn't need explicit closing
|
532 |
+
|
533 |
+
#
|
534 |
+
# End of file
|
535 |
+
############################################################################################################
|