Skip to main content

uqa_sql/catalog/dependencies/
address.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! A catalog object as `PostgreSQL`'s `ObjectAddress` names it.
8
9use std::cmp::Ordering;
10
11/// `pg_class`: relations, and with a column number, their columns.
12pub const RELATION_CLASS: u32 = 1259;
13/// `pg_type`.
14pub const TYPE_CLASS: u32 = 1247;
15/// `pg_proc`.
16pub const PROCEDURE_CLASS: u32 = 1255;
17pub const OPCLASS_CLASS: u32 = 2616;
18/// `pg_constraint`.
19pub const CONSTRAINT_CLASS: u32 = 2606;
20/// `pg_attrdef`: column defaults and generation expressions.
21pub const ATTRIBUTE_DEFAULT_CLASS: u32 = 2604;
22/// `pg_rewrite`.
23pub const REWRITE_CLASS: u32 = 2618;
24/// `pg_trigger`.
25pub const TRIGGER_CLASS: u32 = 2620;
26/// `pg_namespace`.
27pub const NAMESPACE_CLASS: u32 = 2615;
28/// `pg_collation`.
29pub const COLLATION_CLASS: u32 = 3456;
30/// `pg_language`.
31pub const LANGUAGE_CLASS: u32 = 2612;
32/// `pg_extension`.
33pub const EXTENSION_CLASS: u32 = 3079;
34/// `pg_authid`: roles, which every database shares.
35pub const ROLE_CLASS: u32 = 1260;
36/// `pg_auth_members`: role memberships, which every database shares.
37pub const ROLE_MEMBERSHIP_CLASS: u32 = 1261;
38/// `pg_foreign_data_wrapper`.
39pub const FOREIGN_WRAPPER_CLASS: u32 = 2328;
40
41/// `pg_foreign_server`.
42pub const FOREIGN_SERVER_CLASS: u32 = 1417;
43/// `pg_database`, which every database shares.
44pub const DATABASE_CLASS: u32 = 1262;
45
46/// The catalog holding an object's row, the row's OID, and for a column of a relation its column number; zero names the whole object.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
48pub struct ObjectAddress {
49    pub class_id: u32,
50    pub object_id: u32,
51    pub sub_id: i32,
52}
53
54impl ObjectAddress {
55    #[must_use]
56    pub const fn whole(class_id: u32, object_id: u32) -> Self {
57        Self {
58            class_id,
59            object_id,
60            sub_id: 0,
61        }
62    }
63
64    /// A column of a relation.
65    #[must_use]
66    pub const fn column(relation: u32, column_number: i32) -> Self {
67        Self {
68            class_id: RELATION_CLASS,
69            object_id: relation,
70            sub_id: column_number,
71        }
72    }
73
74    /// The whole object this address, or one of its columns, belongs to.
75    #[must_use]
76    pub const fn whole_object(self) -> Self {
77        Self::whole(self.class_id, self.object_id)
78    }
79
80    pub(super) const fn same_object(self, other: Self) -> bool {
81        self.class_id == other.class_id && self.object_id == other.object_id
82    }
83
84    /// `object_address_comparator`: newer objects, by descending OID, come first, then catalog and column in ascending order with the whole object before its columns.
85    pub(super) fn deletion_order(&self, other: &Self) -> Ordering {
86        other
87            .object_id
88            .cmp(&self.object_id)
89            .then(self.class_id.cmp(&other.class_id))
90            .then(
91                self.sub_id
92                    .cast_unsigned()
93                    .cmp(&other.sub_id.cast_unsigned()),
94            )
95    }
96}