# kodaimura/ddlparse architecture

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

- Interactive diagram: https://gitdiagram.com/kodaimura/ddlparse
- Repository: https://github.com/kodaimura/ddlparse

## Overview

The ddlparse project is a Go library designed to take SQL CREATE TABLE statements and convert them into a structured Table object. This object includes details such as schema, column definitions, and constraints. The library supports three SQL dialects—SQLite, MySQL, and PostgreSQL—which means that its architecture includes dialect-specific components for validation.

##### Main Components
- API/Entry Point: The core functionality is exposed in ddlparse.go. This file contains the Parse function that acts as the entry point to the library.
- Validator Module: Located in the internal/validator directory, this handles syntax and dialect-specific validations. Notice there are separate files for MySQL, PostgreSQL, and SQLite validators.
- Lexer Module: Found in internal/lexer/lexer.go. This component tokenizes the SQL DDL input.
- Converter Module: Located in internal/converter/converter.go. This part converts the tokens (or the parsed structure) into the final Table object format.
- Data Types and Common Utilities: The internal/types directory holds the Go type definitions (such as Table, Column, Constraint, etc.), while internal/common provides constants, error definitions, and utility functions.

##### Relationships and Interactions
- The client code calls the exposed Parse function (from ddlparse.go).
- The Parse function first likely invokes the appropriate validator (from internal/validator) based on the SQL dialect provided (MySQL, PostgreSQL, or SQLite). This step ensures that the input DDL conforms to the expected SQL syntax.
- Once validation is successful, the input is sent to the Lexer module (internal/lexer) to perform tokenization.
- The resulting tokens are then processed by the Converter module (internal/converter), which builds the Table object using the definitions from internal/types.
- The common utilities and error handling in internal/common support these modules by providing shared constants and error types.

##### Architectural Patterns and Design Principles
- Layered Architecture: The design exhibits a clear separation of concerns. There is a distinct flow—starting at the API, moving through validation, lexing, and conversion.
- Modularity: Each functionality (validation, lexing, conversion) is separated into dedicated packages. This modular approach facilitates easier maintenance and potential extension (e.g., adding support for additional SQL dialects).
- Dialect-Specific Handling: The validators and converters are designed to handle the unique syntactic variants of SQLite, MySQL, and PostgreSQL.
- Reusability: Common types and utilities ensure that changes in one part of the system (e.g., error logging or constant definitions) can be managed centrally.

##### Summarizing the Approach
- Separation of concerns, showing how SQL input is processed through validation, tokenization, and then conversion to an output object.
- That every component (including error handling and shared utility functions) is represented, as these are critical for understanding the overall system flow and handling of different SQL dialects.

## Diagram (Mermaid)

```mermaid
flowchart TB
    API["API/Entry Point (ddlparse.go)"]:::api
    CoreValidator["Core Validator"]:::validator
    MySQLValidator["MySQL Validator"]:::validator
    PostgreSQLValidator["PostgreSQL Validator"]:::validator
    SQLiteValidator["SQLite Validator"]:::validator
    Lexer["Lexer Module (lexer.go)"]:::module
    Converter["Converter Module (converter.go)"]:::module
    TableObject["Table Object"]:::output
    DataTypes["Data Types (types.go)"]:::shared
    CommonUtilities["Common Utilities (internal/common)"]:::shared

    API -->|"calls"| CoreValidator
    CoreValidator -->|"if_MySQL"| MySQLValidator
    CoreValidator -->|"if_PostgreSQL"| PostgreSQLValidator
    CoreValidator -->|"if_SQLite"| SQLiteValidator

    MySQLValidator -->|"validated_tokens"| Lexer
    PostgreSQLValidator -->|"validated_tokens"| Lexer
    SQLiteValidator -->|"validated_tokens"| Lexer

    Lexer -->|"token_stream"| Converter
    Converter -->|"produces"| TableObject

    Converter -->|"uses"| DataTypes
    Converter -->|"uses"| CommonUtilities
    CoreValidator -->|"uses"| CommonUtilities

    click API "https://github.com/kodaimura/ddlparse/blob/main/ddlparse.go"
    click CoreValidator "https://github.com/kodaimura/ddlparse/blob/main/internal/validator/validator.go"
    click MySQLValidator "https://github.com/kodaimura/ddlparse/blob/main/internal/validator/validator_mysql.go"
    click PostgreSQLValidator "https://github.com/kodaimura/ddlparse/blob/main/internal/validator/validator_postgresql.go"
    click SQLiteValidator "https://github.com/kodaimura/ddlparse/blob/main/internal/validator/validator_sqlite.go"
    click Lexer "https://github.com/kodaimura/ddlparse/blob/main/internal/lexer/lexer.go"
    click Converter "https://github.com/kodaimura/ddlparse/blob/main/internal/converter/converter.go"
    click DataTypes "https://github.com/kodaimura/ddlparse/blob/main/internal/types/types.go"
    click CommonUtilities "https://github.com/kodaimura/ddlparse/tree/main/internal/common"

    classDef api fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef validator fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef module fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef shared fill:#d1ecf1,stroke:#0c5460,stroke-dasharray: 5,5,stroke-width:2px;
    classDef output fill:#f8d7da,stroke:#721c24,stroke-width:2px;
```
