|
1 | | -"""SQLSpec Core Module - SQL Processing System. |
2 | | -
|
3 | | -This module provides the core SQL processing components including statement handling, |
4 | | -parameter processing, compilation, and result management. |
5 | | -
|
6 | | -Components: |
7 | | -- statement.py: SQL class with StatementConfig |
8 | | -- parameters.py: Parameter processing pipeline |
9 | | -- compiler.py: SQL compilation with caching |
10 | | -- result.py: Result classes for query execution |
11 | | -- filters.py: Statement filter system |
12 | | -- cache.py: Unified caching system |
13 | | -- splitter.py: SQL statement splitter |
14 | | -- hashing.py: Cache key generation |
| 1 | +"""SQLSpec Core Module - High-Performance SQL Processing System. |
| 2 | +
|
| 3 | +This module provides the core SQL processing infrastructure for SQLSpec, implementing |
| 4 | +a complete pipeline for SQL statement compilation, parameter processing, caching, |
| 5 | +and result management. All components are optimized for MyPyC compilation and |
| 6 | +designed for maximum performance with minimal overhead. |
| 7 | +
|
| 8 | +Architecture Overview: |
| 9 | + The core module implements a single-pass processing pipeline where SQL statements |
| 10 | + are parsed once, transformed once, and validated once. The SQL object serves as |
| 11 | + the single source of truth throughout the system. |
| 12 | +
|
| 13 | +Key Components: |
| 14 | + statement.py: SQL statement representation and configuration management |
| 15 | + - SQL class for statement encapsulation with lazy compilation |
| 16 | + - StatementConfig for processing pipeline configuration |
| 17 | + - ProcessedState for cached compilation results |
| 18 | + - Support for execute_many and script execution modes |
| 19 | +
|
| 20 | + parameters.py: Type-safe parameter processing and style conversion |
| 21 | + - Automatic parameter style detection and conversion |
| 22 | + - Support for QMARK (?), NAMED (:name), NUMERIC ($1), FORMAT (%s) styles |
| 23 | + - Parameter validation and type coercion |
| 24 | + - Batch parameter handling for execute_many operations |
| 25 | +
|
| 26 | + compiler.py: SQL compilation with validation and optimization |
| 27 | + - SQLProcessor for statement compilation and validation |
| 28 | + - Operation type detection (SELECT, INSERT, UPDATE, DELETE, etc.) |
| 29 | + - AST-based SQL analysis using SQLGlot |
| 30 | + - Support for multiple SQL dialects |
| 31 | + - Compiled result caching for performance |
| 32 | +
|
| 33 | + result.py: Comprehensive result handling for all SQL operations |
| 34 | + - SQLResult for standard query results with metadata |
| 35 | + - ArrowResult for Apache Arrow format integration |
| 36 | + - Support for DML operations with RETURNING clauses |
| 37 | + - Script execution result aggregation |
| 38 | + - Iterator protocol support for result rows |
| 39 | +
|
| 40 | + filters.py: Composable SQL statement filters |
| 41 | + - BeforeAfterFilter for date range filtering |
| 42 | + - InCollectionFilter for IN clause generation |
| 43 | + - LimitOffsetFilter for pagination |
| 44 | + - OrderByFilter for dynamic sorting |
| 45 | + - SearchFilter for text search operations |
| 46 | + - Parameter conflict resolution |
| 47 | +
|
| 48 | + cache.py: Unified caching system with LRU eviction |
| 49 | + - UnifiedCache with configurable TTL and size limits |
| 50 | + - StatementCache for compiled SQL statements |
| 51 | + - ExpressionCache for parsed SQLGlot expressions |
| 52 | + - ParameterCache for processed parameters |
| 53 | + - Thread-safe operations with fine-grained locking |
| 54 | + - Cache statistics and monitoring |
| 55 | +
|
| 56 | + splitter.py: Dialect-aware SQL script splitting |
| 57 | + - Support for Oracle PL/SQL, T-SQL, PostgreSQL, MySQL |
| 58 | + - Proper handling of block structures (BEGIN/END) |
| 59 | + - Dollar-quoted string support for PostgreSQL |
| 60 | + - Batch separator recognition (GO for T-SQL) |
| 61 | + - Comment and string literal preservation |
| 62 | +
|
| 63 | + hashing.py: Efficient cache key generation |
| 64 | + - SQL statement hashing with parameter consideration |
| 65 | + - Expression tree hashing for AST caching |
| 66 | + - Parameter set hashing for batch operations |
| 67 | + - Optimized hash computation with caching |
| 68 | +
|
| 69 | +Performance Optimizations: |
| 70 | + - MyPyC compilation support with proper annotations |
| 71 | + - __slots__ usage for memory efficiency |
| 72 | + - Final annotations for constant folding |
| 73 | + - Lazy evaluation and compilation |
| 74 | + - Comprehensive result caching |
| 75 | + - Minimal object allocation in hot paths |
| 76 | +
|
| 77 | +Thread Safety: |
| 78 | + All caching components are thread-safe with RLock protection. |
| 79 | + The processing pipeline is stateless and safe for concurrent use. |
| 80 | +
|
| 81 | +Example Usage: |
| 82 | + >>> from sqlspec.core import SQL, StatementConfig |
| 83 | + >>> config = StatementConfig(dialect="postgresql") |
| 84 | + >>> stmt = SQL( |
| 85 | + ... "SELECT * FROM users WHERE id = ?", |
| 86 | + ... 1, |
| 87 | + ... statement_config=config, |
| 88 | + ... ) |
| 89 | + >>> compiled_sql, params = stmt.compile() |
15 | 90 | """ |
16 | 91 |
|
17 | 92 | from sqlspec.core import filters |
|
0 commit comments