Spaces:
Running
Running
oceansweep
commited on
Commit
•
639f0d0
1
Parent(s):
2634a4c
Upload 2 files
Browse files
App_Function_Libraries/DB/DB_Manager.py
CHANGED
@@ -1,535 +1,866 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
import
|
8 |
-
|
9 |
-
from
|
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 |
-
def
|
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 |
-
def
|
402 |
-
if db_type == 'sqlite':
|
403 |
-
|
404 |
-
|
405 |
-
#
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
def
|
423 |
-
if db_type == 'sqlite':
|
424 |
-
return
|
425 |
-
elif db_type == 'elasticsearch':
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
raise
|
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 |
-
raise
|
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 |
-
def
|
512 |
-
if db_type == 'sqlite':
|
513 |
-
return
|
514 |
-
elif db_type == 'elasticsearch':
|
515 |
-
# Implement Elasticsearch version
|
516 |
-
raise NotImplementedError("Elasticsearch version of
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# DB_Manager.py
|
2 |
+
# Description: This file contains the DatabaseManager class, which is responsible for managing the database connection, i.e. either SQLite or Elasticsearch.
|
3 |
+
#
|
4 |
+
# Imports
|
5 |
+
import configparser
|
6 |
+
import os
|
7 |
+
import logging
|
8 |
+
import threading
|
9 |
+
from contextlib import contextmanager
|
10 |
+
from typing import Tuple, List, Union, Dict
|
11 |
+
import sqlite3
|
12 |
+
import time
|
13 |
+
#
|
14 |
+
# 3rd-Party Libraries
|
15 |
+
from elasticsearch import Elasticsearch
|
16 |
+
#
|
17 |
+
# Import your existing SQLite functions
|
18 |
+
from App_Function_Libraries.DB.SQLite_DB import (
|
19 |
+
update_media_content as sqlite_update_media_content,
|
20 |
+
list_prompts as sqlite_list_prompts,
|
21 |
+
search_and_display as sqlite_search_and_display,
|
22 |
+
fetch_prompt_details as sqlite_fetch_prompt_details,
|
23 |
+
keywords_browser_interface as sqlite_keywords_browser_interface,
|
24 |
+
add_keyword as sqlite_add_keyword,
|
25 |
+
delete_keyword as sqlite_delete_keyword,
|
26 |
+
export_keywords_to_csv as sqlite_export_keywords_to_csv,
|
27 |
+
ingest_article_to_db as sqlite_ingest_article_to_db,
|
28 |
+
add_media_to_database as sqlite_add_media_to_database,
|
29 |
+
import_obsidian_note_to_db as sqlite_import_obsidian_note_to_db,
|
30 |
+
add_prompt as sqlite_add_prompt,
|
31 |
+
delete_chat_message as sqlite_delete_chat_message,
|
32 |
+
update_chat_message as sqlite_update_chat_message,
|
33 |
+
add_chat_message as sqlite_add_chat_message,
|
34 |
+
get_chat_messages as sqlite_get_chat_messages,
|
35 |
+
search_chat_conversations as sqlite_search_chat_conversations,
|
36 |
+
create_chat_conversation as sqlite_create_chat_conversation,
|
37 |
+
save_chat_history_to_database as sqlite_save_chat_history_to_database,
|
38 |
+
view_database as sqlite_view_database,
|
39 |
+
get_transcripts as sqlite_get_transcripts,
|
40 |
+
get_trashed_items as sqlite_get_trashed_items,
|
41 |
+
user_delete_item as sqlite_user_delete_item,
|
42 |
+
empty_trash as sqlite_empty_trash,
|
43 |
+
create_automated_backup as sqlite_create_automated_backup,
|
44 |
+
add_or_update_prompt as sqlite_add_or_update_prompt,
|
45 |
+
load_prompt_details as sqlite_load_prompt_details,
|
46 |
+
load_preset_prompts as sqlite_load_preset_prompts,
|
47 |
+
insert_prompt_to_db as sqlite_insert_prompt_to_db,
|
48 |
+
delete_prompt as sqlite_delete_prompt,
|
49 |
+
search_and_display_items as sqlite_search_and_display_items,
|
50 |
+
get_conversation_name as sqlite_get_conversation_name,
|
51 |
+
add_media_with_keywords as sqlite_add_media_with_keywords,
|
52 |
+
check_media_and_whisper_model as sqlite_check_media_and_whisper_model,
|
53 |
+
DatabaseError, create_document_version as sqlite_create_document_version,
|
54 |
+
get_document_version as sqlite_get_document_version, sqlite_search_db, sqlite_add_media_chunk,
|
55 |
+
sqlite_update_fts_for_media, sqlite_get_unprocessed_media, fetch_item_details as sqlite_fetch_item_details, \
|
56 |
+
search_media_database as sqlite_search_media_database, mark_as_trash as sqlite_mark_as_trash, \
|
57 |
+
get_media_transcripts as sqlite_get_media_transcripts, get_specific_transcript as sqlite_get_specific_transcript, \
|
58 |
+
get_media_summaries as sqlite_get_media_summaries, get_specific_summary as sqlite_get_specific_summary, \
|
59 |
+
get_media_prompts as sqlite_get_media_prompts, get_specific_prompt as sqlite_get_specific_prompt, \
|
60 |
+
delete_specific_transcript as sqlite_delete_specific_transcript, delete_specific_summary as sqlite_delete_specific_summary, \
|
61 |
+
delete_specific_prompt as sqlite_delete_specific_prompt, fetch_keywords_for_media as sqlite_fetch_keywords_for_media, \
|
62 |
+
update_keywords_for_media as sqlite_update_keywords_for_media, check_media_exists as sqlite_check_media_exists, \
|
63 |
+
search_prompts as sqlite_search_prompts, get_media_content as sqlite_get_media_content, \
|
64 |
+
get_paginated_files as sqlite_get_paginated_files, get_media_title as sqlite_get_media_title, \
|
65 |
+
get_all_content_from_database as sqlite_get_all_content_from_database,
|
66 |
+
)
|
67 |
+
#
|
68 |
+
# Local Imports
|
69 |
+
from App_Function_Libraries.Utils.Utils import load_comprehensive_config, get_database_path, get_project_relative_path
|
70 |
+
#
|
71 |
+
# End of imports
|
72 |
+
############################################################################################################
|
73 |
+
#
|
74 |
+
# Globals
|
75 |
+
|
76 |
+
# Load configuration from config file
|
77 |
+
config_path = get_project_relative_path('Config_Files/config.txt')
|
78 |
+
config = configparser.ConfigParser()
|
79 |
+
config.read(config_path)
|
80 |
+
|
81 |
+
db_path: str = config.get('Database', 'sqlite_path', fallback='./Databases/media_summary.db')
|
82 |
+
|
83 |
+
backup_path: str = config.get('Database', 'backup_path', fallback='database_backups')
|
84 |
+
|
85 |
+
backup_dir: Union[str, bytes] = os.environ.get('DB_BACKUP_DIR', backup_path)
|
86 |
+
|
87 |
+
#
|
88 |
+
# End of Globals
|
89 |
+
############################################################################################################
|
90 |
+
#
|
91 |
+
# Database Manager Class
|
92 |
+
|
93 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
94 |
+
logger = logging.getLogger(__name__)
|
95 |
+
|
96 |
+
|
97 |
+
class Database:
|
98 |
+
def __init__(self, db_name='media_summary.db'):
|
99 |
+
self.db_path = get_database_path(db_name)
|
100 |
+
self.pool = []
|
101 |
+
self.pool_size = 10
|
102 |
+
self.lock = threading.Lock()
|
103 |
+
self.timeout = 60.0 # 60 seconds timeout
|
104 |
+
|
105 |
+
@contextmanager
|
106 |
+
def get_connection(self):
|
107 |
+
retry_count = 5
|
108 |
+
retry_delay = 1
|
109 |
+
while retry_count > 0:
|
110 |
+
try:
|
111 |
+
if self.pool:
|
112 |
+
conn = self.pool.pop()
|
113 |
+
else:
|
114 |
+
conn = sqlite3.connect(self.db_path, timeout=self.timeout, check_same_thread=False)
|
115 |
+
conn.execute("PRAGMA journal_mode=WAL;") # Enable WAL mode
|
116 |
+
yield conn
|
117 |
+
self.pool.append(conn)
|
118 |
+
return
|
119 |
+
except sqlite3.OperationalError as e:
|
120 |
+
if 'database is locked' in str(e):
|
121 |
+
logger.warning(f"Database is locked, retrying in {retry_delay} seconds...")
|
122 |
+
retry_count -= 1
|
123 |
+
time.sleep(retry_delay)
|
124 |
+
retry_delay *= 2 # Exponential backoff
|
125 |
+
else:
|
126 |
+
raise DatabaseError(f"Database error: {e}")
|
127 |
+
except Exception as e:
|
128 |
+
raise DatabaseError(f"Unexpected error: {e}")
|
129 |
+
raise DatabaseError("Database is locked and retries have been exhausted")
|
130 |
+
|
131 |
+
def execute_query(self, query: str, params: Tuple = ()) -> None:
|
132 |
+
with self.lock: # Use a global lock for write operations
|
133 |
+
with self.get_connection() as conn:
|
134 |
+
try:
|
135 |
+
cursor = conn.cursor()
|
136 |
+
cursor.execute(query, params)
|
137 |
+
conn.commit()
|
138 |
+
except sqlite3.Error as e:
|
139 |
+
logger.error(f"Database error: {e}, Query: {query}")
|
140 |
+
raise DatabaseError(f"Database error: {e}, Query: {query}")
|
141 |
+
|
142 |
+
def execute_many(self, query: str, params_list: List[Tuple]) -> None:
|
143 |
+
with self.lock: # Use a global lock for write operations
|
144 |
+
with self.get_connection() as conn:
|
145 |
+
try:
|
146 |
+
cursor = conn.cursor()
|
147 |
+
cursor.executemany(query, params_list)
|
148 |
+
conn.commit()
|
149 |
+
except sqlite3.Error as e:
|
150 |
+
logger.error(f"Database error: {e}, Query: {query}")
|
151 |
+
raise DatabaseError(f"Database error: {e}, Query: {query}")
|
152 |
+
|
153 |
+
def close_all_connections(self):
|
154 |
+
for conn in self.pool:
|
155 |
+
conn.close()
|
156 |
+
self.pool.clear()
|
157 |
+
|
158 |
+
|
159 |
+
#
|
160 |
+
# class Database:
|
161 |
+
# def __init__(self, db_name='media_summary.db'):
|
162 |
+
# self.db_path = get_database_path(db_name)
|
163 |
+
# self.pool = []
|
164 |
+
# self.pool_size = 10
|
165 |
+
#
|
166 |
+
# @contextmanager
|
167 |
+
# def get_connection(self):
|
168 |
+
# retry_count = 5
|
169 |
+
# retry_delay = 1
|
170 |
+
# while retry_count > 0:
|
171 |
+
# try:
|
172 |
+
# if self.pool:
|
173 |
+
# conn = self.pool.pop()
|
174 |
+
# else:
|
175 |
+
# conn = sqlite3.connect(self.db_path, check_same_thread=False)
|
176 |
+
# yield conn
|
177 |
+
# self.pool.append(conn)
|
178 |
+
# return
|
179 |
+
# except sqlite3.OperationalError as e:
|
180 |
+
# if 'database is locked' in str(e):
|
181 |
+
# logger.warning(f"Database is locked, retrying in {retry_delay} seconds...")
|
182 |
+
# retry_count -= 1
|
183 |
+
# time.sleep(retry_delay)
|
184 |
+
# retry_delay *= 2 # Exponential backoff
|
185 |
+
# else:
|
186 |
+
# raise DatabaseError(f"Database error: {e}")
|
187 |
+
# except Exception as e:
|
188 |
+
# raise DatabaseError(f"Unexpected error: {e}")
|
189 |
+
# raise DatabaseError("Database is locked and retries have been exhausted")
|
190 |
+
#
|
191 |
+
# def execute_query(self, query: str, params: Tuple = ()) -> None:
|
192 |
+
# with self.get_connection() as conn:
|
193 |
+
# try:
|
194 |
+
# cursor = conn.cursor()
|
195 |
+
# cursor.execute(query, params)
|
196 |
+
# conn.commit()
|
197 |
+
# except sqlite3.Error as e:
|
198 |
+
# logger.error(f"Database error: {e}, Query: {query}")
|
199 |
+
# raise DatabaseError(f"Database error: {e}, Query: {query}")
|
200 |
+
#
|
201 |
+
# def close_all_connections(self):
|
202 |
+
# for conn in self.pool:
|
203 |
+
# conn.close()
|
204 |
+
# self.pool.clear()
|
205 |
+
#
|
206 |
+
|
207 |
+
#
|
208 |
+
# End of Database Manager Class
|
209 |
+
############################################################################################################
|
210 |
+
#
|
211 |
+
# Database Config loading
|
212 |
+
|
213 |
+
def get_db_config():
|
214 |
+
try:
|
215 |
+
config = load_comprehensive_config()
|
216 |
+
|
217 |
+
if 'Database' not in config:
|
218 |
+
print("Warning: 'Database' section not found in config. Using default values.")
|
219 |
+
return default_db_config()
|
220 |
+
|
221 |
+
return {
|
222 |
+
'type': config.get('Database', 'type', fallback='sqlite'),
|
223 |
+
'sqlite_path': config.get('Database', 'sqlite_path', fallback='Databases/media_summary.db'),
|
224 |
+
'elasticsearch_host': config.get('Database', 'elasticsearch_host', fallback='localhost'),
|
225 |
+
'elasticsearch_port': config.getint('Database', 'elasticsearch_port', fallback=9200)
|
226 |
+
}
|
227 |
+
except FileNotFoundError:
|
228 |
+
print("Warning: Config file not found. Using default database configuration.")
|
229 |
+
return default_db_config()
|
230 |
+
except Exception as e:
|
231 |
+
print(f"Error reading config: {str(e)}. Using default database configuration.")
|
232 |
+
return default_db_config()
|
233 |
+
|
234 |
+
|
235 |
+
def default_db_config():
|
236 |
+
"""Return the default database configuration with project-relative paths."""
|
237 |
+
return {
|
238 |
+
'type': 'sqlite',
|
239 |
+
'sqlite_path': get_database_path('media_summary.db'),
|
240 |
+
'elasticsearch_host': 'localhost',
|
241 |
+
'elasticsearch_port': 9200
|
242 |
+
}
|
243 |
+
|
244 |
+
|
245 |
+
def ensure_directory_exists(file_path):
|
246 |
+
directory = os.path.dirname(file_path)
|
247 |
+
if not os.path.exists(directory):
|
248 |
+
os.makedirs(directory)
|
249 |
+
print(f"Created directory: {directory}")
|
250 |
+
|
251 |
+
# Use the config to set up the database
|
252 |
+
db_config = get_db_config()
|
253 |
+
db_type = db_config['type']
|
254 |
+
|
255 |
+
if db_type == 'sqlite':
|
256 |
+
db = Database(os.path.basename(db_config['sqlite_path']))
|
257 |
+
elif db_type == 'elasticsearch':
|
258 |
+
# Implement Elasticsearch setup here if needed
|
259 |
+
raise NotImplementedError("Elasticsearch support not yet implemented")
|
260 |
+
else:
|
261 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
262 |
+
|
263 |
+
# Print database path for debugging
|
264 |
+
print(f"Database path: {db.db_path}")
|
265 |
+
|
266 |
+
# Sanity Check for SQLite DB
|
267 |
+
# FIXME - Remove this after testing / Writing Unit tests
|
268 |
+
# try:
|
269 |
+
# db.execute_query("CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY)")
|
270 |
+
# logger.info("Successfully created test table")
|
271 |
+
# except DatabaseError as e:
|
272 |
+
# logger.error(f"Failed to create test table: {e}")
|
273 |
+
|
274 |
+
#
|
275 |
+
# End of Database Config loading
|
276 |
+
############################################################################################################
|
277 |
+
#
|
278 |
+
# DB Search functions
|
279 |
+
|
280 |
+
def search_db(search_query: str, search_fields: List[str], keywords: str, page: int = 1, results_per_page: int = 10):
|
281 |
+
if db_type == 'sqlite':
|
282 |
+
return sqlite_search_db(search_query, search_fields, keywords, page, results_per_page)
|
283 |
+
elif db_type == 'elasticsearch':
|
284 |
+
# Implement Elasticsearch version when available
|
285 |
+
raise NotImplementedError("Elasticsearch version of search_db not yet implemented")
|
286 |
+
else:
|
287 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
288 |
+
|
289 |
+
def view_database(*args, **kwargs):
|
290 |
+
if db_type == 'sqlite':
|
291 |
+
return sqlite_view_database(*args, **kwargs)
|
292 |
+
elif db_type == 'elasticsearch':
|
293 |
+
# Implement Elasticsearch version
|
294 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
295 |
+
|
296 |
+
def search_and_display_items(*args, **kwargs):
|
297 |
+
if db_type == 'sqlite':
|
298 |
+
return sqlite_search_and_display_items(*args, **kwargs)
|
299 |
+
elif db_type == 'elasticsearch':
|
300 |
+
# Implement Elasticsearch version
|
301 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
302 |
+
|
303 |
+
def get_all_content_from_database():
|
304 |
+
if db_type == 'sqlite':
|
305 |
+
return sqlite_get_all_content_from_database()
|
306 |
+
elif db_type == 'elasticsearch':
|
307 |
+
# Implement Elasticsearch version
|
308 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
309 |
+
|
310 |
+
def search_and_display(*args, **kwargs):
|
311 |
+
if db_type == 'sqlite':
|
312 |
+
return sqlite_search_and_display(*args, **kwargs)
|
313 |
+
elif db_type == 'elasticsearch':
|
314 |
+
# Implement Elasticsearch version
|
315 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
316 |
+
|
317 |
+
def check_media_exists(*args, **kwargs):
|
318 |
+
if db_type == 'sqlite':
|
319 |
+
return sqlite_check_media_exists(*args, **kwargs)
|
320 |
+
elif db_type == 'elasticsearch':
|
321 |
+
# Implement Elasticsearch version
|
322 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
323 |
+
|
324 |
+
def get_paginated_files(*args, **kwargs):
|
325 |
+
if db_type == 'sqlite':
|
326 |
+
return sqlite_get_paginated_files(*args, **kwargs)
|
327 |
+
elif db_type == 'elasticsearch':
|
328 |
+
# Implement Elasticsearch version
|
329 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
330 |
+
|
331 |
+
def get_media_title(*args, **kwargs):
|
332 |
+
if db_type == 'sqlite':
|
333 |
+
return sqlite_get_media_title(*args, **kwargs)
|
334 |
+
elif db_type == 'elasticsearch':
|
335 |
+
# Implement Elasticsearch version
|
336 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
337 |
+
|
338 |
+
|
339 |
+
#
|
340 |
+
# End of DB-Searching functions
|
341 |
+
############################################################################################################
|
342 |
+
|
343 |
+
|
344 |
+
############################################################################################################
|
345 |
+
#
|
346 |
+
# Transcript-related Functions
|
347 |
+
|
348 |
+
def get_transcripts(*args, **kwargs):
|
349 |
+
if db_type == 'sqlite':
|
350 |
+
return sqlite_get_transcripts(*args, **kwargs)
|
351 |
+
elif db_type == 'elasticsearch':
|
352 |
+
# Implement Elasticsearch version
|
353 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
354 |
+
|
355 |
+
#
|
356 |
+
# End of Transcript-related Functions
|
357 |
+
############################################################################################################
|
358 |
+
|
359 |
+
|
360 |
+
############################################################################################################
|
361 |
+
#
|
362 |
+
# DB-Ingestion functions
|
363 |
+
|
364 |
+
def add_media_to_database(*args, **kwargs):
|
365 |
+
if db_type == 'sqlite':
|
366 |
+
result = sqlite_add_media_to_database(*args, **kwargs)
|
367 |
+
|
368 |
+
# Extract content
|
369 |
+
segments = args[2]
|
370 |
+
if isinstance(segments, list):
|
371 |
+
content = ' '.join([segment.get('Text', '') for segment in segments if 'Text' in segment])
|
372 |
+
elif isinstance(segments, dict):
|
373 |
+
content = segments.get('text', '') or segments.get('content', '')
|
374 |
+
else:
|
375 |
+
content = str(segments)
|
376 |
+
|
377 |
+
# Extract media_id from the result
|
378 |
+
# Assuming the result is in the format "Media 'Title' added/updated successfully with ID: {media_id}"
|
379 |
+
import re
|
380 |
+
match = re.search(r"with ID: (\d+)", result)
|
381 |
+
if match:
|
382 |
+
media_id = int(match.group(1))
|
383 |
+
|
384 |
+
# Create initial document version
|
385 |
+
sqlite_create_document_version(media_id, content)
|
386 |
+
|
387 |
+
return result
|
388 |
+
elif db_type == 'elasticsearch':
|
389 |
+
# Implement Elasticsearch version
|
390 |
+
raise NotImplementedError("Elasticsearch version of add_media_to_database not yet implemented")
|
391 |
+
|
392 |
+
|
393 |
+
def import_obsidian_note_to_db(*args, **kwargs):
|
394 |
+
if db_type == 'sqlite':
|
395 |
+
return sqlite_import_obsidian_note_to_db(*args, **kwargs)
|
396 |
+
elif db_type == 'elasticsearch':
|
397 |
+
# Implement Elasticsearch version
|
398 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
399 |
+
|
400 |
+
|
401 |
+
def update_media_content(*args, **kwargs):
|
402 |
+
if db_type == 'sqlite':
|
403 |
+
result = sqlite_update_media_content(*args, **kwargs)
|
404 |
+
|
405 |
+
# Extract media_id and content
|
406 |
+
selected_item = args[0]
|
407 |
+
item_mapping = args[1]
|
408 |
+
content_input = args[2]
|
409 |
+
|
410 |
+
if selected_item and item_mapping and selected_item in item_mapping:
|
411 |
+
media_id = item_mapping[selected_item]
|
412 |
+
|
413 |
+
# Create new document version
|
414 |
+
sqlite_create_document_version(media_id, content_input)
|
415 |
+
|
416 |
+
return result
|
417 |
+
elif db_type == 'elasticsearch':
|
418 |
+
# Implement Elasticsearch version
|
419 |
+
raise NotImplementedError("Elasticsearch version of update_media_content not yet implemented")
|
420 |
+
|
421 |
+
|
422 |
+
def add_media_with_keywords(*args, **kwargs):
|
423 |
+
if db_type == 'sqlite':
|
424 |
+
return sqlite_add_media_with_keywords(*args, **kwargs)
|
425 |
+
elif db_type == 'elasticsearch':
|
426 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
427 |
+
|
428 |
+
def check_media_and_whisper_model(*args, **kwargs):
|
429 |
+
if db_type == 'sqlite':
|
430 |
+
return sqlite_check_media_and_whisper_model(*args, **kwargs)
|
431 |
+
elif db_type == 'elasticsearch':
|
432 |
+
raise NotImplementedError("Elasticsearch version of check_media_and_whisper_model not yet implemented")
|
433 |
+
|
434 |
+
def ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt):
|
435 |
+
if db_type == 'sqlite':
|
436 |
+
return sqlite_ingest_article_to_db(url, title, author, content, keywords, summary, ingestion_date, custom_prompt)
|
437 |
+
elif db_type == 'elasticsearch':
|
438 |
+
# Implement Elasticsearch version
|
439 |
+
raise NotImplementedError("Elasticsearch version of ingest_article_to_db not yet implemented")
|
440 |
+
else:
|
441 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
442 |
+
|
443 |
+
|
444 |
+
def add_media_chunk(media_id: int, chunk_text: str, start_index: int, end_index: int, chunk_id: str):
|
445 |
+
if db_type == 'sqlite':
|
446 |
+
sqlite_add_media_chunk(db, media_id, chunk_text, start_index, end_index, chunk_id)
|
447 |
+
elif db_type == 'elasticsearch':
|
448 |
+
# Implement Elasticsearch version
|
449 |
+
raise NotImplementedError("Elasticsearch version not yet implemented")
|
450 |
+
else:
|
451 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
452 |
+
|
453 |
+
def update_fts_for_media(media_id: int):
|
454 |
+
if db_type == 'sqlite':
|
455 |
+
sqlite_update_fts_for_media(db, media_id)
|
456 |
+
elif db_type == 'elasticsearch':
|
457 |
+
# Implement Elasticsearch version
|
458 |
+
raise NotImplementedError("Elasticsearch version not yet implemented")
|
459 |
+
else:
|
460 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
461 |
+
|
462 |
+
|
463 |
+
def get_unprocessed_media():
|
464 |
+
if db_type == 'sqlite':
|
465 |
+
return sqlite_get_unprocessed_media(db)
|
466 |
+
elif db_type == 'elasticsearch':
|
467 |
+
# Implement Elasticsearch version
|
468 |
+
raise NotImplementedError("Elasticsearch version of get_unprocessed_media not yet implemented")
|
469 |
+
else:
|
470 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
471 |
+
|
472 |
+
|
473 |
+
#
|
474 |
+
# End of DB-Ingestion functions
|
475 |
+
############################################################################################################
|
476 |
+
|
477 |
+
|
478 |
+
############################################################################################################
|
479 |
+
#
|
480 |
+
# Prompt-related functions #FIXME rename /resort
|
481 |
+
|
482 |
+
def list_prompts(*args, **kwargs):
|
483 |
+
if db_type == 'sqlite':
|
484 |
+
return sqlite_list_prompts(*args, **kwargs)
|
485 |
+
elif db_type == 'elasticsearch':
|
486 |
+
# Implement Elasticsearch version
|
487 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
488 |
+
|
489 |
+
def search_prompts(query):
|
490 |
+
if db_type == 'sqlite':
|
491 |
+
return sqlite_search_prompts(query)
|
492 |
+
elif db_type == 'elasticsearch':
|
493 |
+
# Implement Elasticsearch version
|
494 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
495 |
+
|
496 |
+
def fetch_prompt_details(*args, **kwargs):
|
497 |
+
if db_type == 'sqlite':
|
498 |
+
return sqlite_fetch_prompt_details(*args, **kwargs)
|
499 |
+
elif db_type == 'elasticsearch':
|
500 |
+
# Implement Elasticsearch version
|
501 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
502 |
+
|
503 |
+
def add_prompt(*args, **kwargs):
|
504 |
+
if db_type == 'sqlite':
|
505 |
+
return sqlite_add_prompt(*args, **kwargs)
|
506 |
+
elif db_type == 'elasticsearch':
|
507 |
+
# Implement Elasticsearch version
|
508 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
509 |
+
|
510 |
+
|
511 |
+
def add_or_update_prompt(*args, **kwargs):
|
512 |
+
if db_type == 'sqlite':
|
513 |
+
return sqlite_add_or_update_prompt(*args, **kwargs)
|
514 |
+
elif db_type == 'elasticsearch':
|
515 |
+
# Implement Elasticsearch version
|
516 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
517 |
+
|
518 |
+
def load_prompt_details(*args, **kwargs):
|
519 |
+
if db_type == 'sqlite':
|
520 |
+
return sqlite_load_prompt_details(*args, **kwargs)
|
521 |
+
elif db_type == 'elasticsearch':
|
522 |
+
# Implement Elasticsearch version
|
523 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
524 |
+
|
525 |
+
def load_preset_prompts(*args, **kwargs):
|
526 |
+
if db_type == 'sqlite':
|
527 |
+
return sqlite_load_preset_prompts()
|
528 |
+
elif db_type == 'elasticsearch':
|
529 |
+
# Implement Elasticsearch version
|
530 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
531 |
+
|
532 |
+
def insert_prompt_to_db(*args, **kwargs):
|
533 |
+
if db_type == 'sqlite':
|
534 |
+
return sqlite_insert_prompt_to_db(*args, **kwargs)
|
535 |
+
elif db_type == 'elasticsearch':
|
536 |
+
# Implement Elasticsearch version
|
537 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
538 |
+
|
539 |
+
def delete_prompt(*args, **kwargs):
|
540 |
+
if db_type == 'sqlite':
|
541 |
+
return sqlite_delete_prompt(*args, **kwargs)
|
542 |
+
elif db_type == 'elasticsearch':
|
543 |
+
# Implement Elasticsearch version
|
544 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
545 |
+
|
546 |
+
def search_media_database(query: str) -> List[Tuple[int, str, str]]:
|
547 |
+
if db_type == 'sqlite':
|
548 |
+
return sqlite_search_media_database(query)
|
549 |
+
elif db_type == 'elasticsearch':
|
550 |
+
# Implement Elasticsearch version when available
|
551 |
+
raise NotImplementedError("Elasticsearch version of search_media_database not yet implemented")
|
552 |
+
else:
|
553 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
554 |
+
|
555 |
+
def mark_as_trash(media_id: int) -> None:
|
556 |
+
if db_type == 'sqlite':
|
557 |
+
return sqlite_mark_as_trash(media_id)
|
558 |
+
elif db_type == 'elasticsearch':
|
559 |
+
# Implement Elasticsearch version when available
|
560 |
+
raise NotImplementedError("Elasticsearch version of mark_as_trash not yet implemented")
|
561 |
+
else:
|
562 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
563 |
+
|
564 |
+
def get_media_content(media_id: int) -> str:
|
565 |
+
if db_type == 'sqlite':
|
566 |
+
return sqlite_get_media_content(media_id)
|
567 |
+
elif db_type == 'elasticsearch':
|
568 |
+
raise NotImplementedError("Elasticsearch version of get_media_content not yet implemented")
|
569 |
+
else:
|
570 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
571 |
+
|
572 |
+
def get_media_transcripts(media_id: int) -> List[Dict]:
|
573 |
+
if db_type == 'sqlite':
|
574 |
+
return sqlite_get_media_transcripts(media_id)
|
575 |
+
elif db_type == 'elasticsearch':
|
576 |
+
raise NotImplementedError("Elasticsearch version of get_media_transcripts not yet implemented")
|
577 |
+
else:
|
578 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
579 |
+
|
580 |
+
def get_specific_transcript(transcript_id: int) -> Dict:
|
581 |
+
if db_type == 'sqlite':
|
582 |
+
return sqlite_get_specific_transcript(transcript_id)
|
583 |
+
elif db_type == 'elasticsearch':
|
584 |
+
raise NotImplementedError("Elasticsearch version of get_specific_transcript not yet implemented")
|
585 |
+
else:
|
586 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
587 |
+
|
588 |
+
def get_media_summaries(media_id: int) -> List[Dict]:
|
589 |
+
if db_type == 'sqlite':
|
590 |
+
return sqlite_get_media_summaries(media_id)
|
591 |
+
elif db_type == 'elasticsearch':
|
592 |
+
raise NotImplementedError("Elasticsearch version of get_media_summaries not yet implemented")
|
593 |
+
else:
|
594 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
595 |
+
|
596 |
+
def get_specific_summary(summary_id: int) -> Dict:
|
597 |
+
if db_type == 'sqlite':
|
598 |
+
return sqlite_get_specific_summary(summary_id)
|
599 |
+
elif db_type == 'elasticsearch':
|
600 |
+
raise NotImplementedError("Elasticsearch version of get_specific_summary not yet implemented")
|
601 |
+
else:
|
602 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
603 |
+
|
604 |
+
def get_media_prompts(media_id: int) -> List[Dict]:
|
605 |
+
if db_type == 'sqlite':
|
606 |
+
return sqlite_get_media_prompts(media_id)
|
607 |
+
elif db_type == 'elasticsearch':
|
608 |
+
raise NotImplementedError("Elasticsearch version of get_media_prompts not yet implemented")
|
609 |
+
else:
|
610 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
611 |
+
|
612 |
+
def get_specific_prompt(prompt_id: int) -> Dict:
|
613 |
+
if db_type == 'sqlite':
|
614 |
+
return sqlite_get_specific_prompt(prompt_id)
|
615 |
+
elif db_type == 'elasticsearch':
|
616 |
+
raise NotImplementedError("Elasticsearch version of get_specific_prompt not yet implemented")
|
617 |
+
else:
|
618 |
+
return {'error': f"Unsupported database type: {db_type}"}
|
619 |
+
|
620 |
+
def delete_specific_transcript(transcript_id: int) -> str:
|
621 |
+
if db_type == 'sqlite':
|
622 |
+
return sqlite_delete_specific_transcript(transcript_id)
|
623 |
+
elif db_type == 'elasticsearch':
|
624 |
+
raise NotImplementedError("Elasticsearch version of delete_specific_transcript not yet implemented")
|
625 |
+
else:
|
626 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
627 |
+
|
628 |
+
def delete_specific_summary(summary_id: int) -> str:
|
629 |
+
if db_type == 'sqlite':
|
630 |
+
return sqlite_delete_specific_summary(summary_id)
|
631 |
+
elif db_type == 'elasticsearch':
|
632 |
+
raise NotImplementedError("Elasticsearch version of delete_specific_summary not yet implemented")
|
633 |
+
else:
|
634 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
635 |
+
|
636 |
+
def delete_specific_prompt(prompt_id: int) -> str:
|
637 |
+
if db_type == 'sqlite':
|
638 |
+
return sqlite_delete_specific_prompt(prompt_id)
|
639 |
+
elif db_type == 'elasticsearch':
|
640 |
+
raise NotImplementedError("Elasticsearch version of delete_specific_prompt not yet implemented")
|
641 |
+
else:
|
642 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
643 |
+
|
644 |
+
|
645 |
+
#
|
646 |
+
# End of Prompt-related functions
|
647 |
+
############################################################################################################
|
648 |
+
|
649 |
+
############################################################################################################
|
650 |
+
#
|
651 |
+
# Keywords-related Functions
|
652 |
+
|
653 |
+
def keywords_browser_interface(*args, **kwargs):
|
654 |
+
if db_type == 'sqlite':
|
655 |
+
return sqlite_keywords_browser_interface()
|
656 |
+
elif db_type == 'elasticsearch':
|
657 |
+
# Implement Elasticsearch version
|
658 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
659 |
+
|
660 |
+
def add_keyword(*args, **kwargs):
|
661 |
+
if db_type == 'sqlite':
|
662 |
+
with db.get_connection() as conn:
|
663 |
+
cursor = conn.cursor()
|
664 |
+
return sqlite_add_keyword(*args, **kwargs)
|
665 |
+
elif db_type == 'elasticsearch':
|
666 |
+
# Implement Elasticsearch version
|
667 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
668 |
+
|
669 |
+
def delete_keyword(*args, **kwargs):
|
670 |
+
if db_type == 'sqlite':
|
671 |
+
return sqlite_delete_keyword(*args, **kwargs)
|
672 |
+
elif db_type == 'elasticsearch':
|
673 |
+
# Implement Elasticsearch version
|
674 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
675 |
+
|
676 |
+
def export_keywords_to_csv(*args, **kwargs):
|
677 |
+
if db_type == 'sqlite':
|
678 |
+
return sqlite_export_keywords_to_csv()
|
679 |
+
elif db_type == 'elasticsearch':
|
680 |
+
# Implement Elasticsearch version
|
681 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
682 |
+
|
683 |
+
def update_keywords_for_media(*args, **kwargs):
|
684 |
+
if db_type == 'sqlite':
|
685 |
+
return sqlite_update_keywords_for_media(*args, **kwargs)
|
686 |
+
elif db_type == 'elasticsearch':
|
687 |
+
# Implement Elasticsearch version
|
688 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
689 |
+
|
690 |
+
def fetch_keywords_for_media(*args, **kwargs):
|
691 |
+
if db_type == 'sqlite':
|
692 |
+
return sqlite_fetch_keywords_for_media(*args, **kwargs)
|
693 |
+
elif db_type == 'elasticsearch':
|
694 |
+
# Implement Elasticsearch version
|
695 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
696 |
+
|
697 |
+
#
|
698 |
+
# End of Keywords-related Functions
|
699 |
+
############################################################################################################
|
700 |
+
|
701 |
+
############################################################################################################
|
702 |
+
#
|
703 |
+
# Chat-related Functions
|
704 |
+
|
705 |
+
def delete_chat_message(*args, **kwargs):
|
706 |
+
if db_type == 'sqlite':
|
707 |
+
return sqlite_delete_chat_message(*args, **kwargs)
|
708 |
+
elif db_type == 'elasticsearch':
|
709 |
+
# Implement Elasticsearch version
|
710 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
711 |
+
|
712 |
+
def update_chat_message(*args, **kwargs):
|
713 |
+
if db_type == 'sqlite':
|
714 |
+
return sqlite_update_chat_message(*args, **kwargs)
|
715 |
+
elif db_type == 'elasticsearch':
|
716 |
+
# Implement Elasticsearch version
|
717 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
718 |
+
|
719 |
+
def add_chat_message(*args, **kwargs):
|
720 |
+
if db_type == 'sqlite':
|
721 |
+
return sqlite_add_chat_message(*args, **kwargs)
|
722 |
+
elif db_type == 'elasticsearch':
|
723 |
+
# Implement Elasticsearch version
|
724 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
725 |
+
|
726 |
+
def get_chat_messages(*args, **kwargs):
|
727 |
+
if db_type == 'sqlite':
|
728 |
+
return sqlite_get_chat_messages(*args, **kwargs)
|
729 |
+
elif db_type == 'elasticsearch':
|
730 |
+
# Implement Elasticsearch version
|
731 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
732 |
+
|
733 |
+
def search_chat_conversations(*args, **kwargs):
|
734 |
+
if db_type == 'sqlite':
|
735 |
+
return sqlite_search_chat_conversations(*args, **kwargs)
|
736 |
+
elif db_type == 'elasticsearch':
|
737 |
+
# Implement Elasticsearch version
|
738 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
739 |
+
|
740 |
+
def create_chat_conversation(*args, **kwargs):
|
741 |
+
if db_type == 'sqlite':
|
742 |
+
return sqlite_create_chat_conversation(*args, **kwargs)
|
743 |
+
elif db_type == 'elasticsearch':
|
744 |
+
# Implement Elasticsearch version
|
745 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
746 |
+
|
747 |
+
def save_chat_history_to_database(*args, **kwargs):
|
748 |
+
if db_type == 'sqlite':
|
749 |
+
return sqlite_save_chat_history_to_database(*args, **kwargs)
|
750 |
+
elif db_type == 'elasticsearch':
|
751 |
+
# Implement Elasticsearch version
|
752 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
753 |
+
|
754 |
+
def get_conversation_name(*args, **kwargs):
|
755 |
+
if db_type == 'sqlite':
|
756 |
+
return sqlite_get_conversation_name(*args, **kwargs)
|
757 |
+
elif db_type == 'elasticsearch':
|
758 |
+
# Implement Elasticsearch version
|
759 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
760 |
+
|
761 |
+
#
|
762 |
+
# End of Chat-related Functions
|
763 |
+
############################################################################################################
|
764 |
+
|
765 |
+
############################################################################################################
|
766 |
+
#
|
767 |
+
# Trash-related Functions
|
768 |
+
|
769 |
+
def get_trashed_items(*args, **kwargs):
|
770 |
+
if db_type == 'sqlite':
|
771 |
+
return sqlite_get_trashed_items()
|
772 |
+
elif db_type == 'elasticsearch':
|
773 |
+
# Implement Elasticsearch version
|
774 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
775 |
+
|
776 |
+
def user_delete_item(*args, **kwargs):
|
777 |
+
if db_type == 'sqlite':
|
778 |
+
return sqlite_user_delete_item(*args, **kwargs)
|
779 |
+
elif db_type == 'elasticsearch':
|
780 |
+
# Implement Elasticsearch version
|
781 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
782 |
+
|
783 |
+
def empty_trash(*args, **kwargs):
|
784 |
+
if db_type == 'sqlite':
|
785 |
+
return sqlite_empty_trash(*args, **kwargs)
|
786 |
+
elif db_type == 'elasticsearch':
|
787 |
+
# Implement Elasticsearch version
|
788 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
789 |
+
|
790 |
+
|
791 |
+
def fetch_item_details(media_id: int) -> Tuple[str, str, str]:
|
792 |
+
"""
|
793 |
+
Fetch the details of a media item including content, prompt, and summary.
|
794 |
+
|
795 |
+
Args:
|
796 |
+
media_id (int): The ID of the media item.
|
797 |
+
|
798 |
+
Returns:
|
799 |
+
Tuple[str, str, str]: A tuple containing (content, prompt, summary).
|
800 |
+
If an error occurs, it returns empty strings for each field.
|
801 |
+
"""
|
802 |
+
if db_type == 'sqlite':
|
803 |
+
return sqlite_fetch_item_details(media_id)
|
804 |
+
elif db_type == 'elasticsearch':
|
805 |
+
# Implement Elasticsearch version when available
|
806 |
+
raise NotImplementedError("Elasticsearch version of fetch_item_details not yet implemented")
|
807 |
+
else:
|
808 |
+
raise ValueError(f"Unsupported database type: {db_type}")
|
809 |
+
|
810 |
+
#
|
811 |
+
# End of Trash-related Functions
|
812 |
+
############################################################################################################
|
813 |
+
|
814 |
+
|
815 |
+
############################################################################################################
|
816 |
+
#
|
817 |
+
# DB-Backup Functions
|
818 |
+
|
819 |
+
def create_automated_backup(*args, **kwargs):
|
820 |
+
if db_type == 'sqlite':
|
821 |
+
return sqlite_create_automated_backup(*args, **kwargs)
|
822 |
+
elif db_type == 'elasticsearch':
|
823 |
+
# Implement Elasticsearch version
|
824 |
+
raise NotImplementedError("Elasticsearch version of add_media_with_keywords not yet implemented")
|
825 |
+
|
826 |
+
#
|
827 |
+
# End of DB-Backup Functions
|
828 |
+
############################################################################################################
|
829 |
+
|
830 |
+
|
831 |
+
############################################################################################################
|
832 |
+
#
|
833 |
+
# Document Versioning Functions
|
834 |
+
|
835 |
+
def create_document_version(*args, **kwargs):
|
836 |
+
if db_type == 'sqlite':
|
837 |
+
return sqlite_create_document_version(*args, **kwargs)
|
838 |
+
elif db_type == 'elasticsearch':
|
839 |
+
# Implement Elasticsearch version
|
840 |
+
raise NotImplementedError("Elasticsearch version of create_document_version not yet implemented")
|
841 |
+
|
842 |
+
def get_document_version(*args, **kwargs):
|
843 |
+
if db_type == 'sqlite':
|
844 |
+
return sqlite_get_document_version(*args, **kwargs)
|
845 |
+
elif db_type == 'elasticsearch':
|
846 |
+
# Implement Elasticsearch version
|
847 |
+
raise NotImplementedError("Elasticsearch version of get_document_version not yet implemented")
|
848 |
+
|
849 |
+
#
|
850 |
+
# End of Document Versioning Functions
|
851 |
+
############################################################################################################
|
852 |
+
|
853 |
+
|
854 |
+
|
855 |
+
############################################################################################################
|
856 |
+
#
|
857 |
+
# Function to close the database connection for SQLite
|
858 |
+
|
859 |
+
def close_connection():
|
860 |
+
if db_type == 'sqlite':
|
861 |
+
db.close_all_connections()
|
862 |
+
# Elasticsearch doesn't need explicit closing
|
863 |
+
|
864 |
+
#
|
865 |
+
# End of file
|
866 |
+
############################################################################################################
|
App_Function_Libraries/DB/SQLite_DB.py
CHANGED
The diff for this file is too large to render.
See raw diff
|
|