uqa_execution/statement/plan_executor/
completion.rs1use super::{StatementOutput, UnifiedPlanExecutor};
10use uqa_sql::{
11 plan::{CommandPlan, UnifiedPlan},
12 SQLError, SQLResult,
13};
14
15impl<S: Clone + Send + Sync + 'static> UnifiedPlanExecutor<'_, '_, S> {
16 pub fn execute_for_output(&mut self, plan: &UnifiedPlan) -> Result<StatementOutput, SQLError> {
17 self.execute_for_output_mode(plan, super::QueryOutputMode::SharedSpill)
18 }
19
20 pub(crate) fn execute_for_output_mode(
22 &mut self,
23 plan: &UnifiedPlan,
24 mode: super::QueryOutputMode<'_, S>,
25 ) -> Result<StatementOutput, SQLError> {
26 let output = match plan {
27 UnifiedPlan::Query(_) => StatementOutput::Query(self.execute_query_output(plan, mode)?),
28 UnifiedPlan::Command(command)
29 if matches!(
30 command.as_ref(),
31 CommandPlan::Insert(_)
32 | CommandPlan::Update(_)
33 | CommandPlan::Delete(_)
34 | CommandPlan::Merge(_)
35 ) =>
36 {
37 crate::statement::validation::validate_plan(
38 &self.context.validation,
39 self.context.runtime.cancellation,
40 plan,
41 )?;
42 self.validate_command_target(command)?;
43 let consumer = match mode {
44 super::QueryOutputMode::RowConsumer(factory) => Some(factory.bind(None)?),
45 _ => None,
46 };
47 let schema = if consumer.is_some() {
48 let inputs = self.context.mutations.mutation_initialization_context();
49 let mut analysis = self.context.mutations.returning_analysis_context();
50 if let Some(parent) = &self.inherited_ctes {
51 analysis.scope = parent;
52 }
53 crate::mutation::entry::command_returning_schema(
54 &self.context.mutations.returning_execution_context(),
55 analysis,
56 inputs.mutation.scopes,
57 command,
58 self.params,
59 )?
60 } else {
61 None
62 };
63 let destination = consumer
64 .as_deref()
65 .zip(schema.as_ref())
66 .map(|(consumer, schema)| {
67 crate::mutation::returning::ReturningDestination::new(consumer, schema)
68 })
69 .transpose()?;
70 let result = match command.as_ref() {
71 CommandPlan::Insert(plan) => self.execute_insert(plan, destination),
72 CommandPlan::Update(plan) => self.execute_update(plan, destination),
73 CommandPlan::Delete(plan) => self.execute_delete(plan, destination),
74 CommandPlan::Merge(plan) => self.execute_merge(plan, destination),
75 _ => unreachable!("mutation command selected above"),
76 }?;
77 StatementOutput::Mutation(result)
78 }
79 UnifiedPlan::Command(_) => return self.execute(plan).map(StatementOutput::Command),
80 };
81 self.context
82 .validation
83 .transactions
84 .retain_completed_statement_snapshot();
85 Ok(output)
86 }
87
88 pub fn execute(&mut self, plan: &UnifiedPlan) -> Result<SQLResult, SQLError> {
89 crate::statement::validation::validate_plan(
90 &self.context.validation,
91 self.context.runtime.cancellation,
92 plan,
93 )?;
94 let transaction_failed = self.context.controls.transaction_failed();
95 let mut result = match plan {
96 UnifiedPlan::Query(query) => self.execute_query(query),
97 UnifiedPlan::Command(command) => self.execute_command(command),
98 }?;
99 uqa_sql::result::completion::set_command_completion(plan, &mut result, transaction_failed);
100 self.context
101 .validation
102 .transactions
103 .retain_completed_statement_snapshot();
104 Ok(result)
105 }
106}