Core¶
The core module contains the fundamental components for SQL processing, including statement handling, parameter binding, result processing, compilation, and caching.
Overview¶
Core components:
SQL Statement (
statement.py) - SQL wrapper with metadata and parameter bindingParameters (
parameters.py) - Parameter style conversion and bindingResults (
result.py) - Result set handling and type mappingCompiler (
compiler.py) - SQL compilation and validation using sqlglotCache (
cache.py) - Statement caching for performanceFilters (
filters.py) - SQL transformation filters
SQL Statement¶
- class sqlspec.core.statement.SQL[source]¶
Bases:
objectSQL statement with parameter and filter support.
Represents a SQL statement that can be compiled with parameters and filters. Supports both positional and named parameters, statement filtering, and various execution modes including batch operations.
The main SQL statement class that wraps raw SQL with parameters.
Features:
Parameter binding (positional and named)
SQL compilation and validation
Statement metadata
Cache integration
Filter application
Usage:
from sqlspec.core import SQL # Simple SQL stmt = SQL("SELECT * FROM users") # With positional parameters stmt = SQL("SELECT * FROM users WHERE id = ?", 123) # With named parameters stmt = SQL("SELECT * FROM users WHERE id = :user_id", user_id=123) # With keyword parameters stmt = SQL("SELECT * FROM users WHERE id = :id", id=123) # Access SQL and parameters print(stmt.sql) # "SELECT * FROM users WHERE id = :id" print(stmt.parameters) # {"id": 123}
- __init__(statement, *parameters, statement_config=None, is_many=None, **kwargs)[source]¶
Initialize SQL statement.
- property positional_parameters: list[TypeAliasForwardRef('typing.Any')]¶
Get positional parameters (public API).
- property named_parameters: dict[str, TypeAliasForwardRef('typing.Any')]¶
Get named parameters (public API).
- property operation_type: Literal['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'COPY', 'COPY_FROM', 'COPY_TO', 'EXECUTE', 'SCRIPT', 'DDL', 'PRAGMA', 'MERGE', 'UNKNOWN']¶
SQL operation type.
- property statement_config: StatementConfig¶
Statement configuration.
- property filters: list[StatementFilter]¶
Applied filters.
- get_filters_view()[source]¶
Get zero-copy filters view (public API).
- Return type:
FiltersView- Returns:
Read-only view of filters without copying
- property statement_expression: Expression | None¶
Get parsed statement expression (public API).
- Returns:
Parsed SQLGlot expression or None if not parsed
- returns_rows()[source]¶
Check if statement returns rows.
- Return type:
- Returns:
True if the SQL statement returns result rows
- is_modifying_operation()[source]¶
Check if the SQL statement is a modifying operation.
- Return type:
- Returns:
True if the operation modifies data (INSERT/UPDATE/DELETE)
- as_script()[source]¶
Create copy marked for script execution.
- Return type:
- Returns:
New SQL instance configured for script execution
- class sqlspec.core.statement.StatementConfig[source]¶
Bases:
objectConfiguration for SQL statement processing.
Controls SQL parsing, validation, transformations, parameter handling, and other processing options for SQL statements.
Configuration for SQL statement processing.
Options:
enable_validation- Validate SQL syntaxenable_analysis- Analyze SQL for optimization hintsenable_transformations- Apply SQL transformationscache_statements- Cache compiled statements
- __init__(parameter_config=None, enable_parsing=True, enable_validation=True, enable_transformations=True, enable_analysis=False, enable_expression_simplification=False, enable_parameter_type_wrapping=True, enable_caching=True, parameter_converter=None, parameter_validator=None, dialect=None, pre_process_steps=None, post_process_steps=None, execution_mode=None, execution_args=None, output_transformer=None)[source]¶
Initialize StatementConfig.
- Parameters:
parameter_config¶ – Parameter style configuration
enable_parsing¶ – Enable SQL parsing
enable_validation¶ – Run SQL validators
enable_transformations¶ – Apply SQL transformers
enable_analysis¶ – Run SQL analyzers
enable_expression_simplification¶ – Apply expression simplification
enable_parameter_type_wrapping¶ – Wrap parameters with type information
enable_caching¶ – Cache processed SQL statements
parameter_converter¶ – Handles parameter style conversions
parameter_validator¶ – Validates parameter usage and styles
dialect¶ – SQL dialect
pre_process_steps¶ – Optional list of preprocessing steps
post_process_steps¶ – Optional list of postprocessing steps
execution_mode¶ – Special execution mode
execution_args¶ – Arguments for special execution modes
output_transformer¶ – Optional output transformation function
- enable_parsing¶
- enable_validation¶
- enable_transformations¶
- enable_analysis¶
- enable_expression_simplification¶
- enable_parameter_type_wrapping¶
- enable_caching¶
- parameter_converter¶
- parameter_validator¶
- parameter_config¶
- dialect¶
- pre_process_steps¶
- post_process_steps¶
- execution_mode¶
- execution_args¶
- output_transformer¶
Parameter Handling¶
- class sqlspec.core.parameters.ParameterProcessor[source]¶
Bases:
objectParameter processing engine coordinating conversion phases.
Processes SQL parameters by converting between different parameter styles.
Supported parameter styles:
ParameterStyle.QMARK-?(SQLite/DuckDB positional)ParameterStyle.NUMERIC-$1, $2(PostgreSQL positional for asyncpg, psqlpy)ParameterStyle.POSITIONAL_PYFORMAT-%s(PostgreSQL/MySQL format style)ParameterStyle.NAMED_COLON-:name(Named parameters for Oracle, SQLite)ParameterStyle.NAMED_AT-@name(BigQuery named parameters)ParameterStyle.NAMED_PYFORMAT-%(name)s(Python format style)
- DEFAULT_CACHE_SIZE = 1000¶
- process(sql, parameters, config, dialect=None, is_many=False)[source]¶
- Return type:
ParameterProcessingResult
- class sqlspec.core.parameters.ParameterConverter[source]¶
Bases:
objectParameter style conversion helper.
Converts parameters between different styles.
- validator¶
- class sqlspec.core.parameters.ParameterValidator[source]¶
Bases:
objectExtracts placeholder metadata and dialect compatibility information.
Validates parameter format and style.
- extract_parameters(sql)[source]¶
Extract ordered parameter metadata from SQL text.
- Return type:
list[ParameterInfo]
- class sqlspec.core.parameters.ParameterStyleConfig[source]¶
Bases:
objectConfiguration describing parameter behaviour for a statement.
Configuration for parameter style handling.
- __init__(default_parameter_style, supported_parameter_styles=None, supported_execution_parameter_styles=None, default_execution_parameter_style=None, type_coercion_map=None, has_native_list_expansion=False, needs_static_script_compilation=False, allow_mixed_parameter_styles=False, preserve_parameter_format=True, preserve_original_params_for_many=False, output_transformer=None, ast_transformer=None, json_serializer=None, json_deserializer=None)[source]¶
- default_parameter_style¶
- supported_parameter_styles¶
- supported_execution_parameter_styles¶
- default_execution_parameter_style¶
- type_coercion_map¶
- has_native_list_expansion¶
- output_transformer¶
- ast_transformer¶
- needs_static_script_compilation¶
- allow_mixed_parameter_styles¶
- preserve_parameter_format¶
- preserve_original_params_for_many¶
- json_serializer¶
- json_deserializer¶
- class sqlspec.core.parameters.ParameterStyle[source]¶
-
Enumeration of supported SQL parameter placeholder styles.
Enum defining supported parameter styles.
- NONE = 'none'¶
- STATIC = 'static'¶
- QMARK = 'qmark'¶
- NUMERIC = 'numeric'¶
- NAMED_COLON = 'named_colon'¶
- POSITIONAL_COLON = 'positional_colon'¶
- NAMED_AT = 'named_at'¶
- NAMED_DOLLAR = 'named_dollar'¶
- NAMED_PYFORMAT = 'pyformat_named'¶
- POSITIONAL_PYFORMAT = 'pyformat_positional'¶
- __new__(value)¶
Result Processing¶
- class sqlspec.core.result.SQLResult[source]¶
Bases:
StatementResultResult class for SQL operations that return rows or affect rows.
Handles SELECT, INSERT, UPDATE, DELETE operations. For DML operations with RETURNING clauses, the returned data is stored in the data attribute. The operation_type attribute indicates the nature of the operation.
For script execution, tracks multiple statement results and errors.
Result container for executed SQL queries.
Attributes:
data- List of result rows (dicts) - prefer using helper methods insteadrows_affected- Number of rows affected by INSERT/UPDATE/DELETEcolumns- Column namesmetadata- Query metadata
Recommended Helper Methods:
result = await session.execute("SELECT * FROM users") # Get all rows (replaces result.data) all_rows = result.all() # Get exactly one row (raises if not exactly one) user = result.one() # Get one row or None (raises if multiple) user = result.one_or_none() # Get first row without validation first_row = result.get_first() # Get scalar value (first column of first row) count = result.scalar() # Map to typed models from pydantic import BaseModel class User(BaseModel): id: int name: str email: str # Get all rows as typed models users: list[User] = result.all(schema_type=User) # Get exactly one row as typed model user: User = result.one(schema_type=User) # Get one or none as typed model user: User | None = result.one_or_none(schema_type=User)
- __init__(statement, data=None, rows_affected=0, last_inserted_id=None, execution_time=None, metadata=None, error=None, operation_type='SELECT', operation_index=None, parameters=None, column_names=None, total_count=None, has_more=False, inserted_ids=None, statement_results=None, errors=None, total_statements=0, successful_statements=0)[source]¶
Initialize SQL result.
- Parameters:
statement¶ (
SQL) – The original SQL statement that was executed.data¶ (
list[dict[str,Any]] |None) – The result data from the operation.rows_affected¶ (
int) – Number of rows affected by the operation.last_inserted_id¶ (
int|str|None) – Last inserted ID from the operation.execution_time¶ (
float|None) – Time taken to execute the statement in seconds.metadata¶ (
Optional[dict[str, typing.Any]]) – Additional metadata about the operation.error¶ (
Exception|None) – Exception that occurred during execution.operation_type¶ (
Literal['SELECT','INSERT','UPDATE','DELETE','COPY','COPY_FROM','COPY_TO','EXECUTE','SCRIPT','DDL','PRAGMA','MERGE','UNKNOWN']) – Type of SQL operation performed.operation_index¶ (
int|None) – Index of operation in a script.column_names¶ (
Optional[list[str]]) – Names of columns in the result set.total_count¶ (
int|None) – Total number of rows in the complete result set.has_more¶ (
bool) – Whether there are additional result pages available.inserted_ids¶ (
Optional[list[int|str]]) – List of IDs from INSERT operations.statement_results¶ (
Optional[list[SQLResult]]) – Results from individual statements in a script.errors¶ (
Optional[list[str]]) – List of error messages for script execution.total_statements¶ (
int) – Total number of statements in a script.successful_statements¶ (
int) – Count of successful statements in a script.
- error¶
- operation_index¶
- parameters¶
- has_more¶
- inserted_ids¶
- statement_results¶
- errors¶
- total_statements¶
- successful_statements¶
- column_names¶
- total_count¶
- property operation_type: Literal['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'COPY', 'COPY_FROM', 'COPY_TO', 'EXECUTE', 'SCRIPT', 'DDL', 'PRAGMA', 'MERGE', 'UNKNOWN']¶
Get operation type for this result.
- Returns:
The type of SQL operation that produced this result.
- is_success()[source]¶
Check if the operation was successful.
- Return type:
- Returns:
True if operation was successful, False otherwise.
- get_data(*, schema_type=None)[source]¶
Get the data from the result.
For regular operations, returns the list of rows. For script operations, returns a summary dictionary containing execution statistics and results.
- get_total_rows_affected()[source]¶
Get the total number of rows affected across all statements.
- Return type:
- Returns:
Total rows affected.
- property num_rows: int¶
Get the number of rows affected (alias for get_total_rows_affected).
- Returns:
Total rows affected.
- property num_columns: int¶
Get the number of columns in the result data.
- Returns:
Number of columns.
- get_first(*, schema_type=None)[source]¶
Get the first row from the result, if any.
- get_count()[source]¶
Get the number of rows in the current result set (e.g., a page of data).
- Return type:
- Returns:
Number of rows in current result set.
- is_empty()[source]¶
Check if the result set (self.data) is empty.
- Return type:
- Returns:
True if result set is empty.
- get_affected_count()[source]¶
Get the number of rows affected by a DML operation.
- Return type:
- Returns:
Number of affected rows.
- was_inserted()[source]¶
Check if this was an INSERT operation.
- Return type:
- Returns:
True if INSERT operation.
- was_updated()[source]¶
Check if this was an UPDATE operation.
- Return type:
- Returns:
True if UPDATE operation.
- was_deleted()[source]¶
Check if this was a DELETE operation.
- Return type:
- Returns:
True if DELETE operation.
- __len__()[source]¶
Get the number of rows in the result set.
- Return type:
- Returns:
Number of rows in the data.
- all(*, schema_type=None)[source]¶
Return all rows as a list.
- one(*, schema_type=None)[source]¶
Return exactly one row.
- Parameters:
schema_type¶ (
type[TypeVar(SchemaT)] |None) – Optional schema type to transform the data into. Supports Pydantic models, dataclasses, msgspec structs, attrs classes, and TypedDict.- Return type:
- Returns:
The single row (optionally transformed to schema_type)
- Raises:
ValueError – If no results or more than one result
- one_or_none(*, schema_type=None)[source]¶
Return at most one row.
- Parameters:
schema_type¶ (
type[TypeVar(SchemaT)] |None) – Optional schema type to transform the data into. Supports Pydantic models, dataclasses, msgspec structs, attrs classes, and TypedDict.- Return type:
- Returns:
The single row (optionally transformed to schema_type) or None if no results
- Raises:
ValueError – If more than one result
- scalar()[source]¶
Return the first column of the first row.
- Return type:
- Returns:
The scalar value from first column of first row
- scalar_or_none()[source]¶
Return the first column of the first row, or None if no results.
- Return type:
- Returns:
The scalar value from first column of first row, or None
Type Mapping:
SQLResult supports mapping to various Python type systems:
Pydantic models
msgspec.Struct
attrs classes
dataclasses
TypedDict
Plain dicts
Type mapping is handled directly by methods on the SQLResult class such as as_type(), as_type_one(), and as_type_one_or_none().
SQL Compilation¶
- class sqlspec.core.compiler.SQLProcessor[source]¶
Bases:
objectSQL processor with compilation and caching.
Processes SQL statements by compiling them into executable format with parameter substitution. Includes LRU-style caching for compilation results to avoid re-processing identical statements.
SQL processor with compilation and caching.
Processes SQL statements by compiling them into executable format with parameter substitution. Includes LRU-style caching for compilation results to avoid re-processing identical statements.
Usage:
from sqlspec.core import StatementConfig from sqlspec.core import SQLProcessor config = StatementConfig(dialect="postgres") processor = SQLProcessor(config) # Compile SQL with parameters compiled = processor.compile( "SELECT * FROM users WHERE active = ?", parameters=[True] )
- __init__(config, max_cache_size=1000)[source]¶
Initialize processor.
- Parameters:
config¶ (
StatementConfig) – Statement configurationmax_cache_size¶ (
int) – Maximum number of compilation results to cache
- class sqlspec.core.compiler.CompiledSQL[source]¶
Bases:
objectCompiled SQL result.
Contains the result of SQL compilation with information needed for execution. Immutable container holding compiled SQL text, processed parameters, operation type, and execution metadata.
Compiled SQL result containing the compiled SQL text, processed parameters, operation type, and execution metadata.
- __init__(compiled_sql, execution_parameters, operation_type, expression=None, parameter_style=None, supports_many=False, parameter_casts=None, parameter_profile=None, operation_profile=None)[source]¶
Initialize compiled result.
- Parameters:
execution_parameters¶ (
Any) – Parameters in driver-specific formatoperation_type¶ (
Literal['SELECT','INSERT','UPDATE','DELETE','COPY','COPY_FROM','COPY_TO','EXECUTE','SCRIPT','DDL','PRAGMA','MERGE','UNKNOWN']) – SQL operation type (SELECT, INSERT, etc.)parameter_style¶ (
str|None) – Parameter style used in compilationsupports_many¶ (
bool) – Whether this supports execute_many operationsparameter_casts¶ (
Optional[dict[int,str]]) – Mapping of parameter positions to cast typesparameter_profile¶ (
ParameterProfile|None) – Profile describing detected placeholdersoperation_profile¶ (
OperationProfile|None) – Profile describing semantic characteristics
- compiled_sql¶
- execution_parameters¶
-
operation_type:
Literal['SELECT','INSERT','UPDATE','DELETE','COPY','COPY_FROM','COPY_TO','EXECUTE','SCRIPT','DDL','PRAGMA','MERGE','UNKNOWN']¶
- expression¶
- parameter_style¶
- supports_many¶
- parameter_casts¶
- parameter_profile¶
- operation_profile¶
Statement Caching¶
- class sqlspec.core.cache.UnifiedCache[source]¶
Bases:
objectCache with LRU eviction and TTL support.
- Parameters:
Unified LRU cache for SQL statements and compilation results.
- class sqlspec.core.cache.MultiLevelCache[source]¶
Bases:
objectSingle cache with namespace isolation - no connection pool complexity.
Multi-level cache system for different caching strategies.
- class sqlspec.core.cache.CacheConfig[source]¶
Bases:
objectGlobal cache configuration for SQLSpec.
Configuration for caching behavior.
- __init__(*, compiled_cache_enabled=True, sql_cache_enabled=True, fragment_cache_enabled=True, optimized_cache_enabled=True, sql_cache_size=1000, fragment_cache_size=5000, optimized_cache_size=2000)[source]¶
Initialize cache configuration.
- class sqlspec.core.cache.CacheStats[source]¶
Bases:
objectCache statistics tracking.
Tracks cache metrics including hit rates, evictions, and memory usage.
Cache statistics tracking hits, misses, and evictions.
- hits¶
- misses¶
- evictions¶
- total_operations¶
- memory_usage¶
SQL Filters¶
Filters transform SQL statements by adding or modifying clauses.
- class sqlspec.core.filters.StatementFilter[source]¶
Bases:
ABCAbstract base class for filters that can be appended to a statement.
Base class for SQL statement filters.
All filters implement the
append_to_statement()method to modify SQL statements.- abstractmethod append_to_statement(statement)[source]¶
Append the filter to the statement.
This method should modify the SQL expression only, not the parameters. Parameters should be provided via extract_parameters().
- Return type:
- extract_parameters()[source]¶
Extract parameters that this filter contributes.
- Returns:
positional_parameters: List of positional parameter values
named_parameters: Dict of parameter name to value
- Return type:
Tuple of (positional_parameters, named_parameters) where
Built-in Filters¶
- class sqlspec.core.filters.LimitOffsetFilter[source]¶
Bases:
PaginationFilterFilter for LIMIT and OFFSET clauses.
Adds pagination support through LIMIT/OFFSET SQL clauses.
Adds LIMIT and OFFSET clauses.
from sqlspec.core import LimitOffsetFilter filter = LimitOffsetFilter(limit=10, offset=20) filtered_sql = filter.append_to_statement(base_sql) # Adds: LIMIT 10 OFFSET 20
- class sqlspec.core.filters.OrderByFilter[source]¶
Bases:
StatementFilterFilter for ORDER BY clauses.
Adds sorting capability to SQL queries.
Adds ORDER BY clause.
from sqlspec.core import OrderByFilter filter = OrderByFilter(field_name="created_at", sort_order="desc") filtered_sql = filter.append_to_statement(base_sql) # Adds: ORDER BY created_at DESC
- class sqlspec.core.filters.SearchFilter[source]¶
Bases:
StatementFilterFilter for text search queries.
Constructs WHERE field_name LIKE ‘%value%’ clauses.
Adds WHERE search condition.
from sqlspec.core import SearchFilter filter = SearchFilter(field_name="name", value="John", operator="ILIKE") filtered_sql = filter.append_to_statement(base_sql) # Adds: WHERE name ILIKE '%John%'
Filter Composition¶
Filters can be composed and chained:
from sqlspec.core import (
LimitOffsetFilter,
OrderByFilter,
SearchFilter
)
from sqlspec.core import SQL
base_sql = SQL("SELECT * FROM users")
# Apply multiple filters
filtered = base_sql
for filter_obj in [
SearchFilter(field_name="name", value="Alice"),
OrderByFilter(field_name="created_at", sort_order="desc"),
LimitOffsetFilter(limit=10, offset=0)
]:
filtered = filter_obj.append_to_statement(filtered)
# Result: SELECT * FROM users
# WHERE name ILIKE '%Alice%'
# ORDER BY created_at DESC
# LIMIT 10 OFFSET 0
Type Conversions¶
- class sqlspec.core.type_conversion.BaseTypeConverter[source]¶
Bases:
objectUniversal type detection and conversion for all adapters.
Provides centralized type detection and conversion functionality that can be used across all database adapters to ensure consistent behavior. Users can extend this class for custom type conversion needs.
Base class for type conversion between Python types and database types.
Advanced Features¶
Statement Analysis¶
from sqlspec.core import SQLProcessor
compiler = SQLProcessor(dialect="postgres")
analysis = compiler.analyze("""
SELECT u.name, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.active = TRUE
GROUP BY u.id, u.name
HAVING COUNT(o.id) > 5
""")
# Analysis includes:
# - Tables accessed
# - Columns referenced
# - Join types
# - Aggregate functions
# - Filter conditions
# - Potential optimizations
Custom Filters¶
Create custom filters for specific needs:
from sqlspec.core import StatementFilter
from sqlspec.core import SQL
class TenantFilter(StatementFilter):
def __init__(self, tenant_id: int):
self.tenant_id = tenant_id
def apply(self, sql: SQL) -> SQL:
# Add tenant condition to WHERE clause
condition = f"tenant_id = {self.tenant_id}"
if "WHERE" in sql.sql.upper():
new_sql = sql.sql.replace("WHERE", f"WHERE {condition} AND")
else:
new_sql = sql.sql + f" WHERE {condition}"
return SQL(new_sql, sql.parameters)
# Usage
filter = TenantFilter(tenant_id=123)
filtered = filter.apply(SQL("SELECT * FROM users"))
# Result: SELECT * FROM users WHERE tenant_id = 123
Performance Tips¶
Enable Caching
config = StatementConfig(cache_statements=True) sql = SQL("SELECT * FROM users", config=config)
Reuse Compiled Statements
# Compile once stmt = SQL("SELECT * FROM users WHERE id = ?") # Execute many times for user_id in user_ids: result = await session.execute(stmt, user_id)
Use Positional Parameters
# Positional parameters stmt = SQL("SELECT * FROM users WHERE id = ?", 123) # Named parameters (requires parsing) stmt = SQL("SELECT * FROM users WHERE id = :id", id=123)
See Also¶
Drivers and Querying - Query execution
Builder - SQL builder integration
Driver - Driver implementation
Base - SQLSpec configuration