diff --git a/src/entity/column.rs b/src/entity/column.rs index a5017126..ae13282b 100644 --- a/src/entity/column.rs +++ b/src/entity/column.rs @@ -1,15 +1,37 @@ use crate::{EntityName, IdenStatic, Iterable}; -pub use sea_query::ColumnType; use sea_query::{DynIden, Expr, SeaRc, SimpleExpr, Value}; -// TODO: ColumnDef with builder API to replace sea_query::ColumnType in Entity def +#[derive(Debug, Clone)] pub struct ColumnDef { pub(crate) col_type: ColumnType, - pub(crate) not_null: bool, + pub(crate) null: bool, pub(crate) unique: bool, pub(crate) indexed: bool, } +#[derive(Debug, Copy, Clone)] +pub enum ColumnType { + Char, + String, + Text, + TinyInteger, + SmallInteger, + Integer, + BigInteger, + Float, + Double, + Decimal, + DateTime, + Timestamp, + Time, + Date, + Binary, + Boolean, + Money, + Json, + JsonBinary, +} + macro_rules! bind_oper { ( $op: ident ) => { fn $op(&self, v: V) -> SimpleExpr @@ -46,7 +68,7 @@ macro_rules! bind_vec_func { pub trait ColumnTrait: IdenStatic + Iterable { type EntityName: EntityName; - fn def(&self) -> ColumnType; + fn def(&self) -> ColumnDef; fn entity_name(&self) -> DynIden { SeaRc::new(Self::EntityName::default()) as DynIden @@ -192,3 +214,67 @@ pub trait ColumnTrait: IdenStatic + Iterable { bind_vec_func!(is_in); bind_vec_func!(is_not_in); } + +impl From for ColumnDef { + fn from(col_type: ColumnType) -> Self { + Self { + col_type, + null: false, + unique: false, + indexed: false, + } + } +} + +impl Into for ColumnType { + fn into(self) -> sea_query::ColumnType { + match self { + Self::Char => sea_query::ColumnType::Char(None), + Self::String => sea_query::ColumnType::String(None), + Self::Text => sea_query::ColumnType::Text, + Self::TinyInteger => sea_query::ColumnType::TinyInteger(None), + Self::SmallInteger => sea_query::ColumnType::SmallInteger(None), + Self::Integer => sea_query::ColumnType::Integer(None), + Self::BigInteger => sea_query::ColumnType::BigInteger(None), + Self::Float => sea_query::ColumnType::Float(None), + Self::Double => sea_query::ColumnType::Double(None), + Self::Decimal => sea_query::ColumnType::Decimal(None), + Self::DateTime => sea_query::ColumnType::DateTime(None), + Self::Timestamp => sea_query::ColumnType::Timestamp(None), + Self::Time => sea_query::ColumnType::Time(None), + Self::Date => sea_query::ColumnType::Date, + Self::Binary => sea_query::ColumnType::Binary(None), + Self::Boolean => sea_query::ColumnType::Boolean, + Self::Money => sea_query::ColumnType::Money(None), + Self::Json => sea_query::ColumnType::Json, + Self::JsonBinary => sea_query::ColumnType::JsonBinary, + } + } +} + +impl From for ColumnType { + fn from(col_type: sea_query::ColumnType) -> Self { + match col_type { + sea_query::ColumnType::Char(_) => Self::Char, + sea_query::ColumnType::String(_) => Self::String, + sea_query::ColumnType::Text => Self::Text, + sea_query::ColumnType::TinyInteger(_) => Self::TinyInteger, + sea_query::ColumnType::SmallInteger(_) => Self::SmallInteger, + sea_query::ColumnType::Integer(_) => Self::Integer, + sea_query::ColumnType::BigInteger(_) => Self::BigInteger, + sea_query::ColumnType::Float(_) => Self::Float, + sea_query::ColumnType::Double(_) => Self::Double, + sea_query::ColumnType::Decimal(_) => Self::Decimal, + sea_query::ColumnType::DateTime(_) => Self::DateTime, + sea_query::ColumnType::Timestamp(_) => Self::Timestamp, + sea_query::ColumnType::Time(_) => Self::Time, + sea_query::ColumnType::Date => Self::Date, + sea_query::ColumnType::Binary(_) => Self::Binary, + sea_query::ColumnType::Boolean => Self::Boolean, + sea_query::ColumnType::Money(_) => Self::Money, + sea_query::ColumnType::Json => Self::Json, + sea_query::ColumnType::JsonBinary => Self::JsonBinary, + sea_query::ColumnType::Custom(_) => panic!("custom column type unsupported"), + } + } +} diff --git a/src/entity/prelude.rs b/src/entity/prelude.rs index 3b18545f..f7eb5cc1 100644 --- a/src/entity/prelude.rs +++ b/src/entity/prelude.rs @@ -1,5 +1,5 @@ pub use crate::{ - ActiveModelBehavior, ActiveModelTrait, ColumnTrait, ColumnType, DeriveActiveModel, + ActiveModelBehavior, ActiveModelTrait, ColumnDef, ColumnTrait, ColumnType, DeriveActiveModel, DeriveActiveModelBehavior, DeriveColumn, DeriveEntity, DeriveModel, DerivePrimaryKey, EntityName, EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, QueryResult, Related, RelationDef, RelationTrait, Select, diff --git a/src/tests_cfg/cake.rs b/src/tests_cfg/cake.rs index 1ad182f8..9d48b8ec 100644 --- a/src/tests_cfg/cake.rs +++ b/src/tests_cfg/cake.rs @@ -41,10 +41,10 @@ pub enum Relation { impl ColumnTrait for Column { type EntityName = Entity; - fn def(&self) -> ColumnType { + fn def(&self) -> ColumnDef { match self { - Self::Id => ColumnType::Integer(None), - Self::Name => ColumnType::String(None), + Self::Id => ColumnType::Integer.into(), + Self::Name => ColumnType::String.into(), } } } diff --git a/src/tests_cfg/cake_filling.rs b/src/tests_cfg/cake_filling.rs index fa5e3a74..2dff2e43 100644 --- a/src/tests_cfg/cake_filling.rs +++ b/src/tests_cfg/cake_filling.rs @@ -43,10 +43,10 @@ pub enum Relation { impl ColumnTrait for Column { type EntityName = Entity; - fn def(&self) -> ColumnType { + fn def(&self) -> ColumnDef { match self { - Self::CakeId => ColumnType::Integer(None), - Self::FillingId => ColumnType::Integer(None), + Self::CakeId => ColumnType::Integer.into(), + Self::FillingId => ColumnType::Integer.into(), } } } diff --git a/src/tests_cfg/filling.rs b/src/tests_cfg/filling.rs index 34df9cba..8f58aa85 100644 --- a/src/tests_cfg/filling.rs +++ b/src/tests_cfg/filling.rs @@ -39,10 +39,10 @@ pub enum Relation {} impl ColumnTrait for Column { type EntityName = Entity; - fn def(&self) -> ColumnType { + fn def(&self) -> ColumnDef { match self { - Self::Id => ColumnType::Integer(None), - Self::Name => ColumnType::String(None), + Self::Id => ColumnType::Integer.into(), + Self::Name => ColumnType::String.into(), } } } diff --git a/src/tests_cfg/fruit.rs b/src/tests_cfg/fruit.rs index 32a0e553..64817fcd 100644 --- a/src/tests_cfg/fruit.rs +++ b/src/tests_cfg/fruit.rs @@ -41,11 +41,11 @@ pub enum Relation {} impl ColumnTrait for Column { type EntityName = Entity; - fn def(&self) -> ColumnType { + fn def(&self) -> ColumnDef { match self { - Self::Id => ColumnType::Integer(None), - Self::Name => ColumnType::String(None), - Self::CakeId => ColumnType::Integer(None), + Self::Id => ColumnType::Integer.into(), + Self::Name => ColumnType::String.into(), + Self::CakeId => ColumnType::Integer.into(), } } }