Skip to main content

uqa_sql/catalog/resolution/
production.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Session captures admit each owned namespace buffer before copying borrowed state.
8
9use super::{RelationLookupMode, RelationNameResolution, RoleReference};
10use crate::SQLError;
11use uqa_core::memory::{Produced, ProductionControl, ProductionVec};
12
13impl RelationNameResolution {
14    /// Capture already expanded search-path names and a separately admitted temporary name. Bound roles retain their immutable incarnation without copying its name.
15    pub fn from_inputs_with_control<'a>(
16        search_path: impl IntoIterator<Item = &'a str>,
17        temporary_schema: Produced<String>,
18        temporary_namespace_allocated: bool,
19        current_user: &RoleReference,
20        lookup_mode: RelationLookupMode,
21        control: &ProductionControl<'_>,
22    ) -> Result<Produced<Self>, SQLError> {
23        control.check()?;
24        let mut path = ProductionVec::new(*control);
25        for name in search_path {
26            path.push_produced(control.copy_text(name)?)?;
27        }
28        let role = match current_user {
29            RoleReference::Named(name) => {
30                let (name, memory) = control.copy_text(name)?.into_parts();
31                control.finish(RoleReference::Named(name), memory)?
32            }
33            RoleReference::Bound(binding) => control.finish(
34                RoleReference::Bound(binding.clone()),
35                control.empty_reservation(),
36            )?,
37        };
38        let (search_path, path_memory) = path.finish()?.into_parts();
39        let (temporary_schema, temporary_memory) = temporary_schema.into_parts();
40        let (current_user, role_memory) = role.into_parts();
41        Ok(control.finish(
42            Self {
43                search_path,
44                temporary_schema,
45                temporary_namespace_allocated,
46                current_user,
47                lookup_mode,
48            },
49            control.combine(control.combine(path_memory, temporary_memory), role_memory),
50        )?)
51    }
52
53    pub fn copy_with_control(
54        &self,
55        control: &ProductionControl<'_>,
56    ) -> Result<Produced<Self>, SQLError> {
57        Self::from_inputs_with_control(
58            self.search_path.iter().map(String::as_str),
59            control.copy_text(&self.temporary_schema)?,
60            self.temporary_namespace_allocated,
61            &self.current_user,
62            self.lookup_mode,
63            control,
64        )
65    }
66}
67
68#[cfg(test)]
69mod tests;