toasty-core 0.3.0

Core types, schema representations, and driver interface for Toasty
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Statement AST types for Toasty's query compilation pipeline.
//!
//! This module defines the abstract syntax tree (AST) for statements that
//! Toasty's query engine processes. The top-level type is [`Statement`], which
//! represents one of four operations: [`Query`], [`Insert`], [`Update`], or
//! [`Delete`].
//!
//! Statements exist at two layers:
//!
//! - **Model-level**: references models, fields, and associations from the app
//!   schema. This is what user-facing code produces.
//! - **Table-level**: references tables, columns, and joins from the DB schema.
//!   This is what the query engine lowers model-level statements into before
//!   handing them to a database driver.
//!
//! The query engine pipeline transforms statements through several phases:
//! simplify, lower, plan, and execute. Types in this module appear throughout
//! all phases.
//!
//! # Examples
//!
//! ```ignore
//! use toasty_core::stmt::{Statement, Query, Values};
//!
//! // Create a simple values-based query statement
//! let query = Query::unit();
//! let stmt = Statement::Query(query);
//! assert!(stmt.is_query());
//! ```

mod assignments;
pub use assignments::{Assignment, Assignments};

mod association;
pub use association::Association;

mod condition;
pub use condition::Condition;

mod cte;
pub use cte::Cte;

mod cx;
pub use cx::{DerivedRef, ExprContext, ExprTarget, IntoExprTarget, Resolve, ResolvedRef};

mod delete;
pub use delete::Delete;

mod direction;
pub use direction::Direction;

mod entry;
pub use entry::Entry;

mod entry_mut;
pub use entry_mut::EntryMut;

mod entry_path;
pub use entry_path::EntryPath;

mod eval;

mod expr;
pub use expr::Expr;

mod expr_and;
pub use expr_and::ExprAnd;

mod expr_any;
pub use expr_any::ExprAny;

mod expr_arg;
pub use expr_arg::ExprArg;

mod expr_binary_op;
pub use expr_binary_op::ExprBinaryOp;

mod expr_cast;
pub use expr_cast::ExprCast;

mod expr_error;
pub use expr_error::ExprError;

mod expr_exists;
pub use expr_exists::ExprExists;

mod expr_func;
pub use expr_func::ExprFunc;

mod expr_in_list;
pub use expr_in_list::ExprInList;

mod expr_in_subquery;
pub use expr_in_subquery::ExprInSubquery;

mod expr_is_null;
pub use expr_is_null::ExprIsNull;

mod expr_is_variant;
pub use expr_is_variant::ExprIsVariant;

mod expr_let;
pub use expr_let::ExprLet;

mod expr_list;
pub use expr_list::ExprList;

mod expr_map;
pub use expr_map::ExprMap;

mod expr_match;
pub use expr_match::{ExprMatch, MatchArm};

mod expr_not;
pub use expr_not::ExprNot;

mod expr_or;
pub use expr_or::ExprOr;

mod expr_project;
pub use expr_project::ExprProject;

mod expr_record;
pub use expr_record::ExprRecord;

mod expr_reference;
pub use expr_reference::{ExprColumn, ExprReference};

mod expr_set;
pub use expr_set::ExprSet;

mod expr_set_op;
pub use expr_set_op::ExprSetOp;

mod expr_stmt;
pub use expr_stmt::ExprStmt;

mod filter;
pub use filter::Filter;

mod hash_index;
pub use hash_index::HashIndex;

mod sorted_index;
pub use sorted_index::SortedIndex;

mod func_count;
pub use func_count::FuncCount;

mod func_last_insert_id;
pub use func_last_insert_id::FuncLastInsertId;

mod insert;
pub use insert::Insert;

mod insert_table;
pub use insert_table::InsertTable;

mod insert_target;
pub use insert_target::InsertTarget;

mod input;
pub use input::{ConstInput, Input, TypedInput};

mod join;
pub use join::{Join, JoinOp};

mod limit;
pub use limit::{Limit, LimitCursor, LimitOffset};

#[cfg(feature = "assert-struct")]
mod like;

mod node;
pub use node::Node;

mod num;

mod op_binary;
pub use op_binary::BinaryOp;

mod order_by;
pub use order_by::OrderBy;

mod order_by_expr;
pub use order_by_expr::OrderByExpr;

mod op_set;
pub use op_set::SetOp;

mod path;
pub use path::{Path, PathRoot};

mod path_field_set;
pub use path_field_set::PathFieldSet;

mod projection;
pub use projection::{Project, Projection};

mod query;
pub use query::{Lock, Query};

mod returning;
pub use returning::Returning;

mod select;
pub use select::Select;

mod source;
pub use source::{Source, SourceModel};

mod source_table;
pub use source_table::SourceTable;

mod source_table_id;
pub use source_table_id::SourceTableId;

