1use super::{
10 CommandPlan, CtePlan, DeletePlan, InsertPlan, MergePlan, ProjectionPlan, QueryPlan, SourcePlan,
11 UpdatePlan,
12};
13
14impl CommandPlan {
15 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 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 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 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 pub fn query_inputs(&self) -> impl Iterator<Item = &QueryPlan> {
164 self.subqueries.iter()
165 }
166
167 pub fn source_input(&self) -> Option<&SourcePlan> {
169 self.source.as_deref()
170 }
171}
172
173impl DeletePlan {
174 pub fn query_inputs(&self) -> impl Iterator<Item = &QueryPlan> {
176 self.subqueries.iter()
177 }
178
179 pub fn source_input(&self) -> Option<&SourcePlan> {
181 self.source.as_deref()
182 }
183}
184
185impl MergePlan {
186 pub fn query_inputs(&self) -> impl Iterator<Item = &QueryPlan> {
188 self.subqueries.iter()
189 }
190
191 pub fn source_input(&self) -> Option<&SourcePlan> {
193 Some(&self.source)
194 }
195}