How mithrandie/csvq works
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).