codeflow-ai / schema_analyzer.py
unknown
Initial commit: CodeFlow AI - NL to SQL Generator
7814c1f
raw
history blame contribute delete
956 Bytes
import sqlite3
class SchemaAnalyzer:
"""Analyzes database schema"""
def __init__(self, db_path='sample.db'):
self.db_path = db_path
def get_schema(self):
"""Get complete database schema"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Get all tables
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = [row[0] for row in cursor.fetchall()]
schema = {}
for table in tables:
cursor.execute(f"PRAGMA table_info({table})")
columns = []
for row in cursor.fetchall():
columns.append({
"name": row[1],
"type": row[2],
"nullable": not row[3],
"primary_key": bool(row[5])
})
schema[table] = columns
conn.close()
return schema