Skip to content

DarshD22/SQL_Runner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ—„οΈ SQL Runner

A full-stack web application that allows users to execute SQL queries in real-time with a modern, intuitive interface. Built with FastAPI (Python) backend and Next.js (React) frontend.

SQL Runner Demo Python Next.js Docker

✨ Features

πŸ” Authentication & Security

  • User registration and login with JWT tokens
  • Secure session management
  • Protected routes and API endpoints
  • Read-only mode for safe query execution

πŸ’Ύ Query Execution

  • Execute SQL queries in real-time
  • View results in formatted tables
  • Support for complex queries (JOINs, GROUP BY, etc.)
  • Query history tracking
  • Export results to CSV or JSON

πŸ“Š Table Explorer

  • Browse all available database tables
  • View table schemas (column names and types)
  • Preview sample data from each table
  • Quick query templates (SELECT, COUNT, etc.)

πŸ• Query History

  • Track all executed queries
  • Replay previous queries with one click
  • Delete individual queries or clear all history
  • Relative timestamps for easy reference

🎨 Modern UI/UX

  • Clean, responsive design with Tailwind CSS
  • Three-panel layout (Tables, Editor, History)
  • Loading states and error handling
  • Keyboard shortcuts (Ctrl+Enter to run)
  • SQL query formatting

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Frontend (Next.js)                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚  β”‚   Auth UI    β”‚  β”‚ Query Editor β”‚  β”‚Table Explorerβ”‚     β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚ REST API (JWT Auth)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       Backend (FastAPI)                      β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚  β”‚  Auth Routes β”‚  β”‚Query Executorβ”‚  β”‚Table Manager β”‚     β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚ SQLAlchemy ORM
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    SQLite Database                           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚  β”‚    Users     β”‚  β”‚QueryHistory  β”‚  β”‚Sample Tables β”‚     β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ› οΈ Tech Stack

Backend

  • Framework: FastAPI 0.109
  • Database: SQLite with SQLAlchemy ORM
  • Authentication: JWT (python-jose) + bcrypt
  • Validation: Pydantic schemas
  • Server: Uvicorn ASGI server

Frontend

  • Framework: Next.js 14 (Pages Directory)
  • Language: JavaScript/TypeScript
  • Styling: Tailwind CSS
  • HTTP Client: Native Fetch API
  • State Management: React Context + useState

DevOps

  • Containerization: Docker & Docker Compose
  • Database: SQLite (persistent volume)

πŸ“‹ Prerequisites

Option 1: Using Docker (Recommended)

  • Docker Desktop 20.10+ (Windows/Mac) or Docker Engine (Linux)
  • Docker Compose 2.0+
  • 4GB RAM minimum
  • 2GB free disk space

Option 2: Manual Setup

  • Python 3.11+
  • Node.js 18+
  • npm or yarn
  • SQLite3

πŸš€ Quick Start with Docker

1. Clone the Repository

git clone https://github.com/DarshD22/SQL_Runner.git
cd SQL_Runner

2. Create Environment File

Create a .env file in the root directory:

# Backend Configuration
SECRET_KEY=your-super-secret-key
READ_ONLY_MODE=True
DEBUG=False

# Frontend Configuration
NEXT_PUBLIC_API_URL=http://localhost:8000

3. Start with Docker Compose

# Build and start all services
docker-compose up --build

First build takes 5-10 minutes. Subsequent starts take ~30 seconds.

4. Access the Application

5. Register and Login

  1. Navigate to http://localhost:3000/register
  2. Create an account (username, password)
  3. Login with your credentials
  4. Start running SQL queries!

6. Stop the Application

# Stop containers (keep data)
docker-compose down

# Stop and remove volumes (delete data)
docker-compose down -v

πŸ”§ Manual Setup (Without Docker)

Backend Setup

  1. Navigate to backend directory

    cd backend
  2. Create virtual environment

    python -m venv venv
    
    # Activate on Windows
    venv\Scripts\activate
    
    # Activate on Mac/Linux
    source venv/bin/activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Create .env file

    # Create backend/.env
    SECRET_KEY=your-secret-key
    READ_ONLY_MODE=True
    DEBUG=True
  5. Run the backend

    python -m app.main
    # or
    uvicorn app.main:app --reload

    Backend will start at: http://localhost:8000

