Skip to main content

uqa_sql/schema/namespaces/
creation.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Resolve schema authorization before checking the invoking role's database privileges.
8use crate::{
9    ast::SchemaAuthorization,
10    catalog::roles::{guards::RoleCatalogGuards, require_role_exists, RoleReferenceNames},
11    SQLError,
12};
13pub struct SchemaCreationTarget {
14    pub name: String,
15    pub role_owner: String,
16}
17pub fn schema_creation_target(
18    names: &dyn RoleReferenceNames,
19    roles: &dyn RoleCatalogGuards,
20    current_user: &str,
21    name: Option<&str>,
22    authorization: Option<&SchemaAuthorization>,
23) -> Result<SchemaCreationTarget, SQLError> {
24    let role_owner = match authorization {
25        None | Some(SchemaAuthorization::CurrentUser) => current_user.to_string(),
26        Some(SchemaAuthorization::SessionUser) => names.session_user_name(),
27        Some(SchemaAuthorization::Role(role)) => role.clone(),
28    };
29    if authorization.is_some() {
30        require_role_exists(&roles.role_definitions(), &role_owner)?;
31    }
32    Ok(SchemaCreationTarget {
33        name: name.unwrap_or(&role_owner).to_string(),
34        role_owner,
35    })
36}
37pub fn validate_schema_creation_name(name: &str) -> Result<(), SQLError> {
38    if name.starts_with("pg_") {
39        return Err(SQLError::Routine {
40            sqlstate: "42939".into(),
41            message: format!(r#"unacceptable schema name "{name}""#),
42        });
43    }
44    Ok(())
45}
46
47#[cfg(test)]
48mod tests;