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