Core¶
The core module provides SQL statement handling, parameter conversion, result helpers, and caching utilities used by every driver.
Example¶
core SQL usage¶from sqlspec import SQLSpec
from sqlspec.adapters.sqlite import SqliteConfig
from sqlspec.core import SQL
db_path = tmp_path / "core_api.db"
spec = SQLSpec()
config = spec.add_config(SqliteConfig(connection_config={"database": str(db_path)}))
with spec.provide_session(config) as session:
session.execute("create table if not exists notes (id integer primary key, body text)")
session.execute("insert into notes (body) values ('Note')")
query = SQL("select id, body from notes where id = :note_id or id = :note_id").select_only("body")
result = session.execute(query, {"note_id": 1})
print(result.one_or_none())
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.
- __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', 'COMMAND', 'UNKNOWN']¶
SQL operation type.
- property statement_config: StatementConfig¶
Statement configuration.
- property raw_expression: Expression | None¶
Original expression supplied at construction, if available.
- 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
- paginate(page, page_size)[source]¶
Add LIMIT and OFFSET for pagination.
- Parameters:
- Return type:
- Returns:
New SQL instance with LIMIT and OFFSET applied
Example
# Get page 3 with 20 items per page stmt = SQL(“SELECT * FROM users”).paginate(3, 20) # Results in: SELECT * FROM users LIMIT 20 OFFSET 40
- select_only(*columns, prune_columns=None)[source]¶
Replace SELECT columns with only the specified columns.
This is useful for narrowing down the columns returned by a query without modifying the FROM clause or WHERE conditions.
- Parameters:
- Return type:
- Returns:
New SQL instance with only the specified columns
Example
stmt = SQL(“SELECT * FROM users WHERE active = 1”) narrow = stmt.select_only(“id”, “name”, “email”) # Results in: SELECT id, name, email FROM users WHERE active = 1
# With column pruning on a subquery: stmt = SQL(“SELECT * FROM (SELECT id, name, email, created_at FROM users) AS u”) narrow = stmt.select_only(“id”, “name”, prune_columns=True) # Results in: SELECT id, name FROM (SELECT id, name FROM users) AS u
- explain(analyze=False, verbose=False, format=None)[source]¶
Create an EXPLAIN statement for this SQL.
Wraps the current SQL statement in an EXPLAIN clause with dialect-aware syntax generation.
- Parameters:
- Return type:
- Returns:
New SQL instance containing the EXPLAIN statement
Examples
- Basic EXPLAIN:
stmt = SQL(“SELECT * FROM users”) explain_stmt = stmt.explain()
- With options:
explain_stmt = stmt.explain(analyze=True, format=”json”)
- builder(dialect=None)[source]¶
Create a query builder seeded from this SQL statement.
- Parameters:
dialect¶ (
Union[str,Dialect,Type[Dialect],None]) – Optional SQL dialect override for parsing and rendering.- Return type:
QueryBuilder- Returns:
QueryBuilder instance initialized with the parsed statement.
- Raises:
SQLBuilderError – If the statement cannot be parsed.
Notes
Statements outside the DML set return an ExpressionBuilder without DML-specific helper methods.
- 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.
- __init__(parameter_config=None, enable_parsing=True, enable_validation=True, enable_transformations=True, enable_analysis=False, enable_expression_simplification=False, enable_column_pruning=False, enable_parameter_type_wrapping=True, enable_caching=True, parameter_converter=None, parameter_validator=None, dialect=None, execution_mode=None, execution_args=None, output_transformer=None, statement_transformers=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_column_pruning¶ – Remove unused columns from subqueries during select_only
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
execution_mode¶ – Special execution mode
execution_args¶ – Arguments for special execution modes
output_transformer¶ – Optional output transformation function
statement_transformers¶ – Optional AST transformers executed during compilation
- enable_parsing¶
- enable_validation¶
- enable_transformations¶
- enable_analysis¶
- enable_expression_simplification¶
- enable_column_pruning¶
- enable_parameter_type_wrapping¶
- enable_caching¶
- parameter_converter¶
- parameter_validator¶
- parameter_config¶
- dialect¶
- execution_mode¶
- execution_args¶
- output_transformer¶
- statement_transformers¶
Parameter Handling¶
- class sqlspec.core.parameters.ParameterProcessor[source]¶
Bases:
objectParameter processing engine coordinating conversion phases.
- DEFAULT_CACHE_SIZE = 1000¶
- __init__(*, converter=None, validator=None, cache_max_size=None, validator_cache_max_size=None)[source]¶
- process(sql, parameters, config, dialect=None, is_many=False, wrap_types=True, param_fingerprint=None)[source]¶
- Return type:
ParameterProcessingResult
- process_for_execution(sql, parameters, config, dialect=None, is_many=False, wrap_types=True, parsed_expression=None, param_fingerprint=None)[source]¶
Process parameters for execution without parse normalization.
- Parameters:
parameters¶ (sqlspec.core.parameters.ParameterPayload) – Parameter payload.
config¶ (
ParameterStyleConfig) – Parameter style configuration.wrap_types¶ (
bool) – Whether to wrap parameters with type metadata.parsed_expression¶ (
Any) – Pre-parsed SQLGlot expression to preserve through pipeline.param_fingerprint¶ (
Any|None) – Pre-computed parameter fingerprint for cache key.
- Return type:
ParameterProcessingResult- Returns:
ParameterProcessingResult with execution SQL and parameters.
- class sqlspec.core.parameters.ParameterConverter[source]¶
Bases:
objectParameter style conversion helper.
- validator¶
- class sqlspec.core.parameters.ParameterValidator[source]¶
Bases:
objectExtracts placeholder metadata and dialect compatibility information.
- class sqlspec.core.parameters.ParameterStyleConfig[source]¶
Bases:
objectConfiguration describing parameter behaviour for a statement.
- __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, strict_named_parameters=True)[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¶
- strict_named_parameters¶
- json_serializer¶
- json_deserializer¶
- class sqlspec.core.parameters.ParameterStyle[source]¶
-
Enumeration of supported SQL parameter placeholder 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.
- __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, row_format='dict')[source]¶
Initialize SQL result.
- Parameters:
statement¶ (
SQL) – The original SQL statement that was executed.data¶ (
list[typing.Any] |None) – The result data from the operation (raw driver-native format).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¶ (
dict[str, typing.Any] |None) – 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','COMMAND','UNKNOWN']) – Type of SQL operation performed.operation_index¶ (
int|None) – Index of operation in a script.column_names¶ (
list[str] |None) – 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¶ (
list[int|str] |None) – List of IDs from INSERT operations.statement_results¶ (
list[SQLResult] |None) – Results from individual statements in a script.errors¶ (
list[str] |None) – 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.row_format¶ (
str) – Format of raw rows - “tuple”, “dict”, or “record”.
- 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', 'COMMAND', 'UNKNOWN']¶
Get operation type for this result.
- Returns:
The type of SQL operation that produced this result.
- property raw_data: tuple[list[TypeAliasForwardRef('typing.Any')], list[str]]¶
Zero-copy access to raw driver-native rows and column names.
- Returns:
Tuple of (raw_rows, column_names).
- 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
- to_arrow()[source]¶
Convert result data to Apache Arrow Table.
- Return type:
Table- Returns:
Arrow Table containing the result data.
- Raises:
ValueError – If no data available.
Examples
>>> result = session.select("SELECT * FROM users") >>> table = result.to_arrow() >>> print(table.num_rows) 3
- to_pandas()[source]¶
Convert result data to pandas DataFrame.
- Return type:
DataFrame- Returns:
pandas DataFrame containing the result data.
- Raises:
ValueError – If no data available.
Examples
>>> result = session.select("SELECT * FROM users") >>> df = result.to_pandas() >>> print(df.head())
- to_polars()[source]¶
Convert result data to Polars DataFrame.
- Return type:
DataFrame- Returns:
Polars DataFrame containing the result data.
- Raises:
ValueError – If no data available.
Examples
>>> result = session.select("SELECT * FROM users") >>> df = result.to_polars() >>> print(df.head())
SQLSpec results expose helper methods like all(), one(), one_or_none(),
scalar(), to_pandas(), and to_arrow() for structured access.