Skip to main content

uqa_engine/capabilities/
query_scope.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Capture engine-owned session and routine state for a query execution scope.
8
9pub(crate) type CteScope = uqa_execution::query::CteScope<crate::session::StatementReadSnapshot>;
10use crate::Engine;
11use uqa_sql::catalog::roles::RoleReference;
12use uqa_sql::{catalog::resolution::RelationLookupMode, SQLError};
13
14pub(crate) fn new_for_current_routine(engine: &Engine) -> CteScope {
15    new_for_statement(engine, None)
16}
17
18pub(crate) fn new_for_statement(
19    engine: &Engine,
20    privilege_subject: Option<&RoleReference>,
21) -> CteScope {
22    let revision = engine.runtime.regtype_output_cache.revision();
23    let mut scope = CteScope::with_catalog(
24        engine.catalog_read_view(),
25        engine.session_execution_view().relation_name_resolution(),
26        privilege_subject.cloned(),
27    );
28    scope.set_catalog_revision(engine.session_id, revision);
29    scope
30        .rows
31        .extend(uqa_execution::mutation::triggers::current_transition_relations());
32    if crate::roles::active_routine_reads_command_overlay() == Some(false) {
33        scope.set_reads_command_overlay(false);
34    }
35    scope
36}
37
38pub(crate) fn new_for_command(
39    engine: &Engine,
40    privilege_subject: Option<&RoleReference>,
41    relations_bound: bool,
42) -> Result<CteScope, SQLError> {
43    let mut scope = new_for_statement(engine, privilege_subject);
44    if relations_bound {
45        scope.set_relation_lookup_mode(RelationLookupMode::Bound)?;
46    }
47    Ok(scope)
48}
49
50pub(crate) fn new_for_catalog_binding(engine: &Engine) -> CteScope {
51    let revision = engine.runtime.regtype_output_cache.revision();
52    let mut resolution = engine.session_execution_view().relation_name_resolution();
53    resolution.set_lookup_mode(RelationLookupMode::Bound);
54    let mut scope = CteScope::with_catalog(engine.restored_catalog_read_view(), resolution, None);
55    scope.set_catalog_revision(engine.session_id, revision);
56    scope
57}
58
59impl
60    uqa_execution::mutation::command_scope::CommandScopeSource<
61        crate::session::StatementReadSnapshot,
62    > for Engine
63{
64    fn command_scope(
65        &self,
66        privilege_subject: Option<&RoleReference>,
67        relations_bound: bool,
68    ) -> Result<CteScope, SQLError> {
69        new_for_command(self, privilege_subject, relations_bound)
70    }
71}
72
73impl uqa_sql::binding::statements::StatementAnalysisScopes for Engine {
74    fn with_scope(
75        &self,
76        analyze: uqa_sql::binding::statements::StatementAnalysisOperation<'_>,
77    ) -> Result<(), SQLError> {
78        let scope = new_for_current_routine(self);
79        analyze(&scope)
80    }
81}