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,18 +86,18 @@ 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]
|
||||||
fn update_1() {
|
fn update_1() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Update::<cake::ActiveModel>::new(cake::ActiveModel {
|
Update::<cake::ActiveModel>::new(cake::ActiveModel {
|
||||||
id: Val::set(1),
|
id: Val::set(1),
|
||||||
name: Val::set("Apple Pie".to_owned()),
|
name: Val::set("Apple Pie".to_owned()),
|
||||||
})
|
})
|
||||||
.build(PostgresQueryBuilder)
|
.build(PostgresQueryBuilder)
|
||||||
.to_string(),
|
.to_string(),
|
||||||
r#"UPDATE "cake" SET "name" = 'Apple Pie' WHERE "cake"."id" = 1"#,
|
r#"UPDATE "cake" SET "name" = 'Apple Pie' WHERE "cake"."id" = 1"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -106,12 +106,12 @@ mod tests {
|
|||||||
fn update_2() {
|
fn update_2() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Update::<fruit::ActiveModel>::new(fruit::ActiveModel {
|
Update::<fruit::ActiveModel>::new(fruit::ActiveModel {
|
||||||
id: Val::set(1),
|
id: Val::set(1),
|
||||||
name: Val::set("Orange".to_owned()),
|
name: Val::set("Orange".to_owned()),
|
||||||
cake_id: Val::unset(),
|
cake_id: Val::unset(),
|
||||||
})
|
})
|
||||||
.build(PostgresQueryBuilder)
|
.build(PostgresQueryBuilder)
|
||||||
.to_string(),
|
.to_string(),
|
||||||
r#"UPDATE "fruit" SET "name" = 'Orange' WHERE "fruit"."id" = 1"#,
|
r#"UPDATE "fruit" SET "name" = 'Orange' WHERE "fruit"."id" = 1"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -120,13 +120,13 @@ mod tests {
|
|||||||
fn update_3() {
|
fn update_3() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Update::<fruit::ActiveModel>::new(fruit::ActiveModel {
|
Update::<fruit::ActiveModel>::new(fruit::ActiveModel {
|
||||||
id: Val::set(2),
|
id: Val::set(2),
|
||||||
name: Val::unset(),
|
name: Val::unset(),
|
||||||
cake_id: Val::set(Some(3)),
|
cake_id: Val::set(Some(3)),
|
||||||
})
|
})
|
||||||
.build(PostgresQueryBuilder)
|
.build(PostgresQueryBuilder)
|
||||||
.to_string(),
|
.to_string(),
|
||||||
r#"UPDATE "fruit" SET "cake_id" = 3 WHERE "fruit"."id" = 2"#,
|
r#"UPDATE "fruit" SET "cake_id" = 3 WHERE "fruit"."id" = 2"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user