Frontend Setup

  1. Navigate to frontend directory

    cd frontend
  2. Install dependencies

    npm install
    # or
    yarn install
  3. Create .env.local file

    # Create frontend/.env.local
    NEXT_PUBLIC_API_URL=http://localhost:8000
  4. Run the frontend

    npm run dev
    # or
    yarn dev

    Frontend will start at: http://localhost:3000


πŸ“‚ Project Structure

sql-runner/
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ main.py              # FastAPI application entry point
β”‚   β”‚   β”œβ”€β”€ config.py            # Configuration settings
β”‚   β”‚   β”œβ”€β”€ db.py                # Database connection and helpers
β”‚   β”‚   β”œβ”€β”€ models.py            # SQLAlchemy models (User, QueryHistory)
β”‚   β”‚   β”œβ”€β”€ schemas.py           # Pydantic schemas for validation
β”‚   β”‚   β”œβ”€β”€ auth.py              # Authentication helpers (JWT, bcrypt)
β”‚   β”‚   β”œβ”€β”€ utils.py             # Utility functions
β”‚   β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”‚   β”œβ”€β”€ auth_routes.py   # Authentication endpoints
β”‚   β”‚   β”‚   β”œβ”€β”€ query_routes.py  # Query execution endpoints
β”‚   β”‚   β”‚   β”œβ”€β”€ tables_routes.py # Table information endpoints
β”‚   β”‚   β”‚   └── history_routes.py# Query history endpoints
β”‚   β”œβ”€β”€ requirements.txt         # Python dependencies
β”‚   β”œβ”€β”€ Dockerfile              # Backend Docker configuration
β”‚   └── .dockerignore
β”‚
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”‚   β”œβ”€β”€ _app.jsx         # Next.js app wrapper
β”‚   β”‚   β”‚   β”œβ”€β”€ index.jsx        # Main SQL runner page
β”‚   β”‚   β”‚   β”œβ”€β”€ login.jsx        # Login page
β”‚   β”‚   β”‚   └── register.jsx     # Registration page
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ Layout.jsx       # Page layout wrapper
β”‚   β”‚   β”‚   β”œβ”€β”€ QueryEditor.jsx  # SQL query input component
β”‚   β”‚   β”‚   β”œβ”€β”€ ResultsTable.jsx # Query results display
β”‚   β”‚   β”‚   β”œβ”€β”€ TablesSidebar.jsx# Database tables list
β”‚   β”‚   β”‚   β”œβ”€β”€ TablePreview.jsx # Table schema preview
β”‚   β”‚   β”‚   β”œβ”€β”€ RecentQueries.jsx# Query history sidebar
β”‚   β”‚   β”‚   β”œβ”€β”€ Loading.jsx      # Loading spinner
β”‚   β”‚   β”‚   └── Error.jsx        # Error display
β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   β”œβ”€β”€ api.ts           # Generic API client
β”‚   β”‚   β”‚   β”œβ”€β”€ auth.ts          # Authentication service
β”‚   β”‚   β”‚   └── sqlRunner.ts     # SQL execution service
β”‚   β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”‚   └── useAuth.js       # Authentication hook
β”‚   β”‚   └── styles/
β”‚   β”‚       └── globals.css      # Global styles
β”‚   β”œβ”€β”€ package.json            # Node dependencies
β”‚   β”œβ”€β”€ next.config.js          # Next.js configuration
β”‚   β”œβ”€β”€ tailwind.config.js      # Tailwind CSS configuration
β”‚   β”œβ”€β”€ Dockerfile              # Frontend Docker configuration
β”‚   └── .dockerignore
β”‚
β”œβ”€β”€ docker-compose.yml          # Docker Compose orchestration
β”œβ”€β”€ .env                        # Environment variables
└── README.md                   # This file

πŸ—„οΈ Database Schema

Sample Tables (Auto-created on startup)

Customers

CREATE TABLE Customers (
    customer_id INTEGER PRIMARY KEY AUTOINCREMENT,
    first_name VARCHAR(100),
    last_name VARCHAR(100),
    age INTEGER,
    country VARCHAR(100)
);

Orders

CREATE TABLE Orders (
    order_id INTEGER PRIMARY KEY AUTOINCREMENT,
    item VARCHAR(100),
    amount INTEGER,
    customer_id INTEGER,
    FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)
);

Shippings

CREATE TABLE Shippings (
    shipping_id INTEGER PRIMARY KEY AUTOINCREMENT,
    status VARCHAR(100),
    customer INTEGER
);

