use std::ops::Deref;
use anyhow::Result;
use reblessive::tree::Stk;
use surrealdb_types::{SqlFormat, ToSql};
use tracing::instrument;
use uuid::Uuid;
use crate::catalog::TableDefinition;
use crate::catalog::providers::TableProvider;
use crate::ctx::FrozenContext;
use crate::dbs::Options;
use crate::doc::CursorDoc;
use crate::err::Error;
use crate::expr::parameterize::expr_to_ident;
use crate::expr::statements::alter::AlterKind;
use crate::expr::{Base, Expr, Literal, Value};
use crate::iam::{Action, ResourceKind};
use crate::val::TableName;
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub(crate) struct AlterIndexStatement {
pub name: Expr,
pub table: Expr,
pub if_exists: bool,
pub prepare_remove: bool,
pub comment: AlterKind<String>,
}
impl Default for AlterIndexStatement {
fn default() -> Self {
Self {
name: Expr::Literal(Literal::None),
table: Expr::Literal(Literal::None),
if_exists: false,
prepare_remove: false,
comment: AlterKind::None,
}
}
}
impl AlterIndexStatement {
#[instrument(level = "trace", name = "AlterIndexStatement::compute", skip_all)]
pub(crate) async fn compute(
&self,
stk: &mut Stk,
ctx: &FrozenContext,
opt: &Options,
doc: Option<&CursorDoc>,
) -> Result<Value> {
ctx.is_allowed(opt, Action::Edit, ResourceKind::Index, Base::Db)?;
let name = expr_to_ident(stk, ctx, opt, doc, &self.name, "index name").await?;
let table =
TableName::new(expr_to_ident(stk, ctx, opt, doc, &self.table, "table name").await?);
let (ns_name, db_name) = opt.ns_db()?;
let (ns, db) = ctx.expect_ns_db_ids(opt).await?;
let txn = ctx.tx();
let mut ix = match txn.get_tb_index(ns, db, &table, &name, None).await? {
Some(tb) => tb.deref().clone(),
None => {
if self.if_exists {
return Ok(Value::None);
} else {
return Err(Error::IxNotFound {
name,
}
.into());
}
}
};
match self.comment {
AlterKind::Set(ref k) => ix.comment = Some(k.clone()),
AlterKind::Drop => ix.comment = None,
AlterKind::None => {}
}
if self.prepare_remove && !ix.prepare_remove {
ix.prepare_remove = true;
}
txn.put_tb_index(ns, db, &table, &ix).await?;
let tb = txn.expect_tb(ns, db, &table).await?;
txn.put_tb(
ns_name,
db_name,
&TableDefinition {
cache_indexes_ts: Uuid::now_v7(),
..tb.as_ref().clone()
},
)
.await?;
txn.clear_cache();
Ok(Value::None)
}
}
impl ToSql for AlterIndexStatement {
fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
let stmt: crate::sql::statements::alter::AlterIndexStatement = self.clone().into();
stmt.fmt_sql(f, fmt);
}
}