toasty_sql/stmt/
drop_column.rs

1use super::{Name, Statement};
2
3use toasty_core::schema::db::{Column, TableId};
4
5/// A statement to drop a column from a table.
6#[derive(Debug, Clone)]
7pub struct DropColumn {
8    /// ID of the table to drop the column from.
9    pub table: TableId,
10
11    /// Name of the column to drop.
12    pub name: Name,
13
14    /// Whether or not to add an `IF EXISTS` clause.
15    pub if_exists: bool,
16}
17
18impl Statement {
19    /// Drops a column from a table.
20    ///
21    /// This function _does not_ add an `IF EXISTS` clause.
22    pub fn drop_column(column: &Column) -> Self {
23        DropColumn {
24            table: column.id.table,
25            name: Name::from(&column.name[..]),
26            if_exists: false,
27        }
28        .into()
29    }
30
31    /// Drops a column from a table if it exists.
32    ///
33    /// This function _does_ add an `IF EXISTS` clause.
34    pub fn drop_column_if_exists(column: &Column) -> Self {
35        DropColumn {
36            table: column.id.table,
37            name: Name::from(&column.name[..]),
38            if_exists: true,
39        }
40        .into()
41    }
42}
43
44impl From<DropColumn> for Statement {
45    fn from(value: DropColumn) -> Self {
46        Self::DropColumn(value)
47    }
48}