Skip to main content

uqa_sql/semantics/
parameters.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Configuration parameters: their definitions and values, and the lifetimes of SET, SET LOCAL and temporary function configuration over values the host retains.
8
9use std::collections::BTreeMap;
10
11pub mod arguments;
12pub mod catalog;
13pub mod custom;
14pub mod datestyle;
15pub mod definition;
16pub mod explain;
17pub mod identifier_list;
18pub mod intervalstyle;
19pub mod setting;
20pub(crate) mod timezone;
21pub mod units;
22pub mod value;
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25pub enum ParameterAssignment {
26    Session,
27    Local,
28    Save,
29}
30
31#[derive(Clone, Copy, Debug)]
32pub struct ParameterScope(usize);
33
34/// Values remain owned by the host; SQL decides which assignment lifetime restores them.
35#[derive(Clone)]
36pub struct ParameterScopes<T> {
37    transaction: BTreeMap<String, T>,
38    functions: Vec<BTreeMap<String, T>>,
39}
40
41impl<T> Default for ParameterScopes<T> {
42    fn default() -> Self {
43        Self {
44            transaction: BTreeMap::new(),
45            functions: Vec::new(),
46        }
47    }
48}
49
50impl<T> ParameterScopes<T> {
51    /// A session assignment supersedes pending LOCAL and function configuration restoration for this parameter.
52    pub fn session_assignment(&mut self, name: &str) {
53        self.transaction.remove(name);
54        for saved in &mut self.functions {
55            saved.remove(name);
56        }
57    }
58
59    pub fn enter_function(&mut self) -> ParameterScope {
60        self.functions.push(BTreeMap::new());
61        ParameterScope(self.functions.len())
62    }
63
64    pub fn leave_function(&mut self, scope: ParameterScope) -> BTreeMap<String, T> {
65        assert_eq!(scope.0, self.functions.len(), "parameter scope order");
66        self.functions.pop().expect("active parameter scope")
67    }
68
69    /// Record a successful assignment. An out-of-transaction LOCAL returns its immediate restoration value.
70    pub fn assigned(
71        &mut self,
72        name: String,
73        previous: T,
74        action: ParameterAssignment,
75        in_transaction: bool,
76    ) -> Option<T> {
77        match action {
78            ParameterAssignment::Session => {
79                self.session_assignment(&name);
80            }
81            ParameterAssignment::Local => {
82                if self.functions.iter().any(|saved| saved.contains_key(&name)) {
83                    return None;
84                }
85                if !in_transaction {
86                    return Some(previous);
87                }
88                self.transaction.entry(name).or_insert(previous);
89            }
90            ParameterAssignment::Save => {
91                self.functions
92                    .last_mut()
93                    .expect("function configuration requires a parameter scope")
94                    .entry(name)
95                    .or_insert(previous);
96            }
97        }
98        None
99    }
100
101    pub fn finish_transaction(&mut self) -> BTreeMap<String, T> {
102        std::mem::take(&mut self.transaction)
103    }
104
105    /// RESET ALL overrides ordinary settings while leaving both authorization parameters alone.
106    pub fn reset_all(&mut self) {
107        fn authorization(name: &str) -> bool {
108            matches!(name, "role" | "session_authorization")
109        }
110        self.transaction.retain(|name, _| authorization(name));
111        for saved in &mut self.functions {
112            saved.retain(|name, _| authorization(name));
113        }
114    }
115}
116
117#[cfg(test)]
118mod tests;