Refactor PrimaryKeyTrait
This commit is contained in:
parent
f706eb261d
commit
c711d83e32
@ -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::PrimaryKeyTrait for #ident {}
|
||||||
|
|
||||||
impl sea_orm::PrimaryKeyToColumn<Entity> for #ident {
|
impl sea_orm::PrimaryKeyToColumn for #ident {
|
||||||
fn into_column(self) -> <Entity as EntityTrait>::Column {
|
type Column = Column;
|
||||||
|
|
||||||
|
fn into_column(self) -> Self::Column {
|
||||||
match self {
|
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 {
|
match col {
|
||||||
#(Column::#variant => Some(Self::#variant),)*
|
#(Self::Column::#variant => Some(Self::#variant),)*
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ pub trait EntityTrait: EntityName {
|
|||||||
|
|
||||||
type Relation: RelationTrait;
|
type Relation: RelationTrait;
|
||||||
|
|
||||||
type PrimaryKey: PrimaryKeyTrait + PrimaryKeyToColumn<Self>;
|
type PrimaryKey: PrimaryKeyTrait + PrimaryKeyToColumn<Column = Self::Column>;
|
||||||
|
|
||||||
fn has_one<R>(entity: R) -> RelationBuilder<Self, R>
|
fn has_one<R>(entity: R) -> RelationBuilder<Self, R>
|
||||||
where
|
where
|
||||||
@ -78,7 +78,6 @@ pub trait EntityTrait: EntityName {
|
|||||||
fn find_by<V>(values: V) -> Select<Self>
|
fn find_by<V>(values: V) -> Select<Self>
|
||||||
where
|
where
|
||||||
V: IntoValueTuple,
|
V: IntoValueTuple,
|
||||||
Self::PrimaryKey: PrimaryKeyToColumn<Self>,
|
|
||||||
{
|
{
|
||||||
let mut select = Self::find();
|
let mut select = Self::find();
|
||||||
let mut keys = Self::PrimaryKey::iter();
|
let mut keys = Self::PrimaryKey::iter();
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
use super::{EntityTrait, IdenStatic, Iterable};
|
use super::{ColumnTrait, IdenStatic, Iterable};
|
||||||
|
|
||||||
pub trait PrimaryKeyTrait: IdenStatic + Iterable {}
|
pub trait PrimaryKeyTrait: IdenStatic + Iterable {}
|
||||||
|
|
||||||
pub trait PrimaryKeyToColumn<E>
|
pub trait PrimaryKeyToColumn {
|
||||||
where
|
type Column: ColumnTrait;
|
||||||
E: EntityTrait,
|
|
||||||
{
|
|
||||||
fn into_column(self) -> E::Column;
|
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -86,7 +86,7 @@ where
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::tests_cfg::{cake, fruit};
|
use crate::tests_cfg::{cake, fruit};
|
||||||
use crate::{Update, QueryTrait, Val};
|
use crate::{QueryTrait, Update, Val};
|
||||||
use sea_query::PostgresQueryBuilder;
|
use sea_query::PostgresQueryBuilder;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user