QueryX ("Ask the Database in Plain English") is a production-ready Text-to-SQL system designed to translate natural language inquiries into secure, performant SQLite queries, execute them safely against the target database, and return both conversational insights and rich visual charts.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Web Frontend (UI) β
β - Input bar with Sample Chips β
β - Tabular Results View & Chart.js Visualization β
β - SQL Inspection & Schema Explorer β
ββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β HTTP POST /ask
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FastAPI Application (main.py) β
β - Endpoint routing & CORS Middleware β
β - Request validation via Pydantic β
β - Static file serving for Frontend UI β
ββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β
ββββββββββββββββββ΄βββββββββββββββββ
βΌ βΌ
βββββββββββββββββββββββββββ ββββββββββββββββββββββββββββ
β SchemaService β β LLM Factory β
β - sqlite_master inspect β β - Google Gemini (GenAI) β
β - Schema DDL extraction β β - OpenAI (ChatCompl) β
β - Dynamic prompt inject β β - MockLLM (Offline test) β
βββββββββββββ¬ββββββββββββββ βββββββββββββ¬βββββββββββββββ
β β
ββββββββββββββββββ¬βββββββββββββββββ
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β QueryService β
β 1. Injects schema context + question into LLM prompt β
β 2. Cleans raw response (strips markdown fences ```sql) β
β 3. Formulates natural language conversational synthesis β
ββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β SQL Validator β
β - Enforces SELECT-only queries β
β - Prohibits destructive keywords (DROP, DELETE, UPDATE) β
β - Prevents multi-statement SQL injection attacks β
ββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β SQLite Database Layer β
β - database/company.db (100 employee records) β
β - Connection pooling with sqlite3.Row dict mapping β
β - database/schema.sql & database/seed.sql β
ββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β ChartService β
β - Auto-detects categorical vs numerical columns β
β - Recommends Bar, Doughnut, or Line chart configs β
β - Emits Chart.js payload for instant UI rendering β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
| Directory / File | Role | Connected To |
|---|---|---|
app/main.py |
Central API server & pipeline coordinator | Connects QueryService, SchemaService, ChartService, sql_validator, and mounts frontend/. |
app/sql_validator.py |
Security guardrail | Called by app/main.py before any SQL statement reaches the database. |
app/llm/ |
LLM abstraction & provider adapters | get_llm() creates GeminiLLM, OpenAILLM, or MockLLM, used by QueryService. |
app/services/schema_service.py |
Database introspection engine | Reads database/company.db, formats schema context for QueryService and /api/schema. |
app/services/query_services.py |
Text-to-SQL and Answer synthesizer | Combines LLM with schema context to produce SQL and conversational summaries. |
app/services/chart_service.py |
Data visualization recommender | Inspects query output rows and constructs Chart.js configurations. |
database/ |
Storage & persistence | Houses SQLite company.db, schema.sql, and seed.sql. |
frontend/ |
Dashboard interface | Interacts with /ask, /api/schema, and /api/sample-questions via fetch(). |
tests/ |
Quality assurance suite | Pytest suite validating safety, schema, services, and API endpoints. |
- SELECT-Only Enforcement: Queries must begin with
SELECT. Any statement beginning with or containing forbidden keywords (DROP,DELETE,INSERT,UPDATE,ALTER,TRUNCATE,PRAGMA,ATTACH) is blocked before database execution. - Multiple Statements Blocked: Prohibits semicolon chaining (e.g.
SELECT 1; DROP TABLE...) to eliminate SQL injection vectors. - Environment Security: API keys are loaded via
python-dotenvand isolated in.env(excluded by.gitignore).