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,7 +86,7 @@ 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]