|
| 1 | +# pySQLY Design Document |
| 2 | + |
| 3 | +This document outlines the architectural design and patterns used in pySQLY. |
| 4 | + |
| 5 | +## Architecture Overview |
| 6 | + |
| 7 | +pySQLY follows a layered architecture with the following components: |
| 8 | + |
| 9 | +1. **Core Layer**: Handles parsing, validation, and execution of SQLY queries |
| 10 | +2. **Connector Layer**: Provides database-specific implementations |
| 11 | +3. **Error Handling Layer**: Centralizes error management |
| 12 | +4. **CLI Layer**: Provides command-line interface functionality |
| 13 | + |
| 14 | +The following diagram illustrates the high-level architecture: |
| 15 | + |
| 16 | +```bash |
| 17 | +┌─────────────┐ ┌─────────────┐ |
| 18 | +│ Client │ │ CLI │ |
| 19 | +└─────┬───────┘ └──────┬──────┘ |
| 20 | + │ │ |
| 21 | + ▼ ▼ |
| 22 | +┌─────────────────────────────────┐ |
| 23 | +│ SQLYExecutor │ |
| 24 | +└─────────────┬───────────────────┘ |
| 25 | + │ |
| 26 | + ┌─────────┴─────────┐ |
| 27 | + │ │ |
| 28 | + ▼ ▼ |
| 29 | +┌─────────┐ ┌──────────────┐ |
| 30 | +│SQLYParser│ │SQLYUtils │ |
| 31 | +└─────────┘ └──────┬───────┘ |
| 32 | + │ |
| 33 | + ▼ |
| 34 | + ┌───────────────────┐ |
| 35 | + │DatabaseConnector │ |
| 36 | + └─────────┬─────────┘ |
| 37 | + │ |
| 38 | + ┌───────┴────────┐ |
| 39 | + │ │ |
| 40 | + ▼ ▼ |
| 41 | + ┌────────────┐ ┌─────────────┐ |
| 42 | + │ Specific │...│ Specific │ |
| 43 | + │Connectors │ │Connectors │ |
| 44 | + └────────────┘ └─────────────┘ |
| 45 | +``` |
| 46 | + |
| 47 | +## Design Patterns |
| 48 | + |
| 49 | +1. **Factory Pattern**: The `DBConnectorFactory` class creates appropriate database connector instances based on the database type, abstracting the creation logic. |
| 50 | + |
| 51 | +2. **Strategy Pattern**: Different database connectors implement a common interface (`IDBConnector`), allowing the rest of the application to work with different databases uniformly. |
| 52 | + |
| 53 | +3. **Facade Pattern**: The `SQLYExecutor` class provides a simplified interface to the complex subsystem of parsing, validation, and execution. |
| 54 | + |
| 55 | +4. **Composite Pattern**: SQLY queries are composed as structured YAML objects that can represent complex SQL statements. |
| 56 | + |
| 57 | +## Core Components |
| 58 | + |
| 59 | +### SQLYParser |
| 60 | + |
| 61 | +Responsible for parsing YAML queries into Python dictionaries. Uses PyYAML for YAML parsing. |
| 62 | + |
| 63 | +### SQLYUtils |
| 64 | + |
| 65 | +Contains utility functions for validating and translating SQLY queries to SQL statements. |
| 66 | + |
| 67 | +### SQLYExecutor |
| 68 | + |
| 69 | +The main entry point for query execution. Orchestrates the parsing, validation, and execution process. |
| 70 | + |
| 71 | +## Database Connectors |
| 72 | + |
| 73 | +The connector layer follows an interface-based design: |
| 74 | + |
| 75 | +1. `IDBConnector`: Interface defining the contract for all database connectors |
| 76 | +2. `BaseDBConnector`: Abstract implementation with common functionality |
| 77 | +3. Specific connectors: Database-specific implementations (SQLite, MariaDB, PostgreSQL, Oracle, MSSQL) |
| 78 | + |
| 79 | +## Error Handling Strategy |
| 80 | + |
| 81 | +pySQLY implements a hierarchical exception system: |
| 82 | + |
| 83 | +1. `SQLYError`: Base exception class |
| 84 | +2. `SQLYParseError`: For parsing errors |
| 85 | +3. `SQLYExecutionError`: For execution errors |
| 86 | +4. `SQLYConnectorError`: For database connector errors |
| 87 | + |
| 88 | +This allows for fine-grained exception handling and appropriate error messages. |
| 89 | + |
| 90 | +## Extension Points |
| 91 | + |
| 92 | +pySQLY is designed to be extensible in several ways: |
| 93 | + |
| 94 | +1. **New Database Support**: Add new connectors by implementing the `IDBConnector` interface |
| 95 | +2. **Query Features**: Extend the YAML format and the translation logic in `SQLYUtils` |
| 96 | +3. **Result Processing**: Add post-processing capabilities to transform query results |
| 97 | + |
| 98 | +## Performance Considerations |
| 99 | + |
| 100 | +1. Connection pooling is handled at the database driver level |
| 101 | +2. YAML parsing is optimized using PyYAML's safe_load |
| 102 | +3. Parameter binding is used to prevent SQL injection and improve performance |
| 103 | + |
| 104 | +## Future Architectural Improvements |
| 105 | + |
| 106 | +1. **Connection Pooling**: Implement a custom connection pooling mechanism |
| 107 | +2. **Async Support**: Add asynchronous query execution capabilities |
| 108 | +3. **Query Caching**: Implement caching for frequently executed queries |
| 109 | +4. **Result Set Pagination**: Support for paginated results for large data sets |
0 commit comments