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.5.tar.gz (23.0 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.5-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: python_pgsql_parser-0.1.5.tar.gz
  • Upload date:
  • Size: 23.0 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.5.tar.gz
Algorithm Hash digest
SHA256 7e1bfdb25ec77a69fcb660fb7a3c0bc03a6f6324f98914ca69358265890a04d2
MD5 6b138699de81caeab863b2e5e1f4fd4a
BLAKE2b-256 68f994e7ad37d4cb288930305f820293d5633f1b1d44dd0cb0ab78d10a977e57

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_pgsql_parser-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 80a6c86a4e88a6139426417d21d13ed1428fdaba9249a400f956bdda050719ed
MD5 1bc06d2a7d47b7b0b45f7a6f6be8160a
BLAKE2b-256 a2a69ec43fac0e9d8ce716d67fc2a9372f54a576314cfbf07999d1a95f367eb3

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