WIP
This commit is contained in:
parent
d525c710ce
commit
734608471c
@ -1,4 +1,4 @@
|
||||
use crate::{ColumnDef, DbErr, TryGetable};
|
||||
use crate::{ColumnDef, DbErr, Iterable, TryGetable};
|
||||
use sea_query::{Nullable, Value, ValueType};
|
||||
|
||||
/// A Rust representation of enum defined in database.
|
||||
@ -17,7 +17,7 @@ use sea_query::{Nullable, Value, ValueType};
|
||||
/// use sea_orm::entity::prelude::*;
|
||||
///
|
||||
/// // Using the derive macro
|
||||
/// #[derive(Debug, PartialEq, DeriveActiveEnum)]
|
||||
/// #[derive(Debug, PartialEq, EnumIter, DeriveActiveEnum)]
|
||||
/// #[sea_orm(rs_type = "String", db_type = "String(Some(1))")]
|
||||
/// pub enum DeriveCategory {
|
||||
/// #[sea_orm(string_value = "B")]
|
||||
@ -27,7 +27,7 @@ use sea_query::{Nullable, Value, ValueType};
|
||||
/// }
|
||||
///
|
||||
/// // Implementing it manually
|
||||
/// #[derive(Debug, PartialEq)]
|
||||
/// #[derive(Debug, PartialEq, EnumIter)]
|
||||
/// pub enum Category {
|
||||
/// Big,
|
||||
/// Small,
|
||||
@ -80,7 +80,7 @@ use sea_query::{Nullable, Value, ValueType};
|
||||
/// Small,
|
||||
/// }
|
||||
///
|
||||
/// #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
|
||||
/// #[derive(Clone, Debug, PartialEq, EnumIter, DeriveEntityModel)]
|
||||
/// #[sea_orm(table_name = "active_enum")]
|
||||
/// pub struct Model {
|
||||
/// #[sea_orm(primary_key)]
|
||||
@ -95,7 +95,7 @@ use sea_query::{Nullable, Value, ValueType};
|
||||
///
|
||||
/// impl ActiveModelBehavior for ActiveModel {}
|
||||
/// ```
|
||||
pub trait ActiveEnum: Sized {
|
||||
pub trait ActiveEnum: Sized + Iterable {
|
||||
/// Define the Rust type that each enum variant represents.
|
||||
type Value: Into<Value> + ValueType + Nullable + TryGetable;
|
||||
|
||||
@ -117,7 +117,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn active_enum_string() {
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[derive(Debug, PartialEq, EnumIter)]
|
||||
pub enum Category {
|
||||
Big,
|
||||
Small,
|
||||
@ -150,7 +150,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, DeriveActiveEnum)]
|
||||
#[derive(Debug, PartialEq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = "String(Some(1))")]
|
||||
pub enum DeriveCategory {
|
||||
#[sea_orm(string_value = "B")]
|
||||
@ -201,7 +201,7 @@ mod tests {
|
||||
fn active_enum_derive_signed_integers() {
|
||||
macro_rules! test_int {
|
||||
($ident: ident, $rs_type: expr, $db_type: expr, $col_def: ident) => {
|
||||
#[derive(Debug, PartialEq, DeriveActiveEnum)]
|
||||
#[derive(Debug, PartialEq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = $rs_type, db_type = $db_type)]
|
||||
pub enum $ident {
|
||||
#[sea_orm(num_value = 1)]
|
||||
@ -241,7 +241,7 @@ mod tests {
|
||||
fn active_enum_derive_unsigned_integers() {
|
||||
macro_rules! test_uint {
|
||||
($ident: ident, $rs_type: expr, $db_type: expr, $col_def: ident) => {
|
||||
#[derive(Debug, PartialEq, DeriveActiveEnum)]
|
||||
#[derive(Debug, PartialEq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = $rs_type, db_type = $db_type)]
|
||||
pub enum $ident {
|
||||
#[sea_orm(num_value = 1)]
|
||||
|
@ -34,6 +34,7 @@ pub enum ColumnType {
|
||||
JsonBinary,
|
||||
Custom(String),
|
||||
Uuid,
|
||||
Enum(String),
|
||||
}
|
||||
|
||||
macro_rules! bind_oper {
|
||||
@ -295,6 +296,9 @@ impl From<ColumnType> for sea_query::ColumnType {
|
||||
sea_query::ColumnType::Custom(sea_query::SeaRc::new(sea_query::Alias::new(&s)))
|
||||
}
|
||||
ColumnType::Uuid => sea_query::ColumnType::Uuid,
|
||||
ColumnType::Enum(s) => {
|
||||
sea_query::ColumnType::Custom(sea_query::SeaRc::new(sea_query::Alias::new(&s)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||
let am = ActiveModel {
|
||||
category: Set(None),
|
||||
color: Set(None),
|
||||
// tea: Set(None),
|
||||
tea: Set(None),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(db)
|
||||
@ -36,14 +36,14 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||
id: 1,
|
||||
category: None,
|
||||
color: None,
|
||||
// tea: None,
|
||||
tea: None,
|
||||
}
|
||||
);
|
||||
|
||||
ActiveModel {
|
||||
category: Set(Some(Category::Big)),
|
||||
color: Set(Some(Color::Black)),
|
||||
// tea: Set(Some(Tea::EverydayTea)),
|
||||
tea: Set(Some(Tea::EverydayTea)),
|
||||
..am
|
||||
}
|
||||
.save(db)
|
||||
@ -55,7 +55,7 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||
id: 1,
|
||||
category: Some(Category::Big),
|
||||
color: Some(Color::Black),
|
||||
// tea: Some(Tea::EverydayTea),
|
||||
tea: Some(Tea::EverydayTea),
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -7,7 +7,7 @@ pub struct Model {
|
||||
pub id: i32,
|
||||
pub category: Option<Category>,
|
||||
pub color: Option<Color>,
|
||||
// pub tea: Option<Tea>,
|
||||
pub tea: Option<Tea>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
@ -15,7 +15,7 @@ pub enum Relation {}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, DeriveActiveEnum)]
|
||||
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = "String(Some(1))")]
|
||||
pub enum Category {
|
||||
#[sea_orm(string_value = "B")]
|
||||
@ -24,7 +24,7 @@ pub enum Category {
|
||||
Small,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, DeriveActiveEnum)]
|
||||
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "i32", db_type = r#"Integer"#)]
|
||||
pub enum Color {
|
||||
#[sea_orm(num_value = 0)]
|
||||
@ -33,8 +33,8 @@ pub enum Color {
|
||||
White,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = r#"Custom("tea".to_owned())"#)]
|
||||
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum)]
|
||||
#[sea_orm(rs_type = "String", db_type = r#"Enum("tea".to_owned())"#)]
|
||||
pub enum Tea {
|
||||
#[sea_orm(string_value = "EverydayTea")]
|
||||
EverydayTea,
|
||||
|
Loading…
x
Reference in New Issue
Block a user