use crate::dialect::SqlType;
use crate::index::IndexDef;
pub use crate::model::ForeignKeyAction;
#[derive(Debug, Clone, PartialEq)]
pub enum DefaultValue {
Bool(bool),
Int(i64),
Real(f64),
Text(String),
CurrentTimestamp,
Null,
Uuid,
Raw(String),
}
impl From<bool> for DefaultValue {
fn from(value: bool) -> Self {
DefaultValue::Bool(value)
}
}
impl From<i64> for DefaultValue {
fn from(value: i64) -> Self {
DefaultValue::Int(value)
}
}
impl From<i32> for DefaultValue {
fn from(value: i32) -> Self {
DefaultValue::Int(i64::from(value))
}
}
impl From<f64> for DefaultValue {
fn from(value: f64) -> Self {
DefaultValue::Real(value)
}
}
impl From<&str> for DefaultValue {
fn from(value: &str) -> Self {
DefaultValue::Text(value.to_string())
}
}
impl From<String> for DefaultValue {
fn from(value: String) -> Self {
DefaultValue::Text(value)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ColumnSpec {
pub name: String,
pub ty: SqlType,
pub nullable: bool,
pub primary_key: bool,
pub auto_increment: bool,
pub unique: bool,
pub default: Option<DefaultValue>,
}
impl ColumnSpec {
pub fn new(name: impl Into<String>, ty: SqlType) -> Self {
Self {
name: name.into(),
ty,
nullable: false,
primary_key: false,
auto_increment: false,
unique: false,
default: None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ForeignKeySpec {
pub columns: Vec<String>,
pub ref_table: String,
pub ref_columns: Vec<String>,
pub on_delete: ForeignKeyAction,
pub on_update: ForeignKeyAction,
}
#[derive(Debug, Clone)]
pub struct TableDef {
pub name: String,
pub if_not_exists: bool,
pub columns: Vec<ColumnSpec>,
pub primary_key: Vec<String>,
pub foreign_keys: Vec<ForeignKeySpec>,
pub checks: Vec<String>,
pub indexes: Vec<IndexDef>,
}
impl TableDef {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
if_not_exists: false,
columns: Vec::new(),
primary_key: Vec::new(),
foreign_keys: Vec::new(),
checks: Vec::new(),
indexes: Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum AlterAction {
AddColumn(ColumnSpec),
DropColumn(String),
RenameColumn {
from: String,
to: String,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct AlterTable {
pub table: String,
pub actions: Vec<AlterAction>,
}