How kodaimura/ddlparse works
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.