cryogenic22 commited on
Commit
b39c0ad
·
verified ·
1 Parent(s): 9d6a41a

Update utils/database.py

Browse files
Files changed (1) hide show
  1. utils/database.py +77 -0
utils/database.py CHANGED
@@ -267,6 +267,83 @@ def generate_document_tags(content: str) -> List[str]:
267
  st.error(f"Error generating tags: {e}")
268
  return []
269
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  def add_document_tags(conn: sqlite3.Connection, document_id: int, tags: List[str]) -> bool:
271
  """Add tags to a document."""
272
  try:
 
267
  st.error(f"Error generating tags: {e}")
268
  return []
269
 
270
+
271
+ def add_document_to_collection(conn: sqlite3.Connection, document_id: int, collection_id: int) -> bool:
272
+ """Link a document to a collection."""
273
+ try:
274
+ with conn_lock:
275
+ cursor = conn.cursor()
276
+ cursor.execute('''
277
+ INSERT OR IGNORE INTO document_collections (document_id, collection_id)
278
+ VALUES (?, ?)
279
+ ''', (document_id, collection_id))
280
+ conn.commit()
281
+ return True
282
+ except sqlite3.Error as e:
283
+ st.error(f"Error linking document to collection: {e}")
284
+ return False
285
+
286
+ def get_collection_documents(conn: sqlite3.Connection, collection_id: int) -> List[Dict]:
287
+ """Get all documents in a collection."""
288
+ try:
289
+ with conn_lock:
290
+ cursor = conn.cursor()
291
+ cursor.execute('''
292
+ SELECT
293
+ d.id,
294
+ d.name,
295
+ d.content,
296
+ d.upload_date
297
+ FROM documents d
298
+ JOIN document_collections dc ON d.id = dc.document_id
299
+ WHERE dc.collection_id = ?
300
+ ORDER BY d.upload_date DESC
301
+ ''', (collection_id,))
302
+
303
+ documents = []
304
+ for row in cursor.fetchall():
305
+ documents.append({
306
+ 'id': row[0],
307
+ 'name': row[1],
308
+ 'content': row[2],
309
+ 'upload_date': row[3]
310
+ })
311
+ return documents
312
+ except sqlite3.Error as e:
313
+ st.error(f"Error retrieving collection documents: {e}")
314
+ return []
315
+
316
+ def get_collections(conn: sqlite3.Connection) -> List[Dict]:
317
+ """Get all collections with document counts."""
318
+ try:
319
+ with conn_lock:
320
+ cursor = conn.cursor()
321
+ cursor.execute('''
322
+ SELECT
323
+ c.id,
324
+ c.name,
325
+ c.description,
326
+ c.created_at,
327
+ COUNT(DISTINCT dc.document_id) as doc_count
328
+ FROM collections c
329
+ LEFT JOIN document_collections dc ON c.id = dc.collection_id
330
+ GROUP BY c.id
331
+ ORDER BY c.name
332
+ ''')
333
+
334
+ collections = []
335
+ for row in cursor.fetchall():
336
+ collections.append({
337
+ 'id': row[0],
338
+ 'name': row[1],
339
+ 'description': row[2],
340
+ 'created_at': row[3],
341
+ 'doc_count': row[4]
342
+ })
343
+ return collections
344
+ except sqlite3.Error as e:
345
+ st.error(f"Error retrieving collections: {e}")
346
+ return []
347
  def add_document_tags(conn: sqlite3.Connection, document_id: int, tags: List[str]) -> bool:
348
  """Add tags to a document."""
349
  try: