toasty_sql/stmt/
create_index.rs

1use super::Statement;
2
3use toasty_core::{
4    schema::db::{Index, IndexId, IndexOp, TableId},
5    stmt,
6};
7
8#[derive(Debug, Clone)]
9pub struct CreateIndex {
10    /// Name of the index
11    pub index: IndexId,
12
13    /// Which table to index
14    pub on: TableId,
15
16    /// The columns to index
17    pub columns: Vec<stmt::OrderByExpr>,
18
19    /// When true, the index is unique
20    pub unique: bool,
21}
22
23impl Statement {
24    pub fn create_index(index: &Index) -> Self {
25        CreateIndex {
26            index: index.id,
27            on: index.on,
28            columns: index
29                .columns
30                .iter()
31                .map(|index_column| stmt::OrderByExpr {
32                    expr: stmt::Expr::column(index_column.column),
33                    order: match index_column.op {
34                        IndexOp::Eq => None,
35                        IndexOp::Sort(direction) => Some(direction),
36                    },
37                })
38                .collect(),
39            unique: index.unique,
40        }
41        .into()
42    }
43}
44
45impl From<CreateIndex> for Statement {
46    fn from(value: CreateIndex) -> Self {
47        Self::CreateIndex(value)
48    }
49}