Skip to main content

uqa_sql/plan/
command_children.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Direct relational children of data-modifying commands.
8
9use super::{
10    CommandPlan, CtePlan, DeletePlan, InsertPlan, MergePlan, ProjectionPlan, QueryPlan, SourcePlan,
11    UpdatePlan,
12};
13
14impl CommandPlan {
15    /// WITH definitions owned by this command, in declaration order.
16    pub fn ctes(&self) -> &[CtePlan] {
17        match self {
18            Self::Insert(plan) => &plan.ctes,
19            Self::Update(plan) => &plan.ctes,
20            Self::Delete(plan) => &plan.ctes,
21            Self::Merge(plan) => &plan.ctes,
22            _ => &[],
23        }
24    }
25
26    pub fn ctes_mut(&mut self) -> Option<&mut Vec<CtePlan>> {
27        match self {
28            Self::Insert(plan) => Some(&mut plan.ctes),
29            Self::Update(plan) => Some(&mut plan.ctes),
30            Self::Delete(plan) => Some(&mut plan.ctes),
31            Self::Merge(plan) => Some(&mut plan.ctes),
32            _ => None,
33        }
34    }
35
36    /// Borrow query children evaluated in the command's WITH scope without collecting a child list. INSERT's source precedes the scalar-query arena. Source-plan subqueries are owned by `source_input`; visitors should not traverse either owner twice.
37    pub fn query_inputs(&self) -> impl Iterator<Item = &QueryPlan> {
38        let (source, subqueries): (Option<&QueryPlan>, &[QueryPlan]) = match self {
39            Self::Insert(plan) => (plan.source.as_deref(), &plan.subqueries),
40            Self::Update(plan) => (None, &plan.subqueries),
41            Self::Delete(plan) => (None, &plan.subqueries),
42            Self::Merge(plan) => (None, &plan.subqueries),
43            _ => (None, &[]),
44        };
45        source.into_iter().chain(subqueries)
46    }
47
48    /// Borrow mutable query children in the same source-then-arena order, without allocating a reference vector.
49    pub fn query_inputs_mut(&mut self) -> impl Iterator<Item = &mut QueryPlan> {
50        let (source, subqueries): (Option<&mut QueryPlan>, &mut [QueryPlan]) = match self {
51            Self::Insert(plan) => (plan.source.as_deref_mut(), &mut plan.subqueries),
52            Self::Update(plan) => (None, &mut plan.subqueries),
53            Self::Delete(plan) => (None, &mut plan.subqueries),
54            Self::Merge(plan) => (None, &mut plan.subqueries),
55            _ => (None, &mut []),
56        };
57        source.into_iter().chain(subqueries)
58    }
59
60    pub fn source_input(&self) -> Option<&SourcePlan> {
61        match self {
62            Self::Update(plan) => plan.source_input(),
63            Self::Delete(plan) => plan.source_input(),
64            Self::Merge(plan) => plan.source_input(),
65            _ => None,
66        }
67    }
68
69    pub fn source_input_mut(&mut self) -> Option<&mut SourcePlan> {
70        match self {
71            Self::Update(plan) => plan.source.as_deref_mut(),
72            Self::Delete(plan) => plan.source.as_deref_mut(),
73            Self::Merge(plan) => Some(&mut plan.source),
74            _ => None,
75        }
76    }
77
78    pub fn mutation_target(&self) -> Option<&str> {
79        match self {
80            Self::Insert(plan) => Some(&plan.table),
81            Self::Update(plan) => Some(&plan.table),
82            Self::Delete(plan) => Some(&plan.table),
83            Self::Merge(plan) => Some(&plan.target),
84            _ => None,
85        }
86    }
87
88    pub fn mutation_target_mut(&mut self) -> Option<&mut String> {
89        match self {
90            Self::Insert(plan) => Some(&mut plan.table),
91            Self::Update(plan) => Some(&mut plan.table),
92            Self::Delete(plan) => Some(&mut plan.table),
93            Self::Merge(plan) => Some(&mut plan.target),
94            _ => None,
95        }
96    }
97
98    pub fn returning(&self) -> Option<&[ProjectionPlan]> {
99        match self {
100            Self::Insert(plan) => Some(&plan.returning),
101            Self::Update(plan) => Some(&plan.returning),
102            Self::Delete(plan) => Some(&plan.returning),
103            Self::Merge(plan) => Some(&plan.returning),
104            _ => None,
105        }
106    }
107
108    pub fn returning_mut(&mut self) -> Option<&mut [ProjectionPlan]> {
109        match self {
110            Self::Insert(plan) => Some(&mut plan.returning),
111            Self::Update(plan) => Some(&mut plan.returning),
112            Self::Delete(plan) => Some(&mut plan.returning),
113            Self::Merge(plan) => Some(&mut plan.returning),
114            _ => None,
115        }
116    }
117}
118
119impl CommandPlan {
120    pub fn scalar_subqueries(&self) -> &[QueryPlan] {
121        match self {
122            Self::Insert(plan) => &plan.subqueries,
123            Self::Update(plan) => &plan.subqueries,
124            Self::Delete(plan) => &plan.subqueries,
125            Self::Merge(plan) => &plan.subqueries,
126            _ => &[],
127        }
128    }
129
130    pub fn target_qualifier(&self) -> Option<&str> {
131        match self {
132            Self::Insert(plan) => Some(&plan.target_qualifier),
133            Self::Update(plan) => Some(&plan.target_qualifier),
134            Self::Delete(plan) => Some(&plan.target_qualifier),
135            Self::Merge(plan) => Some(&plan.target_qualifier),
136            _ => None,
137        }
138    }
139
140    pub fn returning_aliases(&self) -> Option<&crate::ast::ReturningAliases> {
141        match self {
142            Self::Insert(plan) => Some(&plan.returning_aliases),
143            Self::Update(plan) => Some(&plan.returning_aliases),
144            Self::Delete(plan) => Some(&plan.returning_aliases),
145            Self::Merge(plan) => Some(&plan.returning_aliases),
146            _ => None,
147        }
148    }
149}
150
151impl InsertPlan {
152    /// Query children evaluated in the command's WITH scope: the source query and every `subqueries` entry.
153    pub fn query_inputs(&self) -> impl Iterator<Item = &QueryPlan> {
154        self.source
155            .iter()
156            .map(Box::as_ref)
157            .chain(self.subqueries.iter())
158    }
159}
160
161impl UpdatePlan {
162    /// Query children evaluated in the command's WITH scope, which are its `subqueries` entries.
163    pub fn query_inputs(&self) -> impl Iterator<Item = &QueryPlan> {
164        self.subqueries.iter()
165    }
166
167    /// The `FROM` relation the command joins to its target.
168    pub fn source_input(&self) -> Option<&SourcePlan> {
169        self.source.as_deref()
170    }
171}
172
173impl DeletePlan {
174    /// Query children evaluated in the command's WITH scope, which are its `subqueries` entries.
175    pub fn query_inputs(&self) -> impl Iterator<Item = &QueryPlan> {
176        self.subqueries.iter()
177    }
178
179    /// The `USING` relation the command joins to its target.
180    pub fn source_input(&self) -> Option<&SourcePlan> {
181        self.source.as_deref()
182    }
183}
184
185impl MergePlan {
186    /// Query children evaluated in the command's WITH scope, which are its `subqueries` entries.
187    pub fn query_inputs(&self) -> impl Iterator<Item = &QueryPlan> {
188        self.subqueries.iter()
189    }
190
191    /// The `USING` relation the command merges into its target.
192    pub fn source_input(&self) -> Option<&SourcePlan> {
193        Some(&self.source)
194    }
195}