Skip to main content

uqa_sql/catalog/security/
grants.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Shared namespace binding for relation privilege declarations.
8use crate::SQLError;
9pub trait GrantNamespace {
10    fn temporary_schema_name(&self) -> String;
11    fn temporary_namespace_allocated(&self) -> bool;
12    fn has_namespace(&self, name: &str) -> Result<bool, String>;
13}
14pub fn bind_grant_schemas(
15    namespace: &dyn GrantNamespace,
16    schemas: &[String],
17) -> Result<Vec<String>, SQLError> {
18    let temporary_schema = namespace.temporary_schema_name();
19    let mut resolved_schemas = Vec::with_capacity(schemas.len());
20    for schema in schemas {
21        let resolved = if schema == "pg_temp" {
22            temporary_schema.clone()
23        } else {
24            schema.clone()
25        };
26        let exists = if resolved == temporary_schema {
27            namespace.temporary_namespace_allocated()
28        } else {
29            namespace.has_namespace(&resolved).map_err(|error| {
30                SQLError::Internal(format!("resolve schema `{schema}`: {error}"))
31            })?
32        };
33        if !exists {
34            return Err(SQLError::Routine {
35                sqlstate: "3F000".into(),
36                message: format!("schema \"{schema}\" does not exist"),
37            });
38        }
39        if !resolved_schemas.contains(&resolved) {
40            resolved_schemas.push(resolved);
41        }
42    }
43    Ok(resolved_schemas)
44}