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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
use crate::{IdenStatic, EntityTrait};
use crate::{EntityName, IdenStatic};
pub use sea_query::ColumnType;
use sea_query::{Expr, Iden, SimpleExpr, Value};
@ -16,12 +16,12 @@ macro_rules! bind_oper {
}
pub trait ColumnTrait: IdenStatic {
type Entity: EntityTrait;
type EntityName: EntityName;
fn def(&self) -> ColumnType;
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);

View File

@ -2,6 +2,7 @@ mod base;
mod column;
mod identity;
mod model;
pub mod prelude;
mod primary_key;
mod relation;
@ -9,5 +10,6 @@ pub use base::*;
pub use column::*;
pub use identity::*;
pub use model::*;
// pub use prelude::*;
pub use primary_key::*;
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 {}

View File

@ -1,7 +1,7 @@
mod connector;
mod database;
mod driver;
mod entity;
pub mod entity;
mod query;
pub mod tests_cfg;
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::marker::PhantomData;
pub use sea_query::JoinType;
@ -88,7 +91,10 @@ where
}
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};
if let Some(key) = R::PrimaryKey::iter().next() {
@ -249,16 +255,13 @@ mod tests {
#[test]
fn join_5() {
let cake_model = cake::Model {
id: 12,
name: "".to_owned(),
};
assert_eq!(
cake_model.find_fruit()
.build(MysqlQueryBuilder)
.to_string(),
cake_model.find_fruit().build(MysqlQueryBuilder).to_string(),
[
"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`",
"INNER JOIN `cake` ON `cake`.`id` = `fruit`.`cake_id`",

View File

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

View File

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