Queries#
DML query builders for SELECT, INSERT, UPDATE, DELETE, and MERGE operations.
Select#
- class sqlspec.builder.Select[source]#
Bases:
QueryBuilder,WhereClauseMixin,OrderByClauseMixin,LimitOffsetClauseMixin,SelectClauseMixin,JoinClauseMixin,HavingClauseMixin,SetOperationMixin,CommonTableExpressionMixin,PivotClauseMixin,UnpivotClauseMixin,ExplainMixinBuilder for SELECT queries.
Provides a fluent interface for constructing SQL SELECT statements with parameter binding and validation.
- with_hint(hint, *, location='statement', table=None, dialect=None)[source]#
Attach an optimizer or dialect-specific hint to the query.
- Parameters:
- Return type:
Self- Returns:
The current builder instance for method chaining.
- build(dialect=None)[source]#
Builds the SQL query string and parameters with hint injection.
- Parameters:
dialect¶ (sqlglot.dialects.dialect.DialectType) -- Optional dialect override for SQL generation.
- Returns:
A dataclass containing the SQL string and parameters.
- Return type:
- for_update(*, skip_locked=False, nowait=False, of=None)[source]#
Add FOR UPDATE clause to SELECT statement for row-level locking.
Add FOR SHARE clause for shared row-level locking.
Add FOR KEY SHARE clause (PostgreSQL-specific).
FOR KEY SHARE is like FOR SHARE, but the lock is weaker: SELECT FOR UPDATE is blocked, but not SELECT FOR NO KEY UPDATE.
- Return type:
Self- Returns:
Self for method chaining
Select Clause Mixins#
- class sqlspec.builder.SelectClauseMixin[source]#
Bases:
objectMixin providing SELECT clause methods.
Window Functions#
Case Expressions#
Subqueries#
Insert#
- class sqlspec.builder.Insert[source]#
Bases:
QueryBuilder,ReturningClauseMixin,InsertValuesMixin,InsertFromSelectMixin,InsertIntoClauseMixin,ExplainMixinBuilder for INSERT statements.
Constructs SQL INSERT queries with parameter binding and validation.
- values_from(data, *, exclude_unset=True)[source]#
Add a row of values from a dict, dataclass, msgspec.Struct, Pydantic model, or attrs class.
Schema instances are normalised via
sqlspec.utils.serializers.schema_dump()withwire_format=Falsethen bound as a single row. The dict shape uses Python attribute names regardless of msgspecrename=or PydanticField(alias=...); wire-aligned names never make sense for SQL column binding.- Parameters:
data¶ (
Any) -- A dict, dataclass instance,msgspec.Struct,pydantic.BaseModel, orattrs-decorated class instance.exclude_unset¶ (
bool) -- If True, exclude fields that were never set. Honoured by msgspec (UNSET fields), Pydantic (model_fields_set), and dataclass (via empty default-field semantics). No-op for attrs.
- Return type:
Self- Returns:
The current builder instance for method chaining.
- values_from_many(items, *, exclude_unset=True)[source]#
Add multiple rows from a sequence of dicts, dataclasses, msgspec Structs, Pydantic models, or attrs classes.
Each item is normalised via
sqlspec.utils.serializers.schema_dump()withwire_format=Falsethen bound as a multi-row INSERT. Mixed schema kinds in the same sequence are supported (each item normalises independently); however, the resulting dict shapes must agree on key sets.- Parameters:
items¶ (
Sequence[typing.Any]) -- A sequence of schema instances or dicts.exclude_unset¶ (
bool) -- Seevalues_from()for per-library semantics.
- Return type:
Self- Returns:
The current builder instance for method chaining. Empty input returns the builder unchanged.
- on_conflict(*columns)[source]#
Adds an ON CONFLICT clause with specified columns.
- class sqlspec.builder._insert.ConflictBuilder[source]#
Bases:
objectBuilder for ON CONFLICT clauses in INSERT statements.
Constructs conflict resolution clauses using PostgreSQL-style syntax, which SQLGlot can transpile to other dialects.
Update#
- class sqlspec.builder.Update[source]#
Bases:
QueryBuilder,WhereClauseMixin,ReturningClauseMixin,UpdateSetClauseMixin,UpdateFromClauseMixin,UpdateTableClauseMixin,ExplainMixinBuilder for UPDATE statements.
Constructs SQL UPDATE statements with parameter binding and validation.
- join(table, on, alias=None, join_type='INNER')[source]#
Add JOIN clause to the UPDATE statement.
- Parameters:
- Return type:
Self- Returns:
The current builder instance for method chaining.
- Raises:
SQLBuilderError -- If the current expression is not an UPDATE statement.
- build(dialect=None)[source]#
Build the UPDATE query with validation.
- Parameters:
dialect¶ (sqlglot.dialects.dialect.DialectType) -- Optional dialect override for SQL generation.
- Returns:
The built query with SQL and parameters.
- Return type:
- Raises:
SQLBuilderError -- If no table is set or expression is not an UPDATE.
Delete#
- class sqlspec.builder.Delete[source]#
Bases:
QueryBuilder,WhereClauseMixin,ReturningClauseMixin,DeleteFromClauseMixin,ExplainMixinBuilder for DELETE statements.
Constructs SQL DELETE statements with parameter binding and validation. Does not support JOIN operations to maintain cross-dialect compatibility.
- build(dialect=None)[source]#
Build the DELETE query with validation.
- Parameters:
dialect¶ (sqlglot.dialects.dialect.DialectType) -- Optional dialect override for SQL generation.
- Returns:
The built query with SQL and parameters.
- Return type:
- Raises:
SQLBuilderError -- If the table is not specified.
Merge#
- class sqlspec.builder.Merge[source]#
Bases:
QueryBuilder,MergeUsingClauseMixin,MergeOnClauseMixin,MergeMatchedClauseMixin,MergeNotMatchedClauseMixin,MergeIntoClauseMixin,MergeNotMatchedBySourceClauseMixin,ExplainMixinBuilder for MERGE statements.
Constructs SQL MERGE statements (also known as UPSERT in some databases) with parameter binding and validation.
Explain#
- class sqlspec.builder.Explain[source]#
Bases:
objectBuilder for EXPLAIN statements with dialect-aware rendering.
Provides a fluent API for constructing EXPLAIN statements with various options that are translated to dialect-specific syntax.
- settings(enabled=True)[source]#
Enable SETTINGS option (show configuration parameters, PostgreSQL 12+).
- generic_plan(enabled=True)[source]#
Enable GENERIC_PLAN option (ignore parameter values, PostgreSQL 16+).
- with_options(options)[source]#
Replace all options with the provided ExplainOptions.
- Parameters:
options¶ (
ExplainOptions) -- New options to use- Return type:
Self- Returns:
Self for method chaining
- property options: ExplainOptions#
Get current ExplainOptions.
- property dialect: TypeAliasForwardRef('sqlglot.dialects.dialect.DialectType') | None#
Get current dialect.
Joins#
- final class sqlspec.builder.JoinBuilder[source]#
Bases:
objectBuilder for JOIN operations with fluent syntax.