use selene_core::DbString;
use selene_graph::DropBehavior as GraphDropBehavior;
use crate::{
DropBehavior, SourceSpan,
runtime::{BindingTable, ExecutorError, TxContext},
};
use super::{catalog_graph_error, closed_graph_type, edge_type_exists, node_type_exists};
const fn graph_drop_behavior(behavior: DropBehavior) -> GraphDropBehavior {
match behavior {
DropBehavior::Restrict => GraphDropBehavior::Restrict,
DropBehavior::Cascade => GraphDropBehavior::Cascade,
}
}
pub(super) fn execute_drop_node_type(
label: DbString,
if_exists: bool,
behavior: DropBehavior,
span: SourceSpan,
table: BindingTable,
ctx: &mut TxContext<'_, '_>,
) -> Result<BindingTable, ExecutorError> {
ctx.ensure_write_txn("catalog op invoked without write transaction", span)?;
let graph_type = closed_graph_type(ctx.snapshot(), span)?;
if !node_type_exists(Some(&graph_type), label.clone()) && if_exists {
return Ok(table);
}
ctx.mutator_with_span("catalog op invoked without write transaction", span)?
.drop_node_type(label, graph_drop_behavior(behavior))
.map_err(|source| catalog_graph_error(source, span))?;
Ok(table)
}
pub(super) fn execute_drop_edge_type(
label: DbString,
if_exists: bool,
behavior: DropBehavior,
span: SourceSpan,
table: BindingTable,
ctx: &mut TxContext<'_, '_>,
) -> Result<BindingTable, ExecutorError> {
ctx.ensure_write_txn("catalog op invoked without write transaction", span)?;
let graph_type = closed_graph_type(ctx.snapshot(), span)?;
if !edge_type_exists(Some(&graph_type), label.clone()) && if_exists {
return Ok(table);
}
ctx.mutator_with_span("catalog op invoked without write transaction", span)?
.drop_edge_type(label, graph_drop_behavior(behavior))
.map_err(|source| catalog_graph_error(source, span))?;
Ok(table)
}