pySQLY Design Document
This document outlines the architectural design and patterns used in pySQLY.
Architecture Overview
pySQLY follows a layered architecture with the following components:
- Core Layer: Handles parsing, validation, and execution of SQLY queries
- Connector Layer: Provides database-specific implementations
- Error Handling Layer: Centralizes error management
- CLI Layer: Provides command-line interface functionality
The following diagram illustrates the high-level architecture:
┌─────────────┐ ┌─────────────┐
│ Client │ │ CLI │
└─────┬───────┘ └──────┬──────┘
│ │
▼ ▼
┌─────────────────────────────────┐
│ SQLYExecutor │
└─────────────┬───────────────────┘
│
┌─────────┴─────────┐
│ │
▼ ▼
┌─────────┐ ┌──────────────┐
│SQLYParser│ │SQLYUtils │
└─────────┘ └──────┬───────┘
│
▼
┌───────────────────┐
│DatabaseConnector │
└─────────┬─────────┘
│
┌───────┴────────┐
│ │
▼ ▼
┌────────────┐ ┌─────────────┐
│ Specific │...│ Specific │
│Connectors │ │Connectors │
└────────────┘ └─────────────┘
Design Patterns
-
Factory Pattern: The
DBConnectorFactory
class creates appropriate database connector instances based on the database type, abstracting the creation logic. -
Strategy Pattern: Different database connectors implement a common interface (
IDBConnector
), allowing the rest of the application to work with different databases uniformly. -
Facade Pattern: The
SQLYExecutor
class provides a simplified interface to the complex subsystem of parsing, validation, and execution. -
Composite Pattern: SQLY queries are composed as structured YAML objects that can represent complex SQL statements.
Core Components
SQLYParser
Responsible for parsing YAML queries into Python dictionaries. Uses PyYAML for YAML parsing.
SQLYUtils
Contains utility functions for validating and translating SQLY queries to SQL statements.
SQLYExecutor
The main entry point for query execution. Orchestrates the parsing, validation, and execution process.
Database Connectors
The connector layer follows an interface-based design:
IDBConnector
: Interface defining the contract for all database connectorsBaseDBConnector
: Abstract implementation with common functionality- Specific connectors: Database-specific implementations (SQLite, MariaDB, PostgreSQL, Oracle, MSSQL)
Error Handling Strategy
pySQLY implements a hierarchical exception system:
SQLYError
: Base exception classSQLYParseError
: For parsing errorsSQLYExecutionError
: For execution errorsSQLYConnectorError
: For database connector errors
This allows for fine-grained exception handling and appropriate error messages.
Extension Points
pySQLY is designed to be extensible in several ways:
- New Database Support: Add new connectors by implementing the
IDBConnector
interface - Query Features: Extend the YAML format and the translation logic in
SQLYUtils
- Result Processing: Add post-processing capabilities to transform query results
Performance Considerations
- Connection pooling is handled at the database driver level
- YAML parsing is optimized using PyYAML’s safe_load
- Parameter binding is used to prevent SQL injection and improve performance
Future Architectural Improvements
- Connection Pooling: Implement a custom connection pooling mechanism
- Async Support: Add asynchronous query execution capabilities
- Query Caching: Implement caching for frequently executed queries
- Result Set Pagination: Support for paginated results for large data sets