Delete Statement
Relevant source files
The following files were used as context for generating this wiki page:
The DeleteStatement is a core component of the SeaQuery AST (Abstract Syntax Tree) designed to facilitate the programmatic construction of SQL DELETE queries. It provides a type-safe, fluent API for Rust developers to define deletion logic, including target tables, filtering criteria via WHERE clauses, result ordering, and row limits.
As a dynamic query builder, SeaQuery ensures that DeleteStatement can be translated into valid SQL for multiple database backends, including MySQL, PostgreSQL, and SQLite. It handles complex tasks such as parameter binding and dialect-specific syntax (e.g., RETURNING clauses in PostgreSQL vs. lack thereof in MySQL) through backend-specific QueryBuilder implementations.
Sources: src/query/delete.rs:1-35, src/lib.rs:1-40, README.md:1-50
Architecture and Data Structure
The DeleteStatement struct encapsulates all components necessary to represent a deletion operation. It implements several traits that allow it to integrate with the broader SeaQuery ecosystem, such as QueryStatementBuilder for SQL generation and ConditionalStatement for applying filters.
Core Data Structure
The internal state of a DeleteStatement is managed through the following fields:
| Field | Type | Description |
|---|---|---|
table | Option<Box<TableRef>> | The target table from which rows will be deleted. |
where | ConditionHolder | A collection of conditions used to filter which rows are removed. |
orders | Vec<OrderExpr> | Expressions defining the order in which rows are processed (backend dependent). |
limit | Option<Value> | An optional limit on the number of rows to delete. |
returning | Option<ReturningClause> | Defines values to be returned from deleted rows (Postgres/SQLite). |
with | Option<WithClause> | Common Table Expressions (CTE) associated with the delete operation. |
Sources: src/query/delete.rs:41-48, src/backend/query_builder.rs:400-425
Relationship Diagram
The following diagram illustrates how DeleteStatement interacts with other query components and the backend translation layer.
Query Construction Logic
Constructing a DeleteStatement follows a fluent builder pattern. Users typically start with Query::delete() and chain methods to define the query's scope.
Target and Filtering
The from_table method specifies the target TableRef. Filtering is performed using cond_where or and_where, which populate the internal ConditionHolder. This allows for complex, nested logic using Cond::any() or Cond::all().
let query = Query::delete()
.from_table(Glyph::Table)
.cond_where(
Cond::any()
.add(Expr::col(Glyph::Id).lt(1))
.add(Expr::col(Glyph::Id).gt(10)),
)
.to_owned();
Sources: src/query/delete.rs:18-30, src/query/delete.rs:80-90
Returning and CTEs
PostgreSQL and SQLite support the RETURNING clause, which can be configured to return specific columns or all columns (*) of the deleted rows. Additionally, DeleteStatement supports Common Table Expressions via the with and with_cte methods, allowing deletion based on temporary result sets.
Sources: src/query/delete.rs:105-130, src/query/delete.rs:200-240
Backend Translation Flow
The DeleteStatement does not generate SQL directly. Instead, it passes itself to a QueryBuilder implementation (like PostgresQueryBuilder or MysqlQueryBuilder).
Execution Flow
The sequence diagram below shows the interaction during the build process:
Audit and Explanation
SeaQuery provides utility features for auditing and analyzing queries.
Audit Trait
The DeleteStatement implements AuditTrait. This allows the system to inspect the statement before execution to determine which tables are being accessed and what type of access is occurring (e.g., AccessType::Delete and potentially AccessType::Select if a RETURNING clause is present).
Sources: src/audit/delete.rs:4-30
Explainable Statement
DeleteStatement can be wrapped in an ExplainStatement to generate EXPLAIN queries for debugging performance issues. The ExplainableStatement enum includes a Delete variant to facilitate this.
Sources: src/query/explain.rs:45-60, src/query/explain.rs:88-100
Summary
The DeleteStatement module provides a robust and flexible way to generate SQL deletion queries. By abstracting the syntax into a structured AST, it allows for dynamic query generation at runtime, supports advanced features like CTEs and RETURNING clauses, and ensures cross-database compatibility through specialized backend builders.
Sources: src/query/delete.rs:41-48, README.md:380-405