use crate::catalog::aggregate::Aggregate;
use crate::catalog::cast::Cast;
use crate::catalog::constraint::Constraint;
use crate::catalog::custom_type::CustomType;
use crate::catalog::domain::Domain;
use crate::catalog::extension::Extension;
use crate::catalog::function::Function;
use crate::catalog::id::{DbObjectId, DependsOn};
use crate::catalog::index::Index;
use crate::catalog::operator::Operator;
use crate::catalog::policy::Policy;
use crate::catalog::schema::Schema;
use crate::catalog::sequence::Sequence;
use crate::catalog::table::Table;
use crate::catalog::target::AttrTarget;
use crate::catalog::triggers::Trigger;
use crate::catalog::view::View;
pub trait Attached {
fn object_id(&self) -> DbObjectId;
fn own_comment(&self) -> Option<String>;
fn sub_comments(&self) -> Vec<(AttrTarget, Option<String>)> {
Vec::new()
}
fn comment_targets(&self) -> Vec<(AttrTarget, Option<String>)> {
let mut targets = vec![(AttrTarget::object(self.object_id()), self.own_comment())];
targets.extend(self.sub_comments());
targets
}
}
impl Attached for View {
fn object_id(&self) -> DbObjectId {
self.id()
}
fn own_comment(&self) -> Option<String> {
self.comment.clone()
}
fn sub_comments(&self) -> Vec<(AttrTarget, Option<String>)> {
let id = self.id();
self.columns
.iter()
.map(|c| {
(
AttrTarget::column(id.clone(), c.name.clone()),
c.comment.clone(),
)
})
.collect()
}
}
impl Attached for Table {
fn object_id(&self) -> DbObjectId {
self.id()
}
fn own_comment(&self) -> Option<String> {
self.comment.clone()
}
fn sub_comments(&self) -> Vec<(AttrTarget, Option<String>)> {
let id = self.id();
let mut subs: Vec<(AttrTarget, Option<String>)> = self
.columns
.iter()
.map(|c| {
(
AttrTarget::column(id.clone(), c.name.clone()),
c.comment.clone(),
)
})
.collect();
if let Some(pk) = &self.primary_key {
subs.push((
AttrTarget::object(DbObjectId::Constraint {
schema: self.schema.clone(),
table: self.name.clone(),
name: pk.name.clone(),
}),
pk.comment.clone(),
));
}
subs
}
}
impl Attached for CustomType {
fn object_id(&self) -> DbObjectId {
self.id()
}
fn own_comment(&self) -> Option<String> {
self.comment.clone()
}
fn sub_comments(&self) -> Vec<(AttrTarget, Option<String>)> {
let id = self.id();
self.composite_attributes
.iter()
.map(|a| {
(
AttrTarget::column(id.clone(), a.name.clone()),
a.comment.clone(),
)
})
.collect()
}
}
impl Attached for Schema {
fn object_id(&self) -> DbObjectId {
DbObjectId::Schema {
name: self.name.clone(),
}
}
fn own_comment(&self) -> Option<String> {
if self.name == "public" && self.comment.as_deref() == Some("standard public schema") {
None
} else {
self.comment.clone()
}
}
}
macro_rules! impl_attached {
($($t:ty),+ $(,)?) => {
$(
impl Attached for $t {
fn object_id(&self) -> DbObjectId {
self.id()
}
fn own_comment(&self) -> Option<String> {
self.comment.clone()
}
}
)+
};
}
impl_attached!(
Domain, Function, Aggregate, Operator, Cast, Sequence, Index, Constraint, Trigger, Policy,
Extension,
);