use anyhow::Result;
use sqlx::postgres::types::Oid;
use super::exclusion::{Converted, Excluded, ExclusionReason, is_system_schema};
use super::oid_index::OidIndex;
use super::shared::{SharedCatalog, class};
use crate::catalog::id::DbObjectId;
use crate::catalog::schema::Schema;
#[allow(dead_code)]
pub fn load(shared: &SharedCatalog) -> Result<Vec<Schema>> {
Ok(load_with_exclusions(shared)?.log_and_take_objects("schema"))
}
pub fn load_with_exclusions(shared: &SharedCatalog) -> Result<Converted<Schema>> {
let mut converted = convert(shared)?;
let index = OidIndex::from_pairs(
class::PG_NAMESPACE,
converted.objects.iter().map(|(oid, schema)| {
(
*oid,
DbObjectId::Schema {
name: schema.name.clone(),
},
)
}),
)?;
let comments = index.object_comments(&shared.descriptions, class::PG_NAMESPACE);
for (_, schema) in &mut converted.objects {
let id = DbObjectId::Schema {
name: schema.name.clone(),
};
schema.comment = comments.get(&id).map(|text| text.to_string());
}
converted.index = index;
Ok(converted.map(|(_, schema)| schema))
}
pub fn convert(shared: &SharedCatalog) -> Result<Converted<(Oid, Schema)>> {
let mut converted: Converted<(Oid, Schema)> = Converted::new();
for (oid, name) in shared.namespaces.iter() {
if is_system_schema(name) {
converted.excluded.push(Excluded::new(
oid,
"schema",
name,
name,
ExclusionReason::SystemSchema,
));
continue;
}
converted.objects.push((
oid,
Schema {
name: name.to_string(),
comment: None,
},
));
}
converted
.objects
.sort_by(|(_, a), (_, b)| a.name.cmp(&b.name));
Ok(converted)
}