Rename traits

This commit is contained in:
Chris Tsang 2021-05-08 14:40:05 +08:00
parent 66df42702c
commit 3f5b0080da
9 changed files with 50 additions and 48 deletions

View File

@ -1,4 +1,4 @@
use sea_orm::{tests_cfg::*, Database, Entity};
use sea_orm::{tests_cfg::*, Database, EntityTrait};
#[async_std::main]
async fn main() {
@ -9,7 +9,7 @@ async fn main() {
println!("{:?}", db);
println!();
let cakes = cake::Cake::find().all(&db).await.unwrap();
let cakes = cake::Entity::find().all(&db).await.unwrap();
for cc in cakes.iter() {
println!("{:?}", cc);

View File

@ -1,13 +1,13 @@
use crate::{Connection, Database, Entity, Model, QueryErr, Select};
use crate::{Connection, Database, EntityTrait, ModelTrait, QueryErr, Select};
impl<E: 'static> Select<'_, E>
where
E: Entity,
E: EntityTrait,
{
pub async fn one(self, db: &Database) -> Result<E::Model, QueryErr> {
let builder = db.get_query_builder_backend();
let row = db.get_connection().query_one(self.build(builder)).await?;
Ok(<E as Entity>::Model::from_query_result(row)?)
Ok(<E as EntityTrait>::Model::from_query_result(row)?)
}
pub async fn all(self, db: &Database) -> Result<Vec<E::Model>, QueryErr> {
@ -15,7 +15,7 @@ where
let rows = db.get_connection().query_all(self.build(builder)).await?;
let mut models = Vec::new();
for row in rows.into_iter() {
models.push(<E as Entity>::Model::from_query_result(row)?);
models.push(<E as EntityTrait>::Model::from_query_result(row)?);
}
Ok(models)
}

View File

@ -1,15 +1,15 @@
use super::{Column, Identity, Model, Relation};
use super::{ColumnTrait, Identity, ModelTrait, RelationTrait};
use crate::Select;
use sea_query::Iden;
use std::fmt::Debug;
pub use strum::IntoEnumIterator as Iterable;
pub trait Entity: Iden + Default + Debug {
type Model: Model;
pub trait EntityTrait: Iden + Default + Debug {
type Model: ModelTrait;
type Column: Column + Iterable;
type Column: ColumnTrait + Iterable;
type Relation: Relation + Iterable;
type Relation: RelationTrait + Iterable;
fn primary_key() -> Identity;

View File

@ -1,6 +1,6 @@
pub use sea_query::ColumnType;
use sea_query::Iden;
pub trait Column: Iden {
pub trait ColumnTrait: Iden {
fn col_type(&self) -> ColumnType;
}

View File

@ -1,6 +1,6 @@
use crate::{QueryResult, TypeErr};
pub trait Model {
pub trait ModelTrait {
fn from_query_result(row: QueryResult) -> Result<Self, TypeErr>
where
Self: std::marker::Sized;

View File

@ -1,5 +1,5 @@
use super::{Identity, IntoIdentity};
use crate::Entity;
use crate::EntityTrait;
use sea_query::{Iden, IntoIden};
use std::rc::Rc;
@ -10,7 +10,7 @@ pub enum RelationType {
BelongsTo,
}
pub trait Relation {
pub trait RelationTrait {
fn rel_def(&self) -> RelationDef;
}
@ -31,28 +31,28 @@ pub struct RelationBuilder {
impl RelationBuilder {
pub fn has_one<E: 'static>(entity: E) -> Self
where
E: Entity,
E: EntityTrait,
{
Self::new(RelationType::HasOne, entity)
}
pub fn has_many<E: 'static>(entity: E) -> Self
where
E: Entity,
E: EntityTrait,
{
Self::new(RelationType::HasMany, entity)
}
pub fn belongs_to<E: 'static>(entity: E) -> Self
where
E: Entity,
E: EntityTrait,
{
Self::new(RelationType::BelongsTo, entity)
}
fn new<E: 'static>(rel_type: RelationType, entity: E) -> Self
where
E: Entity,
E: EntityTrait,
{
Self {
rel_type,

View File

@ -11,3 +11,6 @@ pub use database::*;
pub use driver::*;
pub use entity::*;
pub use query::*;
pub use sea_query::Iden;
pub use strum::EnumIter;

View File

@ -1,4 +1,4 @@
use crate::{entity::*, Iterable, RelationDef, Statement};
use crate::{EntityTrait, Identity, Iterable, RelationDef, Statement};
use core::fmt::Debug;
use core::marker::PhantomData;
pub use sea_query::JoinType;
@ -8,7 +8,7 @@ use std::rc::Rc;
#[derive(Debug)]
pub struct Select<'s, E: 'static>
where
E: Entity,
E: EntityTrait,
{
select: SelectStatement,
entity: PhantomData<&'s E>,
@ -16,7 +16,7 @@ where
impl<E: 'static> Select<'_, E>
where
E: Entity,
E: EntityTrait,
{
pub(crate) fn new(_: E) -> Self {
Self {
@ -89,13 +89,13 @@ where
#[cfg(test)]
mod tests {
use crate::tests_cfg::cake;
use crate::Entity;
use crate::EntityTrait;
use sea_query::MySqlQueryBuilder;
#[test]
fn test_1() {
assert_eq!(
cake::Cake::find()
cake::Entity::find()
.build(MySqlQueryBuilder::default())
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake`"

View File

@ -1,41 +1,49 @@
use crate::{
Column, ColumnType, Entity, Identity, IntoIdentity, Model, QueryResult, Relation, RelationDef,
TypeErr,
ColumnTrait, ColumnType, EntityTrait, Identity, IntoIdentity, ModelTrait, QueryResult, RelationDef,
RelationTrait, TypeErr, EnumIter, Iden
};
use sea_query::Iden;
use strum::EnumIter;
#[derive(Iden, Default, Debug)]
pub struct Cake;
#[iden = "cake"]
pub struct Entity;
#[derive(Debug, Default, PartialEq)]
pub struct CakeModel {
pub struct Model {
pub id: i32,
pub name: String,
}
#[derive(Iden, EnumIter)]
pub enum CakeColumn {
pub enum Column {
Id,
Name,
}
#[derive(EnumIter)]
pub enum CakeRelation {}
pub enum Relation {}
impl Entity for Cake {
type Model = CakeModel;
impl EntityTrait for Entity {
type Model = Model;
type Column = CakeColumn;
type Column = Column;
type Relation = CakeRelation;
type Relation = Relation;
fn primary_key() -> Identity {
CakeColumn::Id.into_identity()
Column::Id.into_identity()
}
}
impl Column for CakeColumn {
impl ModelTrait for Model {
fn from_query_result(row: QueryResult) -> Result<Self, TypeErr> {
Ok(Self {
id: row.try_get("id")?,
name: row.try_get("name")?,
})
}
}
impl ColumnTrait for Column {
fn col_type(&self) -> ColumnType {
match self {
Self::Id => ColumnType::Integer(None),
@ -44,17 +52,8 @@ impl Column for CakeColumn {
}
}
impl Relation for CakeRelation {
impl RelationTrait for Relation {
fn rel_def(&self) -> RelationDef {
panic!()
}
}
impl Model for CakeModel {
fn from_query_result(row: QueryResult) -> Result<Self, TypeErr> {
Ok(Self {
id: row.try_get("id")?,
name: row.try_get("name")?,
})
}
}