uqa_execution/mutation/rules/returning/
projection.rs1use crate::{
10 mutation::returning::{
11 build_returning_value_row, DmlReturningShape, ReturningDestination,
12 ReturningExecutionContext, ReturningValueProjectionRow,
13 },
14 OwnedPhysicalRow,
15};
16use uqa_core::Value;
17use uqa_sql::SQLError;
18
19pub trait RuleReturningProjection {
20 fn project(
21 &self,
22 values: &[Value],
23 width: usize,
24 source: Option<&OwnedPhysicalRow>,
25 ) -> Result<OwnedPhysicalRow, SQLError>;
26
27 fn destination(&self) -> Option<ReturningDestination<'_>>;
28}
29
30pub struct RuleOutputProjection<'a, S: Clone + 'static> {
31 pub context: ReturningExecutionContext<'a, S>,
32 pub shape: DmlReturningShape<'a, S>,
33}
34
35impl<'a, S: Clone + 'static> RuleOutputProjection<'a, S> {
36 pub fn insert(
37 context: ReturningExecutionContext<'a, S>,
38 plan: &'a uqa_sql::plan::InsertPlan,
39 params: &'a [uqa_sql::SQLParam],
40 ctes: &'a crate::query::CteScope<S>,
41 ) -> Self {
42 Self {
43 context,
44 shape: DmlReturningShape {
45 table: &plan.table,
46 target_qualifier: &plan.target_qualifier,
47 aliases: &plan.returning_aliases,
48 returning: &plan.returning,
49 params,
50 ctes,
51 supplemental_schema: None,
52 },
53 }
54 }
55
56 pub fn update(
57 context: ReturningExecutionContext<'a, S>,
58 plan: &'a uqa_sql::plan::UpdatePlan,
59 params: &'a [uqa_sql::SQLParam],
60 ctes: &'a crate::query::CteScope<S>,
61 ) -> Self {
62 Self {
63 context,
64 shape: DmlReturningShape {
65 table: &plan.table,
66 target_qualifier: &plan.target_qualifier,
67 aliases: &plan.returning_aliases,
68 returning: &plan.returning,
69 params,
70 ctes,
71 supplemental_schema: None,
72 },
73 }
74 }
75
76 pub fn delete(
77 context: ReturningExecutionContext<'a, S>,
78 plan: &'a uqa_sql::plan::DeletePlan,
79 params: &'a [uqa_sql::SQLParam],
80 ctes: &'a crate::query::CteScope<S>,
81 ) -> Self {
82 Self {
83 context,
84 shape: DmlReturningShape {
85 table: &plan.table,
86 target_qualifier: &plan.target_qualifier,
87 aliases: &plan.returning_aliases,
88 returning: &plan.returning,
89 params,
90 ctes,
91 supplemental_schema: None,
92 },
93 }
94 }
95 pub fn with_table(mut self, table: &'a str) -> Self {
96 self.shape.table = table;
97 self
98 }
99}
100
101impl<S: Clone + 'static> RuleReturningProjection for RuleOutputProjection<'_, S> {
102 fn project(
103 &self,
104 values: &[Value],
105 width: usize,
106 source: Option<&OwnedPhysicalRow>,
107 ) -> Result<OwnedPhysicalRow, SQLError> {
108 build_returning_value_row(
109 self.context,
110 ReturningValueProjectionRow {
111 table: self.shape.table,
112 target_qualifier: self.shape.target_qualifier,
113 current: &values[..width],
114 old: Some(&values[width..width * 2]),
115 new: Some(&values[width * 2..width * 3]),
116 aliases: self.shape.aliases,
117 context: source,
118 },
119 self.shape.returning,
120 self.shape.params,
121 self.shape.ctes,
122 )
123 }
124
125 fn destination(&self) -> Option<ReturningDestination<'_>> {
126 self.context.destination
127 }
128}