parsing package

parsing.parsing module

parsing.parsing.parse_create_tables(sql_script, dialect='postgres')

Parse SQL CREATE TABLE statements and extract schema details.

This function uses sqlglot to parse a given SQL script (in the specified dialect) and returns a dictionary keyed by table names. Each table entry includes metadata such as column definitions, foreign keys, primary keys, unique constraints, and check constraints.

Parameters:
  • sql_script (str) – The SQL script containing one or more CREATE TABLE statements.

  • dialect (str, optional) – The SQL dialect to use for parsing (default is ‘postgres’).

Returns:

A dictionary where each key is a table name and the value is a dictionary containing:

  • ”columns”: a list of column metadata dictionaries,

  • ”foreign_keys”: a list of foreign key dictionaries,

  • ”primary_key”: a list of primary key column names,

  • ”unique_constraints”: a list of unique constraint lists,

  • ”check_constraints”: a list of check constraint expressions.

Return type:

Dict[str, Any]