Skip to main content

no_mistakes_postgres_engine/catalog/
search_path.rs

1//! Ordered search-path resolution from explicitly requested catalog inventory.
2use super::SchemaCatalog;
3use no_mistakes_postgres_sql::decoded_parts;
4
5pub use no_mistakes_postgres_sql::SearchPathResolution;
6
7impl no_mistakes_postgres_sql::CatalogLifecycleLookup for SchemaCatalog {
8    fn current_database(&self) -> Option<&str> {
9        SchemaCatalog::current_database(self)
10    }
11
12    fn resolve_before_temp(&self, earlier: &[String], name: &str) -> SearchPathResolution {
13        SchemaCatalog::resolve_before_temp(self, earlier, name)
14    }
15
16    fn scalar_function_names(&self) -> std::sync::Arc<std::collections::BTreeSet<Vec<String>>> {
17        SchemaCatalog::scalar_function_names(self)
18    }
19}
20
21impl SchemaCatalog {
22    pub fn resolve_before_temp(&self, earlier: &[String], name: &str) -> SearchPathResolution {
23        let bare = decoded_parts(name).last().cloned().unwrap_or_default();
24        for schema in earlier {
25            if schema == "$user" {
26                return SearchPathResolution::Unknown;
27            }
28            let Some(Some(relations)) = self.search_path_evidence.get(schema) else {
29                return SearchPathResolution::Unknown;
30            };
31            if relations.contains(&bare) {
32                return SearchPathResolution::Physical(schema.clone());
33            }
34        }
35        SearchPathResolution::Temporary
36    }
37    /// Whether explicit evidence proves an earlier search-path entry or pg_temp hides the
38    /// selected catalog relation. Unknown entries preserve the conservative catalog check.
39    pub fn hides_selected_relation(&self, earlier: &[String], name: &str) -> bool {
40        let bare = super::decoded_parts(name)
41            .last()
42            .cloned()
43            .unwrap_or_default();
44        for schema in earlier {
45            if schema == "$user" {
46                return false;
47            }
48            let Some(relations) = self.search_path_evidence.get(schema) else {
49                return false;
50            };
51            if let Some(relations) = relations {
52                if relations.contains(&bare) {
53                    return self.schema.as_deref() != Some(schema) || self.relation(name).is_none();
54                }
55            }
56        }
57        true
58    }
59}