# mithrandie/csvq architecture

> Architecture diagram of the GitHub repository mithrandie/csvq, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mithrandie/csvq
- Repository: https://github.com/mithrandie/csvq

## Overview

##### Project type and purpose
- This is a Go-based command-line application (“csvq”) that implements an SQL-like query engine over CSV (and other flat-file) formats.
- It supports both one-off queries and interactive/procedural execution (transactions, variables, cursors, temp tables).

##### File structure
- Top level:
- main.go – entrypoint
- lib/cli – command-line argument parsing and help
- lib/action – business logic for commands (run, update, calc, etc.)
- lib/parser & lib/syntax – lexical analysis, parser, AST construction
- lib/query – query planning and execution engine (processors, transaction management, aggregate/analytic functions, cursors)
- lib/file & lib/json – connectors for reading/writing CSV, TSV, LTSV, fixed-width, JSON/JSON-Lines
- lib/option – environment variable and flag handling
- lib/terminal – interactive shell (readline, completer, prompt)
- lib/doc – generation of reference documentation (used for building docs site)
- Supporting files: go.mod, tools for CI, docs/ (static site), testdata/ for integration tests.

##### README for additional insights
- Confirms SQL-like operations (SELECT, INSERT, UPDATE, DELETE, CREATE/ALTER TABLE).
- Mentions interactive shell, transactions, temporary tables, external formats.
- No external dependencies beyond Go standard library and single binary distribution.

##### Main components & their interactions
a. Components:
- CLI Frontend (main.go + lib/cli)
- Option/Environment Manager (lib/option)
- Action Dispatcher (lib/action)
- Parser & AST Builder (lib/syntax + lib/parser)
- Query Engine (lib/query)
- File Connector Layer (lib/file + lib/json + lib/excmd)
- Transaction Manager (within lib/query/session & transaction)
- Interactive Shell (lib/terminal)
- Output Formatter & Writer (part of lib/query + lib/doc for docs)
b. Relationships & data flow:
1. User invokes `csvq` → CLI Frontend
2. CLI parses flags/env → Option Manager
3. Action Dispatcher receives SQL (inline or script)
4. If interactive → Terminal Shell loop triggers actions per statement
5. Each SQL statement → Syntax Lexer → Parser → AST
6. AST → Query Engine (semantic analysis, planning)
7. Query Engine invokes File Connector to load tables (CSV/JSON)
8. In‐memory data model manipulated (selection, aggregation, updates)
9. Transaction Manager commits or rolls back file writes
10. Results → Formatter → stdout / file output
c. Architectural patterns & principles:
- Layered architecture (CLI → parsing → execution → I/O)
- Separation of concerns: syntax, query processing, file I/O, CLI
- Pipeline pattern for SQL processing: tokenize → parse → analyze → execute → format
- In-memory data model (no external DB)
d. Key technologies & libraries:
- Go modules, built-in `bufio`/`encoding/csv`/`encoding/json`
- Custom parsers built with Go and `go yacc` (`.y` files)
- Readline support for interactive shell

##### Details to capture
- The role of `lib/option` in injecting environment variables and flags before dispatch.
- The `.y` grammar files under lib/parser and lib/json showing yacc‐based parsing.
- How `lib/doc/writer` is used offline to generate the static site (outside the core runtime flow).
- The testdata directory as a reference for supported formats (CSV, TSV, LTSV, fixed, JSON).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "CLI/Terminal Layer"
        CLI["CLI Frontend"]:::cli
        Terminal["Interactive Shell"]:::cli
    end

    subgraph "Option Layer"
        Option["Option/Environment Manager"]:::option
    end

    subgraph "Action Layer"
        Action["Action Dispatcher"]:::action
    end

    subgraph "Parser & AST Layer"
        Lexer["Lexer & Scanner"]:::parser
        ParserAST["Parser & AST Builder"]:::parser
    end

    subgraph "Execution Layer"
        Engine["Query Engine"]:::engine
        Transaction["Transaction Manager"]:::engine
    end

    subgraph "I/O Connector Layer"
        FileConn["File Connector"]:::io
    end

    subgraph "Output Layer"
        Formatter["Output Formatter & Writer"]:::formatter
    end

    subgraph "Documentation (Offline)"
        Doc["Doc Generator"]:::doc
    end

    CLI --> Option
    Option --> Action
    Action --> Lexer
    Lexer --> ParserAST
    ParserAST --> Engine
    Engine --> FileConn
    Engine --> Transaction
    Transaction --> FileConn
    Engine --> Formatter
    CLI --> Terminal
    Terminal --> Action

    click CLI "https://github.com/mithrandie/csvq/blob/master/main.go"
    click Terminal "https://github.com/mithrandie/csvq/blob/master/lib/terminal/terminal.go"
    click Option "https://github.com/mithrandie/csvq/blob/master/lib/option/default_env.go"
    click Action "https://github.com/mithrandie/csvq/blob/master/lib/action/run.go"
    click Lexer "https://github.com/mithrandie/csvq/blob/master/lib/parser/lexer.go"
    click ParserAST "https://github.com/mithrandie/csvq/blob/master/lib/parser/parser.go"
    click Engine "https://github.com/mithrandie/csvq/blob/master/lib/query/query.go"
    click Transaction "https://github.com/mithrandie/csvq/blob/master/lib/query/transaction.go"
    click FileConn "https://github.com/mithrandie/csvq/blob/master/lib/file/reader.go"
    click Formatter "https://github.com/mithrandie/csvq/blob/master/lib/query/string_formatter.go"
    click Doc "https://github.com/mithrandie/csvq/blob/master/lib/doc/writer.go"

    classDef cli fill:#ADD8E6,stroke:#000
    classDef option fill:#87CEFA,stroke:#000
    classDef action fill:#F08080,stroke:#000
    classDef parser fill:#FFFF99,stroke:#000
    classDef engine fill:#98FB98,stroke:#000
    classDef io fill:#FFA500,stroke:#000
    classDef formatter fill:#DDA0DD,stroke:#000
    classDef doc fill:#D3D3D3,stroke:#000
```
