uqa_execution/mutation/rules/
context.rs1use crate::{query::runtime::QueryMemorySettings, statement::plan_executor::StatementOutput};
9use uqa_sql::catalog::roles::RoleReference;
10use uqa_sql::{
11 assignment::AssignmentContext,
12 semantics::{returning::ReturningAnalysisContext, rules::analysis::RuleAnalysisContext},
13 SQLError,
14};
15pub trait RuleSecurity {
16 fn privilege_subject(&self, table: &str) -> Result<RoleReference, SQLError>;
17
18 fn privilege_subject_with_control(
19 &self,
20 table: &str,
21 control: &uqa_core::memory::ProductionControl<'_>,
22 ) -> Result<uqa_core::memory::Produced<RoleReference>, SQLError> {
23 control.check()?;
24 self.privilege_subject(table)?.clone_with_control(control)
25 }
26}
27pub trait RuleStatements<S: Clone + 'static> {
28 fn read_control(&self) -> Result<uqa_storage::read_control::StorageReadControl, SQLError>;
29 fn execute(
30 &self,
31 action: uqa_sql::semantics::rules::binding::RuleAction,
32 params: &[uqa_sql::SQLParam],
33 inherited_ctes: Option<&crate::query::CteScope<S>>,
34 privilege_subject: &RoleReference,
35 consumer: Option<std::rc::Rc<dyn crate::query::consumer::QueryRowConsumer + '_>>,
36 ) -> Result<StatementOutput, SQLError>;
37}
38#[derive(Clone)]
39pub struct RuleContext<'a, S: Clone + 'static> {
40 pub memory: &'a dyn QueryMemorySettings,
41 pub cancellation: &'a uqa_core::CancellationToken,
42 pub analysis: RuleAnalysisContext<'a>,
43 pub binding: uqa_sql::binding::stored_routines::analysis::CatalogRoutineAnalysisContext<'a>,
44 pub security: &'a dyn RuleSecurity,
45 pub statements: &'a dyn RuleStatements<S>,
46 pub returning: ReturningAnalysisContext<'a>,
47 pub assignment: &'a dyn AssignmentContext,
48 pub projection: Option<&'a dyn super::RuleReturningProjection>,
49 pub volatility: &'a dyn uqa_sql::semantics::volatility::VolatilityCatalog,
50}
51
52impl<S: Clone + 'static> Copy for RuleContext<'_, S> {}
53
54impl<'a, S: Clone + 'static> RuleContext<'a, S> {
55 pub fn with_projection<'b>(
56 self,
57 projection: &'b dyn super::RuleReturningProjection,
58 ) -> RuleContext<'b, S>
59 where
60 'a: 'b,
61 {
62 RuleContext {
63 projection: Some(projection),
64 ..self
65 }
66 }
67}
68
69#[derive(Clone)]
70pub struct ViewRuleContext<'a, S: Clone + 'static> {
71 pub rules: RuleContext<'a, S>,
72 pub views: uqa_sql::semantics::view_rewrite::context::ViewRewriteContext<'a>,
73}
74impl<S: Clone + 'static> Copy for ViewRuleContext<'_, S> {}
75
76impl<S: Clone + 'static> RuleContext<'_, S> {
77 pub(crate) fn bind_insert_queries(
78 &self,
79 insert: &uqa_sql::plan::InsertPlan,
80 subject: &RoleReference,
81 ) -> Result<uqa_sql::semantics::rules::binding::OwnedInsertRuleQueries, SQLError> {
82 let retained = self.statements.read_control()?;
83 let control = uqa_core::memory::ProductionControl::new(
84 retained.memory(),
85 retained.cancellation(),
86 self.cancellation,
87 );
88 uqa_sql::semantics::rules::binding::OwnedInsertRuleQueries::bind(
89 self.analysis,
90 insert,
91 &|table| self.security.privilege_subject(table),
92 subject,
93 &control,
94 )
95 }
96
97 pub(super) fn returning_columns(
98 &self,
99 table: &str,
100 ) -> Result<uqa_core::memory::Produced<Vec<uqa_sql::ast::ColumnDef>>, SQLError> {
101 let retained = self.statements.read_control()?;
102 let control = uqa_core::memory::ProductionControl::new(
103 retained.memory(),
104 retained.cancellation(),
105 self.cancellation,
106 );
107 uqa_sql::semantics::rules::analysis::rule_returning_columns_with_control(
108 self.analysis,
109 table,
110 &control,
111 )
112 }
113
114 pub(crate) fn capture_rules(
115 &self,
116 table: &str,
117 event: uqa_sql::ast::RuleEvent,
118 ) -> Result<uqa_sql::catalog::events::selection::CapturedRules, SQLError> {
119 let retained = self.statements.read_control()?;
120 let control = uqa_core::memory::ProductionControl::new(
121 retained.memory(),
122 retained.cancellation(),
123 self.cancellation,
124 );
125 self.analysis
126 .rules
127 .capture_rules_with_control(table, event, &control)
128 }
129
130 pub(super) fn returning_request(
131 &self,
132 returning: &[uqa_sql::plan::ProjectionPlan],
133 aliases: &uqa_sql::ast::ReturningAliases,
134 subqueries: &[uqa_sql::plan::QueryPlan],
135 ) -> Result<super::RuleReturningRequest, SQLError> {
136 let retained = self.statements.read_control()?;
137 let control = uqa_core::memory::ProductionControl::new(
138 retained.memory(),
139 retained.cancellation(),
140 self.cancellation,
141 );
142 super::RuleReturningRequest::from_plan_with_control(
143 returning, aliases, subqueries, &control,
144 )
145 }
146
147 pub(super) fn execute_bound_action(
148 &self,
149 statement: &uqa_sql::semantics::rules::action_binding::BoundRuleStatement,
150 input: Option<uqa_sql::semantics::rules::binding::OwnedRuleQueryInput>,
151 params: &[uqa_sql::SQLParam],
152 scope: &crate::query::CteScope<S>,
153 subject: &RoleReference,
154 consumer: Option<std::rc::Rc<dyn crate::query::consumer::QueryRowConsumer + '_>>,
155 ) -> Result<StatementOutput, SQLError> {
156 let _stored =
157 uqa_sql::semantics::sets::placement_scope::SetPlacementScope::stored_definition();
158 let retained = self.statements.read_control()?;
159 let control = uqa_core::memory::ProductionControl::new(
160 retained.memory(),
161 retained.cancellation(),
162 self.cancellation,
163 );
164 self.statements.execute(
165 uqa_sql::semantics::rules::binding::RuleAction::bind_with_control(
166 self.analysis,
167 self.returning.aggregates,
168 statement,
169 input,
170 &control,
171 )?,
172 params,
173 Some(scope),
174 subject,
175 consumer,
176 )
177 }
178
179 pub(super) fn source_schema(
180 &self,
181 input: uqa_sql::semantics::rules::binding::RuleMutation<'_>,
182 params: &[uqa_sql::SQLParam],
183 scope: &crate::query::CteScope<S>,
184 ) -> Result<Option<crate::RowSchema>, SQLError> {
185 let retained = self.statements.read_control()?;
186 let control = uqa_core::memory::ProductionControl::new(
187 retained.memory(),
188 retained.cancellation(),
189 self.cancellation,
190 );
191 let binding = crate::query::binding::binding_context_with_control(scope, &control)?;
192 input
193 .source()
194 .map(|source| {
195 uqa_sql::binding::production::analyze_source_plan_schema_with_control(
196 self.returning.routines,
197 source,
198 params,
199 binding,
200 None,
201 &control,
202 )
203 })
204 .transpose()
205 }
206
207 pub(super) fn mutation_input_schemas(
208 &self,
209 input: uqa_sql::semantics::rules::binding::RuleMutation<'_>,
210 params: &[uqa_sql::SQLParam],
211 binding: &uqa_sql::binding::production::ControlledBindingContext<'_>,
212 control: &uqa_core::memory::ProductionControl<'_>,
213 ) -> Result<(crate::RowSchema, Option<crate::RowSchema>), SQLError> {
214 let analyze = |source: &uqa_sql::plan::SourcePlan| {
215 uqa_sql::binding::production::analyze_source_plan_schema_with_control(
216 self.returning.routines,
217 source,
218 params,
219 binding.copy_with_control(control)?,
220 None,
221 control,
222 )
223 };
224 Ok((
225 analyze(&*input.target_source_with_control(control)?)?,
226 input.source().map(analyze).transpose()?,
227 ))
228 }
229}