Skip to main content

uqa_execution/statement/plan_executor/
completion.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Successful statement completion retains the exact state maintained by execution.
8
9use 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        let output = match plan {
18            UnifiedPlan::Query(_) => StatementOutput::Query(self.execute_query_to_spill(plan)?),
19            UnifiedPlan::Command(command)
20                if matches!(
21                    command.as_ref(),
22                    CommandPlan::Insert(_)
23                        | CommandPlan::Update(_)
24                        | CommandPlan::Delete(_)
25                        | CommandPlan::Merge(_)
26                ) =>
27            {
28                crate::statement::validation::validate_plan(
29                    &self.context.validation,
30                    self.context.runtime.cancellation,
31                    plan,
32                )?;
33                self.validate_command_target(command)?;
34                let result = match command.as_ref() {
35                    CommandPlan::Insert(plan) => self.execute_insert(plan),
36                    CommandPlan::Update(plan) => self.execute_update(plan),
37                    CommandPlan::Delete(plan) => self.execute_delete(plan),
38                    CommandPlan::Merge(plan) => self.execute_merge(plan),
39                    _ => unreachable!("mutation command selected above"),
40                }?;
41                StatementOutput::Mutation(result)
42            }
43            UnifiedPlan::Command(_) => return self.execute(plan).map(StatementOutput::Command),
44        };
45        self.context
46            .validation
47            .transactions
48            .retain_completed_statement_snapshot();
49        Ok(output)
50    }
51
52    pub fn execute(&mut self, plan: &UnifiedPlan) -> Result<SQLResult, SQLError> {
53        crate::statement::validation::validate_plan(
54            &self.context.validation,
55            self.context.runtime.cancellation,
56            plan,
57        )?;
58        let transaction_failed = self.context.controls.transaction_failed();
59        let mut result = match plan {
60            UnifiedPlan::Query(query) => self.execute_query(query),
61            UnifiedPlan::Command(command) => self.execute_command(command),
62        }?;
63        uqa_sql::result::completion::set_command_completion(plan, &mut result, transaction_failed);
64        self.context
65            .validation
66            .transactions
67            .retain_completed_statement_snapshot();
68        Ok(result)
69    }
70}