-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_db_direct.py
More file actions
573 lines (471 loc) · 22.1 KB
/
Copy pathquery_db_direct.py
File metadata and controls
573 lines (471 loc) · 22.1 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#!/usr/bin/env python3
"""
Generic SQLite Database Query Tool
A read-only tool for exploring and querying SQLite databases with intelligent schema discovery.
Designed primarily for AI agents and CLI workflows. All database connections are
opened in read-only mode -- for writes, use a tool built for that purpose. This is
a safety AND a convenience choice: the tool can be pointed at any database without
risk of accidental mutation.
KEY FEATURES:
- Raw SQL execution: --sql "SELECT * FROM table_name LIMIT 5"
- Schema exploration: --schema table_name (validated against sqlite_master)
- Database structure discovery: --tables, --analyze, --suggest
- Output formats: table (default), JSON (--json), CSV (--csv)
- Error handling with helpful suggestions
- Works with any SQLite database
USAGE EXAMPLES:
python query_db_direct.py --sql "SELECT COUNT(*) FROM users"
python query_db_direct.py --tables
python query_db_direct.py --schema users
python query_db_direct.py --db my_data.db --analyze
Author: Ben Keilman
"""
import sqlite3
import sys
import json
import argparse
import os
import csv
from pathlib import Path
class DirectDBQuery:
def __init__(self, db_path="database.db"):
"""Initialize the query tool. Resolves the path; existence is enforced
on first connect (read-only mode requires the file to exist)."""
self.db_path = db_path
# Handle relative paths relative to current working directory
if not os.path.isabs(self.db_path):
self.db_path = os.path.abspath(self.db_path)
if not os.path.exists(self.db_path):
raise FileNotFoundError("Database not found: {}".format(self.db_path))
def _connect(self):
"""Open a read-only SQLite connection.
Read-only mode is enforced via SQLite's URI form (?mode=ro). This makes
the tool safe to point at any database -- destructive SQL like DROP,
DELETE, UPDATE will be rejected by SQLite itself.
Uses pathlib.Path.as_uri() to build the file URI so paths containing
spaces, '#', or other URL-special characters are percent-encoded
correctly across Windows and Unix.
"""
uri = Path(self.db_path).as_uri() + "?mode=ro"
return sqlite3.connect(uri, uri=True)
@staticmethod
def _quote_ident(name):
"""Quote an SQL identifier (table/column name) for safe interpolation.
SQL identifiers can't be passed as bind parameters, so when we need to
embed a table name in a query we double-quote it and escape any internal
quotes per the SQL standard. Used only after the identifier has been
validated against sqlite_master.
"""
return '"' + name.replace('"', '""') + '"'
def execute_raw_sql(self, sql, output_format='table'):
"""Execute raw SQL and return results in specified format"""
try:
with self._connect() as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
# Handle multiple statements
statements = [stmt.strip() for stmt in sql.split(';') if stmt.strip()]
all_results = []
for stmt in statements:
cursor.execute(stmt)
results = cursor.fetchall()
all_results.append([dict(row) for row in results])
if output_format == 'json':
return json.dumps(all_results, indent=2, default=str)
elif output_format == 'table':
return self._format_table_results(all_results)
else:
return all_results
except Exception as e:
error_msg = "SQL Error: {}".format(e)
# Try to provide helpful suggestions
if "no such table" in str(e).lower():
tables = self.get_table_names()
error_msg += "\nAvailable tables: {}".format(", ".join(tables))
elif "no such column" in str(e).lower():
error_msg += "\nTip: Use --schema <table> to see available columns"
if output_format == 'json':
return json.dumps({"error": error_msg})
else:
return error_msg
def get_table_names(self):
"""Get list of all table names in database"""
try:
with self._connect() as conn:
cursor = conn.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
tables = [row[0] for row in cursor.fetchall()]
return tables
except Exception:
return []
def get_table_schema(self, table_name):
"""Get schema information for a table.
The table name is validated against sqlite_master before any
identifier interpolation, and then quoted via _quote_ident.
"""
try:
with self._connect() as conn:
cursor = conn.cursor()
# Whitelist check: confirm the table exists before interpolating
# its name into PRAGMA / COUNT statements.
cursor.execute(
"SELECT name FROM sqlite_master WHERE type='table' AND name=?",
(table_name,)
)
if not cursor.fetchone():
cursor.execute(
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"
)
available = [r[0] for r in cursor.fetchall()]
return {"error": "Table '{}' not found. Available tables: {}".format(
table_name, ", ".join(available) if available else "(none)")}
quoted = self._quote_ident(table_name)
# Get column info
cursor.execute("PRAGMA table_info({})".format(quoted))
columns = cursor.fetchall()
# Get row count
cursor.execute("SELECT COUNT(*) FROM {}".format(quoted))
row_count = cursor.fetchone()[0]
# Get indexes
cursor.execute("PRAGMA index_list({})".format(quoted))
indexes = cursor.fetchall()
schema_info = {
'table': table_name,
'columns': [
{
'name': col[1],
'type': col[2],
'not_null': bool(col[3]),
'primary_key': bool(col[5])
}
for col in columns
],
'row_count': row_count,
'indexes': [idx[1] for idx in indexes]
}
return schema_info
except Exception as e:
return {"error": "Error getting schema for {}: {}".format(table_name, e)}
def _format_table_results(self, all_results):
"""Format query results as readable tables"""
output = []
for i, results in enumerate(all_results):
if i > 0:
output.append("\n" + "="*60 + "\n")
if not results:
output.append("No results returned")
continue
# Get column names
columns = list(results[0].keys())
# Calculate column widths
col_widths = {}
for col in columns:
col_widths[col] = max(
len(col),
max(len(str(row.get(col, ''))) for row in results[:20]) # Sample first 20 rows
)
col_widths[col] = min(col_widths[col], 30) # Max width 30
# Header
header = " | ".join(col.ljust(col_widths[col]) for col in columns)
output.append(header)
output.append("-" * len(header))
# Rows
for row in results:
row_str = " | ".join(
str(row.get(col, ''))[:col_widths[col]].ljust(col_widths[col])
for col in columns
)
output.append(row_str)
output.append("\nRows returned: {}".format(len(results)))
return "\n".join(output)
def export_to_csv(self, sql_query, output_file=None):
"""Export query results to CSV format"""
try:
with self._connect() as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute(sql_query)
results = cursor.fetchall()
if not results:
return {"error": "No results to export"}
if output_file is None:
output_file = "query_results.csv"
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
# Get column names from the first row
fieldnames = list(results[0].keys())
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in results:
writer.writerow(dict(row))
return {"success": "Data exported to {}".format(output_file), "rows": len(results)}
except Exception as e:
return {"error": "CSV export failed: {}".format(e)}
def analyze_database_schema(self):
"""Analyze entire database schema and suggest useful queries"""
analysis = {
'database_path': self.db_path,
'tables': {},
'suggested_queries': [],
'insights': []
}
tables = self.get_table_names()
if not tables:
return analysis
for table in tables:
schema = self.get_table_schema(table)
if 'error' in schema:
continue
table_analysis = self._analyze_table_schema(table, schema)
analysis['tables'][table] = table_analysis
# Generate suggested queries for this table
suggestions = self._generate_table_queries(table, table_analysis)
analysis['suggested_queries'].extend(suggestions)
# Generate database-wide insights
analysis['insights'] = self._generate_database_insights(analysis['tables'])
return analysis
def _analyze_table_schema(self, table_name, schema):
"""Analyze a single table's schema"""
analysis = {
'row_count': schema.get('row_count', 0),
'column_types': {},
'date_columns': [],
'numeric_columns': [],
'text_columns': [],
'primary_keys': [],
'has_timestamps': False,
'likely_relationships': []
}
for col in schema.get('columns', []):
col_name = col['name']
col_type = col['type'].upper()
analysis['column_types'][col_name] = col_type
if col['primary_key']:
analysis['primary_keys'].append(col_name)
# Categorize columns by type
if 'INT' in col_type or 'REAL' in col_type or 'FLOAT' in col_type or 'NUMERIC' in col_type:
analysis['numeric_columns'].append(col_name)
elif 'TEXT' in col_type or 'CHAR' in col_type or 'VARCHAR' in col_type:
analysis['text_columns'].append(col_name)
elif 'DATE' in col_type or 'TIME' in col_type:
analysis['date_columns'].append(col_name)
# Detect timestamp patterns (but not numeric scores that happen to have time/date in name)
if (any(keyword in col_name.lower() for keyword in ['created', 'updated', 'modified', 'time', 'date']) and
not any(keyword in col_name.lower() for keyword in ['score', 'label', 'amount', 'value'])):
analysis['has_timestamps'] = True
if col_name not in analysis['date_columns']:
analysis['date_columns'].append(col_name)
return analysis
def _generate_table_queries(self, table_name, table_analysis):
"""Generate useful queries for a specific table.
Identifiers in the generated SQL are quoted via _quote_ident so the
suggestions remain valid for tables/columns with spaces, hyphens,
or names that collide with SQL keywords. The 'name' and 'description'
fields keep raw names for display readability.
"""
queries = []
qt = self._quote_ident(table_name)
# Basic exploration queries
if table_analysis['row_count'] > 0:
queries.append({
'name': f'sample_{table_name}',
'description': f'Sample data from {table_name}',
'sql': f'SELECT * FROM {qt} LIMIT 5'
})
queries.append({
'name': f'count_{table_name}',
'description': f'Total rows in {table_name}',
'sql': f'SELECT COUNT(*) as total_rows FROM {qt}'
})
# Date-based queries
if table_analysis['date_columns']:
date_col = table_analysis['date_columns'][0]
qd = self._quote_ident(date_col)
queries.append({
'name': f'recent_{table_name}',
'description': f'Recent records from {table_name}',
'sql': f'SELECT * FROM {qt} ORDER BY {qd} DESC LIMIT 10'
})
queries.append({
'name': f'date_range_{table_name}',
'description': f'Date range in {table_name}',
'sql': f'SELECT MIN({qd}) as earliest, MAX({qd}) as latest FROM {qt}'
})
# Numeric analysis
for num_col in table_analysis['numeric_columns'][:3]: # Limit to first 3
if 'id' not in num_col.lower(): # Skip ID columns
qn = self._quote_ident(num_col)
queries.append({
'name': f'stats_{table_name}_{num_col}',
'description': f'Statistics for {num_col} in {table_name}',
'sql': f'SELECT AVG({qn}) as avg_{num_col}, MIN({qn}) as min_{num_col}, MAX({qn}) as max_{num_col} FROM {qt}'
})
# Text analysis
for text_col in table_analysis['text_columns'][:2]: # Limit to first 2
if any(keyword in text_col.lower() for keyword in ['title', 'name', 'description', 'summary']):
qx = self._quote_ident(text_col)
queries.append({
'name': f'popular_{text_col}_{table_name}',
'description': f'Most common values in {text_col}',
'sql': f'SELECT {qx}, COUNT(*) as frequency FROM {qt} GROUP BY {qx} ORDER BY frequency DESC LIMIT 10'
})
return queries
def _generate_database_insights(self, tables_analysis):
"""Generate insights about the entire database"""
insights = []
total_tables = len(tables_analysis)
total_rows = sum(t.get('row_count', 0) for t in tables_analysis.values())
insights.append(f"Database contains {total_tables} tables with {total_rows:,} total rows")
# Find largest tables
largest_tables = sorted(
[(name, analysis.get('row_count', 0)) for name, analysis in tables_analysis.items()],
key=lambda x: x[1], reverse=True
)[:3]
if largest_tables and largest_tables[0][1] > 0:
insights.append(f"Largest tables: {', '.join(f'{name} ({count:,} rows)' for name, count in largest_tables if count > 0)}")
# Analyze relationships
potential_relationships = []
table_names = list(tables_analysis.keys())
for table_name, analysis in tables_analysis.items():
for col_name in analysis.get('column_types', {}):
# Look for foreign key patterns
for other_table in table_names:
if (other_table != table_name and
(col_name.lower().startswith(other_table.lower()) or
col_name.lower().endswith(other_table.lower()) or
other_table.lower() in col_name.lower())):
potential_relationships.append(f"{table_name}.{col_name} -> {other_table}")
if potential_relationships:
insights.append(f"Potential relationships: {', '.join(potential_relationships[:3])}")
return insights
def main():
parser = argparse.ArgumentParser(
description='Generic SQLite Database Query Tool (read-only)',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# List all tables
python query_db_direct.py --tables
# Show table schema
python query_db_direct.py --schema users
# Raw SQL query (multiple ;-separated statements supported)
python query_db_direct.py --sql "SELECT COUNT(*) FROM users"
# JSON output
python query_db_direct.py --sql "SELECT * FROM users LIMIT 5" --json
# Custom database
python query_db_direct.py --db my_data.db --tables
# Smart analysis
python query_db_direct.py --analyze
python query_db_direct.py --suggest
# CSV export
python query_db_direct.py --csv "SELECT * FROM users LIMIT 100" --csv-file users.csv
""")
# Enhanced options
parser.add_argument('--sql', help='Execute raw SQL query (read-only; multiple ;-separated statements OK)')
parser.add_argument('--schema', help='Show schema for specified table')
parser.add_argument('--tables', action='store_true', help='List all tables')
parser.add_argument('--analyze', action='store_true', help='Analyze database schema and provide insights')
parser.add_argument('--suggest', action='store_true', help='Suggest useful queries based on schema analysis')
parser.add_argument('--csv', help='Execute SQL query and export results to CSV')
parser.add_argument('--csv-file', help='Output filename for CSV export (default: query_results.csv)')
parser.add_argument('--json', action='store_true', help='Output results as JSON')
parser.add_argument('--db', default='database.db',
help='Database path (default: database.db in current directory)')
args = parser.parse_args()
# If no arguments provided, show help and exit before touching the filesystem.
if not (args.sql or args.schema or args.tables or args.analyze or args.suggest or args.csv):
print("Generic SQLite Database Query Tool (read-only)")
print("Use --help for usage information")
print("\nQuick start:")
print(" --tables List all tables")
print(" --analyze Analyze database structure")
print(" --suggest Get suggested queries")
print(" --schema X Show schema for table X")
print(" --sql 'X' Execute SQL query X")
print(" --csv 'X' Export query results to CSV")
return
# Validate the database exists before doing anything else.
try:
db = DirectDBQuery(args.db)
except FileNotFoundError as e:
print(str(e), file=sys.stderr)
sys.exit(1)
output_format = 'json' if args.json else 'table'
# Handle enhanced features
if args.sql:
result = db.execute_raw_sql(args.sql, output_format)
print(result)
return
if args.schema:
schema = db.get_table_schema(args.schema)
if args.json:
print(json.dumps(schema, indent=2))
else:
if 'error' in schema:
print(schema['error'])
else:
print("Table: {}".format(schema['table']))
print("Rows: {:,}".format(schema['row_count']))
print("\nColumns:")
for col in schema['columns']:
pk = " (PRIMARY KEY)" if col['primary_key'] else ""
null = " NOT NULL" if col['not_null'] else ""
print(" {} - {}{}{}".format(col['name'], col['type'], null, pk))
if schema['indexes']:
print("\nIndexes: {}".format(", ".join(schema['indexes'])))
return
if args.tables:
tables = db.get_table_names()
if args.json:
print(json.dumps({"tables": tables}))
else:
print("Tables in database:")
for table in tables:
print(" {}".format(table))
return
if args.analyze:
analysis = db.analyze_database_schema()
if args.json:
print(json.dumps(analysis, indent=2))
else:
print("Database Analysis")
print("=" * 50)
print("Database: {}".format(analysis['database_path']))
for insight in analysis['insights']:
print("• {}".format(insight))
print("\nTable Details:")
print("-" * 30)
for table_name, table_info in analysis['tables'].items():
print("\n{} ({:,} rows)".format(table_name, table_info['row_count']))
if table_info['date_columns']:
print(" Date columns: {}".format(', '.join(table_info['date_columns'])))
if table_info['numeric_columns']:
print(" Numeric columns: {}".format(', '.join(table_info['numeric_columns'][:5])))
if table_info['text_columns']:
print(" Text columns: {}".format(', '.join(table_info['text_columns'][:5])))
return
if args.suggest:
analysis = db.analyze_database_schema()
if args.json:
print(json.dumps(analysis['suggested_queries'], indent=2))
else:
print("Suggested Queries")
print("=" * 50)
if not analysis['suggested_queries']:
print("No queries suggested. Database may be empty or inaccessible.")
return
for i, query in enumerate(analysis['suggested_queries'], 1):
print("\n{}. {} - {}".format(i, query['name'], query['description']))
print(" SQL: {}".format(query['sql']))
print("\nTo execute a query, use:")
print('python query_db_direct.py --sql "QUERY_HERE"')
return
if args.csv:
result = db.export_to_csv(args.csv, args.csv_file)
if 'error' in result:
print(result['error'])
else:
print(result['success'])
print("Rows exported: {}".format(result['rows']))
return
if __name__ == "__main__":
main()