Update utils/database.py
Browse files- utils/database.py +148 -0
utils/database.py
CHANGED
|
@@ -496,7 +496,155 @@ def handle_document_upload(uploaded_files, **kwargs):
|
|
| 496 |
st.session_state.chat_ready = False
|
| 497 |
return False
|
| 498 |
|
|
|
|
| 499 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
|
| 501 |
def get_all_documents(conn: sqlite3.Connection) -> List[Dict]:
|
| 502 |
"""
|
|
|
|
| 496 |
st.session_state.chat_ready = False
|
| 497 |
return False
|
| 498 |
|
| 499 |
+
# Add these to your database.py file
|
| 500 |
|
| 501 |
+
def remove_from_collection(conn: sqlite3.Connection, document_id: int, collection_id: int) -> bool:
|
| 502 |
+
"""
|
| 503 |
+
Remove a document from a collection.
|
| 504 |
+
|
| 505 |
+
Args:
|
| 506 |
+
conn (sqlite3.Connection): Database connection
|
| 507 |
+
document_id (int): ID of the document to remove
|
| 508 |
+
collection_id (int): ID of the collection
|
| 509 |
+
|
| 510 |
+
Returns:
|
| 511 |
+
bool: True if successful
|
| 512 |
+
"""
|
| 513 |
+
try:
|
| 514 |
+
with conn_lock:
|
| 515 |
+
cursor = conn.cursor()
|
| 516 |
+
cursor.execute('''
|
| 517 |
+
DELETE FROM document_collections
|
| 518 |
+
WHERE document_id = ? AND collection_id = ?
|
| 519 |
+
''', (document_id, collection_id))
|
| 520 |
+
conn.commit()
|
| 521 |
+
return True
|
| 522 |
+
|
| 523 |
+
except sqlite3.Error as e:
|
| 524 |
+
st.error(f"Error removing document from collection: {e}")
|
| 525 |
+
return False
|
| 526 |
+
|
| 527 |
+
def update_collection(conn: sqlite3.Connection, collection_id: int, name: Optional[str] = None,
|
| 528 |
+
description: Optional[str] = None) -> bool:
|
| 529 |
+
"""
|
| 530 |
+
Update collection details.
|
| 531 |
+
|
| 532 |
+
Args:
|
| 533 |
+
conn (sqlite3.Connection): Database connection
|
| 534 |
+
collection_id (int): ID of the collection to update
|
| 535 |
+
name (Optional[str]): New name for the collection
|
| 536 |
+
description (Optional[str]): New description for the collection
|
| 537 |
+
|
| 538 |
+
Returns:
|
| 539 |
+
bool: True if successful
|
| 540 |
+
"""
|
| 541 |
+
try:
|
| 542 |
+
with conn_lock:
|
| 543 |
+
updates = []
|
| 544 |
+
params = []
|
| 545 |
+
|
| 546 |
+
if name is not None:
|
| 547 |
+
updates.append("name = ?")
|
| 548 |
+
params.append(name)
|
| 549 |
+
if description is not None:
|
| 550 |
+
updates.append("description = ?")
|
| 551 |
+
params.append(description)
|
| 552 |
+
|
| 553 |
+
if not updates:
|
| 554 |
+
return True # Nothing to update
|
| 555 |
+
|
| 556 |
+
params.append(collection_id)
|
| 557 |
+
|
| 558 |
+
cursor = conn.cursor()
|
| 559 |
+
cursor.execute(f'''
|
| 560 |
+
UPDATE collections
|
| 561 |
+
SET {", ".join(updates)}
|
| 562 |
+
WHERE id = ?
|
| 563 |
+
''', params)
|
| 564 |
+
|
| 565 |
+
conn.commit()
|
| 566 |
+
return True
|
| 567 |
+
|
| 568 |
+
except sqlite3.Error as e:
|
| 569 |
+
st.error(f"Error updating collection: {e}")
|
| 570 |
+
return False
|
| 571 |
+
|
| 572 |
+
def search_documents(conn: sqlite3.Connection, query: str,
|
| 573 |
+
collection_id: Optional[int] = None,
|
| 574 |
+
filters: Optional[Dict] = None) -> List[Dict]:
|
| 575 |
+
"""
|
| 576 |
+
Search documents using fuzzy matching and filters.
|
| 577 |
+
|
| 578 |
+
Args:
|
| 579 |
+
conn (sqlite3.Connection): Database connection
|
| 580 |
+
query (str): Search query
|
| 581 |
+
collection_id (Optional[int]): Filter by collection
|
| 582 |
+
filters (Optional[Dict]): Additional filters
|
| 583 |
+
|
| 584 |
+
Returns:
|
| 585 |
+
List[Dict]: List of matching documents
|
| 586 |
+
"""
|
| 587 |
+
try:
|
| 588 |
+
with conn_lock:
|
| 589 |
+
cursor = conn.cursor()
|
| 590 |
+
|
| 591 |
+
# Base query
|
| 592 |
+
sql = """
|
| 593 |
+
SELECT DISTINCT
|
| 594 |
+
d.id,
|
| 595 |
+
d.name,
|
| 596 |
+
d.content,
|
| 597 |
+
d.upload_date,
|
| 598 |
+
GROUP_CONCAT(c.name) as collections
|
| 599 |
+
FROM documents d
|
| 600 |
+
LEFT JOIN document_collections dc ON d.id = dc.document_id
|
| 601 |
+
LEFT JOIN collections c ON dc.collection_id = c.id
|
| 602 |
+
"""
|
| 603 |
+
|
| 604 |
+
params = []
|
| 605 |
+
where_clauses = []
|
| 606 |
+
|
| 607 |
+
# Add collection filter if specified
|
| 608 |
+
if collection_id:
|
| 609 |
+
where_clauses.append("dc.collection_id = ?")
|
| 610 |
+
params.append(collection_id)
|
| 611 |
+
|
| 612 |
+
# Add date filters if specified
|
| 613 |
+
if filters and 'date_range' in filters:
|
| 614 |
+
start_date, end_date = filters['date_range']
|
| 615 |
+
where_clauses.append("d.upload_date BETWEEN ? AND ?")
|
| 616 |
+
params.extend([start_date, end_date])
|
| 617 |
+
|
| 618 |
+
# Add text search
|
| 619 |
+
if query:
|
| 620 |
+
where_clauses.append("(d.name LIKE ? OR d.content LIKE ?)")
|
| 621 |
+
search_term = f"%{query}%"
|
| 622 |
+
params.extend([search_term, search_term])
|
| 623 |
+
|
| 624 |
+
# Combine WHERE clauses
|
| 625 |
+
if where_clauses:
|
| 626 |
+
sql += " WHERE " + " AND ".join(where_clauses)
|
| 627 |
+
|
| 628 |
+
sql += " GROUP BY d.id ORDER BY d.upload_date DESC"
|
| 629 |
+
|
| 630 |
+
# Execute query
|
| 631 |
+
cursor.execute(sql, params)
|
| 632 |
+
|
| 633 |
+
documents = []
|
| 634 |
+
for row in cursor.fetchall():
|
| 635 |
+
documents.append({
|
| 636 |
+
'id': row[0],
|
| 637 |
+
'name': row[1],
|
| 638 |
+
'content': row[2],
|
| 639 |
+
'upload_date': row[3],
|
| 640 |
+
'collections': row[4].split(',') if row[4] else []
|
| 641 |
+
})
|
| 642 |
+
|
| 643 |
+
return documents
|
| 644 |
+
|
| 645 |
+
except sqlite3.Error as e:
|
| 646 |
+
st.error(f"Error searching documents: {e}")
|
| 647 |
+
return []
|
| 648 |
|
| 649 |
def get_all_documents(conn: sqlite3.Connection) -> List[Dict]:
|
| 650 |
"""
|