Skip to main content

uqa_sql/ast/
enums.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! User-defined enum declarations and their bound type references.
8
9use serde::{Deserialize, Serialize};
10use uqa_core::{
11    memory::{Produced, ProductionControl},
12    ValueRetentionError,
13};
14
15/// `CREATE TYPE name AS ENUM (...)` with labels in declaration order.
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct CreateEnum {
18    /// Rendered qualified type name, resolved against the creation namespace during execution.
19    pub name: String,
20    pub labels: Vec<String>,
21}
22
23/// `ALTER TYPE name ADD VALUE ...` or `ALTER TYPE name RENAME VALUE ...`.
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct AlterEnum {
26    pub name: String,
27    pub action: AlterEnumAction,
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub enum AlterEnumAction {
32    AddValue {
33        label: String,
34        if_not_exists: bool,
35        neighbor: Option<EnumNeighbor>,
36    },
37    RenameValue {
38        old: String,
39        new: String,
40    },
41}
42
43/// The existing label that a new label is placed before or after.
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct EnumNeighbor {
46    pub label: String,
47    pub after: bool,
48}
49
50/// A user-defined enum type bound by catalog identity. The schema and name are the catalog's current names; equality compares the type identity only.
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct EnumTypeReference {
53    pub schema: String,
54    pub name: String,
55    pub oid: u32,
56    pub array_oid: u32,
57}
58
59impl PartialEq for EnumTypeReference {
60    fn eq(&self, other: &Self) -> bool {
61        self.oid == other.oid
62    }
63}
64
65impl Eq for EnumTypeReference {}
66
67impl EnumTypeReference {
68    /// Copy the owned catalog names under the production reservation; the identities are inline.
69    pub fn clone_with_control(
70        &self,
71        control: &ProductionControl<'_>,
72    ) -> Result<Produced<Self>, ValueRetentionError> {
73        let schema = control.copy_text(&self.schema)?;
74        let name = control.copy_text(&self.name)?;
75        let (schema, schema_memory) = schema.into_parts();
76        let (name, name_memory) = name.into_parts();
77        control.finish(
78            Self {
79                schema,
80                name,
81                oid: self.oid,
82                array_oid: self.array_oid,
83            },
84            control.combine(schema_memory, name_memory),
85        )
86    }
87}