Skip to main content

toasty_core/
stmt.rs

1//! Statement AST types for Toasty's query compilation pipeline.
2//!
3//! This module defines the abstract syntax tree (AST) for statements that
4//! Toasty's query engine processes. The top-level type is [`Statement`], which
5//! represents one of four operations: [`Query`], [`Insert`], [`Update`], or
6//! [`Delete`].
7//!
8//! Statements exist at two layers:
9//!
10//! - **Model-level**: references models, fields, and associations from the app
11//!   schema. This is what user-facing code produces.
12//! - **Table-level**: references tables, columns, and joins from the DB schema.
13//!   This is what the query engine lowers model-level statements into before
14//!   handing them to a database driver.
15//!
16//! The query engine pipeline transforms statements through several phases:
17//! simplify, lower, plan, and execute. Types in this module appear throughout
18//! all phases.
19//!
20//! # Examples
21//!
22//! ```ignore
23//! use toasty_core::stmt::{Statement, Query, Values};
24//!
25//! // Create a simple values-based query statement
26//! let query = Query::unit();
27//! let stmt = Statement::Query(query);
28//! assert!(stmt.is_query());
29//! ```
30
31mod assignments;
32pub use assignments::{Assignment, Assignments};
33
34mod association;
35pub use association::Association;
36
37mod condition;
38pub use condition::Condition;
39
40mod cte;
41pub use cte::Cte;
42
43mod cx;
44pub use cx::{DerivedRef, ExprContext, ExprTarget, IntoExprTarget, Resolve, ResolvedRef};
45
46mod delete;
47pub use delete::Delete;
48
49mod direction;
50pub use direction::Direction;
51
52mod entry;
53pub use entry::Entry;
54
55mod entry_mut;
56pub use entry_mut::EntryMut;
57
58mod entry_path;
59pub use entry_path::EntryPath;
60
61mod eval;
62
63mod expr;
64pub use expr::Expr;
65
66mod expr_and;
67pub use expr_and::ExprAnd;
68
69mod expr_any;
70pub use expr_any::ExprAny;
71
72mod expr_arg;
73pub use expr_arg::ExprArg;
74
75mod expr_binary_op;
76pub use expr_binary_op::ExprBinaryOp;
77
78mod expr_cast;
79pub use expr_cast::ExprCast;
80
81mod expr_error;
82pub use expr_error::ExprError;
83
84mod expr_exists;
85pub use expr_exists::ExprExists;
86
87mod expr_func;
88pub use expr_func::ExprFunc;
89
90mod expr_in_list;
91pub use expr_in_list::ExprInList;
92
93mod expr_in_subquery;
94pub use expr_in_subquery::ExprInSubquery;
95
96mod expr_is_null;
97pub use expr_is_null::ExprIsNull;
98
99mod expr_is_variant;
100pub use expr_is_variant::ExprIsVariant;
101
102mod expr_let;
103pub use expr_let::ExprLet;
104
105mod expr_like;
106pub use expr_like::ExprLike;
107
108mod expr_list;
109pub use expr_list::ExprList;
110
111mod expr_map;
112pub use expr_map::ExprMap;
113
114mod expr_match;
115pub use expr_match::{ExprMatch, MatchArm};
116
117mod expr_not;
118pub use expr_not::ExprNot;
119
120mod expr_or;
121pub use expr_or::ExprOr;
122
123mod expr_project;
124pub use expr_project::ExprProject;
125
126mod expr_record;
127pub use expr_record::ExprRecord;
128
129mod expr_reference;
130pub use expr_reference::{ExprColumn, ExprReference};
131
132mod expr_set;
133pub use expr_set::ExprSet;
134
135mod expr_set_op;
136pub use expr_set_op::ExprSetOp;
137
138mod expr_starts_with;
139pub use expr_starts_with::ExprStartsWith;
140
141mod expr_stmt;
142pub use expr_stmt::ExprStmt;
143
144mod filter;
145pub use filter::Filter;
146
147mod hash_index;
148pub use hash_index::HashIndex;
149
150mod sorted_index;
151pub use sorted_index::SortedIndex;
152
153mod func_count;
154pub use func_count::FuncCount;
155
156mod func_last_insert_id;
157pub use func_last_insert_id::FuncLastInsertId;
158
159mod insert;
160pub use insert::Insert;
161
162mod insert_table;
163pub use insert_table::InsertTable;
164
165mod insert_target;
166pub use insert_target::InsertTarget;
167
168mod input;
169pub use input::{ConstInput, Input, TypedInput};
170
171mod join;
172pub use join::{Join, JoinOp};
173
174mod limit;
175pub use limit::{Limit, LimitCursor, LimitOffset};
176
177#[cfg(feature = "assert-struct")]
178mod like;
179
180mod node;
181pub use node::Node;
182
183mod num;
184
185mod op_binary;
186pub use op_binary::BinaryOp;
187
188mod order_by;
189pub use order_by::OrderBy;
190
191mod order_by_expr;
192pub use order_by_expr::OrderByExpr;
193
194mod op_set;
195pub use op_set::SetOp;
196
197mod path;
198pub use path::{Path, PathRoot};
199
200mod path_field_set;
201pub use path_field_set::PathFieldSet;
202
203mod projection;
204pub use projection::{Project, Projection};
205
206mod query;
207pub use query::{Lock, Query};
208
209mod returning;
210pub use returning::Returning;
211
212mod select;
213pub use select::Select;
214
215mod source;
216pub use source::{Source, SourceModel};
217
218mod source_table;
219pub use source_table::SourceTable;
220
221mod source_table_id;
222pub use source_table_id::SourceTableId;
223
224mod sparse_record;
225pub use sparse_record::SparseRecord;
226
227mod substitute;
228use substitute::Substitute;
229
230mod table_derived;
231pub use table_derived::TableDerived;
232
233mod table_ref;
234pub use table_ref::TableRef;
235
236mod table_factor;
237pub use table_factor::TableFactor;
238
239mod table_with_joins;
240pub use table_with_joins::TableWithJoins;
241
242mod ty;
243pub use ty::Type;
244
245mod ty_union;
246pub use ty_union::TypeUnion;
247
248#[cfg(feature = "jiff")]
249mod ty_jiff;
250
251mod update;
252pub use update::{Update, UpdateTarget};
253
254mod value;
255pub use value::Value;
256
257mod value_cmp;
258
259mod values;
260pub use values::Values;
261
262#[cfg(feature = "jiff")]
263mod value_jiff;
264
265mod value_record;
266pub use value_record::ValueRecord;
267
268mod value_set;
269pub use value_set::ValueSet;
270
271/// Mutable AST visitor trait and helpers.
272pub mod visit_mut;
273pub use visit_mut::VisitMut;
274
275mod value_list;
276
277mod value_stream;
278pub use value_stream::ValueStream;
279
280/// Read-only AST visitor trait and helpers.
281pub mod visit;
282pub use visit::Visit;
283
284mod with;
285pub use with::With;
286
287use crate::schema::db::TableId;
288use std::fmt;
289
290/// A top-level statement in Toasty's AST.
291///
292/// Each variant corresponds to one of the four fundamental database operations.
293/// A `Statement` is the primary input to the query engine's compilation
294/// pipeline and the output of code generated by `#[derive(Model)]`.
295///
296/// # Examples
297///
298/// ```ignore
299/// use toasty_core::stmt::{Statement, Query, Values};
300///
301/// let query = Query::unit();
302/// let stmt = Statement::from(query);
303/// assert!(stmt.is_query());
304/// assert!(!stmt.is_insert());
305/// ```
306#[derive(Clone, PartialEq)]
307pub enum Statement {
308    /// Delete one or more existing records.
309    Delete(Delete),
310
311    /// Create one or more new records.
312    Insert(Insert),
313
314    /// Query (read) records from the database.
315    Query(Query),
316
317    /// Update one or more existing records.
318    Update(Update),
319}
320
321impl Statement {
322    /// Returns the statement variant name for logging.
323    pub fn name(&self) -> &str {
324        match self {
325            Statement::Query(_) => "query",
326            Statement::Insert(_) => "insert",
327            Statement::Update(_) => "update",
328            Statement::Delete(_) => "delete",
329        }
330    }
331
332    /// Substitutes argument placeholders in this statement with concrete values
333    /// from `input`.
334    pub fn substitute(&mut self, input: impl Input) {
335        Substitute::new(input).visit_stmt_mut(self);
336    }
337
338    /// Returns `true` if this statement is a query whose body contains only
339    /// constant values (no table references or subqueries) and has no CTEs.
340    pub fn is_const(&self) -> bool {
341        match self {
342            Statement::Query(query) => {
343                if query.with.is_some() {
344                    return false;
345                }
346
347                query.body.is_const()
348            }
349            _ => false,
350        }
351    }
352
353    /// Attempts to return a reference to an inner [`Update`].
354    ///
355    /// * If `self` is a [`Statement::Update`], a reference to the inner [`Update`] is
356    ///   returned wrapped in [`Some`].
357    /// * Else, [`None`] is returned.
358    pub fn as_update(&self) -> Option<&Update> {
359        match self {
360            Self::Update(update) => Some(update),
361            _ => None,
362        }
363    }
364
365    /// Consumes `self` and attempts to return the inner [`Update`].
366    ///
367    /// * If `self` is a [`Statement::Update`], inner [`Update`] is returned wrapped in
368    ///   [`Some`].
369    /// * Else, [`None`] is returned.
370    pub fn into_update(self) -> Option<Update> {
371        match self {
372            Self::Update(update) => Some(update),
373            _ => None,
374        }
375    }
376
377    /// Returns `true` if this statement expects at most one result row.
378    pub fn is_single(&self) -> bool {
379        match self {
380            Statement::Query(q) => q.single,
381            Statement::Insert(i) => i.source.single,
382            Statement::Update(i) => match &i.target {
383                UpdateTarget::Query(q) => q.single,
384                UpdateTarget::Model(_) => true,
385                _ => false,
386            },
387            Statement::Delete(d) => d.selection().single,
388        }
389    }
390
391    /// Consumes `self` and returns the inner [`Update`].
392    ///
393    /// # Panics
394    ///
395    /// If `self` is not a [`Statement::Update`].
396    pub fn into_update_unwrap(self) -> Update {
397        match self {
398            Self::Update(update) => update,
399            v => panic!("expected `Update`, found {v:#?}"),
400        }
401    }
402}
403
404impl Node for Statement {
405    fn visit<V: Visit>(&self, mut visit: V) {
406        visit.visit_stmt(self);
407    }
408
409    fn visit_mut<V: VisitMut>(&mut self, mut visit: V) {
410        visit.visit_stmt_mut(self);
411    }
412}
413
414impl fmt::Debug for Statement {
415    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
416        match self {
417            Self::Delete(v) => v.fmt(f),
418            Self::Insert(v) => v.fmt(f),
419            Self::Query(v) => v.fmt(f),
420            Self::Update(v) => v.fmt(f),
421        }
422    }
423}