Refactor PrimaryKeyTrait

This commit is contained in:
Chris Tsang 2021-06-03 02:48:44 +08:00
parent f706eb261d
commit c711d83e32
4 changed files with 35 additions and 33 deletions

View File

@ -47,16 +47,18 @@ pub fn expand_derive_primary_key(ident: Ident, data: Data) -> syn::Result<TokenS
impl sea_orm::PrimaryKeyTrait for #ident {}
impl sea_orm::PrimaryKeyToColumn<Entity> for #ident {
fn into_column(self) -> <Entity as EntityTrait>::Column {
impl sea_orm::PrimaryKeyToColumn for #ident {
type Column = Column;
fn into_column(self) -> Self::Column {
match self {
#(Self::#variant => Column::#variant,)*
#(Self::#variant => Self::Column::#variant,)*
}
}
fn from_column(col: <Entity as EntityTrait>::Column) -> Option<Self> {
fn from_column(col: Self::Column) -> Option<Self> {
match col {
#(Column::#variant => Some(Self::#variant),)*
#(Self::Column::#variant => Some(Self::#variant),)*
_ => None,
}
}

View File

@ -20,7 +20,7 @@ pub trait EntityTrait: EntityName {
type Relation: RelationTrait;
type PrimaryKey: PrimaryKeyTrait + PrimaryKeyToColumn<Self>;
type PrimaryKey: PrimaryKeyTrait + PrimaryKeyToColumn<Column = Self::Column>;
fn has_one<R>(entity: R) -> RelationBuilder<Self, R>
where
@ -78,7 +78,6 @@ pub trait EntityTrait: EntityName {
fn find_by<V>(values: V) -> Select<Self>
where
V: IntoValueTuple,
Self::PrimaryKey: PrimaryKeyToColumn<Self>,
{
let mut select = Self::find();
let mut keys = Self::PrimaryKey::iter();

View File

@ -1,12 +1,13 @@
use super::{EntityTrait, IdenStatic, Iterable};
use super::{ColumnTrait, IdenStatic, Iterable};
pub trait PrimaryKeyTrait: IdenStatic + Iterable {}
pub trait PrimaryKeyToColumn<E>
where
E: EntityTrait,
{
fn into_column(self) -> E::Column;
pub trait PrimaryKeyToColumn {
type Column: ColumnTrait;
fn from_column(col: E::Column) -> Option<Self> where Self: std::marker::Sized;
fn into_column(self) -> Self::Column;
fn from_column(col: Self::Column) -> Option<Self>
where
Self: Sized;
}

View File

@ -86,18 +86,18 @@ where
#[cfg(test)]
mod tests {
use crate::tests_cfg::{cake, fruit};
use crate::{Update, QueryTrait, Val};
use crate::{QueryTrait, Update, Val};
use sea_query::PostgresQueryBuilder;
#[test]
fn update_1() {
assert_eq!(
Update::<cake::ActiveModel>::new(cake::ActiveModel {
id: Val::set(1),
name: Val::set("Apple Pie".to_owned()),
})
.build(PostgresQueryBuilder)
.to_string(),
id: Val::set(1),
name: Val::set("Apple Pie".to_owned()),
})
.build(PostgresQueryBuilder)
.to_string(),
r#"UPDATE "cake" SET "name" = 'Apple Pie' WHERE "cake"."id" = 1"#,
);
}
@ -106,12 +106,12 @@ mod tests {
fn update_2() {
assert_eq!(
Update::<fruit::ActiveModel>::new(fruit::ActiveModel {
id: Val::set(1),
name: Val::set("Orange".to_owned()),
cake_id: Val::unset(),
})
.build(PostgresQueryBuilder)
.to_string(),
id: Val::set(1),
name: Val::set("Orange".to_owned()),
cake_id: Val::unset(),
})
.build(PostgresQueryBuilder)
.to_string(),
r#"UPDATE "fruit" SET "name" = 'Orange' WHERE "fruit"."id" = 1"#,
);
}
@ -120,13 +120,13 @@ mod tests {
fn update_3() {
assert_eq!(
Update::<fruit::ActiveModel>::new(fruit::ActiveModel {
id: Val::set(2),
name: Val::unset(),
cake_id: Val::set(Some(3)),
})
.build(PostgresQueryBuilder)
.to_string(),
id: Val::set(2),
name: Val::unset(),
cake_id: Val::set(Some(3)),
})
.build(PostgresQueryBuilder)
.to_string(),
r#"UPDATE "fruit" SET "cake_id" = 3 WHERE "fruit"."id" = 2"#,
);
}
}
}