uqa_sql/plan/mutable/sources/
parents.rs1use super::{
8 empty_source, MutableQueryPlan, MutableSourcePlan, SQLError, SourceChildSlot, SourcePlan,
9};
10use uqa_core::memory::ProductionControl;
11
12impl MutableSourcePlan {
13 pub fn map_query_with_parent(
15 mut self,
16 control: &ProductionControl<'_>,
17 map: impl FnOnce(&SourcePlan, MutableQueryPlan) -> Result<MutableQueryPlan, SQLError>,
18 ) -> Result<Self, SQLError> {
19 self.assert_owner(control);
20 control.check()?;
21 let (query, field) = match &mut self.source {
22 SourcePlan::Subquery { body, .. } => (body, "body"),
23 SourcePlan::Table {
24 executable_query: Some(query),
25 ..
26 } => (query, "executable_query"),
27 SourcePlan::Table {
28 executable_query: None,
29 ..
30 } => return Ok(self),
31 _ => {
32 return Err(SQLError::Internal(
33 "source has no directly retained query".into(),
34 ))
35 }
36 };
37 let input = MutableQueryPlan {
38 query: std::mem::replace(query.as_mut(), super::super::queries::empty_query()),
39 memory: self.memory.as_mut().and_then(|memory| {
40 memory
41 .field_mut(field)
42 .as_mut()
43 .expect("source query box")
44 .field_mut("query")
45 .take()
46 }),
47 };
48 let output = map(&self.source, input)?;
49 output.assert_owner(control);
50 control.check()?;
51 let query = match &mut self.source {
52 SourcePlan::Subquery { body, .. } => body,
53 SourcePlan::Table {
54 executable_query: Some(query),
55 ..
56 } => query,
57 _ => unreachable!("read-only parent cannot change topology"),
58 };
59 **query = output.query;
60 if let Some(memory) = &mut self.memory {
61 *memory
62 .field_mut(field)
63 .as_mut()
64 .expect("source query box")
65 .field_mut("query") = output.memory;
66 }
67 Ok(self)
68 }
69
70 pub fn map_child_with_parent(
72 mut self,
73 slot: SourceChildSlot,
74 control: &ProductionControl<'_>,
75 map: impl FnOnce(&SourcePlan, Self) -> Result<Self, SQLError>,
76 ) -> Result<Self, SQLError> {
77 self.assert_owner(control);
78 control.check()?;
79 let input = Self {
80 source: std::mem::replace(slot.source_mut(&mut self.source), empty_source()),
81 memory: self
82 .memory
83 .as_mut()
84 .and_then(|memory| slot.memory_mut(memory).take()),
85 };
86 let output = map(&self.source, input)?;
87 output.assert_owner(control);
88 control.check()?;
89 *slot.source_mut(&mut self.source) = output.source;
90 if let Some(memory) = &mut self.memory {
91 *slot.memory_mut(memory) = output.memory;
92 }
93 Ok(self)
94 }
95}