uqa_execution/schema/
shell_types.rs1use super::namespaces::{
10 relations::RelationCreationContext, NamespaceCatalogChanges, SchemaStatementWriter,
11};
12use crate::catalog::{
13 identity::{allocate_catalog_object_id, CatalogIdentityReservationContext},
14 shell_type::{self, ShellRegistryPublication},
15};
16use uqa_sql::{
17 catalog::{roles::memberships::role_is_superuser, shell_type::StoredShellType},
18 schema::constraint_metadata::{CatalogObjectAllocator, CatalogOidClass},
19 SQLError,
20};
21
22pub struct ShellCompletionContext<'a> {
24 pub publication: &'a dyn ShellRegistryPublication,
25 pub locks: &'a dyn crate::row_locks::shared_objects::SharedObjectLockSession,
26}
27
28impl ShellCompletionContext<'_> {
29 pub fn retain(
30 &self,
31 name: &str,
32 owner: uqa_sql::catalog::roles::RoleIdentity,
33 ) -> Result<Option<StoredShellType>, SQLError> {
34 use crate::row_locks::{shared_objects::SharedCatalogLock, RelationLockMode};
35 loop {
36 let Some(initial) = self.publication.shell_registry().get(name).cloned() else {
37 return Ok(None);
38 };
39 let guard = self.locks.acquire_shared_catalog(
40 SharedCatalogLock::Object {
41 class_id: CatalogOidClass::Type.class_id(),
42 oid: initial.oid,
43 },
44 RelationLockMode::AccessExclusive,
45 )?;
46 self.locks.refresh_shared_catalog()?;
47 let current = self.publication.shell_registry().get(name).cloned();
48 if let Some(current) = current.filter(|current| current.object_id == initial.object_id)
49 {
50 if current.owner != owner {
52 return Err(SQLError::Routine {
53 sqlstate: "42501".into(),
54 message: format!("must be owner of type {}", current.identity.name),
55 });
56 }
57 guard.retain();
58 return Ok(Some(current));
59 }
60 }
61 }
62
63 pub fn complete(&self, shell: Option<&StoredShellType>) -> Result<(), SQLError> {
65 let Some(shell) = shell else { return Ok(()) };
66 let before = self.publication.shell_registry().clone();
67 let mut registry = before.clone();
68 let name = shell.identity.qualified_name();
69 if registry
70 .get(&name)
71 .is_none_or(|current| current.object_id != shell.object_id)
72 {
73 return Err(SQLError::Routine {
74 sqlstate: "40001".into(),
75 message: format!("shell type `{name}` changed during completion"),
76 });
77 }
78 registry.remove(&name);
79 shell_type::publish(self.publication, &before, registry)
80 }
81}
82
83pub struct ShellTypeContext<'a> {
84 pub registries: super::types::arrays::UserTypeRegistries<'a>,
85 pub creation: RelationCreationContext<'a>,
86 pub identities: CatalogIdentityReservationContext<'a>,
87 pub writer: &'a dyn SchemaStatementWriter,
88 pub allocate_identity: fn() -> Result<[u8; 16], SQLError>,
89 pub publication: &'a dyn ShellRegistryPublication,
90 pub changes: &'a dyn NamespaceCatalogChanges,
91}
92
93pub fn create_shell(context: &ShellTypeContext<'_>, name: &str) -> Result<(), SQLError> {
94 let owner = context.creation.bind_owner()?;
95 if !role_is_superuser(&context.creation.roles.role_definitions(), &owner) {
97 return Err(SQLError::Routine {
98 sqlstate: "42501".into(),
99 message: "must be superuser to create a base type".into(),
100 });
101 }
102 context.writer.prepare_writer()?;
103 let name = context.creation.persistent_name(name)?;
104 let requested =
105 uqa_core::RelationIdentity::from_legacy_name(&name).map_err(SQLError::Internal)?;
106 super::types::arrays::displace_array_type(&context.creation, context.registries, &requested)?;
107 let identity = context.creation.reserve_type_name(&name)?;
108 let object_id = (context.allocate_identity)()?;
109 let oid = context
110 .identities
111 .allocator(allocate_catalog_object_id)
112 .allocate_catalog_oid(CatalogOidClass::Type, &object_id)
113 .map_err(|error| {
114 uqa_sql::catalog::errors::storage_error("shell catalog identity", &error)
115 })?;
116 let oid =
117 u32::try_from(oid).map_err(|_| SQLError::Internal(format!("invalid shell OID {oid}")))?;
118 context.creation.retain_owner(&owner)?;
119 let before = context.publication.shell_registry().clone();
120 let mut registry = before.clone();
121 registry.insert(
122 identity.qualified_name(),
123 StoredShellType {
124 object_id,
125 oid,
126 identity,
127 owner: owner.identity(),
128 usage_acl: None,
129 },
130 );
131 shell_type::publish(context.publication, &before, registry)?;
132 context.changes.catalog_registry_changed();
133 Ok(())
134}