#[derive(Debug, Clone, PartialEq)]
pub enum ColumnDefault {
CurrentTimestamp,
Null,
Integer(i64),
Float(f64),
Text(String),
}
impl ColumnDefault {
pub fn to_sql(&self) -> String {
match self {
ColumnDefault::CurrentTimestamp => "CURRENT_TIMESTAMP".to_string(),
ColumnDefault::Null => "NULL".to_string(),
ColumnDefault::Integer(n) => n.to_string(),
ColumnDefault::Float(f) => format!("{f}"),
ColumnDefault::Text(s) => format!("'{}'", s.replace('\'', "''")),
}
}
}
pub struct Column {
pub name: String,
pub col_type: String,
pub is_nullable: bool,
pub is_primary_key: bool,
pub is_auto_increment: bool,
pub default_value: Option<ColumnDefault>,
}
impl Column {
pub fn new(name: impl Into<String>, col_type: impl Into<String>) -> Self {
Self {
name: name.into(),
col_type: col_type.into(),
is_nullable: true,
is_primary_key: false,
is_auto_increment: false,
default_value: None,
}
}
pub fn not_null(&mut self) -> &mut Self {
self.is_nullable = false;
self
}
pub fn nullable(&mut self) -> &mut Self {
self.is_nullable = true;
self
}
pub fn default(&mut self, val: ColumnDefault) -> &mut Self {
self.default_value = Some(val);
self
}
pub fn primary(&mut self) -> &mut Self {
self.is_primary_key = true;
self
}
}