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