1pub(crate) type CteScope = uqa_execution::query::CteScope<crate::session::StatementReadSnapshot>;
10use crate::Engine;
11use uqa_core::memory::ProductionControl;
12use uqa_sql::catalog::roles::RoleReference;
13use uqa_sql::{catalog::resolution::RelationLookupMode, SQLError};
14
15pub(crate) fn new_for_current_routine(engine: &Engine) -> Result<CteScope, SQLError> {
16 let resources =
17 uqa_execution::query::relational::QueryExpressionFactory::production_resources(engine)?;
18 new_for_current_routine_with_control(engine, &resources.control())
19}
20
21pub(crate) fn new_for_statement(
22 engine: &Engine,
23 privilege_subject: Option<&RoleReference>,
24) -> Result<CteScope, SQLError> {
25 let retained = engine.query_retention_control()?;
26 let runtime = engine.query_runtime_view();
27 let control = ProductionControl::new(
28 retained.memory(),
29 retained.cancellation(),
30 runtime.cancellation,
31 );
32 new_for_statement_with_control(engine, privilege_subject, &control)
33}
34
35pub(crate) fn new_for_statement_with_control(
36 engine: &Engine,
37 privilege_subject: Option<&RoleReference>,
38 control: &ProductionControl<'_>,
39) -> Result<CteScope, SQLError> {
40 control.check()?;
41 let revision = engine.runtime.regtype_output_cache.revision();
42 let catalog = engine.catalog_read_view_with_control(control)?;
43 let resolution =
44 uqa_execution::catalog::services::CatalogSession::relation_name_resolution_with_control(
45 &engine.session_execution_view(),
46 control,
47 )?;
48 let scope = CteScope::with_catalog_with_control(catalog, resolution, control)?;
49 finish_statement_scope(engine, scope, privilege_subject, revision, control)
50}
51
52pub(crate) fn new_for_command(
53 engine: &Engine,
54 privilege_subject: Option<&RoleReference>,
55 relations_bound: bool,
56) -> Result<CteScope, SQLError> {
57 let mut scope = new_for_statement(engine, privilege_subject)?;
58 if relations_bound {
59 scope.set_relation_lookup_mode(RelationLookupMode::Bound)?;
60 }
61 Ok(scope)
62}
63
64pub(crate) fn new_for_catalog_binding(engine: &Engine) -> Result<CteScope, SQLError> {
65 let resources =
66 uqa_execution::query::relational::QueryExpressionFactory::production_resources(engine)?;
67 new_for_catalog_binding_with_control(engine, &resources.control())
68}
69
70impl
71 uqa_execution::mutation::command_scope::CommandScopeSource<
72 crate::session::StatementReadSnapshot,
73 > for Engine
74{
75 fn read_control(&self) -> Result<uqa_storage::read_control::StorageReadControl, SQLError> {
76 self.query_retention_control()
77 }
78 fn cancellation(&self) -> &uqa_core::CancellationToken {
79 self.query_runtime_view().cancellation
80 }
81 fn command_scope(
82 &self,
83 privilege_subject: Option<&RoleReference>,
84 relations_bound: bool,
85 ) -> Result<CteScope, SQLError> {
86 new_for_command(self, privilege_subject, relations_bound)
87 }
88}
89
90impl uqa_sql::binding::statements::StatementAnalysisScopes for Engine {
91 fn planning_control(&self) -> Result<uqa_core::memory::OwnedProductionControl, SQLError> {
92 let resources = self.query_retention_control()?;
93 Ok(ProductionControl::new(
94 resources.memory(),
95 resources.cancellation(),
96 &self.runtime.cancellation,
97 )
98 .capture())
99 }
100
101 fn with_controlled_scope(
102 &self,
103 control: &uqa_core::memory::ProductionControl<'_>,
104 analyze: uqa_sql::binding::statements::ControlledStatementAnalysisOperation<'_>,
105 ) -> Result<(), SQLError> {
106 let scope = new_for_current_routine_with_control(self, control)?;
107 analyze(uqa_execution::query::binding::binding_context_with_control(
108 &scope, control,
109 )?)
110 }
111
112 fn with_scope(
113 &self,
114 analyze: uqa_sql::binding::statements::StatementAnalysisOperation<'_>,
115 ) -> Result<(), SQLError> {
116 let scope = new_for_current_routine(self)?;
117 analyze(&scope)
118 }
119}
120
121pub(crate) fn new_for_current_routine_with_control(
122 engine: &Engine,
123 control: &ProductionControl<'_>,
124) -> Result<CteScope, SQLError> {
125 control.check()?;
126 let revision = engine.runtime.regtype_output_cache.revision();
127 let resolution =
128 uqa_execution::catalog::services::CatalogSession::relation_name_resolution_with_control(
129 &engine.session_execution_view(),
130 control,
131 )?;
132 let scope = CteScope::with_catalog_with_control(
133 engine.catalog_read_view_with_control(control)?,
134 resolution,
135 control,
136 )?;
137 finish_statement_scope(engine, scope, None, revision, control)
138}
139
140fn finish_statement_scope(
141 engine: &Engine,
142 mut scope: CteScope,
143 privilege_subject: Option<&RoleReference>,
144 revision: u64,
145 control: &ProductionControl<'_>,
146) -> Result<CteScope, SQLError> {
147 scope.set_privilege_subject_with_control(privilege_subject, control)?;
148 scope.set_catalog_revision(engine.session_id, revision);
149 uqa_execution::mutation::triggers::with_current_transition_relations(|relations| {
150 if let Some(relations) = relations {
151 for (name, rows) in relations {
152 scope.insert_shared_with_control(name, rows.clone(), control)?;
153 }
154 }
155 Ok::<_, SQLError>(())
156 })?;
157 if crate::roles::active_routine_reads_command_overlay() == Some(false) {
158 scope.set_reads_command_overlay(false);
159 }
160 Ok(scope)
161}
162
163pub(crate) fn new_for_catalog_binding_with_control(
165 engine: &Engine,
166 control: &ProductionControl<'_>,
167) -> Result<CteScope, SQLError> {
168 control.check()?;
169 let resolution =
170 uqa_execution::catalog::services::CatalogSession::relation_name_resolution_with_control(
171 &engine.session_execution_view(),
172 control,
173 )?;
174 let (mut resolution, memory) = resolution.into_parts();
175 resolution.set_lookup_mode(uqa_sql::catalog::resolution::RelationLookupMode::Bound);
176 let mut scope = CteScope::with_catalog_with_control(
177 engine.restored_catalog_read_view_with_control(control)?,
178 control.finish(resolution, memory)?,
179 control,
180 )?;
181 scope.set_catalog_revision(
182 engine.session_id,
183 engine.runtime.regtype_output_cache.revision(),
184 );
185 Ok(scope)
186}