use either::Either;
use crate::{
Column, ColumnArity, ColumnType, ColumnTypeFamily, EnumWalker, TableColumnId, TableDefaultValueId,
TableDefaultValueWalker, TableId, TableWalker, Walker,
};
use super::ColumnWalker;
pub type TableColumnWalker<'a> = Walker<'a, TableColumnId>;
impl<'a> TableColumnWalker<'a> {
fn get(self) -> &'a (TableId, Column) {
&self.schema.table_columns[self.id.0 as usize]
}
pub fn coarsen(self) -> ColumnWalker<'a> {
self.walk(Either::Left(self.id))
}
pub fn name(self) -> &'a str {
self.coarsen().name()
}
pub fn arity(self) -> ColumnArity {
self.coarsen().arity()
}
pub fn column_has_enum_default_value(self, enum_name: &str, value: &str) -> bool {
self.coarsen().column_has_enum_default_value(enum_name, value)
}
pub fn column_type_is_enum(self, enum_name: &str) -> bool {
self.coarsen().column_type_is_enum(enum_name)
}
pub fn column_type_family(self) -> &'a ColumnTypeFamily {
self.coarsen().column_type_family()
}
pub fn column_type_family_as_enum(self) -> Option<EnumWalker<'a>> {
self.coarsen().column_type_family_as_enum()
}
pub fn default(self) -> Option<TableDefaultValueWalker<'a>> {
self.schema
.table_default_values
.binary_search_by_key(&self.id, |(id, _)| *id)
.ok()
.map(|id| self.walk(TableDefaultValueId(id as u32)))
}
pub fn column_type(self) -> &'a ColumnType {
self.coarsen().column_type()
}
pub fn column_native_type<T: std::any::Any + 'static>(self) -> Option<&'a T> {
self.coarsen().column_native_type()
}
pub fn is_autoincrement(self) -> bool {
self.coarsen().is_autoincrement()
}
pub fn is_same_column(self, other: TableColumnWalker<'_>) -> bool {
self.name() == other.name()
&& self.table().name() == other.table().name()
&& self.table().namespace_id() == other.table().namespace_id()
}
pub fn is_part_of_secondary_index(self) -> bool {
self.table().indexes().any(|idx| idx.contains_column(self.id))
}
pub fn is_part_of_primary_key(self) -> bool {
match self.table().primary_key() {
Some(pk) => pk.contains_column(self.id),
None => false,
}
}
pub fn is_part_of_foreign_key(self) -> bool {
let column_id = self.id;
self.table()
.foreign_keys()
.any(|fk| fk.constrained_columns().any(|col| col.id == column_id))
}
pub fn is_single_primary_key(self) -> bool {
self.table()
.primary_key()
.map(|pk| pk.columns().len() == 1 && pk.columns().next().map(|c| c.name() == self.name()).unwrap_or(false))
.unwrap_or(false)
}
pub fn table(self) -> TableWalker<'a> {
self.walk(self.get().0)
}
}