Redesign EntityName + entity::prelude

This commit is contained in:
Chris Tsang 2021-05-09 19:03:54 +08:00
parent 19b95eb64b
commit 00f2e125df
11 changed files with 67 additions and 57 deletions

View File

@ -1,7 +1,4 @@
use sea_orm::{ use sea_orm::entity::prelude::*;
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic, PrimaryKeyOfModel,
ModelTrait, QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, PrimaryKeyTrait
};
#[derive(Copy, Clone, Default, Debug)] #[derive(Copy, Clone, Default, Debug)]
pub struct Entity; pub struct Entity;
@ -39,11 +36,7 @@ impl EntityTrait for Entity {
} }
// TODO: implement with derive macro // TODO: implement with derive macro
impl Iden for Entity { impl EntityName for Entity {}
fn unquoted(&self, s: &mut dyn std::fmt::Write) {
write!(s, "{}", self.as_str()).unwrap();
}
}
// TODO: implement with derive macro // TODO: implement with derive macro
impl IdenStatic for Entity { impl IdenStatic for Entity {
@ -52,6 +45,13 @@ impl IdenStatic for Entity {
} }
} }
// 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 // TODO: implement with derive macro
impl ModelTrait for Model { impl ModelTrait for Model {
type Column = Column; type Column = Column;
@ -96,7 +96,7 @@ impl IdenStatic for Column {
} }
impl ColumnTrait for Column { impl ColumnTrait for Column {
type Entity = Entity; type EntityName = Entity;
fn def(&self) -> ColumnType { fn def(&self) -> ColumnType {
match self { match self {
@ -106,7 +106,6 @@ impl ColumnTrait for Column {
} }
} }
// TODO: implement with derive macro // TODO: implement with derive macro
impl Iden for PrimaryKey { impl Iden for PrimaryKey {
fn unquoted(&self, s: &mut dyn std::fmt::Write) { fn unquoted(&self, s: &mut dyn std::fmt::Write) {

View File

@ -1,7 +1,4 @@
use sea_orm::{ use sea_orm::entity::prelude::*;
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic, PrimaryKeyOfModel,
ModelTrait, QueryResult, RelationDef, RelationTrait, TypeErr, Value, PrimaryKeyTrait
};
#[derive(Copy, Clone, Default, Debug)] #[derive(Copy, Clone, Default, Debug)]
pub struct Entity; pub struct Entity;
@ -39,11 +36,7 @@ impl EntityTrait for Entity {
} }
// TODO: implement with derive macro // TODO: implement with derive macro
impl Iden for Entity { impl EntityName for Entity {}
fn unquoted(&self, s: &mut dyn std::fmt::Write) {
write!(s, "{}", self.as_str()).unwrap();
}
}
// TODO: implement with derive macro // TODO: implement with derive macro
impl IdenStatic for Entity { impl IdenStatic for Entity {
@ -52,6 +45,13 @@ impl IdenStatic for Entity {
} }
} }
// 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 // TODO: implement with derive macro
impl ModelTrait for Model { impl ModelTrait for Model {
type Column = Column; type Column = Column;
@ -100,7 +100,7 @@ impl IdenStatic for Column {
} }
impl ColumnTrait for Column { impl ColumnTrait for Column {
type Entity = Entity; type EntityName = Entity;
fn def(&self) -> ColumnType { fn def(&self) -> ColumnType {
match self { match self {

View File

@ -1,6 +1,6 @@
use crate::{ use crate::{
ColumnTrait, Connection, Database, ModelTrait, QueryErr, RelationBuilder, ColumnTrait, Connection, Database, ModelTrait, PrimaryKeyTrait, QueryErr, RelationBuilder,
RelationTrait, RelationType, Select, PrimaryKeyTrait RelationTrait, RelationType, Select,
}; };
use async_trait::async_trait; use async_trait::async_trait;
use sea_query::{Expr, Iden, IntoIden, Value}; use sea_query::{Expr, Iden, IntoIden, Value};
@ -11,8 +11,10 @@ pub trait IdenStatic: Iden + Copy + Debug + 'static {
fn as_str(&self) -> &str; fn as_str(&self) -> &str;
} }
pub trait EntityName: IdenStatic + Default {}
#[async_trait] #[async_trait]
pub trait EntityTrait: IdenStatic + Default { pub trait EntityTrait: EntityName {
type Model: ModelTrait; type Model: ModelTrait;
type Column: ColumnTrait + Iterable; type Column: ColumnTrait + Iterable;

View File

@ -1,4 +1,4 @@
use crate::{IdenStatic, EntityTrait}; use crate::{EntityName, IdenStatic};
pub use sea_query::ColumnType; pub use sea_query::ColumnType;
use sea_query::{Expr, Iden, SimpleExpr, Value}; use sea_query::{Expr, Iden, SimpleExpr, Value};
@ -16,12 +16,12 @@ macro_rules! bind_oper {
} }
pub trait ColumnTrait: IdenStatic { pub trait ColumnTrait: IdenStatic {
type Entity: EntityTrait; type EntityName: EntityName;
fn def(&self) -> ColumnType; fn def(&self) -> ColumnType;
fn as_iden(&self) -> Rc<dyn Iden> { fn as_iden(&self) -> Rc<dyn Iden> {
Rc::new(Self::Entity::default()) as Rc<dyn Iden> Rc::new(Self::EntityName::default()) as Rc<dyn Iden>
} }
bind_oper!(eq); bind_oper!(eq);

View File

@ -2,6 +2,7 @@ mod base;
mod column; mod column;
mod identity; mod identity;
mod model; mod model;
pub mod prelude;
mod primary_key; mod primary_key;
mod relation; mod relation;
@ -9,5 +10,6 @@ pub use base::*;
pub use column::*; pub use column::*;
pub use identity::*; pub use identity::*;
pub use model::*; pub use model::*;
// pub use prelude::*;
pub use primary_key::*; pub use primary_key::*;
pub use relation::*; pub use relation::*;

5
src/entity/prelude.rs Normal file
View File

@ -0,0 +1,5 @@
pub use crate::{
ColumnTrait, ColumnType, EntityName, EntityTrait, EnumIter, Iden, IdenStatic, ModelTrait,
PrimaryKeyOfModel, PrimaryKeyTrait, QueryResult, Related, RelationDef, RelationTrait, Select,
TypeErr, Value,
};

View File

@ -1,4 +1,4 @@
use super::{ModelTrait, IdenStatic}; use super::{IdenStatic, ModelTrait};
pub trait PrimaryKeyTrait: IdenStatic {} pub trait PrimaryKeyTrait: IdenStatic {}

View File

@ -1,7 +1,7 @@
mod connector; mod connector;
mod database; mod database;
mod driver; mod driver;
mod entity; pub mod entity;
mod query; mod query;
pub mod tests_cfg; pub mod tests_cfg;
mod util; mod util;

View File

@ -1,4 +1,7 @@
use crate::{EntityTrait, Identity, Iterable, PrimaryKeyOfModel, RelationDef, RelationTrait, Statement, Related}; use crate::{
EntityTrait, Identity, Iterable, PrimaryKeyOfModel, Related, RelationDef, RelationTrait,
Statement,
};
use core::fmt::Debug; use core::fmt::Debug;
use core::marker::PhantomData; use core::marker::PhantomData;
pub use sea_query::JoinType; pub use sea_query::JoinType;
@ -88,7 +91,10 @@ where
} }
pub fn belongs_to<R>(self, model: &R::Model) -> Self pub fn belongs_to<R>(self, model: &R::Model) -> Self
where R: EntityTrait + Related<E>, R::PrimaryKey: PrimaryKeyOfModel<R::Model> { where
R: EntityTrait + Related<E>,
R::PrimaryKey: PrimaryKeyOfModel<R::Model>,
{
use crate::{ColumnTrait, ModelTrait}; use crate::{ColumnTrait, ModelTrait};
if let Some(key) = R::PrimaryKey::iter().next() { if let Some(key) = R::PrimaryKey::iter().next() {
@ -249,16 +255,13 @@ mod tests {
#[test] #[test]
fn join_5() { fn join_5() {
let cake_model = cake::Model { let cake_model = cake::Model {
id: 12, id: 12,
name: "".to_owned(), name: "".to_owned(),
}; };
assert_eq!( assert_eq!(
cake_model.find_fruit() cake_model.find_fruit().build(MysqlQueryBuilder).to_string(),
.build(MysqlQueryBuilder)
.to_string(),
[ [
"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`", "SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`",
"INNER JOIN `cake` ON `cake`.`id` = `fruit`.`cake_id`", "INNER JOIN `cake` ON `cake`.`id` = `fruit`.`cake_id`",

View File

@ -1,7 +1,4 @@
use crate::{ use crate::entity::prelude::*;
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic, PrimaryKeyOfModel,
ModelTrait, QueryResult, Related, RelationDef, RelationTrait, Select, TypeErr, Value, PrimaryKeyTrait
};
#[derive(Copy, Clone, Default, Debug)] #[derive(Copy, Clone, Default, Debug)]
pub struct Entity; pub struct Entity;
@ -39,11 +36,7 @@ impl EntityTrait for Entity {
} }
// TODO: implement with derive macro // TODO: implement with derive macro
impl Iden for Entity { impl EntityName for Entity {}
fn unquoted(&self, s: &mut dyn std::fmt::Write) {
write!(s, "{}", self.as_str()).unwrap();
}
}
// TODO: implement with derive macro // TODO: implement with derive macro
impl IdenStatic for Entity { impl IdenStatic for Entity {
@ -52,6 +45,13 @@ impl IdenStatic for Entity {
} }
} }
// 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 // TODO: implement with derive macro
impl ModelTrait for Model { impl ModelTrait for Model {
type Column = Column; type Column = Column;
@ -96,7 +96,7 @@ impl IdenStatic for Column {
} }
impl ColumnTrait for Column { impl ColumnTrait for Column {
type Entity = Entity; type EntityName = Entity;
fn def(&self) -> ColumnType { fn def(&self) -> ColumnType {
match self { match self {
@ -106,7 +106,6 @@ impl ColumnTrait for Column {
} }
} }
// TODO: implement with derive macro // TODO: implement with derive macro
impl Iden for PrimaryKey { impl Iden for PrimaryKey {
fn unquoted(&self, s: &mut dyn std::fmt::Write) { fn unquoted(&self, s: &mut dyn std::fmt::Write) {

View File

@ -1,7 +1,4 @@
use crate::{ use crate::entity::prelude::*;
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, IdenStatic, PrimaryKeyOfModel,
ModelTrait, QueryResult, RelationDef, RelationTrait, TypeErr, Value, PrimaryKeyTrait
};
#[derive(Copy, Clone, Default, Debug)] #[derive(Copy, Clone, Default, Debug)]
pub struct Entity; pub struct Entity;
@ -39,11 +36,7 @@ impl EntityTrait for Entity {
} }
// TODO: implement with derive macro // TODO: implement with derive macro
impl Iden for Entity { impl EntityName for Entity {}
fn unquoted(&self, s: &mut dyn std::fmt::Write) {
write!(s, "{}", self.as_str()).unwrap();
}
}
// TODO: implement with derive macro // TODO: implement with derive macro
impl IdenStatic for Entity { impl IdenStatic for Entity {
@ -52,6 +45,13 @@ impl IdenStatic for Entity {
} }
} }
// 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 // TODO: implement with derive macro
impl ModelTrait for Model { impl ModelTrait for Model {
type Column = Column; type Column = Column;
@ -100,7 +100,7 @@ impl IdenStatic for Column {
} }
impl ColumnTrait for Column { impl ColumnTrait for Column {
type Entity = Entity; type EntityName = Entity;
fn def(&self) -> ColumnType { fn def(&self) -> ColumnType {
match self { match self {