feat: implement ColumnTypeTrait for ColumnDef (#1576)

This commit is contained in:
Billy Chan 2023-05-29 20:35:02 +08:00 committed by GitHub
parent 0b15f4f5e2
commit 33f3f9282d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 7 deletions

View File

@ -315,15 +315,26 @@ impl ColumnTypeTrait for ColumnType {
}
fn get_enum_name(&self) -> Option<&DynIden> {
fn enum_name(col_type: &ColumnType) -> Option<&DynIden> {
enum_name(self)
}
}
impl ColumnTypeTrait for ColumnDef {
fn def(self) -> ColumnDef {
self
}
fn get_enum_name(&self) -> Option<&DynIden> {
enum_name(&self.col_type)
}
}
fn enum_name(col_type: &ColumnType) -> Option<&DynIden> {
match col_type {
ColumnType::Enum { name, .. } => Some(name),
ColumnType::Array(col_type) => enum_name(col_type),
_ => None,
}
}
enum_name(self)
}
}
impl ColumnDef {

View File

@ -0,0 +1,64 @@
use super::sea_orm_active_enums::*;
use crate as sea_orm;
use crate::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity;
impl EntityName for Entity {
fn table_name(&self) -> &str {
"lunch_set"
}
}
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "lunch_set")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
pub tea: Tea,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
Name,
Tea,
}
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
Id,
}
impl PrimaryKeyTrait for PrimaryKey {
type ValueType = i32;
fn auto_increment() -> bool {
true
}
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}
impl ColumnTrait for Column {
type EntityName = Entity;
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Name => ColumnType::String(None).def(),
Self::Tea => Tea::db_type().def(),
}
}
}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -9,6 +9,7 @@ pub mod filling;
pub mod fruit;
pub mod indexes;
pub mod lunch_set;
pub mod lunch_set_expanded;
pub mod rust_keyword;
pub mod sea_orm_active_enums;
pub mod vendor;
@ -20,5 +21,6 @@ pub use cake_filling_price::Entity as CakeFillingPrice;
pub use filling::Entity as Filling;
pub use fruit::Entity as Fruit;
pub use lunch_set::Entity as LunchSet;
pub use lunch_set_expanded::Entity as LunchSetExpanded;
pub use rust_keyword::Entity as RustKeyword;
pub use vendor::Entity as Vendor;