DDB-Rust
A high-performance, secure Rust implementation of DDB (Delimited Database) - a serviceless SQL interface for flat files.
Project Status
This is an in-progress Rust port of the Python DDB project. The goal is to provide better performance, memory safety, and security while maintaining feature parity with the original.
Current Implementation Status
โ Completed:
- Project structure with Cargo workspace
- Error handling with
thiserror - SQL tokenizer/lexer with
nomparser - Token types for all SQL keywords
- Configuration structures (Database, Table, Column)
- File I/O module with file locking (
fs2) - Streaming line reader for memory-efficient processing
- AST (Abstract Syntax Tree) definitions
- CLI interface with
clap - Benchmark suite with
criterion
๐ง In Progress:
- SQL parser (tokens โ AST)
- Query execution engine
๐ Planned:
- WHERE clause evaluation
- SELECT query implementation
- INSERT/UPDATE/DELETE operations
- SQL functions (database(), datetime(), count(), etc.)
- Output formatters (JSON, YAML, XML, terminal tables)
- Transaction support (BEGIN, COMMIT, ROLLBACK)
- ORDER BY, LIMIT, DISTINCT operations
Features
Security First
- Memory-safe Rust implementation
- No SQL injection vulnerabilities (parameterized queries)
- Proper error handling throughout
- File locking prevents concurrent write conflicts
Performance
- Zero-copy parsing with
nom - Streaming/iterator-based file reading (low memory footprint)
- Compiled binary with LTO and optimizations
- Memory-mapped file support for large files
Compatibility
- Same configuration format as Python DDB
- Similar SQL dialect support
- Multiple output formats
Building
# Development build
# Release build (optimized)
# Run tests
# Run benchmarks
Usage
CLI Usage
# Execute a SQL query
# With debug output
# Specify config directory
# Show version
MCP Server (AI Integration)
DDB includes an MCP (Model Context Protocol) server for seamless integration with AI assistants like Claude.
# Build (MCP included by default)
# Run the MCP server
The MCP server provides:
- 6 Tools: execute_query, list_tables, describe_table, query_file, get_functions, validate_query
- Dynamic Resources: Configuration, table schemas, and SQL function documentation
- 7 Prompts: Intelligent templates for common SQL operations
See MCP_SERVER.md for complete documentation.
Architecture
src/
โโโ lib.rs # Main library entry point
โโโ error.rs # Error types and Result alias
โโโ lexer/ # Tokenization
โ โโโ mod.rs
โ โโโ types.rs # Token types
โ โโโ tokenizer.rs # SQL tokenizer
โโโ parser/ # SQL parsing
โ โโโ mod.rs
โ โโโ ast.rs # Abstract syntax tree
โโโ config/ # Configuration management
โ โโโ mod.rs # Database, Table, Column structs
โโโ file_io/ # File operations
โ โโโ locking.rs # File locking for concurrency
โ โโโ reader.rs # Streaming line reader
โโโ engine/ # Query execution
โโโ methods/ # SQL operations (SELECT, INSERT, etc.)
โโโ functions/ # SQL functions
โโโ output/ # Output formatters
โโโ mcp/ # Model Context Protocol server
โ โโโ mod.rs
โ โโโ server.rs # MCP server implementation
โ โโโ tools.rs # MCP tools (6 tools)
โ โโโ resources.rs # MCP resources
โ โโโ prompts.rs # MCP prompts (7 prompts)
โโโ bin/
โโโ ddb.rs # CLI binary
โโโ ddb_mcp.rs # MCP server binary
Design Principles
- Memory Efficiency: Stream data line-by-line, never load entire files into memory
- Safety: Leverage Rust's type system to prevent bugs at compile time
- Performance: Use zero-copy parsing and avoid unnecessary allocations
- Correctness: Comprehensive testing and error handling
- Ergonomics: Clean API and good error messages
Dependencies
nom- Fast parser combinators for SQL tokenizationserde- Serialization/deserializationchrono- Date/time functionsfs2- File locking for concurrent accessclap- CLI argument parsingthiserror/anyhow- Error handlingregex- Pattern matching for LIKE operationsuuid- UUID generation
Benchmarks
Run benchmarks to compare performance:
Current tokenizer performance (SELECT query):
- ~X ยตs per query (TODO: add actual benchmark results)
Contributing
This is an active development project. Key areas that need work:
- Parser - Convert token stream to AST
- Query Executor - Implement SELECT with WHERE/ORDER BY/LIMIT
- Write Operations - INSERT, UPDATE, DELETE with file locking
- Functions - Implement SQL functions
- Output - Terminal tables, JSON, YAML, XML formatters
- Tests - Comprehensive unit and integration tests
License
Creative Commons Attribution-Noncommercial-Share Alike (same as Python DDB)
Acknowledgments
Based on the original Python DDB by Charles Watkins (chris17453@gmail.com)