Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support composite primary key of length up to 12 #1508

Merged
merged 6 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 88 additions & 20 deletions src/entity/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@ use crate::{ColumnTrait, EntityTrait, IdenStatic};
use sea_query::{Alias, DynIden, Iden, IntoIden, SeaRc};
use std::fmt;

/// Defines an operation for an Entity
/// List of column identifier
#[derive(Debug, Clone)]
pub enum Identity {
/// Performs one operation
/// Column identifier consists of 1 column
Unary(DynIden),
/// Performs two operations
/// Column identifier consists of 2 columns
Binary(DynIden, DynIden),
/// Performs three operations
/// Column identifier consists of 3 columns
Ternary(DynIden, DynIden, DynIden),
/// Column identifier consists of more than 3 columns
Many(Vec<DynIden>),
}

impl IntoIterator for Identity {
type Item = DynIden;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
match self {
Identity::Unary(ident1) => vec![ident1].into_iter(),
Identity::Binary(ident1, ident2) => vec![ident1, ident2].into_iter(),
Identity::Ternary(ident1, ident2, ident3) => vec![ident1, ident2, ident3].into_iter(),
Identity::Many(vec) => vec.into_iter(),
}
}
}

impl Iden for Identity {
Expand All @@ -28,6 +44,11 @@ impl Iden for Identity {
write!(s, "{}", iden2.to_string()).unwrap();
write!(s, "{}", iden3.to_string()).unwrap();
}
Identity::Many(vec) => {
for iden in vec.iter() {
write!(s, "{}", iden.to_string()).unwrap();
}
}
}
}
}
Expand All @@ -47,6 +68,12 @@ where
fn identity_of(self) -> Identity;
}

impl IntoIdentity for Identity {
fn into_identity(self) -> Identity {
self
}
}

impl IntoIdentity for String {
fn into_identity(self) -> Identity {
self.as_str().into_identity()
Expand Down Expand Up @@ -89,6 +116,36 @@ where
}
}

macro_rules! impl_into_identity {
( $($T:ident : $N:tt),+ $(,)? ) => {
impl< $($T),+ > IntoIdentity for ( $($T),+ )
where
$($T: IdenStatic),+
{
fn into_identity(self) -> Identity {
Identity::Many(vec![
$(self.$N.into_iden()),+
])
}
}
};
}

#[rustfmt::skip]
mod impl_into_identity {
use super::*;

impl_into_identity!(T0:0, T1:1, T2:2, T3:3);
impl_into_identity!(T0:0, T1:1, T2:2, T3:3, T4:4);
impl_into_identity!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5);
impl_into_identity!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6);
impl_into_identity!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6, T7:7);
impl_into_identity!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6, T7:7, T8:8);
impl_into_identity!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6, T7:7, T8:8, T9:9);
impl_into_identity!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6, T7:7, T8:8, T9:9, T10:10);
impl_into_identity!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6, T7:7, T8:8, T9:9, T10:10, T11:11);
}

impl<E, C> IdentityOf<E> for C
where
E: EntityTrait<Column = C>,
Expand All @@ -99,22 +156,33 @@ where
}
}

impl<E, C> IdentityOf<E> for (C, C)
where
E: EntityTrait<Column = C>,
C: ColumnTrait,
{
fn identity_of(self) -> Identity {
self.into_identity()
}
macro_rules! impl_identity_of {
( $($T:ident),+ $(,)? ) => {
impl<E, C> IdentityOf<E> for ( $($T),+ )
where
E: EntityTrait<Column = C>,
C: ColumnTrait,
{
fn identity_of(self) -> Identity {
self.into_identity()
}
}
};
}

impl<E, C> IdentityOf<E> for (C, C, C)
where
E: EntityTrait<Column = C>,
C: ColumnTrait,
{
fn identity_of(self) -> Identity {
self.into_identity()
}
#[rustfmt::skip]
mod impl_identity_of {
use super::*;

impl_identity_of!(C, C);
impl_identity_of!(C, C, C);
impl_identity_of!(C, C, C, C);
impl_identity_of!(C, C, C, C, C);
impl_identity_of!(C, C, C, C, C, C);
impl_identity_of!(C, C, C, C, C, C, C);
impl_identity_of!(C, C, C, C, C, C, C, C);
impl_identity_of!(C, C, C, C, C, C, C, C, C);
impl_identity_of!(C, C, C, C, C, C, C, C, C, C);
impl_identity_of!(C, C, C, C, C, C, C, C, C, C, C);
impl_identity_of!(C, C, C, C, C, C, C, C, C, C, C, C);
}
194 changes: 194 additions & 0 deletions src/entity/primary_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,197 @@ pub trait PrimaryKeyToColumn {
where
Self: Sized;
}

#[cfg(test)]
mod tests {
#[test]
#[cfg(feature = "macros")]
fn test_composite_primary_key() {
mod primary_key_of_1 {
use crate as sea_orm;
use crate::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "primary_key_of_1")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub owner: String,
pub name: String,
pub description: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
}

mod primary_key_of_2 {
use crate as sea_orm;
use crate::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "primary_key_of_2")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id_1: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id_2: String,
pub owner: String,
pub name: String,
pub description: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
}

mod primary_key_of_3 {
use crate as sea_orm;
use crate::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "primary_key_of_3")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id_1: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id_2: String,
#[sea_orm(primary_key, auto_increment = false)]
pub id_3: Uuid,
pub owner: String,
pub name: String,
pub description: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
}

mod primary_key_of_4 {
use crate as sea_orm;
use crate::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "primary_key_of_4")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id_1: TimeDateTimeWithTimeZone,
#[sea_orm(primary_key, auto_increment = false)]
pub id_2: Uuid,
#[sea_orm(primary_key, auto_increment = false)]
pub id_3: Json,
#[sea_orm(primary_key, auto_increment = false)]
pub id_4: Decimal,
pub owner: String,
pub name: String,
pub description: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
}

