cryogenic22 commited on
Commit
b877222
·
verified ·
1 Parent(s): ecfe110

Update utils/database.py

Browse files
Files changed (1) hide show
  1. utils/database.py +176 -3
utils/database.py CHANGED
@@ -50,7 +50,183 @@ def create_connection(db_file):
50
  except Error as e:
51
  st.error("Failed to connect to database. Please try again or contact support.")
52
  return None
 
 
 
 
 
 
 
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  def create_tables(conn):
56
  """
@@ -169,9 +345,6 @@ def verify_vector_store(vector_store):
169
  st.error(f"Vector store verification failed: {e}")
170
  return False
171
 
172
-
173
-
174
-
175
  def handle_document_upload(uploaded_files, **kwargs):
176
  """
177
  Handle document upload with progress tracking and collection support.
 
50
  except Error as e:
51
  st.error("Failed to connect to database. Please try again or contact support.")
52
  return None
53
+ def create_connection(db_file: str) -> Optional[sqlite3.Connection]:
54
+ """Create a database connection."""
55
+ try:
56
+ conn = sqlite3.connect(db_file, check_same_thread=False)
57
+ return conn
58
+ except sqlite3.Error as e:
59
+ st.error(f"Error connecting to database: {e}")
60
+ return None
61
 
62
+ def create_tables(conn: sqlite3.Connection) -> None:
63
+ """
64
+ Create all necessary tables in the database.
65
+
66
+ Args:
67
+ conn (sqlite3.Connection): SQLite database connection
68
+ """
69
+ try:
70
+ with conn_lock:
71
+ # Documents table
72
+ conn.execute('''
73
+ CREATE TABLE IF NOT EXISTS documents (
74
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
75
+ name TEXT NOT NULL,
76
+ content TEXT NOT NULL,
77
+ upload_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
78
+ )
79
+ ''')
80
+
81
+ # Queries table
82
+ conn.execute('''
83
+ CREATE TABLE IF NOT EXISTS queries (
84
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
85
+ query TEXT NOT NULL,
86
+ response TEXT NOT NULL,
87
+ document_id INTEGER,
88
+ query_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
89
+ FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE
90
+ )
91
+ ''')
92
+
93
+ # Annotations table
94
+ conn.execute('''
95
+ CREATE TABLE IF NOT EXISTS annotations (
96
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
97
+ document_id INTEGER NOT NULL,
98
+ annotation TEXT NOT NULL,
99
+ page_number INTEGER,
100
+ annotation_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
101
+ FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE
102
+ )
103
+ ''')
104
+
105
+ # Collections table
106
+ conn.execute('''
107
+ CREATE TABLE IF NOT EXISTS collections (
108
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
109
+ name TEXT NOT NULL UNIQUE,
110
+ description TEXT,
111
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
112
+ )
113
+ ''')
114
+
115
+ # Document-Collections relationship table
116
+ conn.execute('''
117
+ CREATE TABLE IF NOT EXISTS document_collections (
118
+ document_id INTEGER,
119
+ collection_id INTEGER,
120
+ added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
121
+ PRIMARY KEY (document_id, collection_id),
122
+ FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE,
123
+ FOREIGN KEY (collection_id) REFERENCES collections (id) ON DELETE CASCADE
124
+ )
125
+ ''')
126
+
127
+ # Add indices for better performance
128
+ conn.execute('CREATE INDEX IF NOT EXISTS idx_doc_name ON documents(name)')
129
+ conn.execute('CREATE INDEX IF NOT EXISTS idx_collection_name ON collections(name)')
130
+ conn.execute('CREATE INDEX IF NOT EXISTS idx_query_date ON queries(query_date)')
131
+ conn.execute('CREATE INDEX IF NOT EXISTS idx_doc_collections ON document_collections(collection_id)')
132
+
133
+ conn.commit()
134
+
135
+ except sqlite3.Error as e:
136
+ st.error(f"Error creating tables: {e}")
137
+ raise
138
+
139
+ def get_all_documents(conn: sqlite3.Connection) -> List[Dict]:
140
+ """Get all documents with their metadata and collections."""
141
+ try:
142
+ with conn_lock:
143
+ cursor = conn.cursor()
144
+ cursor.execute('''
145
+ SELECT
146
+ d.id,
147
+ d.name,
148
+ d.content,
149
+ d.upload_date,
150
+ GROUP_CONCAT(c.name) as collections
151
+ FROM documents d
152
+ LEFT JOIN document_collections dc ON d.id = dc.document_id
153
+ LEFT JOIN collections c ON dc.collection_id = c.id
154
+ GROUP BY d.id
155
+ ORDER BY d.upload_date DESC
156
+ ''')
157
+
158
+ documents = []
159
+ for row in cursor.fetchall():
160
+ documents.append({
161
+ 'id': row[0],
162
+ 'name': row[1],
163
+ 'content': row[2],
164
+ 'upload_date': row[3],
165
+ 'collections': row[4].split(',') if row[4] else []
166
+ })
167
+ return documents
168
+
169
+ except sqlite3.Error as e:
170
+ st.error(f"Error retrieving documents: {e}")
171
+ return []
172
+
173
+ def get_document_queries(conn: sqlite3.Connection, document_id: int) -> List[Dict]:
174
+ """Get all queries associated with a document."""
175
+ try:
176
+ with conn_lock:
177
+ cursor = conn.cursor()
178
+ cursor.execute('''
179
+ SELECT id, query, response, query_date
180
+ FROM queries
181
+ WHERE document_id = ?
182
+ ORDER BY query_date DESC
183
+ ''', (document_id,))
184
+
185
+ queries = []
186
+ for row in cursor.fetchall():
187
+ queries.append({
188
+ 'id': row[0],
189
+ 'query': row[1],
190
+ 'response': row[2],
191
+ 'query_date': row[3]
192
+ })
193
+ return queries
194
+
195
+ except sqlite3.Error as e:
196
+ st.error(f"Error retrieving document queries: {e}")
197
+ return []
198
+
199
+ def add_query(conn: sqlite3.Connection, query: str, response: str, document_id: Optional[int] = None) -> bool:
200
+ """Add a new query and its response."""
201
+ try:
202
+ with conn_lock:
203
+ cursor = conn.cursor()
204
+ cursor.execute('''
205
+ INSERT INTO queries (query, response, document_id)
206
+ VALUES (?, ?, ?)
207
+ ''', (query, response, document_id))
208
+ conn.commit()
209
+ return True
210
+
211
+ except sqlite3.Error as e:
212
+ st.error(f"Error adding query: {e}")
213
+ return False
214
+
215
+ def add_annotation(conn: sqlite3.Connection, document_id: int, annotation: str, page_number: Optional[int] = None) -> bool:
216
+ """Add an annotation to a document."""
217
+ try:
218
+ with conn_lock:
219
+ cursor = conn.cursor()
220
+ cursor.execute('''
221
+ INSERT INTO annotations (document_id, annotation, page_number)
222
+ VALUES (?, ?, ?)
223
+ ''', (document_id, annotation, page_number))
224
+ conn.commit()
225
+ return True
226
+
227
+ except sqlite3.Error as e:
228
+ st.error(f"Error adding annotation: {e}")
229
+ return False
230
 
231
  def create_tables(conn):
232
  """
 
345
  st.error(f"Vector store verification failed: {e}")
346
  return False
347
 
 
 
 
348
  def handle_document_upload(uploaded_files, **kwargs):
349
  """
350
  Handle document upload with progress tracking and collection support.