Skip to main content

Advanced PostgreSQL SQL parser for extracting database schema metadata

Project description

PostgreSQL SQL Parser

Python Version License: MIT PyPI Version

A high-performance PostgreSQL SQL parser for extracting comprehensive database schema metadata from DDL scripts.

Features

  • Complete SQL Parsing: Tokenizes and parses PostgreSQL DDL statements
  • Schema Metadata Extraction:
    • Database, schema, and table names
    • Table types (regular, temporary, view, materialized view)
    • Column definitions with data types, lengths, precision, and constraints
    • Primary keys, foreign keys, indexes, and constraints
  • Advanced SQL Support:
    • Quoted identifiers
    • ANSI SQL and PostgreSQL-specific syntax
    • CREATE/ALTER TABLE statements
    • View and materialized view definitions
  • Powerful API:
    • Parse entire scripts or individual statements
    • Retrieve tables by qualified name
    • Iterate through parsed statements
  • Well-Tested: Comprehensive test suite with 95%+ coverage

Installation

pip install python-pgsql-parser

Quick Start

from pgsql_parser import SQLParser

# Initialize parser
parser = SQLParser()

# Parse SQL script
sql_script = """
CREATE TABLE public.users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    email VARCHAR(255) UNIQUE
);

CREATE VIEW user_emails AS
SELECT id, email FROM public.users;
"""

parser.parse_script(sql_script)

# Get all tables
tables = parser.get_tables()
for table in tables:
    print(f"Table: {table.schema}.{table.name} ({table.table_type})")

# Get specific table
users_table = parser.get_table("users", schema="public")
if users_table:
    print(f"\nColumns in users table:")
    for col_name, column in users_table.columns.items():
        print(f"- {col_name}: {column.data_type}")

Documentation

Core Classes

  • SQLParser: Main parser class
  • Table: Represents a table/view definition
  • Column: Contains column metadata
  • PrimaryKey, ForeignKey, Constraint: Schema constraint objects

Key Methods

  • parse_script(sql_script): Parse entire SQL script
  • parse_statement(sql): Parse single SQL statement
  • get_tables(): Get all parsed tables/views
  • get_table(name, schema, database): Get specific table by qualified name
  • statement_generator(sql_script): Iterate through SQL statements

Usage Examples

Extract Table Metadata

table = parser.get_table("users", "public")

print(f"Table: {table.schema}.{table.name}")
print(f"Type: {table.table_type}")
print(f"Columns: {len(table.columns)}")
print(f"Primary Key: {table.primary_key.columns if table.primary_key else None}")

print("\nColumn Details:")
for name, column in table.columns.items():
    print(f"- {name}: {column.data_type}, " +
          f"Nullable: {column.nullable}, " +
          f"Primary Key: {column.is_primary}")

Handle ALTER TABLE Statements

sql = """
ALTER TABLE users 
ADD COLUMN last_login TIMESTAMP,
ADD CONSTRAINT fk_country 
    FOREIGN KEY (country_id) REFERENCES countries(id);
"""

parser.parse_statement(sql)
table = parser.get_table("users")
print(f"New column: {table.columns['last_login'].data_type}")
print(f"New foreign key: {table.foreign_keys[-1].ref_table}")

Process Large Scripts

with open("schema.sql", "r") as f:
    sql_script = f.read()

for statement in parser.statement_generator(sql_script):
    parser.parse_statement(statement)
    
    # Process tables incrementally
    new_tables = parser.get_tables()
    for table in new_tables:
        save_to_database(table)

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/your-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin feature/your-feature)
  5. Open a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For issues and feature requests, please open an issue.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_pgsql_parser-0.1.1.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_pgsql_parser-0.1.1-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file python_pgsql_parser-0.1.1.tar.gz.

File metadata

  • Download URL: python_pgsql_parser-0.1.1.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for python_pgsql_parser-0.1.1.tar.gz
Algorithm Hash digest
SHA256 60cc4de2beaf24f975a2a253db1e809f964b739d4ae68a9bfa6f2404eb079853
MD5 b7cd0c85fcb692b4fcbb3dc984a4179b
BLAKE2b-256 b56cc4391d706198cba30f8bbc285e5b94be652dde103bc6a018c6062ec02859

See more details on using hashes here.

File details

Details for the file python_pgsql_parser-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_pgsql_parser-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 15dd561e150f82143b25e3d2be0bc0e21657f20004d9400e3d9afb747c8c9643
MD5 090a643a3b1d7caf0bc8ddbd22916a9c
BLAKE2b-256 19d1097556d4d7ce365d49e6b119306fa877d11e2e81864894000cea433e4433

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page