Skip to main content

toasty_sql/
stmt.rs

1mod add_column;
2pub use add_column::AddColumn;
3
4mod alter_column;
5pub use alter_column::{AlterColumn, AlterColumnChanges};
6
7mod alter_table;
8pub use alter_table::{AlterTable, AlterTableAction};
9
10mod alter_type;
11pub use alter_type::AlterType;
12
13mod check;
14pub use check::CheckConstraint;
15
16mod column_def;
17pub use column_def::ColumnDef;
18
19mod copy_table;
20pub use copy_table::CopyTable;
21
22mod create_index;
23pub use create_index::CreateIndex;
24
25mod create_table;
26pub use create_table::CreateTable;
27
28mod create_type;
29pub use create_type::CreateType;
30
31mod drop_column;
32pub use drop_column::DropColumn;
33
34mod drop_index;
35pub use drop_index::DropIndex;
36
37mod drop_table;
38pub use drop_table::DropTable;
39
40mod ident;
41pub use ident::Ident;
42
43mod name;
44pub use name::Name;
45
46mod pragma;
47pub use pragma::Pragma;
48
49mod table_name;
50pub use table_name::TableName;
51
52pub use toasty_core::stmt::*;
53
54/// A SQL statement, covering both DDL (schema changes) and DML (data manipulation).
55#[derive(Debug, Clone)]
56pub enum Statement {
57    /// Add a column to an existing table.
58    AddColumn(AddColumn),
59    /// Alter properties of an existing column.
60    AlterColumn(AlterColumn),
61    /// Alter an existing table (e.g. rename).
62    AlterTable(AlterTable),
63    /// Alter a type (e.g. `ALTER TYPE ... ADD VALUE '...'`).
64    AlterType(AlterType),
65    /// Copy rows from one table to another.
66    CopyTable(CopyTable),
67    /// Create an index.
68    CreateIndex(CreateIndex),
69    /// Create a table.
70    CreateTable(CreateTable),
71    /// Create a type (e.g. `CREATE TYPE ... AS ENUM (...)`).
72    CreateType(CreateType),
73    /// Drop a column from an existing table.
74    DropColumn(DropColumn),
75    /// Drop a table.
76    DropTable(DropTable),
77    /// Drop an index.
78    DropIndex(DropIndex),
79    /// A SQLite PRAGMA statement.
80    Pragma(Pragma),
81    /// A DELETE statement.
82    Delete(Delete),
83    /// An INSERT statement.
84    Insert(Insert),
85    /// A SELECT query.
86    Query(Query),
87    /// An UPDATE statement.
88    Update(Update),
89}
90
91impl Statement {
92    /// Returns `true` if this is an [`Update`] statement.
93    pub fn is_update(&self) -> bool {
94        matches!(self, Self::Update(_))
95    }
96
97    /// Returns the number of returned elements within the statement (if one exists).
98    pub fn returning_len(&self) -> Option<usize> {
99        match self {
100            Self::Delete(delete) => delete
101                .returning
102                .as_ref()
103                .map(|ret| ret.as_project_unwrap().as_record_unwrap().len()),
104            Self::Insert(insert) => insert
105                .returning
106                .as_ref()
107                .map(|ret| ret.as_project_unwrap().as_record_unwrap().len()),
108            Self::Query(query) => match &query.body {
109                ExprSet::Select(select) => Some(
110                    select
111                        .returning
112                        .as_project_unwrap()
113                        .as_record_unwrap()
114                        .len(),
115                ),
116                stmt => todo!("returning_len, stmt={stmt:#?}"),
117            },
118            Self::Update(update) => update
119                .returning
120                .as_ref()
121                .map(|ret| ret.as_project_unwrap().as_record_unwrap().len()),
122            _ => None,
123        }
124    }
125}
126
127impl From<toasty_core::stmt::Statement> for Statement {
128    fn from(value: toasty_core::stmt::Statement) -> Self {
129        match value {
130            toasty_core::stmt::Statement::Delete(stmt) => Self::Delete(stmt),
131            toasty_core::stmt::Statement::Insert(stmt) => Self::Insert(stmt),
132            toasty_core::stmt::Statement::Query(stmt) => Self::Query(stmt),
133            toasty_core::stmt::Statement::Update(stmt) => Self::Update(stmt),
134        }
135    }
136}