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