Skip to content

Commit 1d51c0e

Browse files
authored
fix: cleanup tests and core (#59)
Remove unnecessary comments and improve code clarity by cleaning up tests and core functionality. This enhances maintainability and readability.
1 parent 1507bb3 commit 1d51c0e

File tree

100 files changed

+1937
-5524
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1937
-5524
lines changed

sqlspec/_sql.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def _populate_insert_from_sql(self, builder: "Insert", sql_string: str) -> "Inse
466466
"""Parse SQL string and populate INSERT builder using SQLGlot directly."""
467467
try:
468468
# Use SQLGlot directly for parsing - no validation here
469-
parsed_expr = exp.maybe_parse(sql_string, dialect=self.dialect) # type: ignore[var-annotated]
469+
parsed_expr: exp.Expression = exp.maybe_parse(sql_string, dialect=self.dialect)
470470

471471
if isinstance(parsed_expr, exp.Insert):
472472
builder._expression = parsed_expr
@@ -488,7 +488,7 @@ def _populate_select_from_sql(self, builder: "Select", sql_string: str) -> "Sele
488488
"""Parse SQL string and populate SELECT builder using SQLGlot directly."""
489489
try:
490490
# Use SQLGlot directly for parsing - no validation here
491-
parsed_expr = exp.maybe_parse(sql_string, dialect=self.dialect) # type: ignore[var-annotated]
491+
parsed_expr: exp.Expression = exp.maybe_parse(sql_string, dialect=self.dialect)
492492

493493
if isinstance(parsed_expr, exp.Select):
494494
builder._expression = parsed_expr
@@ -504,7 +504,7 @@ def _populate_update_from_sql(self, builder: "Update", sql_string: str) -> "Upda
504504
"""Parse SQL string and populate UPDATE builder using SQLGlot directly."""
505505
try:
506506
# Use SQLGlot directly for parsing - no validation here
507-
parsed_expr = exp.maybe_parse(sql_string, dialect=self.dialect) # type: ignore[var-annotated]
507+
parsed_expr: exp.Expression = exp.maybe_parse(sql_string, dialect=self.dialect)
508508

509509
if isinstance(parsed_expr, exp.Update):
510510
builder._expression = parsed_expr
@@ -520,7 +520,7 @@ def _populate_delete_from_sql(self, builder: "Delete", sql_string: str) -> "Dele
520520
"""Parse SQL string and populate DELETE builder using SQLGlot directly."""
521521
try:
522522
# Use SQLGlot directly for parsing - no validation here
523-
parsed_expr = exp.maybe_parse(sql_string, dialect=self.dialect) # type: ignore[var-annotated]
523+
parsed_expr: exp.Expression = exp.maybe_parse(sql_string, dialect=self.dialect)
524524

525525
if isinstance(parsed_expr, exp.Delete):
526526
builder._expression = parsed_expr
@@ -536,7 +536,7 @@ def _populate_merge_from_sql(self, builder: "Merge", sql_string: str) -> "Merge"
536536
"""Parse SQL string and populate MERGE builder using SQLGlot directly."""
537537
try:
538538
# Use SQLGlot directly for parsing - no validation here
539-
parsed_expr = exp.maybe_parse(sql_string, dialect=self.dialect) # type: ignore[var-annotated]
539+
parsed_expr: exp.Expression = exp.maybe_parse(sql_string, dialect=self.dialect)
540540

541541
if isinstance(parsed_expr, exp.Merge):
542542
builder._expression = parsed_expr
@@ -725,9 +725,8 @@ def raw(sql_fragment: str, **parameters: Any) -> "Union[exp.Expression, SQL]":
725725
if not parameters:
726726
# Original behavior - return pure expression
727727
try:
728-
parsed: Optional[exp.Expression] = exp.maybe_parse(sql_fragment)
729-
if parsed is not None:
730-
return parsed
728+
parsed: exp.Expression = exp.maybe_parse(sql_fragment)
729+
return parsed
731730
if sql_fragment.strip().replace("_", "").replace(".", "").isalnum():
732731
return exp.to_identifier(sql_fragment)
733732
return exp.Literal.string(sql_fragment)
@@ -940,10 +939,8 @@ def any(values: Union[list[Any], exp.Expression, str]) -> FunctionExpression:
940939
return FunctionExpression(exp.Any(this=exp.Array(expressions=literals)))
941940
if isinstance(values, str):
942941
# Parse as SQL
943-
parsed = exp.maybe_parse(values) # type: ignore[var-annotated]
944-
if parsed:
945-
return FunctionExpression(exp.Any(this=parsed))
946-
return FunctionExpression(exp.Any(this=exp.Literal.string(values)))
942+
parsed: exp.Expression = exp.maybe_parse(values)
943+
return FunctionExpression(exp.Any(this=parsed))
947944
return FunctionExpression(exp.Any(this=values))
948945

949946
@staticmethod

sqlspec/core/__init__.py

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,92 @@
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()
1590
"""
1691

1792
from sqlspec.core import filters

0 commit comments

Comments
 (0)