# statebox/cql architecture

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

- Interactive diagram: https://gitdiagram.com/statebox/cql
- Repository: https://github.com/statebox/cql

## Overview

##### Project’s Main Components
- Core Language Modules (Core Engine):
– This is primarily under the src directory (src/Language and its submodules) where all the logic for CQL constructs is implemented. It encompasses parsing (src/Language/CQL/Parser/*), language constructs (like Collage, Congruence, Morphism, Schema, etc.), transformation, and query evaluation.
- Interfaces:
– CLI Frontend: The cli folder contains a Main.hs that is used when executing CQL via the command-line (e.g., “stack exec cql examples/Employee.cql”).
– HTTP API Service: The http folder houses an HTTP server (http/Main.hs plus additional modules under http/src/Api and http/src/Api/Config) which exposes an endpoint for processing CQL specifications over port 8080.
- Examples:
– Located in the examples folder, these sample .cql files provide concrete cases for how CQL scripts are structured.
- Testing & Build Infrastructure:
– The test directory includes modules for unit testing (e.g., CQLSpec.hs, ParserSpec.hs).
– Build and configuration files are laid out for several build systems (Stack, Cabal, Nix) and include package.yaml, stack.yaml, Setup.hs, and various configuration files (e.g., .editorconfig, .travis.yml).

##### Relationships and Interactions
- Both the CLI and HTTP API components are “frontends” that interact with the core engine.
– For the CLI:
- The module in cli/Main.hs invokes functions from the core language modules to parse, evaluate, and execute .cql files.
– For the HTTP API:
- The modules in http (especially under http/src/Api) set up an HTTP server that listens for POST requests containing CQL specifications.
- These modules also call functions from the core engine to process the CQL input and return a proper response.
- The core language modules are composed of several sub-components (parsers, transformers, algebra, schema management, and query execution) that work together to provide the declarative migration capabilities.
- Test modules validate the functionalities provided by these components to ensure correctness.
- The examples offer usage scenarios or demonstrations of CQL files that can be fed to either the CLI or HTTP API.

##### Architectural Patterns, Design Principles, and Technologies
- Separation of Concerns:
– There is a clear separation between the interface layers (CLI, HTTP) and the core processing engine.
- Modularity:
– The src/Language directory is subdivided into logical areas (such as Parser, Transform, Prover, etc.) which hints at a modular design based on functional decomposition.
- Layered Architecture:
– At the top, there are end-user interfaces (CLI and HTTP). The middle layer is the core engine that provides language processing and transformation. The lower layer (implicitly supported by the build system) includes testing and configuration.
- Build and Deployment:
– Multiple build tools (Stack, Cabal, Nix) are supported, emphasizing the importance of reproducible builds. This can be visualized as a supporting “build infrastructure” component.
- Technologies & Tools:
– Haskell is the main language.
– Stack, Cabal, and Nix configurations indicate multi-environment build support.

##### Final Checklist for the Principal Software Engineer
– Ensure all major components are represented: Core Language Engine, CLI, HTTP API, Testing, Examples, and Build Infrastructure.
– Optionally, include annotations about technology choices (Haskell, Stack/Cabal/Nix) and design principles (separation of concerns, modularity).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Interface Layer"
        CLI["CLI Frontend"]:::interface
        HTTP["HTTP API Service"]:::interface
        HTTPApi["HTTP API Modules"]:::interface
        HTTPConfig["HTTP API Config"]:::interface
        EX["Examples (.cql)"]:::example
    end

    subgraph "Core Engine"
        CORE["Core Language Modules"]:::core
        Parser["Parser"]:::core
        AST["Language Constructs"]:::core
        Transform["Transformations & Evaluation"]:::core
    end

    subgraph "Testing & Validation"
        T1["CQLSpec.hs"]:::testing
    end

    subgraph "Build & Configuration"
        B1["Setup.hs"]:::build
        B2["stack.yaml"]:::build
        B3["package.yaml"]:::build
        B4[".travis.yml"]:::build
        B5[".editorconfig"]:::build
        B6["cql.nix/default.nix"]:::build
    end

    CLI -->|"calls"| CORE
    HTTP -->|"calls"| CORE
    HTTPApi -->|"integratesWith"| HTTP
    HTTPConfig -->|"configures"| HTTPApi
    CORE -->|"contains"| Parser
    CORE -->|"contains"| AST
    CORE -->|"contains"| Transform
    T1 -->|"validates"| CORE
    B1 ---|"configures"| CORE
    B2 ---|"configures"| CORE
    B3 ---|"configures"| CORE
    B4 ---|"configures"| CORE
    B5 ---|"configures"| CORE
    B6 ---|"configures"| CORE
    EX ---|"demonstrates"| CLI
    EX ---|"demonstrates"| HTTP

    click CLI "https://github.com/statebox/cql/blob/master/cli/Main.hs"
    click HTTP "https://github.com/statebox/cql/blob/master/http/Main.hs"
    click HTTPApi "https://github.com/statebox/cql/tree/master/http/src/Api"
    click HTTPConfig "https://github.com/statebox/cql/tree/master/http/src/Api/Config"
    click CORE "https://github.com/statebox/cql/tree/master/src/Language"
    click Parser "https://github.com/statebox/cql/tree/master/src/Language/CQL/Parser"
    click EX "https://github.com/statebox/cql/tree/master/examples"
    click T1 "https://github.com/statebox/cql/blob/master/test/CQLSpec.hs"
    click B1 "https://github.com/statebox/cql/blob/master/Setup.hs"
    click B2 "https://github.com/statebox/cql/blob/master/stack.yaml"
    click B3 "https://github.com/statebox/cql/blob/master/package.yaml"
    click B4 "https://github.com/statebox/cql/blob/master/.travis.yml"
    click B5 "https://github.com/statebox/cql/blob/master/.editorconfig"
    click B6 "https://github.com/statebox/cql/blob/master/cql.nix/default.nix"

    classDef interface fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef core fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef example fill:#A3E4D7,stroke:#117864,stroke-width:2px;
    classDef testing fill:#F5B7B1,stroke:#943126,stroke-width:2px;
    classDef build fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
```
