Advanced PostgreSQL SQL parser for extracting database schema metadata
Project description
PostgreSQL SQL Parser
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 classTable: Represents a table/view definitionColumn: Contains column metadataPrimaryKey,ForeignKey,Constraint: Schema constraint objects
Key Methods
parse_script(sql_script): Parse entire SQL scriptparse_statement(sql): Parse single SQL statementget_tables(): Get all parsed tables/viewsget_table(name, schema, database): Get specific table by qualified namestatement_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:
- Fork the repository
- Create your feature branch (
git checkout -b feature/your-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin feature/your-feature) - 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_pgsql_parser-0.1.4.tar.gz.
File metadata
- Download URL: python_pgsql_parser-0.1.4.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
45b822640c35bc3e917e993d68fc86c786819a6889fc34ca2542dbba6eb38a56
|
|
| MD5 |
0f1c7362e7ca82f63ff41ab5f7466f69
|
|
| BLAKE2b-256 |
97f0fe8c98df2b30bfe0f11d891d4fec06327c31697c20b3be5bedc04724e151
|
File details
Details for the file python_pgsql_parser-0.1.4-py3-none-any.whl.
File metadata
- Download URL: python_pgsql_parser-0.1.4-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ed4ee1d267c98e3af1cae435998db912614f8d4f36a7e3dfb8091e344cd58319
|
|
| MD5 |
f7ea38770cce75ca9bbd57eb9cd874f6
|
|
| BLAKE2b-256 |
62c34a6d7d8631e60133b96708c60a97701ad85d137c8b1c896507baad6b34df
|