Skip to main content

uqa_sql/routines/lifecycle/
dependencies.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Atomic removal of resolved routines from the routine registry.
8
9use super::{RoutineDropTarget, RoutineRegistry};
10use crate::SQLError;
11
12/// Remove the routines `targets` names from `next`. Every target is found before any is removed, so a routine registered after the targets were resolved is retained, a routine re-created with a resolved signature under another identity is not removed, and a removal whose target has disappeared removes nothing.
13pub fn remove_routine_registry_targets(
14    next: &mut RoutineRegistry,
15    targets: &[RoutineDropTarget],
16) -> Result<(), SQLError> {
17    for target in targets {
18        if !next
19            .get(&target.name)
20            .is_some_and(|overloads| overloads.iter().any(|function| target.names(function)))
21        {
22            return Err(SQLError::Internal(format!(
23                "resolved {} {} disappeared before DROP",
24                target.kind(),
25                target.label()
26            )));
27        }
28    }
29    for target in targets {
30        if let Some(overloads) = next.get_mut(&target.name) {
31            overloads.retain(|function| !target.names(function));
32            if overloads.is_empty() {
33                next.remove(&target.name);
34            }
35        }
36    }
37    Ok(())
38}
39
40#[cfg(test)]
41mod tests;