use std::sync::Arc;
use vantage_types::EmptyEntity;
use vantage_vista::{ContainedKind, ContainedSpec};
use crate::{table::Table, traits::table_source::TableSource};
pub struct ContainedRelation<T: TableSource> {
name: String,
host_column: String,
kind: ContainedKind,
id_column: Option<String>,
build_target: Arc<dyn Fn(T) -> Table<T, EmptyEntity> + Send + Sync>,
}
impl<T: TableSource> Clone for ContainedRelation<T> {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
host_column: self.host_column.clone(),
kind: self.kind,
id_column: self.id_column.clone(),
build_target: self.build_target.clone(),
}
}
}
impl<T: TableSource + 'static> ContainedRelation<T> {
pub fn new(
name: impl Into<String>,
host_column: impl Into<String>,
kind: ContainedKind,
id_column: Option<String>,
build_target: impl Fn(T) -> Table<T, EmptyEntity> + Send + Sync + 'static,
) -> Self {
Self {
name: name.into(),
host_column: host_column.into(),
kind,
id_column,
build_target: Arc::new(build_target),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn host_column(&self) -> &str {
&self.host_column
}
pub fn kind(&self) -> ContainedKind {
self.kind
}
pub fn id_column(&self) -> Option<&str> {
self.id_column.as_deref()
}
pub fn build_target(&self, db: T) -> Table<T, EmptyEntity> {
(self.build_target)(db)
}
pub fn spec(&self) -> ContainedSpec {
let mut spec = ContainedSpec::new(&self.name, &self.host_column, self.kind);
if let Some(id) = &self.id_column {
spec = spec.with_id_column(id);
}
spec
}
}