mod primary_key_of_11 {
use crate as sea_orm;
use crate::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
#[sea_orm(
rs_type = "String",
db_type = "String(Some(1))",
enum_name = "category"
)]
pub enum DeriveCategory {
#[sea_orm(string_value = "B")]
Big,
#[sea_orm(string_value = "S")]
Small,
}

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "primary_key_of_11")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id_1: Vec<u8>,
#[sea_orm(primary_key, auto_increment = false)]
pub id_2: DeriveCategory,
#[sea_orm(primary_key, auto_increment = false)]
pub id_3: Date,
#[sea_orm(primary_key, auto_increment = false)]
pub id_4: DateTime,
#[sea_orm(primary_key, auto_increment = false)]
pub id_5: Time,
#[sea_orm(primary_key, auto_increment = false)]
pub id_6: TimeTime,
#[sea_orm(primary_key, auto_increment = false)]
pub id_7: DateTime,
#[sea_orm(primary_key, auto_increment = false)]
pub id_8: TimeDateTime,
#[sea_orm(primary_key, auto_increment = false)]
pub id_9: DateTimeLocal,
#[sea_orm(primary_key, auto_increment = false)]
pub id_10: DateTimeUtc,
#[sea_orm(primary_key, auto_increment = false)]
pub id_11: DateTimeWithTimeZone,
pub owner: String,
pub name: String,
pub description: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
}

mod primary_key_of_12 {
use crate as sea_orm;
use crate::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "primary_key_of_12")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id_1: String,
#[sea_orm(primary_key, auto_increment = false)]
pub id_2: i8,
#[sea_orm(primary_key, auto_increment = false)]
pub id_3: u8,
#[sea_orm(primary_key, auto_increment = false)]
pub id_4: i16,
#[sea_orm(primary_key, auto_increment = false)]
pub id_5: u16,
#[sea_orm(primary_key, auto_increment = false)]
pub id_6: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id_7: u32,
#[sea_orm(primary_key, auto_increment = false)]
pub id_8: i64,
#[sea_orm(primary_key, auto_increment = false)]
pub id_9: u64,
#[sea_orm(primary_key, auto_increment = false)]
pub id_10: f32,
#[sea_orm(primary_key, auto_increment = false)]
pub id_11: f64,
#[sea_orm(primary_key, auto_increment = false)]
pub id_12: bool,
pub owner: String,
pub name: String,
pub description: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
}
}
}
36 changes: 11 additions & 25 deletions src/entity/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,31 +365,17 @@ where

macro_rules! set_foreign_key_stmt {
( $relation: ident, $foreign_key: ident ) => {
let from_cols: Vec<String> = match $relation.from_col {
Identity::Unary(o1) => vec![o1],
Identity::Binary(o1, o2) => vec![o1, o2],
Identity::Ternary(o1, o2, o3) => vec![o1, o2, o3],
}
.into_iter()
.map(|col| {
let col_name = col.to_string();
$foreign_key.from_col(col);
col_name
})
.collect();
match $relation.to_col {
Identity::Unary(o1) => {
$foreign_key.to_col(o1);
}
Identity::Binary(o1, o2) => {
$foreign_key.to_col(o1);
$foreign_key.to_col(o2);
}
Identity::Ternary(o1, o2, o3) => {
$foreign_key.to_col(o1);
$foreign_key.to_col(o2);
$foreign_key.to_col(o3);
}
let from_cols: Vec<String> = $relation
.from_col
.into_iter()
.map(|col| {
let col_name = col.to_string();
$foreign_key.from_col(col);
col_name
})
.collect();
for col in $relation.to_col.into_iter() {
$foreign_key.to_col(col);
}
if let Some(action) = $relation.on_delete {
$foreign_key.on_delete(action);
Expand Down
Loading