use std::ops::Deref;
use anyhow::Result;
use reblessive::tree::Stk;
use surrealdb_types::{SqlFormat, ToSql};
use tracing::instrument;
use super::AlterKind;
use crate::catalog::providers::TableProvider;
use crate::catalog::{Permissions, TableType};
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::DefineTableStatement;
use crate::expr::{Base, ChangeFeed, Expr, Literal};
use crate::iam::{Action, ResourceKind};
use crate::val::{TableName, Value};
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub(crate) struct AlterTableStatement {
pub name: Expr,
pub if_exists: bool,
pub(crate) schemafull: AlterKind<()>,
pub permissions: Option<Permissions>,
pub(crate) changefeed: AlterKind<ChangeFeed>,
pub(crate) comment: AlterKind<String>,
pub(crate) compact: bool,
pub kind: Option<TableType>,
}
impl Default for AlterTableStatement {
fn default() -> Self {
Self {
name: Expr::Literal(Literal::None),
if_exists: false,
schemafull: AlterKind::None,
permissions: None,
changefeed: AlterKind::None,
comment: AlterKind::None,
compact: false,
kind: None,
}
}
}
impl AlterTableStatement {
#[instrument(level = "trace", name = "AlterTableStatement::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::Table, Base::Db)?;
let name =
TableName::new(expr_to_ident(stk, ctx, opt, doc, &self.name, "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 dt = match txn.get_tb(ns, db, &name, None).await? {
Some(tb) => tb.deref().clone(),
None => {
if self.if_exists {
return Ok(Value::None);
} else {
return Err(Error::TbNotFound {
name: name.clone(),
}
.into());
}
}
};
match self.schemafull {
AlterKind::Set(_) => dt.schemafull = true,
AlterKind::Drop => dt.schemafull = false,
AlterKind::None => {}
}
if let Some(permissions) = &self.permissions {
dt.permissions = permissions.clone();
}
let mut changefeed_replaced = false;
match self.changefeed {
AlterKind::Set(x) => {
changefeed_replaced = dt.changefeed.is_some();
dt.changefeed = Some(x)
}
AlterKind::Drop => dt.changefeed = None,
AlterKind::None => {}
}
match self.comment {
AlterKind::Set(ref x) => dt.comment = Some(x.clone()),
AlterKind::Drop => dt.comment = None,
AlterKind::None => {}
}
if let Some(kind) = &self.kind {
dt.table_type = kind.clone();
}
if matches!(self.kind, Some(TableType::Relation(_))) {
DefineTableStatement::add_in_out_fields(&txn, ns, db, &mut dt).await?;
}
if changefeed_replaced {
txn.changefeed_buffer_table_change(ns, db, &name, &dt);
}
if self.compact {
let key = crate::key::table::all::new(ns, db, &name);
txn.compact(Some(key)).await?;
}
txn.put_tb(ns_name, db_name, &dt).await?;
txn.clear_cache();
Ok(Value::None)
}
}
impl ToSql for AlterTableStatement {
fn fmt_sql(&self, f: &mut String, fmt: SqlFormat) {
let stmt: crate::sql::statements::alter::AlterTableStatement = self.clone().into();
stmt.fmt_sql(f, fmt);
}
}