From 19b95eb64b57b383f94a98761bfe292d2a2083e6 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 9 May 2021 18:50:31 +0800 Subject: [PATCH] Resign Iden --- examples/sqlx-mysql/src/example_cake.rs | 36 +++++++++++++++++++++--- examples/sqlx-mysql/src/example_fruit.rs | 35 ++++++++++++++++++++--- src/entity/base.rs | 6 +++- src/entity/column.rs | 12 ++------ src/tests_cfg/cake.rs | 36 +++++++++++++++++++++--- src/tests_cfg/fruit.rs | 35 ++++++++++++++++++++--- 6 files changed, 133 insertions(+), 27 deletions(-) diff --git a/examples/sqlx-mysql/src/example_cake.rs b/examples/sqlx-mysql/src/example_cake.rs index 85207281..ee8cc5f4 100644 --- a/examples/sqlx-mysql/src/example_cake.rs +++ b/examples/sqlx-mysql/src/example_cake.rs @@ -3,8 +3,7 @@ use sea_orm::{ ModelTrait, QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, PrimaryKeyTrait }; -#[derive(Default, Debug, Iden)] -#[iden = "cake"] +#[derive(Copy, Clone, Default, Debug)] pub struct Entity; #[derive(Clone, Debug, Default, PartialEq)] @@ -13,13 +12,13 @@ pub struct Model { pub name: String, } -#[derive(Copy, Clone, Debug, Iden, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter)] pub enum Column { Id, Name, } -#[derive(Copy, Clone, Debug, Iden, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter)] pub enum PrimaryKey { Id, } @@ -39,6 +38,20 @@ impl EntityTrait for Entity { type Relation = Relation; } +// TODO: implement with derive macro +impl Iden for Entity { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + +// TODO: implement with derive macro +impl IdenStatic for Entity { + fn as_str(&self) -> &str { + "cake" + } +} + // TODO: implement with derive macro impl ModelTrait for Model { type Column = Column; @@ -65,6 +78,13 @@ impl ModelTrait for Model { } } +// TODO: implement with derive macro +impl Iden for Column { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + // TODO: implement with derive macro impl IdenStatic for Column { fn as_str(&self) -> &str { @@ -86,6 +106,14 @@ impl ColumnTrait for Column { } } + +// TODO: implement with derive macro +impl Iden for PrimaryKey { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + // TODO: implement with derive macro impl IdenStatic for PrimaryKey { fn as_str(&self) -> &str { diff --git a/examples/sqlx-mysql/src/example_fruit.rs b/examples/sqlx-mysql/src/example_fruit.rs index d994fbb2..ac8b9557 100644 --- a/examples/sqlx-mysql/src/example_fruit.rs +++ b/examples/sqlx-mysql/src/example_fruit.rs @@ -3,8 +3,7 @@ use sea_orm::{ ModelTrait, QueryResult, RelationDef, RelationTrait, TypeErr, Value, PrimaryKeyTrait }; -#[derive(Default, Debug, Iden)] -#[iden = "fruit"] +#[derive(Copy, Clone, Default, Debug)] pub struct Entity; #[derive(Clone, Debug, Default, PartialEq)] @@ -14,14 +13,14 @@ pub struct Model { pub cake_id: Option, } -#[derive(Copy, Clone, Debug, Iden, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter)] pub enum Column { Id, Name, CakeId, } -#[derive(Copy, Clone, Debug, Iden, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter)] pub enum PrimaryKey { Id, } @@ -39,6 +38,20 @@ impl EntityTrait for Entity { type Relation = Relation; } +// TODO: implement with derive macro +impl Iden for Entity { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + +// TODO: implement with derive macro +impl IdenStatic for Entity { + fn as_str(&self) -> &str { + "fruit" + } +} + // TODO: implement with derive macro impl ModelTrait for Model { type Column = Column; @@ -68,6 +81,13 @@ impl ModelTrait for Model { } } +// TODO: implement with derive macro +impl Iden for Column { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + // TODO: implement with derive macro impl IdenStatic for Column { fn as_str(&self) -> &str { @@ -91,6 +111,13 @@ impl ColumnTrait for Column { } } +// TODO: implement with derive macro +impl Iden for PrimaryKey { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + // TODO: implement with derive macro impl IdenStatic for PrimaryKey { fn as_str(&self) -> &str { diff --git a/src/entity/base.rs b/src/entity/base.rs index 75c1390c..2f36a515 100644 --- a/src/entity/base.rs +++ b/src/entity/base.rs @@ -7,8 +7,12 @@ use sea_query::{Expr, Iden, IntoIden, Value}; use std::fmt::Debug; pub use strum::IntoEnumIterator as Iterable; +pub trait IdenStatic: Iden + Copy + Debug + 'static { + fn as_str(&self) -> &str; +} + #[async_trait] -pub trait EntityTrait: Iden + Default + Debug + 'static { +pub trait EntityTrait: IdenStatic + Default { type Model: ModelTrait; type Column: ColumnTrait + Iterable; diff --git a/src/entity/column.rs b/src/entity/column.rs index 1e453489..b555db38 100644 --- a/src/entity/column.rs +++ b/src/entity/column.rs @@ -1,7 +1,7 @@ -use crate::EntityTrait; +use crate::{IdenStatic, EntityTrait}; pub use sea_query::ColumnType; use sea_query::{Expr, Iden, SimpleExpr, Value}; -use std::fmt::Debug; + use std::rc::Rc; macro_rules! bind_oper { @@ -15,14 +15,6 @@ macro_rules! bind_oper { }; } -pub trait IdenStatic: Iden + Copy + Debug + 'static { - fn as_str(&self) -> &str; - - fn unquoted(&self, s: &mut dyn std::fmt::Write) { - write!(s, "{}", self.as_str()).unwrap(); - } -} - pub trait ColumnTrait: IdenStatic { type Entity: EntityTrait; diff --git a/src/tests_cfg/cake.rs b/src/tests_cfg/cake.rs index db96d95a..23878a0e 100644 --- a/src/tests_cfg/cake.rs +++ b/src/tests_cfg/cake.rs @@ -3,8 +3,7 @@ use crate::{ ModelTrait, QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, PrimaryKeyTrait }; -#[derive(Default, Debug, Iden)] -#[iden = "cake"] +#[derive(Copy, Clone, Default, Debug)] pub struct Entity; #[derive(Clone, Debug, Default, PartialEq)] @@ -13,13 +12,13 @@ pub struct Model { pub name: String, } -#[derive(Copy, Clone, Debug, Iden, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter)] pub enum Column { Id, Name, } -#[derive(Copy, Clone, Debug, Iden, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter)] pub enum PrimaryKey { Id, } @@ -39,6 +38,20 @@ impl EntityTrait for Entity { type Relation = Relation; } +// TODO: implement with derive macro +impl Iden for Entity { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + +// TODO: implement with derive macro +impl IdenStatic for Entity { + fn as_str(&self) -> &str { + "cake" + } +} + // TODO: implement with derive macro impl ModelTrait for Model { type Column = Column; @@ -65,6 +78,13 @@ impl ModelTrait for Model { } } +// TODO: implement with derive macro +impl Iden for Column { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + // TODO: implement with derive macro impl IdenStatic for Column { fn as_str(&self) -> &str { @@ -86,6 +106,14 @@ impl ColumnTrait for Column { } } + +// TODO: implement with derive macro +impl Iden for PrimaryKey { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + // TODO: implement with derive macro impl IdenStatic for PrimaryKey { fn as_str(&self) -> &str { diff --git a/src/tests_cfg/fruit.rs b/src/tests_cfg/fruit.rs index e3762afd..9d7dd0a1 100644 --- a/src/tests_cfg/fruit.rs +++ b/src/tests_cfg/fruit.rs @@ -3,8 +3,7 @@ use crate::{ ModelTrait, QueryResult, RelationDef, RelationTrait, TypeErr, Value, PrimaryKeyTrait }; -#[derive(Default, Debug, Iden)] -#[iden = "fruit"] +#[derive(Copy, Clone, Default, Debug)] pub struct Entity; #[derive(Clone, Debug, Default, PartialEq)] @@ -14,14 +13,14 @@ pub struct Model { pub cake_id: Option, } -#[derive(Copy, Clone, Debug, Iden, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter)] pub enum Column { Id, Name, CakeId, } -#[derive(Copy, Clone, Debug, Iden, EnumIter)] +#[derive(Copy, Clone, Debug, EnumIter)] pub enum PrimaryKey { Id, } @@ -39,6 +38,20 @@ impl EntityTrait for Entity { type Relation = Relation; } +// TODO: implement with derive macro +impl Iden for Entity { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + +// TODO: implement with derive macro +impl IdenStatic for Entity { + fn as_str(&self) -> &str { + "fruit" + } +} + // TODO: implement with derive macro impl ModelTrait for Model { type Column = Column; @@ -68,6 +81,13 @@ impl ModelTrait for Model { } } +// TODO: implement with derive macro +impl Iden for Column { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + // TODO: implement with derive macro impl IdenStatic for Column { fn as_str(&self) -> &str { @@ -91,6 +111,13 @@ impl ColumnTrait for Column { } } +// TODO: implement with derive macro +impl Iden for PrimaryKey { + fn unquoted(&self, s: &mut dyn std::fmt::Write) { + write!(s, "{}", self.as_str()).unwrap(); + } +} + // TODO: implement with derive macro impl IdenStatic for PrimaryKey { fn as_str(&self) -> &str {