Skip to main content

uqa_sql/plan/mutable/commands/
construction.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Borrowed mutation bodies enter the same complete command constructor without an intermediate unowned copy.
8
9use super::{CommandPlan, MutableCommandPlan, SQLError};
10use crate::plan::copy::Copying;
11use uqa_core::memory::ProductionControl;
12
13macro_rules! mutation_constructor {
14    ($plan:ty, $variant:ident, $method:ident) => {
15        impl $plan {
16            pub fn copy_for_rewrite(
17                &self,
18                control: &ProductionControl<'_>,
19            ) -> Result<MutableCommandPlan, SQLError> {
20                control.check()?;
21                let Some(mut copying) = Copying::for_rewrite(*control) else {
22                    return Ok(MutableCommandPlan::from_rewrite_parts(
23                        CommandPlan::$variant(Box::new(self.clone())),
24                        None,
25                    ));
26                };
27                let command = copying.$method(self)?;
28                control.check()?;
29                Ok(MutableCommandPlan::from_rewrite_parts(
30                    command,
31                    Some(copying.into_rewrite_memory()),
32                ))
33            }
34        }
35    };
36}
37
38mutation_constructor!(crate::plan::InsertPlan, Insert, insert_command);
39mutation_constructor!(crate::plan::UpdatePlan, Update, update_command);
40mutation_constructor!(crate::plan::DeletePlan, Delete, delete_command);
41mutation_constructor!(crate::plan::MergePlan, Merge, merge_command);