Relation with Fruit
This commit is contained in:
parent
826e59309a
commit
add2e1ca0c
@ -5,3 +5,12 @@ CREATE TABLE `cake` (
|
||||
`name` varchar(255) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
DROP TABLE IF EXISTS `fruit`;
|
||||
|
||||
CREATE TABLE `fruit` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(255) DEFAULT NULL,
|
||||
`cake_id` int,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
@ -53,6 +53,17 @@ impl QueryResult {
|
||||
{
|
||||
T::try_get(self, col)
|
||||
}
|
||||
|
||||
pub fn try_get_option<T>(&self, col: &str) -> Result<Option<T>, TypeErr>
|
||||
where
|
||||
T: TryGetable,
|
||||
{
|
||||
if let Ok(v) = T::try_get(self, col) {
|
||||
Ok(Some(v))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TypeErr //
|
||||
|
@ -69,16 +69,16 @@ where
|
||||
self
|
||||
}
|
||||
|
||||
pub fn left_join(self, relation: RelationDef) -> Self {
|
||||
self.prepare_join(JoinType::LeftJoin, relation)
|
||||
pub fn left_join(self, rel: E::Relation) -> Self {
|
||||
self.prepare_join(JoinType::LeftJoin, E::Relation::rel_def(&rel))
|
||||
}
|
||||
|
||||
pub fn right_join(self, relation: RelationDef) -> Self {
|
||||
self.prepare_join(JoinType::RightJoin, relation)
|
||||
pub fn right_join(self, rel: E::Relation) -> Self {
|
||||
self.prepare_join(JoinType::RightJoin, E::Relation::rel_def(&rel))
|
||||
}
|
||||
|
||||
pub fn inner_join(self, relation: RelationDef) -> Self {
|
||||
self.prepare_join(JoinType::InnerJoin, relation)
|
||||
pub fn inner_join(self, rel: E::Relation) -> Self {
|
||||
self.prepare_join(JoinType::InnerJoin, E::Relation::rel_def(&rel))
|
||||
}
|
||||
|
||||
pub fn query(&mut self) -> &mut SelectStatement {
|
||||
@ -103,7 +103,7 @@ where
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::tests_cfg::cake;
|
||||
use crate::tests_cfg::{cake, fruit};
|
||||
use crate::{ColumnTrait, EntityTrait};
|
||||
use sea_query::MysqlQueryBuilder;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, Identity, IntoIdentity, ModelTrait,
|
||||
QueryResult, RelationDef, RelationTrait, TypeErr,
|
||||
QueryResult, RelationBuilder, RelationDef, RelationTrait, TypeErr,
|
||||
};
|
||||
|
||||
#[derive(Default, Debug, Iden)]
|
||||
@ -20,7 +20,9 @@ pub enum Column {
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, EnumIter)]
|
||||
pub enum Relation {}
|
||||
pub enum Relation {
|
||||
Fruit,
|
||||
}
|
||||
|
||||
impl EntityTrait for Entity {
|
||||
type Model = Model;
|
||||
@ -56,6 +58,11 @@ impl ColumnTrait for Column {
|
||||
|
||||
impl RelationTrait for Relation {
|
||||
fn rel_def(&self) -> RelationDef {
|
||||
panic!()
|
||||
match self {
|
||||
Self::Fruit => RelationBuilder::has_many(super::fruit::Entity)
|
||||
.from(Column::Id)
|
||||
.to(super::fruit::Column::CakeId)
|
||||
.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
65
src/tests_cfg/fruit.rs
Normal file
65
src/tests_cfg/fruit.rs
Normal file
@ -0,0 +1,65 @@
|
||||
use crate::{
|
||||
ColumnTrait, ColumnType, EntityTrait, EnumIter, Iden, Identity, IntoIdentity, ModelTrait,
|
||||
QueryResult, RelationDef, RelationTrait, TypeErr,
|
||||
};
|
||||
|
||||
#[derive(Default, Debug, Iden)]
|
||||
#[iden = "fruit"]
|
||||
pub struct Entity;
|
||||
|
||||
#[derive(Debug, Default, PartialEq)]
|
||||
pub struct Model {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub cake_id: Option<i32>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Iden, EnumIter)]
|
||||
pub enum Column {
|
||||
Id,
|
||||
Name,
|
||||
CakeId,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, EnumIter)]
|
||||
pub enum Relation {}
|
||||
|
||||
impl EntityTrait for Entity {
|
||||
type Model = Model;
|
||||
|
||||
type Column = Column;
|
||||
|
||||
type Relation = Relation;
|
||||
|
||||
fn primary_key() -> Identity {
|
||||
Column::Id.into_identity()
|
||||
}
|
||||
}
|
||||
|
||||
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")?,
|
||||
cake_id: row.try_get_option("cake_id")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ColumnTrait for Column {
|
||||
type Entity = Entity;
|
||||
|
||||
fn col_type(&self) -> ColumnType {
|
||||
match self {
|
||||
Self::Id => ColumnType::Integer(None),
|
||||
Self::Name => ColumnType::String(None),
|
||||
Self::CakeId => ColumnType::Integer(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RelationTrait for Relation {
|
||||
fn rel_def(&self) -> RelationDef {
|
||||
panic!()
|
||||
}
|
||||
}
|
@ -1 +1,2 @@
|
||||
pub mod cake;
|
||||
pub mod fruit;
|
||||
|
Loading…
x
Reference in New Issue
Block a user