AI-powered CLI for natural language database queries and automated test data generation
Project description
QueryNL CLI - Natural Language Database Queries from the Terminal
A powerful command-line interface for executing database queries using natural language. QueryNL CLI provides terminal-native access to database querying, connection management, and query history tracking.
๐ฏ MVP Features (Version 0.1.0)
This is the Minimum Viable Product implementation with core functionality:
โ Implemented Features
-
Connection Management (User Story 2 - P1)
- Add/list/test/use/remove database connections
- Secure credential storage via OS keychain
- Support for PostgreSQL, MySQL, SQLite, MongoDB
- SSH tunnel configuration
- Environment variable support for CI/CD
-
Natural Language Queries (User Story 1 - P1)
- Execute database queries using natural language
- ๐ Real LLM Integration (OpenAI GPT-4 & Anthropic Claude)
- SQL generation with syntax highlighting preview
- Confirmation prompts for destructive operations
- Multiple output formats (table, JSON, CSV, markdown)
- Query history tracking
- Scriptable commands with proper exit codes
- Automatic fallback to pattern matching if LLM unavailable
๐ LLM Integration (NEW!)
QueryNL now supports real AI-powered query generation:
- Providers: OpenAI GPT-4, Anthropic Claude 3.5 Sonnet
- Secure: API keys stored in system keychain
- Smart: Understands database schema and context
- Safe: Confidence scoring and destructive operation detection
- Fallback: Works without API keys using pattern matching
Quick Setup:
querynl config llm --provider openai --test
# Enter your OpenAI API key when prompted
See LLM_SETUP.md for detailed configuration guide.
๐ Test Data Generation (NEW!)
Feature Branch: 005-add-test-data
Automatically populate your database schemas with realistic test data using natural language!
- Natural Language: "add sample data", "add 100 users", "add e-commerce product data"
- Smart Dependencies: Automatically handles foreign key relationships
- Constraint Compliance: Respects UNIQUE, NOT NULL, and CHECK constraints
- Multi-Database: Works with MySQL, PostgreSQL, and SQLite
- Realistic Data: Uses Faker library for names, emails, addresses, dates, etc.
- Domain Aware: Generate domain-specific data (e-commerce, blog, medical, etc.)
- Progress Tracking: Real-time progress bars with speed and ETA
- Error Recovery: Graceful handling of constraint violations
Quick Start:
# Create a schema
querynl> \schema design
Schema Designer> blog with users, posts, and comments
Schema Designer> finalize
# Generate test data
querynl> add sample data
# Or specify quantities
querynl> add 100 users and 500 posts
# Or with domain context
querynl> add e-commerce product data
See specs/005-add-test-data/quickstart.md for full documentation.
๐ง Coming in Future Releases
- Migration generation (Phase 8)
- Advanced output formatting (Phase 6)
- Configuration management commands (Phase 10)
๐ฆ Installation
Prerequisites
- Python 3.11 or higher
- pip (Python package manager)
Install from Source
# Clone the repository
cd /Users/marcus/Developer/QueryNLAgent/QueryNL
# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Install in development mode
pip install -e .
Verify Installation
querynl --version
# Output: QueryNL CLI, version 0.1.0
๐ Quick Start
1. Add Your First Connection
# Interactive mode (recommended)
querynl connect add my-db
# You'll be prompted for:
# - Database type (postgresql, mysql, sqlite, mongodb)
# - Host (default: localhost)
# - Port (default: 5432 for PostgreSQL)
# - Database name
# - Username
# - Password (hidden input)
2. Test the Connection
querynl connect test my-db
# Output:
# Testing connection 'my-db'...
# โ Connection successful
# Database: PostgreSQL 15.2
# Latency: 23ms
3. Start Interactive REPL Mode
querynl repl
# Output:
# QueryNL Interactive REPL
# Type '\help' for available commands, 'exit' to quit
#
# querynl (my-db)>
4. Execute Your First Query
In REPL mode:
querynl (my-db)> count all users
Generated SQL:
SELECT COUNT(*) AS count FROM users;
Executing query...
โโโโโโโโโ
โ count โ
โโโโโโโโโค
โ 1523 โ
โโโโโโโโโ
1 row returned (45ms)
Or using CLI:
querynl query exec "count all users"
๐ Usage Examples
Interactive REPL Mode
The REPL (Read-Eval-Print Loop) provides an interactive session for natural language queries with conversation context.
Start REPL:
querynl repl
# Or specify a connection
querynl repl --connection prod-db
REPL Features:
-
Natural Language Queries
querynl> show me all active users querynl> count orders by status querynl> find products with price > 100 -
Schema Design Mode
querynl> \schema design Schema Designer> blog with users, posts, and comments Schema Designer> finalize -
Test Data Generation
querynl> add sample data querynl> add 100 users and 500 posts querynl> add e-commerce product data -
REPL Commands
querynl> \help # Show available commands querynl> \tables # List all tables in database querynl> \schema # Display database schema querynl> \history # Show query history querynl> \clear # Clear screen querynl> exit # Exit REPL -
Conversation Context The REPL maintains context across queries:
querynl> show users created this month querynl> now filter them by status = 'active' querynl> order by created_at desc -
Tab Completion
- Press
Tabto autocomplete table names - Works for commands starting with
\
- Press
-
History Navigation
- Use โ/โ arrow keys to navigate previous queries
- History persists across sessions
Connection Management
# List all connections
querynl connect list
# Add a SQLite connection
querynl connect add local-db --type sqlite --database ./app.db
# Add with SSH tunnel
querynl connect add remote-db --ssh-tunnel
# Set default connection
querynl connect use prod-db
# Remove a connection
querynl connect remove dev-db --confirm
Executing Queries
# Basic natural language query
querynl query exec "show all active users"
# JSON output (for piping to jq)
querynl query exec --format json "count orders by status"
# Save results to file
querynl query exec --output results.csv --format csv "all orders from last week"
# Non-interactive mode (skip confirmations)
querynl query exec -y "delete from temp_table"
# Explain mode (show SQL without executing)
querynl query exec --explain "update user status"
# Use specific connection
querynl query exec --connection staging-db "list tables"
# Limit results
querynl query exec --limit 100 "all users"
Query History
# View recent queries
querynl query history
# Show last 50 queries
querynl query history --limit 50
# Filter by connection
querynl query history --connection prod-db
# JSON output
querynl query history --format json
CI/CD Integration
# Use environment variables for credentials
export QUERYNL_CONNECTION_STRING="postgresql://user:pass@host:5432/db"
# Non-interactive query execution
querynl query exec -y --format json "SELECT 1" > /dev/null
echo $? # Exit code: 0 = success, non-zero = error
# Pipe results to other tools
querynl query exec --format json "all active users" | jq '.rows | length'
๐ง Configuration
Config File Location
- Linux:
~/.config/querynl/config.yaml - macOS:
~/Library/Application Support/querynl/config.yaml - Windows:
%APPDATA%\querynl\config.yaml
Config File Structure
version: "1.0"
default_connection: prod-db
default_output_format: table
llm_provider: openai
enable_telemetry: false
repl_history_size: 1000
confirm_destructive: true
color_output: auto
connections:
prod-db:
database_type: postgresql
host: prod.example.com
port: 5432
database_name: production
username: app_user
ssl_enabled: true
created_at: '2025-10-14T10:30:00'
Note: Passwords are stored separately in OS keychain, never in the config file.
Environment Variables
| Variable | Description |
|---|---|
QUERYNL_CONNECTION_STRING |
Database connection string (bypasses keychain) |
QUERYNL_KEYRING_PASSWORD |
Master password for encrypted file keyring (headless servers) |
QUERYNL_CONFIG |
Custom config file path |
QUERYNL_NO_COLOR |
Disable color output (set to 1) |
๐ Security
Credential Storage
QueryNL CLI uses platform-native credential storage:
- macOS: Keychain Access
- Windows: Windows Credential Manager
- Linux: Secret Service API (GNOME Keyring, KWallet)
For headless servers without keychain access:
# Option 1: Use encrypted file keyring
export QUERYNL_KEYRING_PASSWORD="your-master-password"
querynl connect add my-db
# Option 2: Use connection string directly
export QUERYNL_CONNECTION_STRING="postgresql://user:pass@host/db"
querynl query exec "your query"
Security Features
- โ Credentials encrypted at rest via OS keychain
- โ Passwords never displayed in logs or error messages
- โ SSL/TLS enabled by default for network connections
- โ Confirmation required for destructive operations (DELETE, DROP, etc.)
- โ Input sanitization to prevent SQL injection
๐๏ธ Project Structure
QueryNL/
โโโ src/cli/
โ โโโ __init__.py # Version and package info
โ โโโ main.py # CLI entry point
โ โโโ config.py # Configuration management
โ โโโ credentials.py # Keyring integration
โ โโโ database.py # Database driver wrapper
โ โโโ errors.py # Custom exceptions
โ โโโ history.py # Query history tracking
โ โโโ llm.py # LLM service integration
โ โโโ logging.py # Logging configuration
โ โโโ models.py # Data models (ConnectionProfile, etc.)
โ โโโ commands/
โ โ โโโ connect.py # Connection management commands
โ โ โโโ query.py # Query execution commands
โ โโโ formatting/
โ โโโ table.py # Result formatting
โโโ tests/cli/ # Tests (coming soon)
โโโ requirements.txt # Python dependencies
โโโ setup.py # Package setup
โโโ README.md # This file
๐งช Testing
# Run tests (when implemented)
pytest
# Run specific test file
pytest tests/cli/test_connect.py
# Run with coverage
pytest --cov=src/cli --cov-report=html
๐ Troubleshooting
"Connection refused"
# Test connectivity
querynl connect test my-db
# Check if database is running
# Verify host/port are correct
# Check firewall rules
"Permission denied to access keychain" (macOS)
- Open Keychain Access app
- Find "querynl" entry
- Right-click โ Get Info โ Access Control
- Allow querynl to access this item
"D-Bus session not available" (Linux headless)
# Use encrypted file keyring fallback
export QUERYNL_KEYRING_PASSWORD="your-master-password"
querynl connect add my-db
"No default connection set"
# Add a connection
querynl connect add my-db
# Or use --connection flag
querynl query exec --connection my-db "your query"
๐ Documentation
- Feature Specification:
specs/002-command-line-interface/spec.md - Implementation Plan:
specs/002-command-line-interface/plan.md - Task Breakdown:
specs/002-command-line-interface/tasks.md - Quick start Guide:
specs/002-command-line-interface/quickstart.md - API Contracts:
specs/002-command-line-interface/contracts/
๐บ๏ธ Roadmap
Phase 1-4: MVP โ (Current Release)
- Project setup and infrastructure
- Connection management
- Natural language query execution
- Query history tracking
Phase 5: REPL Mode ๐ง (Next Release)
- Interactive shell with history
- Tab completion
- Conversation context
- REPL-specific commands (\help, \tables, \schema)
Phase 6-8: Advanced Features ๐ฎ (Future)
- Schema design from natural language
- Migration generation
- Output format enhancements
Phase 9-10: Production Ready ๐ (Future)
- Comprehensive testing
- CI/CD integration examples
- Binary distribution (PyInstaller)
- Package manager releases (Homebrew, apt)
๐ค Contributing
This project follows the QueryNL Constitution:
- Security-First Design: All code must protect credentials and prevent SQL injection
- User Experience: Error messages must be actionable with clear next steps
- Transparency: Generated SQL must be shown before execution
- Multi-Database Parity: All supported databases must work equally well
- Fail-Safe Defaults: Destructive operations require confirmation
๐ License
Copyright ยฉ 2025 QueryNL Team
๐ Acknowledgments
- Built with Click - Command-line framework
- Rich - Beautiful terminal formatting
- Keyring - Secure credential storage
- Pydantic - Data validation
Version: 0.1.0 (MVP) Status: Production-ready for core features Last Updated: 2025-10-14
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file querynl_cli-0.2.4.tar.gz.
File metadata
- Download URL: querynl_cli-0.2.4.tar.gz
- Upload date:
- Size: 341.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d1808d468617a272b76f564bbaa20701b393188ea175b6cee4ac7883802b77e
|
|
| MD5 |
933642c76325fe60b8753e1173b71bb0
|
|
| BLAKE2b-256 |
46342c87d002ea9af5e12f6777a556fcca0222f7422dd0cac786d49a1656728d
|
File details
Details for the file querynl_cli-0.2.4-py3-none-any.whl.
File metadata
- Download URL: querynl_cli-0.2.4-py3-none-any.whl
- Upload date:
- Size: 135.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8767038bc77a8bc22f8a6363644bc56c262a16d25ceee7c7e9d3c09e5447778b
|
|
| MD5 |
0c86fd03a724317360e942016eb51343
|
|
| BLAKE2b-256 |
38293c72bd0c6df6810d99d3ff61590afb59f7f6819b480b10b6e13b64c85dac
|