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