# phpmyadmin/sql-parser architecture

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

- Interactive diagram: https://gitdiagram.com/phpmyadmin/sql-parser
- Repository: https://github.com/phpmyadmin/sql-parser

## Overview

This project is a SQL parser library (with CLI utilities) focused on tokenizing, lexing, and parsing SQL queries (specifically for MySQL and related dialects). It is used standalone or as part of phpMyAdmin, and provides features such as syntax highlighting, linting, and query formatting.

##### Main Components
- CLI Utilities: Represented by the executable (bin/sql-parser) which provides the command line interface for highlighting, linting, and tokenizing queries.
- Core Library (src/):
- Lexer: The module (src/Lexer.php and tests in tests/Lexer) responsible for breaking down SQL queries into tokens.
- Parser: The module (src/Parser.php and src/Parseable.php) that takes tokens from the lexer and creates structured SQL statements.
- Components & Statements: In directories like src/Components and src/Statements. These encapsulate parts of SQL (e.g., expressions, keywords, operations) and the full statement types (SELECT, CREATE, etc.).
- Contexts: Under src/Contexts. This module handles dialect-specific configurations for different versions of MySQL/MariaDB.
- Utilities & Tools: Parts like Formatter, Query, Translator, and helper classes under src/Utils and src/Tools provide additional functionality such as query formatting and test or context generation.
- Exceptions: Custom exceptions defined in src/Exceptions to handle lexer and parser errors.
- Localization/Translation: Implemented by the Translator class (src/Translator.php) and locale files (in the locale directory) to support multiple languages.
- Testing & Validation: The tests directory comprises unit tests, integration tests, data-driven tests, and benchmarks validating each component of the parser. This helps highlight the testing architecture and quality assurance.

##### Interactions & Data Flow
- Input Source (CLI or API call) → Command-line interface (bin/sql-parser) reads SQL query.
- The query is passed to the Lexer, which breaks it down into tokens.
- Tokens are fed into the Parser to produce an Abstract Syntax Tree (AST) composed of various Statement and Component objects.
- The Context component configures parsing behavior depending on the SQL dialect.
- Once parsed, utility modules can then format, validate, or manipulate this AST; for example, Formatter reconstructs the query, Query::getFlags identifies the query type.
- Translation/Localization is used to adapt error messages or outputs based on environment settings.
- The Tools and Tests subsystems (running through CI/CD as seen in GitHub workflows) ensure ongoing validation, regression testing, and code quality.

##### Architectural Patterns & Principles
- Modular Separation: Clear separation into lexical analysis, parsing, statement construction, and utility functions.
- Context/Configuration Design: The use of “Context” classes to handle different SQL dialects and modes.
- Reusability & Extensibility: Components and tool generators (in src/Tools and tools/) that allow customization and extension – such as replacing table names or modifying queries.
- Command Pattern: The CLI utility can be seen as handling different “commands” (--highlight, --lint, --tokenize) that trigger a chain of processing steps.
- Test-Driven Architecture: An extensive tests directory ensures that each module is independently verified.

##### Detailed & Essential Architectural Elements
- Clearly label every component (e.g., “PhpMyAdmin\SqlParser\Parser”, “Lexer”, “SelectStatement”, etc.)
- Interactions: for example, how a SQL query flows from the CLI into the Lexer → Parser → Statement objects → Formatter.

- The entry points (CLI and API)
- The parsing pipeline (Lexer → Parser → AST (Components/Statements))
- The support for varied SQL dialects (Contexts)
- Utility modules for formatting, translation, and error handling.
- Ancillary systems (Tools and Tests) that support development, testing, and localization.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input Interface
    subgraph "Input Interface"
        CLI["CLI Utility"]:::input
    end

    %% Parsing Engine
    subgraph "Parsing Engine"
        LEX["Lexer (Tokenization)"]:::engine
        PAR["Parser (AST Generation)"]:::engine
        COMP["Components"]:::engine
        STMT["Statements"]:::engine
    end

    %% Configuration & Support
    subgraph "Configuration & Support"
        CTX["Contexts"]:::support
        UTLS["Utilities & Tools"]:::support
        EXC["Exceptions"]:::support
        LOCL["Localization & Translation"]:::support
    end

    %% Validation & CI/CD
    subgraph "Validation & CI/CD"
        TEST["Testing & Validation"]:::test
        CICD["CI/CD (GitHub workflows)"]:::external
    end

    %% Data Flow Connections
    CLI -->|"reads|SQL"| LEX
    LEX -->|"tokenizes"| PAR
    PAR -->|"builds AST"| COMP
    PAR -->|"builds AST"| STMT
    CTX -->|"configures"| LEX
    CTX -->|"configures"| PAR
    PAR -->|"formats/validates"| UTLS
    PAR -->|"outputs messages"| LOCL
    TEST -->|"verifies"| CLI
    TEST -->|"verifies"| LEX
    TEST -->|"verifies"| PAR
    CICD -->|"triggers tests"| TEST

    %% Styles
    classDef input fill:#f9e79f,stroke:#000,stroke-width:2px;
    classDef engine fill:#aed6f1,stroke:#000,stroke-width:2px;
    classDef support fill:#abebc6,stroke:#000,stroke-width:2px;
    classDef test fill:#f5cba7,stroke:#000,stroke-width:2px;
    classDef external fill:#e6b0aa,stroke:#000,stroke-width:2px;

    %% Click Events
    click CLI "https://github.com/phpmyadmin/sql-parser/tree/master/bin/sql-parser"
    click LEX "https://github.com/phpmyadmin/sql-parser/blob/master/src/Lexer.php"
    click PAR "https://github.com/phpmyadmin/sql-parser/blob/master/src/Parser.php"
    click COMP "https://github.com/phpmyadmin/sql-parser/tree/master/src/Components"
    click STMT "https://github.com/phpmyadmin/sql-parser/tree/master/src/Statements"
    click CTX "https://github.com/phpmyadmin/sql-parser/tree/master/src/Contexts"
    click UTLS "https://github.com/phpmyadmin/sql-parser/tree/master/src/Utils"
    click EXC "https://github.com/phpmyadmin/sql-parser/tree/master/src/Exceptions"
    click LOCL "https://github.com/phpmyadmin/sql-parser/blob/master/src/Translator.php"
    click TEST "https://github.com/phpmyadmin/sql-parser/tree/master/tests"
```
