DDL#
Data Definition Language builders for tables, indexes, views, and schemas.
Base#
- class sqlspec.builder.DDLBuilder[source]#
Bases:
QueryBuilderBase class for DDL builders (CREATE, DROP, ALTER, etc).
- build(dialect=None)[source]#
Builds the SQL query string and parameters.
- Parameters:
dialect¶ (sqlglot.dialects.dialect.DialectType) -- Optional dialect override. If provided, generates SQL for this dialect instead of the builder's default dialect.
- Returns:
A dataclass containing the SQL string and parameters.
- Return type:
Tables#
- class sqlspec.builder.CreateTable[source]#
Bases:
DDLBuilder,_IfNotExistsDDLMixinBuilder for CREATE TABLE statements with columns and constraints.
- property columns: list[ColumnDefinition]#
Get the list of column definitions for this table.
- Returns:
List of ColumnDefinition objects.
- column(name, dtype, default=None, not_null=False, primary_key=False, unique=False, auto_increment=False, comment=None, check=None, generated=None, collate=None)[source]#
Add a column definition to the table.
- Return type:
Self
- class sqlspec.builder.CreateTableAsSelect[source]#
Bases:
DDLBuilder,_IfNotExistsDDLMixinBuilder for CREATE TABLE [IF NOT EXISTS] ... AS SELECT ... (CTAS).
- - name(table_name
str): Set the table name.
- - if_not_exists()
Add IF NOT EXISTS.
- - columns(*cols
str): Set explicit column list (optional).
- - as_select(select_query)
Set the SELECT source (SQL, SelectBuilder, or str).
- class sqlspec.builder.AlterTable[source]#
Bases:
DDLBuilder,_IfExistsDDLMixinBuilder for ALTER TABLE operations.
- add_column(name, dtype, default=None, not_null=False, unique=False, comment=None, after=None, first=False)[source]#
Add a new column to the table.
- Return type:
Self
- alter_column_type(name, new_type, using=None)[source]#
Change the type of an existing column.
- Return type:
Self
- add_constraint(constraint_type, columns=None, name=None, references_table=None, references_columns=None, condition=None, on_delete=None, on_update=None)[source]#
Add a constraint to the table.
- Parameters:
constraint_type¶ (
str) -- Type of constraint ('PRIMARY KEY', 'FOREIGN KEY', 'UNIQUE', 'CHECK')columns¶ (
str|list[str] |None) -- Column(s) for the constraint (not needed for CHECK)references_table¶ (
str|None) -- Table referenced by foreign keyreferences_columns¶ (
str|list[str] |None) -- Columns referenced by foreign keycondition¶ (
str|ColumnExpression|None) -- CHECK constraint condition
- Return type:
Self
- class sqlspec.builder.DropTable[source]#
Bases:
_SingleObjectDropBuilderBuilder for DROP TABLE [IF EXISTS] ... [CASCADE|RESTRICT].
- class sqlspec.builder.RenameTable[source]#
Bases:
DDLBuilderBuilder for ALTER TABLE ... RENAME TO ... statements.
- class sqlspec.builder.Truncate[source]#
Bases:
DDLBuilder,_CascadeRestrictDDLMixinBuilder for TRUNCATE TABLE ... [CASCADE|RESTRICT] [RESTART IDENTITY|CONTINUE IDENTITY].
Column and Constraint Definitions#
- class sqlspec.builder._ddl.ColumnDefinition[source]#
Bases:
objectColumn definition for CREATE TABLE.
- class sqlspec.builder._ddl.ConstraintDefinition[source]#
Bases:
objectConstraint definition for CREATE TABLE.
Indexes#
- class sqlspec.builder.CreateIndex[source]#
Bases:
DDLBuilder,_IfNotExistsDDLMixinBuilder for CREATE [UNIQUE] INDEX [IF NOT EXISTS] ... ON ... (...).
Views#
- class sqlspec.builder.CreateView[source]#
Bases:
DDLBuilder,_IfNotExistsDDLMixinBuilder for CREATE VIEW [IF NOT EXISTS] ... AS SELECT ...
- class sqlspec.builder.CreateMaterializedView[source]#
Bases:
DDLBuilder,_IfNotExistsDDLMixinBuilder for CREATE MATERIALIZED VIEW [IF NOT EXISTS] ... AS SELECT ...
- class sqlspec.builder.DropView[source]#
Bases:
_SingleObjectDropBuilderBuilder for DROP VIEW [IF EXISTS] ... [CASCADE|RESTRICT].
Schemas#
- class sqlspec.builder.CreateSchema[source]#
Bases:
DDLBuilder,_IfNotExistsDDLMixinBuilder for CREATE SCHEMA [IF NOT EXISTS] schema_name [AUTHORIZATION user_name].