use anyhow::Result;
use reblessive::tree::Stk;
use crate::catalog::providers::DatabaseProvider;
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::{Base, Expr, Literal, Value};
use crate::iam::{Action, ResourceKind};
use crate::key::database::sq::Sq;
use crate::key::sequence::Prefix;
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub(crate) struct RemoveSequenceStatement {
pub name: Expr,
pub if_exists: bool,
}
impl Default for RemoveSequenceStatement {
fn default() -> Self {
Self {
name: Expr::Literal(Literal::None),
if_exists: false,
}
}
}
impl RemoveSequenceStatement {
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::Sequence, Base::Db)?;
let name = expr_to_ident(stk, ctx, opt, doc, &self.name, "sequence name").await?;
let (ns, db) = ctx.expect_ns_db_ids(opt).await?;
let txn = ctx.tx();
let sq = match txn.get_db_sequence(ns, db, &name, None).await {
Ok(x) => x,
Err(e) => {
if self.if_exists && matches!(e.downcast_ref(), Some(Error::SeqNotFound { .. })) {
return Ok(Value::None);
} else {
return Err(e);
}
}
};
if let Some(seq) = ctx.get_sequences() {
seq.sequence_removed(ns, db, &name).await;
}
let ba_range = Prefix::new_ba_range(ns, db, &sq.name)?;
txn.delr(ba_range).await?;
let st_range = Prefix::new_st_range(ns, db, &sq.name)?;
txn.delr(st_range).await?;
let key = Sq::new(ns, db, &name);
txn.del(&key).await?;
txn.clear_cache();
Ok(Value::None)
}
}