use super::Statement;
use toasty_core::{
schema::db::{Index, IndexId, IndexOp, TableId},
stmt,
};
#[derive(Debug, Clone)]
pub struct CreateIndex {
pub index: IndexId,
pub on: TableId,
pub columns: Vec<stmt::OrderByExpr>,
pub unique: bool,
}
impl Statement {
pub fn create_index(index: &Index) -> Self {
CreateIndex {
index: index.id,
on: index.on,
columns: index
.columns
.iter()
.map(|index_column| stmt::OrderByExpr {
expr: stmt::Expr::column(index_column.column),
order: match index_column.op {
IndexOp::Eq => None,
IndexOp::Sort(direction) => Some(direction),
},
})
.collect(),
unique: index.unique,
}
.into()
}
}
impl From<CreateIndex> for Statement {
fn from(value: CreateIndex) -> Self {
Self::CreateIndex(value)
}
}