#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Schema {
pub tables: Vec<TableDef>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TableKind {
Table,
View,
UpdatableView,
}
impl TableKind {
pub fn is_view(self) -> bool {
!matches!(self, TableKind::Table)
}
pub fn is_updatable(self) -> bool {
matches!(self, TableKind::Table | TableKind::UpdatableView)
}
pub(crate) fn noun(self) -> &'static str {
match self {
TableKind::Table => "table",
TableKind::View => "view",
TableKind::UpdatableView => "updatable view",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TableDef {
pub name: String,
pub kind: TableKind,
pub columns: Vec<ColumnDef>,
pub primary_key: Vec<String>,
pub foreign_keys: Vec<ForeignKey>,
pub unique_keys: Vec<Vec<String>>,
}
impl TableDef {
pub fn column(&self, name: &str) -> Option<&ColumnDef> {
self.columns.iter().find(|c| c.name == name)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColumnDef {
pub name: String,
pub db_type: String,
pub nullable: bool,
pub default: Option<String>,
pub autoincrement: bool,
pub comment: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ForeignKey {
pub columns: Vec<String>,
pub ref_table: String,
pub ref_columns: Vec<String>,
}