A Python tool for generating Python classes from SQL files
Project description
splurge-sql-generator
A compact summary of splurge-sql-generator — a tool to generate Python (SQLAlchemy-friendly) classes from SQL template files with schema-aware type inference.
See the full documentation and details in the repository:
- Detailed docs:
docs/README-DETAILS.md - API reference:
docs/api/API-REFERENCE.md - CLI reference:
docs/cli/CLI-REFERENCE.md - Full changelog:
CHANGELOG.md
Key features
- SQL template parsing and method extraction from commented templates
- Schema-based type inference for accurate Python type annotations
- Configurable SQL-to-Python type mapping via YAML files (
types.yaml/--types) - CLI for batch generation with automatic schema discovery and
--generate-types - Strong SQL parsing with sqlparse and explicit error reporting
Getting started
- Install:
pip install splurge-sql-generatororpip install -e .for development - Read the full docs in
docs/README-DETAILS.md
For examples and advanced usage see examples/ in the repository.
Parse column definitions from table body using sqlparse tokens. Raises SqlValidationError if parsing fails or no valid columns are found.
What's new (2025.5.0)
- Centralized Safe I/O adapter (
SafeTextFileIoAdapter) for consistent file error handling and simplified I/O usage across the library. - Stabilized cross-version error messages and improved validation for schema file arguments.
- Documentation: comprehensive API and CLI reference files added to
docs/.
extract_table_names(sql_query: str) -> list[str]
Extract table names from SQL query using sqlparse. Raises SqlValidationError if parsing fails or no table names are found.
generate_types_file(*, output_path: str | None = None) -> str
Generate the default SQL type mapping YAML file.
Supported SQL Features
- Basic DML: SELECT, INSERT, UPDATE, DELETE
- CTEs: Common Table Expressions (WITH clauses)
- Complex Queries: Subqueries, JOINs, aggregations
- Database-Specific: SHOW, EXPLAIN, DESCRIBE, VALUES
- Parameters: Named parameters with
:param_namesyntax - Comments: Single-line (
--) and multi-line (/* */) comments
Generated Code Features
- Accurate Type Hints: Schema-based type inference for precise parameter and return value annotations
- Custom Type Support: Configurable SQL-to-Python type mappings for project-specific needs
- Parameter Validation: Optional validation of SQL parameters against schema definitions
- Multi-Database Types: Built-in support for SQLite, PostgreSQL, MySQL, MSSQL, and Oracle types
- Docstrings: Comprehensive documentation for each method
- Error Handling: Proper SQLAlchemy result handling with fail-fast validation
- Parameter Mapping: Automatic mapping of SQL parameters to Python arguments with inferred types
- Statement Type Detection: Correct return types based on SQL statement type
- Auto-Generated Headers: Clear identification of generated files
Error Handling and Validation
The library provides robust error handling with a fail-fast approach to ensure data integrity and clear error reporting:
SQL Parsing Validation
- Strict SQL Parsing: Functions like
parse_table_columns()andextract_table_names()use sqlparse for reliable parsing - No Fallback Mechanisms: Eliminates unreliable regex-based fallback parsing in favor of clear error reporting
- Clear Error Messages: Functions raise
SqlValidationErrorwith descriptive messages when parsing fails - Validation Checks: Ensures valid column definitions and table names are found before processing
Error Types
SqlValidationError: Raised when SQL parsing fails or validation checks failSqlFileError: Raised for file operation errors (file not found, permission denied, etc.)- Clear Context: Error messages include file paths, referenced tables, and available columns for debugging
Example Error Handling
from splurge_sql_generator.exceptions import SqlValidationError, SqlFileError
try:
# This will raise SqlValidationError if no valid columns are found
columns = parse_table_columns("CONSTRAINT pk_id PRIMARY KEY (id)")
except SqlValidationError as e:
print(f"SQL validation failed: {e}")
try:
# This will raise SqlValidationError if no table names are found
tables = extract_table_names("SELECT 1 as value")
except SqlValidationError as e:
print(f"SQL validation failed: {e}")
try:
# This will raise SqlValidationError for empty input
columns = parse_table_columns("")
except SqlValidationError as e:
print(f"SQL validation failed: {e}")
Development
Running Tests
python -m unittest discover -s tests -v
Project Structure
splurge-sql-generator/
├── splurge_sql_generator/
│ ├── __init__.py # Main package exports
│ ├── sql_helper.py # SQL parsing utilities
│ ├── sql_parser.py # SQL template parser
│ ├── schema_parser.py # SQL schema parser for type inference
│ ├── code_generator.py # Python code generator
│ ├── cli.py # Command-line interface
│ └── templates/ # Jinja2 templates (python_class.j2)
├── tests/ # Test suite
├── examples/ # Example SQL templates and schemas
│ ├── *.sql # SQL template files
│ ├── *.schema # SQL schema files for type inference
│ └── custom_types.yaml # Example custom type mapping
├── output/ # Generated code examples
└── types.yaml # Default SQL type mappings
License
MIT License - see LICENSE file for details.
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
Project details
Release history Release notifications | RSS feed
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 splurge_sql_generator-2025.6.1.tar.gz.
File metadata
- Download URL: splurge_sql_generator-2025.6.1.tar.gz
- Upload date:
- Size: 71.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ce0e2e32fb38f4da0122a5697fb89bff3666fff4fce0f1c22bad22b34f69339
|
|
| MD5 |
f259ae75d6a27d88446218de05b8d41e
|
|
| BLAKE2b-256 |
28b55c4916699cb42cd2a2fe17dd376bf0848658bac272eef03c87a65cd26c3a
|
File details
Details for the file splurge_sql_generator-2025.6.1-py3-none-any.whl.
File metadata
- Download URL: splurge_sql_generator-2025.6.1-py3-none-any.whl
- Upload date:
- Size: 84.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98b4b972ee9ddcdbd745c58d6884e781cff25e865e8991f884e70225d039e206
|
|
| MD5 |
e118c8d5913d20c61d7acdfc2a816dc3
|
|
| BLAKE2b-256 |
c728aeddadb8d4a27d0b55f6741245cff8a2986ca216eb3c39997d981f697b10
|