or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli.mdcore-parsing.mdexceptions.mdindex.mdinput-dialects.mdoutput-formatting.md
tile.json

tessl/pypi-simple-ddl-parser

Simple DDL Parser to parse SQL & dialects like HQL, TSQL (MSSQL), Oracle, AWS Redshift, Snowflake, MySQL, PostgreSQL, etc ddl files to json/python dict with full information about columns: types, defaults, primary keys, etc.; sequences, alters, custom types & other entities from ddl.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/simple-ddl-parser@1.7.x

To install, run

npx @tessl/cli install tessl/pypi-simple-ddl-parser@1.7.0

index.mddocs/

Simple DDL Parser

A comprehensive Python library for parsing DDL (Data Definition Language) SQL statements from various database dialects into structured JSON/Python dictionaries. Built with PLY (Python Lex-Yacc), it converts DDL files into detailed metadata containing information about database schemas, tables, columns (with types, constraints, defaults), primary keys, foreign keys, sequences, custom types, and other database entities.

Package Information

  • Package Name: simple-ddl-parser
  • Language: Python
  • Installation: pip install simple-ddl-parser

Core Imports

from simple_ddl_parser import DDLParser, parse_from_file
from typing import Optional, Union, List, Dict
import logging

Import exceptions:

from simple_ddl_parser import SimpleDDLParserException, DDLParserError

Import supported dialects information:

from simple_ddl_parser import supported_dialects

Basic Usage

from simple_ddl_parser import DDLParser

# Parse DDL from string
ddl_content = """
CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    email VARCHAR(100) UNIQUE
);
"""

parser = DDLParser(ddl_content)
parsed_tables = parser.run()
print(parsed_tables)

# Parse DDL from file
from simple_ddl_parser import parse_from_file

parsed_tables = parse_from_file('schema.sql')
print(parsed_tables)

Architecture

The parser is built with a modular dialect-based architecture:

  • DDLParser: Main parser class that orchestrates parsing and output formatting
  • Dialects: Input dialect classes handle SQL syntax variations across database systems
  • Output Dialects: Format parsed results according to specific database output requirements
  • Lexer/Parser: PLY-based tokenization and parsing engine for DDL syntax analysis
  • Utilities: Helper functions for name normalization, parentheses handling, and data processing

Capabilities

Core Parsing

Primary DDL parsing functionality that converts SQL DDL statements into structured dictionaries. Handles tables, columns, constraints, indexes, and other database objects with full metadata extraction.

class DDLParser:
    def __init__(
        self,
        content: str,
        silent: bool = True,
        debug: bool = False,
        normalize_names: bool = False,
        log_file: Optional[str] = None,
        log_level: Union[str, int] = logging.INFO
    ): ...
    
    def run(
        self,
        *,
        dump: bool = False,
        dump_path: str = "schemas",
        file_path: Optional[str] = None,
        output_mode: str = "sql",
        group_by_type: bool = False,
        json_dump: bool = False
    ) -> List[Dict]: ...

def parse_from_file(
    file_path: str,
    encoding: Optional[str] = "utf-8",
    parser_settings: Optional[dict] = None,
    **kwargs
) -> List[Dict]: ...

Core Parsing

Input Dialect Support

Specialized parsing logic for different SQL database dialects, handling syntax variations and database-specific features like data types, functions, and DDL extensions.

# Available input dialects (classes from simple_ddl_parser.dialects)
class BaseSQL: ...
class MySQL: ...
class PSQL: ...  # PostgreSQL
class MSSQL: ...  # Microsoft SQL Server
class Oracle: ...
class HQL: ...  # Hive Query Language
class BigQuery: ...
class Redshift: ...
class Snowflake: ...
class SparkSQL: ...
class IBMDb2: ...
class Athena: ...

Input Dialects

Output Formatting

Convert parsed DDL metadata back into formatted SQL DDL statements optimized for specific database platforms, with dialect-specific syntax and conventions.

# Available output modes
supported_dialects: Dict[str, Any]  # Maps dialect names to output classes

def dialects_clean_up(output_mode: str, table_data) -> Dict: ...

Output Formatting

Command Line Interface

CLI tool accessible via the sdp command for processing DDL files from the command line with various output options and batch processing capabilities.

def main(): ...  # Main CLI entry point (exposed as 'sdp' command)
def cli(): ...  # Create argument parser
def run_for_file(args): ...
def correct_extension(file_name: str) -> bool: ...

Command Line Interface

Exception Handling

Comprehensive error handling and exception classes for robust DDL parsing with detailed error reporting and debugging capabilities.

class SimpleDDLParserException(Exception): ...
DDLParserError = SimpleDDLParserException  # Alias for backward compatibility

Exception Handling

Types

# Core type aliases and return types
List[Dict]  # Main return type for parsed DDL structures
Dict  # Individual table/entity metadata structure
Optional[str]  # Optional string parameters
Union[str, int]  # Flexible parameter types for logging levels