use crate::{ForeignKey, ForeignKeyAction, ForeignKeyColumn, ForeignKeyId, TableColumnWalker, TableWalker, Walker};
pub type ForeignKeyWalker<'a> = Walker<'a, ForeignKeyId>;
impl<'schema> ForeignKeyWalker<'schema> {
pub(super) fn columns(self) -> &'schema [ForeignKeyColumn] {
let range = super::range_for_key(&self.schema.foreign_key_columns, self.id, |col| col.foreign_key_id);
&self.schema.foreign_key_columns[range]
}
pub fn constrained_columns(self) -> impl ExactSizeIterator<Item = TableColumnWalker<'schema>> {
self.columns().iter().map(move |col| self.walk(col.constrained_column))
}
pub fn constraint_name(self) -> Option<&'schema str> {
self.foreign_key().constraint_name.as_deref()
}
fn foreign_key(self) -> &'schema ForeignKey {
&self.schema.foreign_keys[self.id.0 as usize]
}
pub fn on_delete_action(self) -> ForeignKeyAction {
self.foreign_key().on_delete_action
}
pub fn on_update_action(self) -> ForeignKeyAction {
self.foreign_key().on_update_action
}
pub fn referenced_columns(self) -> impl ExactSizeIterator<Item = TableColumnWalker<'schema>> {
self.columns().iter().map(move |col| self.walk(col.referenced_column))
}
pub fn referenced_table_name(self) -> &'schema str {
self.referenced_table().name()
}
pub fn referenced_table(self) -> TableWalker<'schema> {
self.walk(self.foreign_key().referenced_table)
}
pub fn table(self) -> TableWalker<'schema> {
self.walk(self.foreign_key().constrained_table)
}
pub fn is_self_relation(self) -> bool {
let fk = self.foreign_key();
fk.constrained_table == fk.referenced_table
}
}