Skip to main content

uqa_sql/ast/
routine_security.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Routine security, ownership, configuration, privileges, and role statements.
8
9use serde::{Deserialize, Serialize};
10
11use super::{
12    AclRoleSpecification, FunctionVolatility, RoleSpecification, RoutineColumnTypeReference,
13};
14
15/// `PARALLEL UNSAFE`, `PARALLEL RESTRICTED`, or `PARALLEL SAFE` routine metadata.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
17pub enum FunctionParallel {
18    #[default]
19    Unsafe,
20    Restricted,
21    Safe,
22}
23
24/// Execution identity and planner leakproofness attached to a routine definition.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
26pub struct RoutineSecurityAttributes {
27    /// `SECURITY DEFINER` when true, otherwise `SECURITY INVOKER`.
28    #[serde(default)]
29    pub security_definer: bool,
30    /// `LEAKPROOF` planner metadata.
31    #[serde(default)]
32    pub leakproof: bool,
33}
34
35/// One explicit `EXECUTE` ACL entry. `None` on `CreateFunction::execute_acl` retains `PostgreSQL`'s default public execution privilege.
36pub type RoutineAclEntry = super::ObjectAclEntry;
37
38/// Old parsed declarations used an empty owner string. This accepts only that unbound marker; stored authority is separately required to carry a valid identity.
39pub(super) fn deserialize_routine_owner<'de, D: serde::Deserializer<'de>>(
40    deserializer: D,
41) -> Result<Option<uqa_core::catalog_role::RoleIdentity>, D::Error> {
42    #[derive(Deserialize)]
43    #[serde(untagged)]
44    enum Owner {
45        Bound(Option<uqa_core::catalog_role::RoleIdentity>),
46        Unbound(String),
47    }
48    match Owner::deserialize(deserializer)? {
49        Owner::Bound(identity) => Ok(identity),
50        Owner::Unbound(name) if name.is_empty() => Ok(None),
51        Owner::Unbound(_) => Err(serde::de::Error::custom(
52            "routine owner requires a role identity",
53        )),
54    }
55}
56
57/// Routine namespace selected by `ALTER FUNCTION`, `ALTER PROCEDURE`, or `ALTER ROUTINE`.
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
59pub enum AlterRoutineKind {
60    Function,
61    Procedure,
62    Routine,
63}
64
65/// `ALTER FUNCTION | PROCEDURE | ROUTINE name[(input_types)] ...` with an optional exact declared input identity.
66#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
67pub struct AlterRoutineStmt {
68    pub kind: AlterRoutineKind,
69    pub name: String,
70    #[serde(default, skip_serializing_if = "Option::is_none")]
71    pub arg_types: Option<Vec<String>>,
72    #[serde(default, skip_serializing_if = "Vec::is_empty")]
73    pub arg_type_references: Vec<Option<RoutineColumnTypeReference>>,
74    #[serde(default, skip_serializing_if = "Option::is_none")]
75    pub volatility: Option<FunctionVolatility>,
76    #[serde(default, skip_serializing_if = "Option::is_none")]
77    pub strict: Option<bool>,
78    #[serde(default, skip_serializing_if = "Option::is_none")]
79    pub security_definer: Option<bool>,
80    #[serde(default, skip_serializing_if = "Option::is_none")]
81    pub leakproof: Option<bool>,
82    #[serde(default, skip_serializing_if = "Option::is_none")]
83    pub parallel: Option<FunctionParallel>,
84    #[serde(default, skip_serializing_if = "Option::is_none")]
85    pub support: Option<String>,
86    #[serde(
87        default,
88        skip_serializing_if = "Option::is_none",
89        with = "super::routine_estimate"
90    )]
91    pub cost: Option<f32>,
92    #[serde(
93        default,
94        skip_serializing_if = "Option::is_none",
95        with = "super::routine_estimate"
96    )]
97    pub rows: Option<f32>,
98    #[serde(default, skip_serializing_if = "Vec::is_empty")]
99    pub config_actions: Vec<RoutineConfigAction>,
100    /// The actions in written order, which `AlterFunction` checks once it has found the routine.
101    #[serde(
102        default,
103        skip_serializing_if = "super::RoutineAttributeClauses::is_empty"
104    )]
105    pub attribute_clauses: super::RoutineAttributeClauses,
106}
107
108#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
109pub enum RoutineConfigAction {
110    Set { name: String, value: String },
111    FromCurrent { name: String },
112    Reset { name: String },
113    ResetAll,
114}
115
116#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
117pub struct AlterRoutineOwnerStmt {
118    pub kind: AlterRoutineKind,
119    pub name: String,
120    pub arg_types: Option<Vec<String>>,
121    pub arg_type_references: Vec<Option<RoutineColumnTypeReference>>,
122    pub new_owner: RoleSpecification,
123}
124
125/// `ALTER FUNCTION | PROCEDURE | ROUTINE name[(input_types)] RENAME TO new_name`.
126#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
127pub struct RenameRoutineStmt {
128    pub kind: AlterRoutineKind,
129    pub name: String,
130    #[serde(default, skip_serializing_if = "Option::is_none")]
131    pub arg_types: Option<Vec<String>>,
132    #[serde(default, skip_serializing_if = "Vec::is_empty")]
133    pub arg_type_references: Vec<Option<RoutineColumnTypeReference>>,
134    pub new_name: String,
135}
136
137#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
138pub struct GrantRoutineItem {
139    pub name: String,
140    pub arg_types: Option<Vec<String>>,
141}
142
143#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
144pub enum RoutineRevokeBehavior {
145    #[default]
146    Restrict,
147    Cascade,
148}
149
150/// A routine privilege clause is retained until target and recipient lookup finish.
151#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
152pub enum RoutinePrivilege {
153    Execute,
154    Unsupported(String),
155    ColumnsUnsupported,
156}
157
158#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
159pub struct GrantRoutineStmt {
160    pub kind: AlterRoutineKind,
161    pub is_grant: bool,
162    pub grant_option: bool,
163    pub grant_option_only: bool,
164    pub items: Vec<GrantRoutineItem>,
165    /// `ALL ... IN SCHEMA`, in written order. Legacy statements omit this and keep explicit `items`.
166    #[serde(default, skip_serializing_if = "Option::is_none")]
167    pub schemas: Option<Vec<String>>,
168    /// Empty means `ALL PRIVILEGES`, also the legacy representation of `EXECUTE`.
169    #[serde(default, skip_serializing_if = "Vec::is_empty")]
170    pub privileges: Vec<RoutinePrivilege>,
171    pub grantees: Vec<AclRoleSpecification>,
172    #[serde(default, skip_serializing_if = "Option::is_none")]
173    pub grantor: Option<RoleSpecification>,
174    #[serde(default)]
175    pub revoke_behavior: RoutineRevokeBehavior,
176}
177
178/// `PostgreSQL` 18 options stored on one role-membership grant.
179#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
180pub struct RoleMembershipOptions {
181    pub admin: Option<bool>,
182    pub inherit: Option<bool>,
183    pub set: Option<bool>,
184}
185
186/// `GRANT role TO member` or `REVOKE role FROM member`.
187#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
188pub struct GrantRoleStmt {
189    pub granted_roles: Vec<String>,
190    pub grantee_roles: Vec<RoleSpecification>,
191    pub is_grant: bool,
192    pub options: RoleMembershipOptions,
193    pub grantor: Option<RoleSpecification>,
194    pub cascade: bool,
195}
196
197#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
198pub enum RoleAttribute {
199    Superuser,
200    Inherit,
201    CreateRole,
202    CreateDb,
203    Login,
204    Replication,
205    BypassRls,
206}
207
208#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
209pub struct CreateRoleStmt {
210    pub name: String,
211    pub attributes: uqa_core::memory::OwnedSet<RoleAttribute>,
212    pub connection_limit: i32,
213    #[serde(default, skip_serializing_if = "Vec::is_empty")]
214    pub in_roles: Vec<RoleSpecification>,
215    #[serde(default, skip_serializing_if = "Vec::is_empty")]
216    pub role_members: Vec<RoleSpecification>,
217    #[serde(default, skip_serializing_if = "Vec::is_empty")]
218    pub admin_members: Vec<RoleSpecification>,
219}
220
221#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
222pub enum RoleMembershipAction {
223    Add,
224    Drop,
225}
226
227#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
228pub struct AlterRoleStmt {
229    pub name: RoleSpecification,
230    pub attributes: uqa_core::memory::OwnedMap<RoleAttribute, bool>,
231    pub connection_limit: Option<i32>,
232    pub membership_action: Option<RoleMembershipAction>,
233    pub members: Vec<RoleSpecification>,
234}
235
236#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
237pub struct RenameRoleStmt {
238    pub name: String,
239    pub new_name: String,
240}
241
242#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
243pub struct DropRoleStmt {
244    pub names: Vec<RoleSpecification>,
245    pub if_exists: bool,
246}