mod sparse_record;
pub use sparse_record::SparseRecord;

mod substitute;
use substitute::Substitute;

mod table_derived;
pub use table_derived::TableDerived;

mod table_ref;
pub use table_ref::TableRef;

mod table_factor;
pub use table_factor::TableFactor;

mod table_with_joins;
pub use table_with_joins::TableWithJoins;

mod ty;
pub use ty::Type;

mod ty_union;
pub use ty_union::TypeUnion;

#[cfg(feature = "jiff")]
mod ty_jiff;

mod update;
pub use update::{Update, UpdateTarget};

mod value;
pub use value::Value;

mod value_cmp;

mod values;
pub use values::Values;

#[cfg(feature = "jiff")]
mod value_jiff;

mod value_record;
pub use value_record::ValueRecord;

/// Mutable AST visitor trait and helpers.
pub mod visit_mut;
pub use visit_mut::VisitMut;

mod value_list;

mod value_stream;
pub use value_stream::ValueStream;

/// Read-only AST visitor trait and helpers.
pub mod visit;
pub use visit::Visit;

mod with;
pub use with::With;

use crate::schema::db::TableId;
use std::fmt;

/// A top-level statement in Toasty's AST.
///
/// Each variant corresponds to one of the four fundamental database operations.
/// A `Statement` is the primary input to the query engine's compilation
/// pipeline and the output of code generated by `#[derive(Model)]`.
///
/// # Examples
///
/// ```ignore
/// use toasty_core::stmt::{Statement, Query, Values};
///
/// let query = Query::unit();
/// let stmt = Statement::from(query);
/// assert!(stmt.is_query());
/// assert!(!stmt.is_insert());
/// ```
#[derive(Clone, PartialEq)]
pub enum Statement {
    /// Delete one or more existing records.
    Delete(Delete),

    /// Create one or more new records.
    Insert(Insert),

    /// Query (read) records from the database.
    Query(Query),

    /// Update one or more existing records.
    Update(Update),
}

impl Statement {
    /// Returns the statement variant name for logging.
    pub fn name(&self) -> &str {
        match self {
            Statement::Query(_) => "query",
            Statement::Insert(_) => "insert",
            Statement::Update(_) => "update",
            Statement::Delete(_) => "delete",
        }
    }

    /// Substitutes argument placeholders in this statement with concrete values
    /// from `input`.
    pub fn substitute(&mut self, input: impl Input) {
        Substitute::new(input).visit_stmt_mut(self);
    }

    /// Returns `true` if this statement is a query whose body contains only
    /// constant values (no table references or subqueries) and has no CTEs.
    pub fn is_const(&self) -> bool {
        match self {
            Statement::Query(query) => {
                if query.with.is_some() {
                    return false;
                }

                query.body.is_const()
            }
            _ => false,
        }
    }

    /// Attempts to return a reference to an inner [`Update`].
    ///
    /// * If `self` is a [`Statement::Update`], a reference to the inner [`Update`] is
    ///   returned wrapped in [`Some`].
    /// * Else, [`None`] is returned.
    pub fn as_update(&self) -> Option<&Update> {
        match self {
            Self::Update(update) => Some(update),
            _ => None,
        }
    }

    /// Consumes `self` and attempts to return the inner [`Update`].
    ///
    /// * If `self` is a [`Statement::Update`], inner [`Update`] is returned wrapped in
    ///   [`Some`].
    /// * Else, [`None`] is returned.
    pub fn into_update(self) -> Option<Update> {
        match self {
            Self::Update(update) => Some(update),
            _ => None,
        }
    }

    /// Returns `true` if this statement expects at most one result row.
    pub fn is_single(&self) -> bool {
        match self {
            Statement::Query(q) => q.single,
            Statement::Insert(i) => i.source.single,
            Statement::Update(i) => match &i.target {
                UpdateTarget::Query(q) => q.single,
                UpdateTarget::Model(_) => true,
                _ => false,
            },
            Statement::Delete(d) => d.selection().single,
        }
    }

    /// Consumes `self` and returns the inner [`Update`].
    ///
    /// # Panics
    ///
    /// If `self` is not a [`Statement::Update`].
    pub fn into_update_unwrap(self) -> Update {
        match self {
            Self::Update(update) => update,
            v => panic!("expected `Update`, found {v:#?}"),
        }
    }
}

impl Node for Statement {
    fn visit<V: Visit>(&self, mut visit: V) {
        visit.visit_stmt(self);
    }

    fn visit_mut<V: VisitMut>(&mut self, mut visit: V) {
        visit.visit_stmt_mut(self);
    }
}

impl fmt::Debug for Statement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Delete(v) => v.fmt(f),
            Self::Insert(v) => v.fmt(f),
            Self::Query(v) => v.fmt(f),
            Self::Update(v) => v.fmt(f),
        }
    }
}