Skip to main content

uqa_sql/semantics/rules/
input_contract.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Validate INSERT rule query shapes before source expressions, CTEs or triggers execute.
8
9use super::{RuleCatalog, RuleEvent, SQLError};
10use crate::plan::{InsertPlan, RelationalPlan};
11use uqa_core::memory::ProductionControl;
12
13pub fn validate_insert_rule_query_contract(
14    catalog: &dyn RuleCatalog,
15    table: &str,
16    insert: &InsertPlan,
17    mut original_target: impl FnMut(&str, RuleEvent) -> Result<Option<String>, SQLError>,
18) -> Result<(), SQLError> {
19    let control = ProductionControl::uncontrolled();
20    super::validate_insert_rule_query_contract_with_control(
21        catalog,
22        table,
23        insert,
24        |table, event| {
25            original_target(table, event)?
26                .map(|name| control.finish(name, None).map_err(Into::into))
27                .transpose()
28        },
29        &control,
30    )
31}
32
33pub(super) fn conditional_input(insert: &InsertPlan) -> bool {
34    let Some(source) = &insert.source else {
35        return insert.rows.len() > 1;
36    };
37    if !insert
38        .rule_values_input
39        .as_ref()
40        .is_some_and(crate::plan::RuleValuesInput::is_inline)
41    {
42        return true;
43    }
44    match &source.root {
45        RelationalPlan::QueryBlock(block) => {
46            !source.ctes.is_empty() || block.from.is_some() || block.r#where.is_some()
47        }
48        _ => true,
49    }
50}