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.
npx @tessl/cli install tessl/pypi-simple-ddl-parser@1.7.0A 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.
pip install simple-ddl-parserfrom simple_ddl_parser import DDLParser, parse_from_file
from typing import Optional, Union, List, Dict
import loggingImport exceptions:
from simple_ddl_parser import SimpleDDLParserException, DDLParserErrorImport supported dialects information:
from simple_ddl_parser import supported_dialectsfrom 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)The parser is built with a modular dialect-based architecture:
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]: ...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: ...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: ...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: ...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# 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