cryogenic22 commited on
Commit
97533e4
·
verified ·
1 Parent(s): 40dbd2c

Update utils/database.py

Browse files
Files changed (1) hide show
  1. utils/database.py +68 -0
utils/database.py CHANGED
@@ -219,6 +219,74 @@ def force_recreate_collections_tables(conn: sqlite3.Connection) -> bool:
219
  st.error(f"Error recreating collections tables: {e}")
220
  return False
221
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
 
223
  def get_all_documents(conn: sqlite3.Connection) -> List[Dict]:
224
  """Get all documents with their metadata and collections."""
 
219
  st.error(f"Error recreating collections tables: {e}")
220
  return False
221
 
222
+ def get_uncategorized_documents(conn: sqlite3.Connection) -> List[Dict]:
223
+ """Get documents that aren't in any collection."""
224
+ try:
225
+ with conn_lock:
226
+ cursor = conn.cursor()
227
+ cursor.execute('''
228
+ SELECT
229
+ d.id,
230
+ d.name,
231
+ d.content,
232
+ d.upload_date
233
+ FROM documents d
234
+ LEFT JOIN document_collections dc ON d.id = dc.document_id
235
+ WHERE dc.collection_id IS NULL
236
+ ORDER BY d.upload_date DESC
237
+ ''')
238
+
239
+ return [{
240
+ 'id': row[0],
241
+ 'name': row[1],
242
+ 'content': row[2],
243
+ 'upload_date': row[3],
244
+ 'collections': []
245
+ } for row in cursor.fetchall()]
246
+
247
+ except sqlite3.Error as e:
248
+ st.error(f"Error retrieving uncategorized documents: {e}")
249
+ return []
250
+
251
+ def get_documents_for_chat(conn: sqlite3.Connection, collection_id: Optional[int] = None) -> List[Dict]:
252
+ """Get documents for chat, either from a collection or all documents."""
253
+ try:
254
+ with conn_lock:
255
+ cursor = conn.cursor()
256
+
257
+ if collection_id:
258
+ cursor.execute('''
259
+ SELECT
260
+ d.id,
261
+ d.name,
262
+ d.content,
263
+ d.upload_date
264
+ FROM documents d
265
+ JOIN document_collections dc ON d.id = dc.document_id
266
+ WHERE dc.collection_id = ?
267
+ ORDER BY d.upload_date DESC
268
+ ''', (collection_id,))
269
+ else:
270
+ cursor.execute('''
271
+ SELECT
272
+ d.id,
273
+ d.name,
274
+ d.content,
275
+ d.upload_date
276
+ FROM documents d
277
+ ORDER BY d.upload_date DESC
278
+ ''')
279
+
280
+ return [{
281
+ 'id': row[0],
282
+ 'name': row[1],
283
+ 'content': row[2],
284
+ 'upload_date': row[3]
285
+ } for row in cursor.fetchall()]
286
+
287
+ except sqlite3.Error as e:
288
+ st.error(f"Error retrieving documents for chat: {e}")
289
+ return []
290
 
291
  def get_all_documents(conn: sqlite3.Connection) -> List[Dict]:
292
  """Get all documents with their metadata and collections."""