System Tables

Users

CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username VARCHAR(100) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

QueryHistory

CREATE TABLE query_history (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    sql_text TEXT NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

πŸ”Œ API Endpoints

Authentication

  • POST /auth/register - Register new user
  • POST /auth/login - Login user (returns JWT token)
  • POST /auth/logout - Logout user
  • GET /auth/me - Get current user info

Query Execution

  • POST /api/query/run - Execute SQL query (Protected)

Tables

  • GET /api/tables - List all tables (Protected)
  • GET /api/tables/{table_name} - Get table schema and preview (Protected)

History

  • GET /api/history - Get query history (Protected)
  • DELETE /api/history/{id} - Delete specific query (Protected)
  • DELETE /api/history - Clear all history (Protected)

System

  • GET / - API health check
  • GET /health - Health status

Full API Documentation: http://localhost:8000/docs


🎯 Usage Examples

Example 1: Simple SELECT Query

SELECT * FROM Customers LIMIT 10;

Example 2: JOIN Query

SELECT c.first_name, c.last_name, o.item, o.amount
FROM Customers c
JOIN Orders o ON c.customer_id = o.customer_id
WHERE c.country = 'USA';

Example 3: Aggregation Query

SELECT country, COUNT(*) as customer_count
FROM Customers
GROUP BY country
ORDER BY customer_count DESC;

Example 4: Complex Query

SELECT 
    c.country,
    COUNT(DISTINCT c.customer_id) as customers,
    COUNT(o.order_id) as orders,
    SUM(o.amount) as total_revenue
FROM Customers c
LEFT JOIN Orders o ON c.customer_id = o.customer_id
GROUP BY c.country
HAVING COUNT(o.order_id) > 0
ORDER BY total_revenue DESC;

🐳 Docker Commands

Using docker-compose (Recommended)

# Start services
docker-compose up

# Start in background
docker-compose up -d

# Stop services
docker-compose down

# View logs
docker-compose logs

# Follow logs in real-time
docker-compose logs -f

# Rebuild and restart
docker-compose up --build

# Reset everything
docker-compose down -v
docker-compose up --build

Manual Docker Commands

# Build backend
cd backend
docker build -t sql-runner-backend .

# Run backend
docker run -d -p 8000:8000 --name backend sql-runner-backend

# Build frontend
cd frontend
docker build -t sql-runner-frontend .

# Run frontend
docker run -d -p 3000:3000 --name frontend sql-runner-frontend

πŸ”’ Security Features

Authentication

  • βœ… JWT-based authentication
  • βœ… Password hashing with bcrypt
  • βœ… Secure session management
  • βœ… Protected API endpoints

SQL Safety

  • βœ… Read-only mode (blocks INSERT, UPDATE, DELETE, DROP)
  • βœ… Input validation and sanitization
  • βœ… Query length limits

Application Security

  • βœ… CORS configuration
  • βœ… HTTP security headers
  • βœ… Non-root Docker containers
  • βœ… Environment variable management

βš™οΈ Configuration

Backend Configuration (backend/app/config.py)

# Database
DATABASE_URL = "sqlite:///./sql_runner.db"

# Security
SECRET_KEY = "your-secret-key"  # Change in production!
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 1440  # 24 hours

# SQL Safety
READ_ONLY_MODE = True  # Blocks write operations
BLOCKED_SQL_KEYWORDS = ["INSERT", "UPDATE", "DELETE", "DROP", "ALTER"]

# Limits
MAX_QUERY_LENGTH = 5000
MAX_PREVIEW_ROWS = 10
MAX_HISTORY_ITEMS = 50

Frontend Configuration (frontend/.env.local)

# API URL
NEXT_PUBLIC_API_URL=http://localhost:8000

# Environment
NODE_ENV=development

πŸ§ͺ Testing

Test the Backend

cd backend

# Test health endpoint
curl http://localhost:8000/health

# Test registration (using curl)
curl -X POST http://localhost:8000/auth/register \
  -H "Content-Type: application/json" \
  -d '{"username":"testuser","password":"testpass123"}'

# Test login
curl -X POST http://localhost:8000/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"testuser","password":"testpass123"}'

Test the Frontend

cd frontend

# Run build
npm run build

# Test production build
npm start

API Documentation

Visit http://localhost:8000/docs for interactive API testing with Swagger UI.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published