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;
17/// `pg_constraint`.
18pub const CONSTRAINT_CLASS: u32 = 2606;
19/// `pg_attrdef`: column defaults and generation expressions.
20pub const ATTRIBUTE_DEFAULT_CLASS: u32 = 2604;
21/// `pg_rewrite`.
22pub const REWRITE_CLASS: u32 = 2618;
23/// `pg_trigger`.
24pub const TRIGGER_CLASS: u32 = 2620;
25/// `pg_namespace`.
26pub const NAMESPACE_CLASS: u32 = 2615;
27/// `pg_language`.
28pub const LANGUAGE_CLASS: u32 = 2612;
29/// `pg_authid`: roles, which every database shares.
30pub const ROLE_CLASS: u32 = 1260;
31/// `pg_auth_members`: role memberships, which every database shares.
32pub const ROLE_MEMBERSHIP_CLASS: u32 = 1261;
33/// `pg_foreign_data_wrapper`.
34pub const FOREIGN_WRAPPER_CLASS: u32 = 2328;
35
36/// `pg_foreign_server`.
37pub const FOREIGN_SERVER_CLASS: u32 = 1417;
38/// `pg_database`, which every database shares.
39pub const DATABASE_CLASS: u32 = 1262;
40
41/// 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.
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
43pub struct ObjectAddress {
44    pub class_id: u32,
45    pub object_id: u32,
46    pub sub_id: i32,
47}
48
49impl ObjectAddress {
50    #[must_use]
51    pub const fn whole(class_id: u32, object_id: u32) -> Self {
52        Self {
53            class_id,
54            object_id,
55            sub_id: 0,
56        }
57    }
58
59    /// A column of a relation.
60    #[must_use]
61    pub const fn column(relation: u32, column_number: i32) -> Self {
62        Self {
63            class_id: RELATION_CLASS,
64            object_id: relation,
65            sub_id: column_number,
66        }
67    }
68
69    /// The whole object this address, or one of its columns, belongs to.
70    #[must_use]
71    pub const fn whole_object(self) -> Self {
72        Self::whole(self.class_id, self.object_id)
73    }
74
75    pub(super) const fn same_object(self, other: Self) -> bool {
76        self.class_id == other.class_id && self.object_id == other.object_id
77    }
78
79    /// `object_address_comparator`: newer objects, by descending OID, come first, then catalog and column in ascending order with the whole object before its columns.
80    pub(super) fn deletion_order(&self, other: &Self) -> Ordering {
81        other
82            .object_id
83            .cmp(&self.object_id)
84            .then(self.class_id.cmp(&other.class_id))
85            .then(
86                self.sub_id
87                    .cast_unsigned()
88                    .cmp(&other.sub_id.cast_unsigned()),
89            )
90    }
91}