Skip to main content

uqa_sql/schema/sequences/
dependents.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Declared schema objects that depend on a sequence.
8
9#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
10pub enum SequenceSchemaDependent {
11    Default {
12        table: String,
13        column: String,
14        foreign: bool,
15    },
16    GeneratedColumn {
17        table: String,
18        column: String,
19        foreign: bool,
20    },
21    CheckConstraint {
22        table: String,
23        constraint: String,
24        foreign: bool,
25    },
26}
27
28impl SequenceSchemaDependent {
29    pub fn table(&self) -> &str {
30        match self {
31            Self::Default { table, .. }
32            | Self::GeneratedColumn { table, .. }
33            | Self::CheckConstraint { table, .. } => table,
34        }
35    }
36
37    pub fn is_column(&self, table_name: &str, column_name: &str) -> bool {
38        match self {
39            Self::Default { table, column, .. } | Self::GeneratedColumn { table, column, .. } => {
40                table == table_name && column == column_name
41            }
42            Self::CheckConstraint { .. } => false,
43        }
44    }
45
46    pub fn object_label(&self) -> String {
47        match self {
48            Self::Default {
49                table,
50                column,
51                foreign,
52            } => format!(
53                "default value for column {column} of {} {table}",
54                relation_kind(*foreign)
55            ),
56            Self::GeneratedColumn {
57                table,
58                column,
59                foreign,
60            } => format!("column {column} of {} {table}", relation_kind(*foreign)),
61            Self::CheckConstraint {
62                table,
63                constraint,
64                foreign,
65            } => format!(
66                "constraint {constraint} on {} {table}",
67                relation_kind(*foreign)
68            ),
69        }
70    }
71}
72
73fn relation_kind(foreign: bool) -> &'static str {
74    if foreign {
75        "foreign table"
76    } else {
77        "table"
78    }
79}