use std::{collections::HashMap, fmt::Display};
use crate::raw;
pub type Mapping = HashMap<&'static str, &'static str>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IndexMeta {
Index(String),
Unique(String),
}
impl IndexMeta {
pub fn name(&self, t: &str) -> String {
match self {
IndexMeta::Index(v) => raw!("idx_{}_{}", t, v),
IndexMeta::Unique(v) => raw!("unq_{}_{}", t, v),
}
}
pub fn column(&self) -> String {
let raw = match self {
IndexMeta::Index(v) => v,
IndexMeta::Unique(v) => v,
};
raw.into()
}
}
#[derive(Debug, Clone)]
pub enum Adjust {
Add,
Drop,
Alter,
}
#[derive(Debug, Default, Clone)]
pub struct ColumeMeta {
pub name: String, pub size: usize, pub colume: String, pub nullable: bool, pub default: String, pub comment: String, pub increment: bool, }
impl PartialEq for ColumeMeta {
fn eq(&self, other: &Self) -> bool {
if self.name != other.name
|| self.nullable != other.nullable
|| self.default.trim_matches('\'') != other.default.trim_matches('\'')
{
return false;
}
let colume = format!("{}({})", self.colume, self.size);
if self.size != 0 && colume != other.colume {
return false;
}
other.colume.starts_with(&self.colume)
}
}
impl Display for ColumeMeta {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {}", self.name, self.colume)?;
if self.size != 0 {
write!(f, "({})", self.size)?;
}
if !self.nullable {
write!(f, " {}", "NOT NULL")?;
}
if !self.default.is_empty() {
write!(f, " DEFAULT {}", self.default)?;
}
Ok(())
}
}
#[derive(Debug, Clone, Default)]
pub struct TableMeta {
pub name: String, pub primary: String, pub indexs: Vec<IndexMeta>, pub columes: Vec<ColumeMeta>, }