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