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 rename_type;
50pub use rename_type::RenameType;
51
52mod table_name;
53pub use table_name::TableName;
54
55pub use toasty_core::stmt::*;
56
57/// A SQL statement, covering both DDL (schema changes) and DML (data manipulation).
58#[derive(Debug, Clone)]
59pub enum Statement {
60    /// Add a column to an existing table.
61    AddColumn(AddColumn),
62    /// Alter properties of an existing column.
63    AlterColumn(AlterColumn),
64    /// Alter an existing table (e.g. rename).
65    AlterTable(AlterTable),
66    /// Add a value to an enum type.
67    AlterType(AlterType),
68    /// Copy rows from one table to another.
69    CopyTable(CopyTable),
70    /// Create an index.
71    CreateIndex(CreateIndex),
72    /// Create a table.
73    CreateTable(CreateTable),
74    /// Create a type (e.g. `CREATE TYPE ... AS ENUM (...)`).
75    CreateType(CreateType),
76    /// Drop a column from an existing table.
77    DropColumn(DropColumn),
78    /// Drop a table.
79    DropTable(DropTable),
80    /// Drop an index.
81    DropIndex(DropIndex),
82    /// A SQLite PRAGMA statement.
83    Pragma(Pragma),
84    /// Rename a type.
85    RenameType(RenameType),
86    /// A DELETE statement.
87    Delete(Delete),
88    /// An INSERT statement.
89    Insert(Insert),
90    /// A SELECT query.
91    Query(Query),
92    /// An UPDATE statement.
93    Update(Update),
94}
95
96impl Statement {
97    /// Returns `true` if this is an [`Update`] statement.
98    pub fn is_update(&self) -> bool {
99        matches!(self, Self::Update(_))
100    }
101
102    /// Returns the number of returned elements within the statement (if one exists).
103    pub fn returning_len(&self) -> Option<usize> {
104        match self {
105            Self::Delete(delete) => delete
106                .returning
107                .as_ref()
108                .map(|ret| ret.as_project_unwrap().as_record_unwrap().len()),
109            Self::Insert(insert) => insert
110                .returning
111                .as_ref()
112                .map(|ret| ret.as_project_unwrap().as_record_unwrap().len()),
113            Self::Query(query) => match &query.body {
114                ExprSet::Select(select) => Some(
115                    select
116                        .returning
117                        .as_project_unwrap()
118                        .as_record_unwrap()
119                        .len(),
120                ),
121                stmt => todo!("returning_len, stmt={stmt:#?}"),
122            },
123            Self::Update(update) => update
124                .returning
125                .as_ref()
126                .map(|ret| ret.as_project_unwrap().as_record_unwrap().len()),
127            _ => None,
128        }
129    }
130}
131
132impl From<toasty_core::stmt::Statement> for Statement {
133    fn from(value: toasty_core::stmt::Statement) -> Self {
134        match value {
135            toasty_core::stmt::Statement::Delete(stmt) => Self::Delete(stmt),
136            toasty_core::stmt::Statement::Insert(stmt) => Self::Insert(stmt),
137            toasty_core::stmt::Statement::Query(stmt) => Self::Query(stmt),
138            toasty_core::stmt::Statement::Update(stmt) => Self::Update(stmt),
139        }
140    }
141}