use super::EXIT_CONTRACT_ERROR;
use crate::commands::output::failure_message;
use crate::render::RenderFormat;
use saya_types::{DatabaseObjectRef, SchemaFingerprint, SchemaTree, Table};
pub(super) enum SchemaCheck<'a> {
Found(&'a Table),
Absent,
NoSchema,
}
pub(super) fn resolved_against<'a>(
cached: &'a Option<SchemaTree>,
object: &DatabaseObjectRef,
) -> SchemaCheck<'a> {
let Some(schema) = cached else {
return SchemaCheck::NoSchema;
};
if schema.databases.is_empty() {
return SchemaCheck::NoSchema;
}
match schema.find_table(object.catalog(), object.schema(), object.object()) {
Some(table) => SchemaCheck::Found(table),
None => SchemaCheck::Absent,
}
}
pub(super) fn fingerprint_of(table: &Table) -> SchemaFingerprint {
SchemaFingerprint::of_table(saya_types::DatabaseObjectKind::Table, table)
}
pub(super) fn refuse_unknown(
object: &DatabaseObjectRef,
profile_name: &str,
format: RenderFormat,
) -> Result<i32, Box<dyn std::error::Error>> {
failure_message(
EXIT_CONTRACT_ERROR,
unknown_object_message(object, profile_name),
format,
)
}
fn unknown_object_message(object: &DatabaseObjectRef, profile_name: &str) -> String {
format!(
"no object {} in the cached schema for profile {}; run `connection schema {profile_name} --refresh` and retry",
object.qualified_name(),
profile_name,
)
}