-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatabase_loader.py
More file actions
281 lines (216 loc) · 8.77 KB
/
database_loader.py
File metadata and controls
281 lines (216 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
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
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
"""Database loader for query results and database exports."""
import logging
import sqlite3
from pathlib import Path
from typing import Any
from langchain_core.documents import Document
class DatabaseLoader:
"""Loader for database files and query results."""
def __init__(self, file_path: str, query: str | None = None, logger: logging.Logger | None = None):
"""Initialize the database loader.
Args:
file_path: Path to the database file
query: Optional SQL query to execute
logger: Optional logger instance
"""
self.file_path = Path(file_path)
self.query = query
self.logger = logger or logging.getLogger(__name__)
if not self.file_path.exists():
raise FileNotFoundError(f"Database file not found: {self.file_path}")
self.file_type = self.file_path.suffix.lower()
# Validate supported formats
supported_formats = ['.db', '.sqlite', '.sqlite3']
if self.file_type not in supported_formats:
raise ValueError(f"Unsupported database format: {self.file_type}")
def load(self) -> list[Document]:
"""Load the database and return Document objects.
Returns:
List of Document objects with extracted content
Raises:
Exception: If database loading fails
"""
try:
if self.file_type in ['.db', '.sqlite', '.sqlite3']:
return self._load_sqlite()
else:
raise ValueError(f"Unsupported file type: {self.file_type}")
except Exception as e:
self.logger.error(f"Failed to load {self.file_path}: {e}")
raise
def _load_sqlite(self) -> list[Document]:
"""Load SQLite database.
Returns:
List of Document objects
"""
try:
conn = sqlite3.connect(str(self.file_path))
conn.row_factory = sqlite3.Row # Enable column access by name
cursor = conn.cursor()
documents = []
if self.query:
# Execute custom query
documents.extend(self._execute_query(cursor, self.query))
else:
# Get all tables and their data
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
for table in tables:
table_name = table[0]
# Skip system tables
if table_name.startswith('sqlite_'):
continue
# Get table schema
cursor.execute(f"PRAGMA table_info({table_name});")
schema = cursor.fetchall()
# Get table data
cursor.execute(f"SELECT * FROM {table_name} LIMIT 1000;") # Limit for safety
rows = cursor.fetchall()
if rows:
doc = self._create_table_document(table_name, schema, rows)
documents.append(doc)
conn.close()
self.logger.info(f"Loaded {len(documents)} document(s) from SQLite database")
return documents
except Exception as e:
self.logger.error(f"Failed to load SQLite database: {e}")
raise
def _execute_query(self, cursor: sqlite3.Cursor, query: str) -> list[Document]:
"""Execute a custom SQL query and return documents.
Args:
cursor: Database cursor
query: SQL query to execute
Returns:
List of Document objects
"""
try:
cursor.execute(query)
rows = cursor.fetchall()
if not rows:
return []
# Get column names
columns = [description[0] for description in cursor.description]
# Create content
content_parts = []
content_parts.append(f"Query: {query}")
content_parts.append(f"Results: {len(rows)} rows")
content_parts.append("")
# Add header
content_parts.append(" | ".join(columns))
content_parts.append("-" * 50)
# Add rows
for row in rows:
row_data = [str(value) if value is not None else "NULL" for value in row]
content_parts.append(" | ".join(row_data))
full_content = "\n".join(content_parts)
metadata = {
'source': str(self.file_path.resolve()),
'file_type': self.file_type,
'loader': 'DatabaseLoader',
'query': query,
'row_count': len(rows),
'column_count': len(columns),
'columns': columns
}
return [Document(page_content=full_content, metadata=metadata)]
except Exception as e:
self.logger.error(f"Failed to execute query: {e}")
raise
def _create_table_document(self, table_name: str, schema: list, rows: list) -> Document:
"""Create a document for a database table.
Args:
table_name: Name of the table
schema: Table schema information
rows: Table rows
Returns:
Document object
"""
content_parts = []
# Table header
content_parts.append(f"Table: {table_name}")
content_parts.append(f"Rows: {len(rows)}")
content_parts.append("")
# Schema information
content_parts.append("Schema:")
for col in schema:
col_info = f" {col[1]} ({col[2]})"
if col[3]: # NOT NULL
col_info += " NOT NULL"
if col[5]: # PRIMARY KEY
col_info += " PRIMARY KEY"
content_parts.append(col_info)
content_parts.append("")
# Data
if rows:
# Get column names from schema
columns = [col[1] for col in schema]
# Add header
content_parts.append(" | ".join(columns))
content_parts.append("-" * 50)
# Add rows (limit display for readability)
display_rows = rows[:100] # Show first 100 rows
for row in display_rows:
row_data = [str(value) if value is not None else "NULL" for value in row]
content_parts.append(" | ".join(row_data))
if len(rows) > 100:
content_parts.append(f"... and {len(rows) - 100} more rows")
full_content = "\n".join(content_parts)
# Extract column information
columns = [col[1] for col in schema]
column_types = [col[2] for col in schema]
metadata = {
'source': str(self.file_path.resolve()),
'file_type': self.file_type,
'loader': 'DatabaseLoader',
'table_name': table_name,
'row_count': len(rows),
'column_count': len(columns),
'columns': columns,
'column_types': column_types
}
return Document(page_content=full_content, metadata=metadata)
def get_table_names(self) -> list[str]:
"""Get list of table names in the database.
Returns:
List of table names
"""
try:
conn = sqlite3.connect(str(self.file_path))
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
conn.close()
# Filter out system tables
table_names = [table[0] for table in tables if not table[0].startswith('sqlite_')]
return table_names
except Exception as e:
self.logger.error(f"Failed to get table names: {e}")
return []
def get_table_schema(self, table_name: str) -> list[dict[str, Any]]:
"""Get schema information for a specific table.
Args:
table_name: Name of the table
Returns:
List of column information dictionaries
"""
try:
conn = sqlite3.connect(str(self.file_path))
cursor = conn.cursor()
cursor.execute(f"PRAGMA table_info({table_name});")
schema = cursor.fetchall()
conn.close()
# Convert to more readable format
schema_info = []
for col in schema:
schema_info.append({
'column_id': col[0],
'name': col[1],
'type': col[2],
'not_null': bool(col[3]),
'default_value': col[4],
'primary_key': bool(col[5])
})
return schema_info
except Exception as e:
self.logger.error(f"Failed to get table schema: {e}")
return []