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}
18pub trait RuleStatements<S: Clone + 'static> {
19 fn execute(
20 &self,
21 action: uqa_sql::semantics::rules::binding::RuleAction,
22 params: &[uqa_sql::SQLParam],
23 inherited_ctes: Option<&crate::query::CteScope<S>>,
24 privilege_subject: &RoleReference,
25 consumer: Option<std::rc::Rc<dyn crate::query::consumer::QueryRowConsumer + '_>>,
26 ) -> Result<StatementOutput, SQLError>;
27}
28#[derive(Clone)]
29pub struct RuleContext<'a, S: Clone + 'static> {
30 pub memory: &'a dyn QueryMemorySettings,
31 pub cancellation: &'a uqa_core::CancellationToken,
32 pub analysis: RuleAnalysisContext<'a>,
33 pub binding: uqa_sql::binding::stored_routines::analysis::CatalogRoutineAnalysisContext<'a>,
34 pub security: &'a dyn RuleSecurity,
35 pub statements: &'a dyn RuleStatements<S>,
36 pub returning: ReturningAnalysisContext<'a>,
37 pub assignment: &'a dyn AssignmentContext,
38 pub projection: Option<&'a dyn super::RuleReturningProjection>,
39 pub volatility: &'a dyn uqa_sql::semantics::volatility::VolatilityCatalog,
40}
41
42impl<S: Clone + 'static> Copy for RuleContext<'_, S> {}
43
44impl<'a, S: Clone + 'static> RuleContext<'a, S> {
45 pub fn with_projection<'b>(
46 self,
47 projection: &'b dyn super::RuleReturningProjection,
48 ) -> RuleContext<'b, S>
49 where
50 'a: 'b,
51 {
52 RuleContext {
53 projection: Some(projection),
54 ..self
55 }
56 }
57}
58
59#[derive(Clone)]
60pub struct ViewRuleContext<'a, S: Clone + 'static> {
61 pub rules: RuleContext<'a, S>,
62 pub views: uqa_sql::semantics::view_rewrite::context::ViewRewriteContext<'a>,
63}
64impl<S: Clone + 'static> Copy for ViewRuleContext<'_, S> {}
65
66impl<S: Clone + 'static> RuleContext<'_, S> {
67 pub(super) fn execute_action(
68 &self,
69 statement: uqa_sql::ast::Statement,
70 input: Option<uqa_sql::semantics::rules::binding::RuleQueryInput>,
71 params: &[uqa_sql::SQLParam],
72 scope: &crate::query::CteScope<S>,
73 subject: &RoleReference,
74 consumer: Option<std::rc::Rc<dyn crate::query::consumer::QueryRowConsumer + '_>>,
75 ) -> Result<StatementOutput, SQLError> {
76 let _stored =
77 uqa_sql::semantics::sets::placement_scope::SetPlacementScope::stored_definition();
78 self.statements.execute(
79 uqa_sql::semantics::rules::binding::RuleAction::bind(
80 self.analysis,
81 self.returning.aggregates,
82 statement,
83 input,
84 )?,
85 params,
86 Some(scope),
87 subject,
88 consumer,
89 )
90 }
91
92 pub(super) fn source_schema(
93 &self,
94 input: uqa_sql::semantics::rules::binding::RuleMutation<'_>,
95 params: &[uqa_sql::SQLParam],
96 scope: &crate::query::CteScope<S>,
97 ) -> Result<Option<crate::RowSchema>, SQLError> {
98 let binding = crate::query::binding::binding_context(scope)?;
99 input
100 .source()
101 .map(|source| {
102 uqa_sql::binding::analyze_source_plan_schema(
103 self.returning.routines,
104 source,
105 params,
106 &binding,
107 None,
108 )
109 })
110 .transpose()
111 }
112
113 pub(super) fn mutation_input_schemas(
114 &self,
115 input: uqa_sql::semantics::rules::binding::RuleMutation<'_>,
116 params: &[uqa_sql::SQLParam],
117 binding: &uqa_sql::binding::BindingContext<'_>,
118 ) -> Result<(crate::RowSchema, Option<crate::RowSchema>), SQLError> {
119 let analyze = |source: &uqa_sql::plan::SourcePlan| {
120 uqa_sql::binding::analyze_source_plan_schema(
121 self.returning.routines,
122 source,
123 params,
124 binding,
125 None,
126 )
127 };
128 Ok((
129 analyze(&input.target_source())?,
130 input.source().map(analyze).transpose()?,
131 ))
132 }
133}