1macro_rules! virtual_relations {
10 ($($variant:ident => ($schema:literal, $name:literal, $oid:expr)),* $(,)?) => {
11 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
12 pub enum VirtualRelation { $($variant),* }
13
14 impl VirtualRelation {
15 pub const ALL: &'static [Self] = &[$(Self::$variant),*];
16
17 pub const fn namespace(self) -> &'static str {
18 match self { $(Self::$variant => $schema),* }
19 }
20
21 pub const fn name(self) -> &'static str {
22 match self { $(Self::$variant => $name),* }
23 }
24
25 pub fn oid(self) -> i64 {
26 match self { $(Self::$variant => $oid),* }
27 }
28
29 pub fn at(schema: &str, name: &str) -> Option<Self> {
31 match (schema, name) { $(($schema, $name) => Some(Self::$variant),)* _ => None }
32 }
33 }
34 };
35}
36
37virtual_relations! {
38 InformationSchemaCatalogName => ("information_schema", "information_schema_catalog_name", 13313),
39 InformationSchemata => ("information_schema", "schemata", 13467),
40 InformationTables => ("information_schema", "tables", 13510),
41 InformationColumns => ("information_schema", "columns", 13381),
42 InformationColumnPrivileges => ("information_schema", "column_privileges", 13371),
43 InformationRoleColumnGrants => ("information_schema", "role_column_grants", 13429),
44 InformationViews => ("information_schema", "views", 13568),
45 InformationRoutines => ("information_schema", "routines", 13462),
46 InformationSequences => ("information_schema", "sequences", 13471),
47 InformationTableConstraints => ("information_schema", "table_constraints", 13496),
48 InformationKeyColumnUsage => ("information_schema", "key_column_usage", 13414),
49 PgNamespace => ("pg_catalog", "pg_namespace", 2615),
50 PgClass => ("pg_catalog", "pg_class", 1259),
51 PgInherits => ("pg_catalog", "pg_inherits", 2611),
52 PgPartitionedTable => ("pg_catalog", "pg_partitioned_table", 3350),
53 PgAttribute => ("pg_catalog", "pg_attribute", 1249),
54 PgAttrdef => ("pg_catalog", "pg_attrdef", 2604),
55 PgConstraint => ("pg_catalog", "pg_constraint", 2606),
56 PgIndex => ("pg_catalog", "pg_index", 2610),
57 PgTrigger => ("pg_catalog", "pg_trigger", 2620),
58 PgRewrite => ("pg_catalog", "pg_rewrite", 2618),
59 PgRules => ("pg_catalog", "pg_rules", 12023),
60 PgTables => ("pg_catalog", "pg_tables", 12033),
61 PgViews => ("pg_catalog", "pg_views", 12028),
62 PgIndexes => ("pg_catalog", "pg_indexes", 12043),
63 PgType => ("pg_catalog", "pg_type", 1247),
64 PgRange => ("pg_catalog", "pg_range", 3541),
65 PgProc => ("pg_catalog", "pg_proc", 1255),
66 PgDatabase => ("pg_catalog", "pg_database", 1262),
67 PgAuthid => ("pg_catalog", "pg_authid", 1260),
68 PgAuthMembers => ("pg_catalog", "pg_auth_members", 1261),
69 PgRoles => ("pg_catalog", "pg_roles", 12000),
70 PgUser => ("pg_catalog", "pg_user", 12014),
71 PgSettings => ("pg_catalog", "pg_settings", 12104),
72 PgPreparedStatements => ("pg_catalog", "pg_prepared_statements", 12095),
73 PgCursors => ("pg_catalog", "pg_cursors", 12077),
74 PgDescription => ("pg_catalog", "pg_description", 2609),
75 PgMatviews => ("pg_catalog", "pg_matviews", 12038),
76 PgSequences => ("pg_catalog", "pg_sequences", 12048),
77 AgGraph => ("ag_catalog", "ag_graph", super::oids::relation_oid("relation", "ag_catalog", "ag_graph")),
78 AgLabel => ("ag_catalog", "ag_label", super::oids::relation_oid("relation", "ag_catalog", "ag_label")),
79}
80
81impl VirtualRelation {
82 pub fn qualified_name(self) -> String {
83 format!("{}.{}", self.namespace(), self.name())
84 }
85
86 pub fn from_qualified_name(name: &str) -> Option<Self> {
87 let (schema, local) = uqa_core::RelationIdentity::parse_reference(name).ok()?;
88 Self::at(schema.as_deref()?, &local)
89 }
90
91 pub const fn kind(self) -> &'static str {
92 if self.accepts_row_lock() {
93 "table"
94 } else {
95 "view"
96 }
97 }
98}
99
100pub fn resolve_virtual_relation(search_path: &[String], name: &str) -> Option<VirtualRelation> {
102 let names = crate::parse_regobject_name(name)?;
103 match names.as_slice() {
104 [schema, name] => VirtualRelation::at(schema, name),
105 [name] => {
106 if !search_path.iter().any(|schema| schema == "pg_catalog") {
107 if let Some(relation) = VirtualRelation::at("pg_catalog", name) {
108 return Some(relation);
109 }
110 }
111 search_path
112 .iter()
113 .find_map(|schema| VirtualRelation::at(schema, name))
114 }
115 _ => None,
116 }
117}