uqa_execution/query/scope/
statement_commands.rs1use crate::mutation::candidate::PhysicalDocumentIdentity;
10use crate::mutation::triggers::queue::AfterTriggerQueue;
11use std::collections::BTreeSet;
12use std::sync::atomic::{AtomicBool, Ordering};
13use uqa_core::memory::{MemoryReservation, Produced, ProductionControl, ProductionVec};
14use uqa_sql::{plan::RetainedCtePlan, semantics::cte_strategy::ScheduledCte, SQLError};
15
16type PostponedCtes = Produced<Vec<(RetainedCtePlan, bool)>>;
17
18#[derive(Default)]
20pub struct StatementCommands {
21 modifies_with: AtomicBool,
23 postponed: parking_lot::Mutex<Option<PostponedCtes>>,
25 written: parking_lot::Mutex<BTreeSet<PhysicalDocumentIdentity>>,
27 triggered: parking_lot::Mutex<BTreeSet<PhysicalDocumentIdentity>>,
29 after_triggers: AfterTriggerQueue,
30 _memory: Option<MemoryReservation>,
31}
32
33impl StatementCommands {
34 pub(crate) fn with_control(
36 control: &ProductionControl<'_>,
37 ) -> Result<std::sync::Arc<Self>, SQLError> {
38 let memory = control.reserve(size_of::<Self>())?;
39 Ok(std::sync::Arc::new(Self {
40 _memory: memory,
41 ..Self::default()
42 }))
43 }
44
45 pub(crate) fn begin_data_modifying_with<'a>(
47 &self,
48 postponed: impl IntoIterator<Item = ScheduledCte<'a>>,
49 control: &ProductionControl<'_>,
50 ) -> Result<(), SQLError> {
51 let mut schedule = ProductionVec::new(*control);
52 for entry in postponed {
53 schedule.reserve(1)?;
54 let plan = RetainedCtePlan::capture(entry.plan, control)?;
55 schedule.push_produced(
56 control.finish((plan, entry.deferred), control.empty_reservation())?,
57 )?;
58 }
59 let schedule = schedule.finish()?;
60 *self.postponed.lock() = Some(schedule);
61 self.modifies_with.store(true, Ordering::Relaxed);
62 Ok(())
63 }
64
65 pub fn modifies_with(&self) -> bool {
66 self.modifies_with.load(Ordering::Relaxed)
67 }
68
69 pub fn after_triggers(&self) -> &AfterTriggerQueue {
70 &self.after_triggers
71 }
72
73 pub(crate) fn take_scheduled_postponed(&self) -> Option<PostponedCtes> {
75 self.postponed.lock().take()
76 }
77
78 pub fn note_written(&self, rows: impl IntoIterator<Item = PhysicalDocumentIdentity>) {
79 self.written.lock().extend(rows);
80 }
81
82 pub fn wrote(&self, row: &PhysicalDocumentIdentity) -> bool {
84 self.written.lock().contains(row)
85 }
86
87 pub fn note_triggered(&self, rows: impl IntoIterator<Item = PhysicalDocumentIdentity>) {
88 self.triggered.lock().extend(rows);
89 }
90
91 pub fn triggered_write(&self, row: &PhysicalDocumentIdentity) -> bool {
93 self.triggered.lock().contains(row)
94 }
95}
96
97#[cfg(test)]
98mod tests;