Rename traits
This commit is contained in:
parent
66df42702c
commit
3f5b0080da
@ -1,4 +1,4 @@
|
|||||||
use sea_orm::{tests_cfg::*, Database, Entity};
|
use sea_orm::{tests_cfg::*, Database, EntityTrait};
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@ -9,7 +9,7 @@ async fn main() {
|
|||||||
println!("{:?}", db);
|
println!("{:?}", db);
|
||||||
println!();
|
println!();
|
||||||
|
|
||||||
let cakes = cake::Cake::find().all(&db).await.unwrap();
|
let cakes = cake::Entity::find().all(&db).await.unwrap();
|
||||||
|
|
||||||
for cc in cakes.iter() {
|
for cc in cakes.iter() {
|
||||||
println!("{:?}", cc);
|
println!("{:?}", cc);
|
||||||
|
@ -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>
|
impl<E: 'static> Select<'_, E>
|
||||||
where
|
where
|
||||||
E: Entity,
|
E: EntityTrait,
|
||||||
{
|
{
|
||||||
pub async fn one(self, db: &Database) -> Result<E::Model, QueryErr> {
|
pub async fn one(self, db: &Database) -> Result<E::Model, QueryErr> {
|
||||||
let builder = db.get_query_builder_backend();
|
let builder = db.get_query_builder_backend();
|
||||||
let row = db.get_connection().query_one(self.build(builder)).await?;
|
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> {
|
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 rows = db.get_connection().query_all(self.build(builder)).await?;
|
||||||
let mut models = Vec::new();
|
let mut models = Vec::new();
|
||||||
for row in rows.into_iter() {
|
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)
|
Ok(models)
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
use super::{Column, Identity, Model, Relation};
|
use super::{ColumnTrait, Identity, ModelTrait, RelationTrait};
|
||||||
use crate::Select;
|
use crate::Select;
|
||||||
use sea_query::Iden;
|
use sea_query::Iden;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
pub use strum::IntoEnumIterator as Iterable;
|
pub use strum::IntoEnumIterator as Iterable;
|
||||||
|
|
||||||
pub trait Entity: Iden + Default + Debug {
|
pub trait EntityTrait: Iden + Default + Debug {
|
||||||
type Model: Model;
|
type Model: ModelTrait;
|
||||||
|
|
||||||
type Column: Column + Iterable;
|
type Column: ColumnTrait + Iterable;
|
||||||
|
|
||||||
type Relation: Relation + Iterable;
|
type Relation: RelationTrait + Iterable;
|
||||||
|
|
||||||
fn primary_key() -> Identity;
|
fn primary_key() -> Identity;
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
pub use sea_query::ColumnType;
|
pub use sea_query::ColumnType;
|
||||||
use sea_query::Iden;
|
use sea_query::Iden;
|
||||||
|
|
||||||
pub trait Column: Iden {
|
pub trait ColumnTrait: Iden {
|
||||||
fn col_type(&self) -> ColumnType;
|
fn col_type(&self) -> ColumnType;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use crate::{QueryResult, TypeErr};
|
use crate::{QueryResult, TypeErr};
|
||||||
|
|
||||||
pub trait Model {
|
pub trait ModelTrait {
|
||||||
fn from_query_result(row: QueryResult) -> Result<Self, TypeErr>
|
fn from_query_result(row: QueryResult) -> Result<Self, TypeErr>
|
||||||
where
|
where
|
||||||
Self: std::marker::Sized;
|
Self: std::marker::Sized;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use super::{Identity, IntoIdentity};
|
use super::{Identity, IntoIdentity};
|
||||||
use crate::Entity;
|
use crate::EntityTrait;
|
||||||
use sea_query::{Iden, IntoIden};
|
use sea_query::{Iden, IntoIden};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@ -10,7 +10,7 @@ pub enum RelationType {
|
|||||||
BelongsTo,
|
BelongsTo,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Relation {
|
pub trait RelationTrait {
|
||||||
fn rel_def(&self) -> RelationDef;
|
fn rel_def(&self) -> RelationDef;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,28 +31,28 @@ pub struct RelationBuilder {
|
|||||||
impl RelationBuilder {
|
impl RelationBuilder {
|
||||||
pub fn has_one<E: 'static>(entity: E) -> Self
|
pub fn has_one<E: 'static>(entity: E) -> Self
|
||||||
where
|
where
|
||||||
E: Entity,
|
E: EntityTrait,
|
||||||
{
|
{
|
||||||
Self::new(RelationType::HasOne, entity)
|
Self::new(RelationType::HasOne, entity)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn has_many<E: 'static>(entity: E) -> Self
|
pub fn has_many<E: 'static>(entity: E) -> Self
|
||||||
where
|
where
|
||||||
E: Entity,
|
E: EntityTrait,
|
||||||
{
|
{
|
||||||
Self::new(RelationType::HasMany, entity)
|
Self::new(RelationType::HasMany, entity)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn belongs_to<E: 'static>(entity: E) -> Self
|
pub fn belongs_to<E: 'static>(entity: E) -> Self
|
||||||
where
|
where
|
||||||
E: Entity,
|
E: EntityTrait,
|
||||||
{
|
{
|
||||||
Self::new(RelationType::BelongsTo, entity)
|
Self::new(RelationType::BelongsTo, entity)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new<E: 'static>(rel_type: RelationType, entity: E) -> Self
|
fn new<E: 'static>(rel_type: RelationType, entity: E) -> Self
|
||||||
where
|
where
|
||||||
E: Entity,
|
E: EntityTrait,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
rel_type,
|
rel_type,
|
||||||
|
@ -11,3 +11,6 @@ pub use database::*;
|
|||||||
pub use driver::*;
|
pub use driver::*;
|
||||||
pub use entity::*;
|
pub use entity::*;
|
||||||
pub use query::*;
|
pub use query::*;
|
||||||
|
|
||||||
|
pub use sea_query::Iden;
|
||||||
|
pub use strum::EnumIter;
|
@ -1,4 +1,4 @@
|
|||||||
use crate::{entity::*, Iterable, RelationDef, Statement};
|
use crate::{EntityTrait, Identity, Iterable, RelationDef, 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;
|
||||||
@ -8,7 +8,7 @@ use std::rc::Rc;
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Select<'s, E: 'static>
|
pub struct Select<'s, E: 'static>
|
||||||
where
|
where
|
||||||
E: Entity,
|
E: EntityTrait,
|
||||||
{
|
{
|
||||||
select: SelectStatement,
|
select: SelectStatement,
|
||||||
entity: PhantomData<&'s E>,
|
entity: PhantomData<&'s E>,
|
||||||
@ -16,7 +16,7 @@ where
|
|||||||
|
|
||||||
impl<E: 'static> Select<'_, E>
|
impl<E: 'static> Select<'_, E>
|
||||||
where
|
where
|
||||||
E: Entity,
|
E: EntityTrait,
|
||||||
{
|
{
|
||||||
pub(crate) fn new(_: E) -> Self {
|
pub(crate) fn new(_: E) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -89,13 +89,13 @@ where
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::tests_cfg::cake;
|
use crate::tests_cfg::cake;
|
||||||
use crate::Entity;
|
use crate::EntityTrait;
|
||||||
use sea_query::MySqlQueryBuilder;
|
use sea_query::MySqlQueryBuilder;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_1() {
|
fn test_1() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
cake::Cake::find()
|
cake::Entity::find()
|
||||||
.build(MySqlQueryBuilder::default())
|
.build(MySqlQueryBuilder::default())
|
||||||
.to_string(),
|
.to_string(),
|
||||||
"SELECT `cake`.`id`, `cake`.`name` FROM `cake`"
|
"SELECT `cake`.`id`, `cake`.`name` FROM `cake`"
|
||||||
|
@ -1,41 +1,49 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
Column, ColumnType, Entity, Identity, IntoIdentity, Model, QueryResult, Relation, RelationDef,
|
ColumnTrait, ColumnType, EntityTrait, Identity, IntoIdentity, ModelTrait, QueryResult, RelationDef,
|
||||||
TypeErr,
|
RelationTrait, TypeErr, EnumIter, Iden
|
||||||
};
|
};
|
||||||
use sea_query::Iden;
|
|
||||||
use strum::EnumIter;
|
|
||||||
|
|
||||||
#[derive(Iden, Default, Debug)]
|
#[derive(Iden, Default, Debug)]
|
||||||
pub struct Cake;
|
#[iden = "cake"]
|
||||||
|
pub struct Entity;
|
||||||
|
|
||||||
#[derive(Debug, Default, PartialEq)]
|
#[derive(Debug, Default, PartialEq)]
|
||||||
pub struct CakeModel {
|
pub struct Model {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Iden, EnumIter)]
|
#[derive(Iden, EnumIter)]
|
||||||
pub enum CakeColumn {
|
pub enum Column {
|
||||||
Id,
|
Id,
|
||||||
Name,
|
Name,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(EnumIter)]
|
#[derive(EnumIter)]
|
||||||
pub enum CakeRelation {}
|
pub enum Relation {}
|
||||||
|
|
||||||
impl Entity for Cake {
|
impl EntityTrait for Entity {
|
||||||
type Model = CakeModel;
|
type Model = Model;
|
||||||
|
|
||||||
type Column = CakeColumn;
|
type Column = Column;
|
||||||
|
|
||||||
type Relation = CakeRelation;
|
type Relation = Relation;
|
||||||
|
|
||||||
fn primary_key() -> Identity {
|
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 {
|
fn col_type(&self) -> ColumnType {
|
||||||
match self {
|
match self {
|
||||||
Self::Id => ColumnType::Integer(None),
|
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 {
|
fn rel_def(&self) -> RelationDef {
|
||||||
panic!()
|
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")?,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user