Skip to main content

uqa_execution/mutation/
entry.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Resolve DML targets and enter one mutation command with its inherited CTE scope.
8use super::{dispatch, insert::table::InsertPlanning};
9use crate::mutation::output::MutationOutput;
10use crate::query::CteScope;
11use uqa_sql::{
12    plan::{CommandPlan, DeletePlan, InsertPlan, MergePlan, UpdatePlan},
13    SQLError, SQLParam,
14};
15mod context;
16pub use context::*;
17
18pub fn run_insert<S: Clone + Send + Sync + 'static>(
19    context: &MutationEntryContext<'_, S>,
20    mut statement: InsertPlan,
21    params: &[SQLParam],
22    ctes: Option<&CteScope<S>>,
23    destination: Option<super::returning::ReturningDestination<'_>>,
24) -> Result<MutationOutput, SQLError> {
25    statement.table = context
26        .targets
27        .resolve_target(&statement.table, statement.target_relation_bound)?;
28    context.transactions.with_command(Box::new(move |context| {
29        dispatch::run_insert(
30            &context.statement.with_destination(destination),
31            InsertPlanning {
32                inference: context.inference,
33                returning: context.returning,
34                prune_source_outputs: context.prune_source_outputs,
35            },
36            &statement,
37            params,
38            ctes,
39        )
40    }))
41}
42
43pub fn run_update<S: Clone + Send + Sync + 'static>(
44    context: &MutationEntryContext<'_, S>,
45    mut statement: UpdatePlan,
46    params: &[SQLParam],
47    ctes: Option<&CteScope<S>>,
48    destination: Option<super::returning::ReturningDestination<'_>>,
49) -> Result<MutationOutput, SQLError> {
50    statement.table = context
51        .targets
52        .resolve_target(&statement.table, statement.target_relation_bound)?;
53    context.transactions.with_command(Box::new(move |context| {
54        dispatch::run_update(
55            &context.statement.with_destination(destination),
56            &statement,
57            params,
58            ctes,
59        )
60    }))
61}
62
63pub fn run_delete<S: Clone + Send + Sync + 'static>(
64    context: &MutationEntryContext<'_, S>,
65    mut statement: DeletePlan,
66    params: &[SQLParam],
67    ctes: Option<&CteScope<S>>,
68    destination: Option<super::returning::ReturningDestination<'_>>,
69) -> Result<MutationOutput, SQLError> {
70    statement.table = context
71        .targets
72        .resolve_target(&statement.table, statement.target_relation_bound)?;
73    if ctes.is_none() {
74        uqa_sql::semantics::returning::validate_returning_alias_relations(
75            &statement.target_qualifier,
76            &statement.returning_aliases,
77            None,
78        )?;
79    }
80    context.transactions.with_command(Box::new(move |context| {
81        dispatch::run_delete(
82            &context.statement.with_destination(destination),
83            &statement,
84            params,
85            ctes,
86        )
87    }))
88}
89
90pub fn run_merge<S: Clone + Send + Sync + 'static>(
91    context: &MutationEntryContext<'_, S>,
92    mut statement: MergePlan,
93    params: &[SQLParam],
94    ctes: Option<&CteScope<S>>,
95    destination: Option<super::returning::ReturningDestination<'_>>,
96) -> Result<MutationOutput, SQLError> {
97    statement.target = context.targets.resolve_target(&statement.target, false)?;
98    context.transactions.with_command(Box::new(move |context| {
99        dispatch::run_merge(
100            &context.statement.with_destination(destination),
101            &statement,
102            params,
103            ctes,
104        )
105    }))
106}
107
108pub fn execute_cte_command<S: Clone + Send + Sync + 'static>(
109    context: &MutationEntryContext<'_, S>,
110    command: &CommandPlan,
111    params: &[SQLParam],
112    ctes: &CteScope<S>,
113) -> Result<MutationOutput, SQLError> {
114    if let Some(error) = crate::catalog::projection::virtual_relation_mutation_error(
115        &ctes.catalog_read_view()?,
116        &ctes.relation_name_resolution()?,
117        command,
118    )? {
119        crate::query::binding::analyze_command_parameters(context.routines, command, params, ctes)?;
120        return Err(error);
121    }
122    let mut command = command.clone();
123    let subject = ctes.privilege_subject()?.clone();
124    uqa_sql::semantics::mutation_privileges::inherit_command_privilege_subject(
125        &mut command,
126        subject,
127    )?;
128    match command {
129        CommandPlan::Insert(plan) => run_insert(context, *plan, params, Some(ctes), None),
130        CommandPlan::Update(plan) => run_update(context, *plan, params, Some(ctes), None),
131        CommandPlan::Delete(plan) => run_delete(context, *plan, params, Some(ctes), None),
132        CommandPlan::Merge(plan) => run_merge(context, *plan, params, Some(ctes), None),
133        _ => Err(SQLError::Internal(
134            "non-DML command in a WITH definition".into(),
135        )),
136    }
137}
138
139pub fn command_returning_schema<S: Clone + 'static>(
140    execution: &super::returning::ReturningExecutionContext<'_, S>,
141    analysis: uqa_sql::semantics::returning::ReturningAnalysisContext<'_>,
142    scopes: &dyn super::command_scope::CommandScopeSource<S>,
143    command: &CommandPlan,
144    params: &[SQLParam],
145) -> Result<Option<crate::RowSchema>, SQLError> {
146    match command {
147        CommandPlan::Merge(plan) => {
148            super::merge::analysis::merge_command_returning_schema(execution, scopes, plan, params)
149        }
150        _ => uqa_sql::semantics::returning::dml_command_returning_schema(analysis, command, params),
151    }
152}