use crate::{
walkers::{ModelWalker, RelationFieldId, RelationFieldWalker, ScalarFieldWalker},
ParserDatabase, ReferentialAction,
};
use diagnostics::Span;
use schema_ast::ast;
#[derive(Copy, Clone)]
pub struct CompleteInlineRelationWalker<'db> {
pub(crate) side_a: RelationFieldId,
pub(crate) side_b: RelationFieldId,
pub(crate) db: &'db ParserDatabase,
}
#[allow(missing_docs)]
impl<'db> CompleteInlineRelationWalker<'db> {
pub fn referencing_model(self) -> ModelWalker<'db> {
self.db.walk(self.side_a).model()
}
pub fn referenced_model(self) -> ModelWalker<'db> {
self.db.walk(self.side_b).model()
}
pub fn referencing_field(self) -> RelationFieldWalker<'db> {
self.db.walk(self.side_a)
}
pub fn referenced_field(self) -> RelationFieldWalker<'db> {
self.db.walk(self.side_b)
}
pub fn referenced_fields(self) -> impl ExactSizeIterator<Item = ScalarFieldWalker<'db>> + 'db {
(match self.referencing_field().attributes().references.as_ref() {
Some(references) => references.as_slice(),
None => &[],
})
.iter()
.map(|id| self.db.walk(*id))
}
pub fn referencing_fields(self) -> impl ExactSizeIterator<Item = ScalarFieldWalker<'db>> + 'db {
(match self.referencing_field().attributes().fields.as_ref() {
Some(references) => references.as_slice(),
None => &[],
})
.iter()
.map(|id| self.db.walk(*id))
}
pub fn on_update(self) -> ReferentialAction {
use ReferentialAction::*;
self.referencing_field()
.attributes()
.on_update
.map(|(action, _)| action)
.unwrap_or(Cascade)
}
pub fn on_update_span(self) -> Option<Span> {
self.referencing_field().attributes().on_update.map(|(_, span)| span)
}
pub fn referential_arity(self) -> ast::FieldArity {
self.referencing_field().referential_arity()
}
}