#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColumnType {
Bool,
Int,
BigInt,
Uint,
BigUint,
Double,
Text,
Binary,
Timestamp,
Date,
}
#[derive(Debug, Clone, Copy)]
pub struct ColumnSpec {
pub name: &'static str,
pub ty: ColumnType,
pub nullable: bool,
pub pk: bool,
pub auto_increment: bool,
pub unique: bool,
}
#[derive(Debug, Clone, Copy)]
pub struct IndexSpec {
pub name: &'static str,
pub columns: &'static [&'static str],
pub unique: bool,
}
#[derive(Debug, Clone, Copy)]
pub struct ForeignKeySpec {
pub name: &'static str,
pub columns: &'static [&'static str],
pub foreign_table: &'static str,
pub foreign_columns: &'static [&'static str],
pub on_delete: Option<RefAction>,
pub on_update: Option<RefAction>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RefAction {
Cascade,
Restrict,
SetNull,
SetDefault,
NoAction,
}
pub trait SqlType {
const COLUMN_TYPE: ColumnType;
const NULLABLE: bool = false;
}
macro_rules! sql_type {
($t:ty, $ct:expr) => {
impl SqlType for $t {
const COLUMN_TYPE: ColumnType = $ct;
}
};
}
sql_type!(bool, ColumnType::Bool);
sql_type!(i32, ColumnType::Int);
sql_type!(i64, ColumnType::BigInt);
sql_type!(u32, ColumnType::Uint);
sql_type!(u64, ColumnType::BigUint);
sql_type!(f64, ColumnType::Double);
sql_type!(String, ColumnType::Text);
sql_type!(Vec<u8>, ColumnType::Binary);
sql_type!(chrono::DateTime<chrono::Utc>, ColumnType::Timestamp);
sql_type!(chrono::NaiveDate, ColumnType::Date);
impl<T: SqlType> SqlType for Option<T> {
const COLUMN_TYPE: ColumnType = T::COLUMN_TYPE;
const NULLABLE: bool = true;
}