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 column_def;
11pub use column_def::ColumnDef;
12
13mod copy_table;
14pub use copy_table::CopyTable;
15
16mod create_index;
17pub use create_index::CreateIndex;
18
19mod create_table;
20pub use create_table::CreateTable;
21
22mod drop_column;
23pub use drop_column::DropColumn;
24
25mod drop_index;
26pub use drop_index::DropIndex;
27
28mod drop_table;
29pub use drop_table::DropTable;
30
31mod ident;
32pub use ident::Ident;
33
34mod name;
35pub use name::Name;
36
37mod pragma;
38pub use pragma::Pragma;
39
40mod table_name;
41pub use table_name::TableName;
42
43pub use toasty_core::stmt::*;
44
45#[derive(Debug, Clone)]
46pub enum Statement {
47    AddColumn(AddColumn),
48    AlterColumn(AlterColumn),
49    AlterTable(AlterTable),
50    CopyTable(CopyTable),
51    CreateIndex(CreateIndex),
52    CreateTable(CreateTable),
53    DropColumn(DropColumn),
54    DropTable(DropTable),
55    DropIndex(DropIndex),
56    Pragma(Pragma),
57    Delete(Delete),
58    Insert(Insert),
59    Query(Query),
60    Update(Update),
61}
62
63impl Statement {
64    pub fn is_update(&self) -> bool {
65        matches!(self, Self::Update(_))
66    }
67
68    /// Returns the number of returned elements within the statement (if one exists).
69    pub fn returning_len(&self) -> Option<usize> {
70        match self {
71            Self::Delete(delete) => delete
72                .returning
73                .as_ref()
74                .map(|ret| ret.as_expr_unwrap().as_record_unwrap().len()),
75            Self::Insert(insert) => insert
76                .returning
77                .as_ref()
78                .map(|ret| ret.as_expr_unwrap().as_record_unwrap().len()),
79            Self::Query(query) => match &query.body {
80                ExprSet::Select(select) => {
81                    Some(select.returning.as_expr_unwrap().as_record_unwrap().len())
82                }
83                stmt => todo!("returning_len, stmt={stmt:#?}"),
84            },
85            Self::Update(update) => update
86                .returning
87                .as_ref()
88                .map(|ret| ret.as_expr_unwrap().as_record_unwrap().len()),
89            _ => None,
90        }
91    }
92}
93
94impl From<toasty_core::stmt::Statement> for Statement {
95    fn from(value: toasty_core::stmt::Statement) -> Self {
96        match value {
97            toasty_core::stmt::Statement::Delete(stmt) => Self::Delete(stmt),
98            toasty_core::stmt::Statement::Insert(stmt) => Self::Insert(stmt),
99            toasty_core::stmt::Statement::Query(stmt) => Self::Query(stmt),
100            toasty_core::stmt::Statement::Update(stmt) => Self::Update(stmt),
101        }
102    }
103}