Skip to main content

uqa_sql/catalog/events/
restoration.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Restore retained event expressions and validate their bound function identities.
8use super::definition::EventAnalysisContext;
9use crate::{ast::CreateTrigger, catalog::resolution::RelationLookupMode};
10
11/// Upgrade both retained rule syntax and its analyzed condition without dropping constants or query identities.
12pub fn restore_rule_expressions(
13    rule: &mut super::StoredRule,
14    routines: &dyn crate::FunctionTypeResolver,
15) -> Result<bool, crate::SQLError> {
16    let mut changed = rule.definition.condition.is_some() && rule.bound_condition_plan().is_none();
17    if let Some(condition) = &mut rule.definition.condition {
18        changed |= condition.upgrade_legacy_serialized_dispatches();
19        changed |= crate::binding::legacy_patterns::restore_expression(condition, routines)
20            .map_err(crate::SQLError::Internal)?;
21    }
22    if let Some(condition) = &mut rule.condition_plan {
23        changed |= crate::binding::legacy_patterns::restore_expression_plan(condition, routines);
24        changed |= condition.upgrade_legacy_range_predicates()?;
25    }
26    for action in &mut rule.definition.actions {
27        changed |= action.upgrade_legacy_serialized_dispatches();
28        changed |= crate::binding::legacy_patterns::restore_statement(action, routines)?;
29    }
30    Ok(changed)
31}
32
33pub fn trigger_function_object_id(
34    context: &EventAnalysisContext<'_>,
35    definition: &CreateTrigger,
36) -> Result<[u8; 16], String> {
37    context
38        .resolve_trigger_function(&definition.function, RelationLookupMode::Bound)
39        .map_err(|error| format!("restore trigger function identity: {error}"))?
40        .def
41        .object_id
42        .ok_or_else(|| {
43            format!(
44                "restore trigger catalog: function `{}` has no object identity",
45                definition.function
46            )
47        })
48}