ddb-core 0.1.0

A serviceless SQL interface for flat files written in Rust
Documentation
# 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 `nom` parser
- 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

```bash
# Development build
cargo build

# Release build (optimized)
cargo build --release

# Run tests
cargo test

# Run benchmarks
cargo bench
```

## Usage

### CLI Usage

```bash
# Execute a SQL query
./target/release/ddb --query "SELECT * FROM users WHERE id = 123"

# With debug output
./target/release/ddb --query "SELECT * FROM test" --debug

# Specify config directory
./target/release/ddb --query "SELECT * FROM test" --config ./config

# Show version
./target/release/ddb version
```

### MCP Server (AI Integration)

DDB includes an MCP (Model Context Protocol) server for seamless integration with AI assistants like Claude.

```bash
# Build (MCP included by default)
cargo build --release

# Run the MCP server
./target/release/ddb --mcp
```

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](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

1. **Memory Efficiency**: Stream data line-by-line, never load entire files into memory
2. **Safety**: Leverage Rust's type system to prevent bugs at compile time
3. **Performance**: Use zero-copy parsing and avoid unnecessary allocations
4. **Correctness**: Comprehensive testing and error handling
5. **Ergonomics**: Clean API and good error messages

## Dependencies

- `nom` - Fast parser combinators for SQL tokenization
- `serde` - Serialization/deserialization
- `chrono` - Date/time functions
- `fs2` - File locking for concurrent access
- `clap` - CLI argument parsing
- `thiserror`/`anyhow` - Error handling
- `regex` - Pattern matching for LIKE operations
- `uuid` - UUID generation

## Benchmarks

Run benchmarks to compare performance:

```bash
cargo bench
```

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:

1. **Parser** - Convert token stream to AST
2. **Query Executor** - Implement SELECT with WHERE/ORDER BY/LIMIT
3. **Write Operations** - INSERT, UPDATE, DELETE with file locking
4. **Functions** - Implement SQL functions
5. **Output** - Terminal tables, JSON, YAML, XML formatters
6. **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)