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 Up to 6 Values Composite Primary Key #353

Merged
merged 3 commits into from
Dec 4, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
path: [86, 249, 262, 319, 324]
path: [86, 249, 262, 319, 324, 352]
steps:
- uses: actions/checkout@v2

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ futures-util = { version = "^0.3" }
log = { version = "^0.4", optional = true }
rust_decimal = { version = "^1", optional = true }
sea-orm-macros = { version = "^0.4.0", path = "sea-orm-macros", optional = true }
sea-query = { version = "^0.19.0", features = ["thread-safe"] }
sea-query = { version = "^0.19.0", git = "https://github.com/SeaQL/sea-query.git", branch = "sea-orm/issues/352", features = ["thread-safe"] }
sea-strum = { version = "^0.21", features = ["derive", "sea-orm"] }
serde = { version = "^1.0", features = ["derive"] }
serde_json = { version = "^1", optional = true }
Expand Down
11 changes: 11 additions & 0 deletions issues/352/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[workspace]
# A separate workspace

[package]
name = "sea-orm-issues-352"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
sea-orm = { path = "../../", features = [ "sqlx-mysql", "runtime-async-std-native-tls" ]}
18 changes: 18 additions & 0 deletions issues/352/src/binary_primary_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
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 {}
8 changes: 8 additions & 0 deletions issues/352/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod unary_primary_key;
mod binary_primary_key;
mod ternary_primary_key;
mod quaternary_primary_key;
mod quinary_primary_key;
mod senary_primary_key;

pub fn main() {}
22 changes: 22 additions & 0 deletions issues/352/src/quaternary_primary_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
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: f64,
#[sea_orm(primary_key, auto_increment = false)]
pub id_4: Uuid,
pub owner: String,
pub name: String,
pub description: String,
}

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

impl ActiveModelBehavior for ActiveModel {}
24 changes: 24 additions & 0 deletions issues/352/src/quinary_primary_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
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: f64,
#[sea_orm(primary_key, auto_increment = false)]
pub id_4: Uuid,
#[sea_orm(primary_key, auto_increment = false)]
pub id_5: DateTime,
pub owner: String,
pub name: String,
pub description: String,
}

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

impl ActiveModelBehavior for ActiveModel {}
26 changes: 26 additions & 0 deletions issues/352/src/senary_primary_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
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: f64,
#[sea_orm(primary_key, auto_increment = false)]
pub id_4: Uuid,
#[sea_orm(primary_key, auto_increment = false)]
pub id_5: DateTime,
#[sea_orm(primary_key, auto_increment = false)]
pub id_6: DateTimeWithTimeZone,
pub owner: String,
pub name: String,
pub description: String,
}

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

impl ActiveModelBehavior for ActiveModel {}
20 changes: 20 additions & 0 deletions issues/352/src/ternary_primary_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
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: f64,
pub owner: String,
pub name: String,
pub description: String,
}

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

impl ActiveModelBehavior for ActiveModel {}
16 changes: 16 additions & 0 deletions issues/352/src/unary_primary_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use sea_orm::entity::prelude::*;

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

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

impl ActiveModelBehavior for ActiveModel {}
66 changes: 64 additions & 2 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,48 @@ where
}
}

impl<A, B, C, D, E> TryGetableMany for (A, B, C, D, E)
where
A: TryGetable,
B: TryGetable,
C: TryGetable,
D: TryGetable,
E: TryGetable,
{
fn try_get_many(res: &QueryResult, pre: &str, cols: &[String]) -> Result<Self, TryGetError> {
try_get_many_with_slice_len_of(5, cols)?;
Ok((
A::try_get(res, pre, &cols[0])?,
B::try_get(res, pre, &cols[1])?,
C::try_get(res, pre, &cols[2])?,
D::try_get(res, pre, &cols[3])?,
E::try_get(res, pre, &cols[4])?,
))
}
}

impl<A, B, C, D, E, F> TryGetableMany for (A, B, C, D, E, F)
where
A: TryGetable,
B: TryGetable,
C: TryGetable,
D: TryGetable,
E: TryGetable,
F: TryGetable,
{
fn try_get_many(res: &QueryResult, pre: &str, cols: &[String]) -> Result<Self, TryGetError> {
try_get_many_with_slice_len_of(6, cols)?;
Ok((
A::try_get(res, pre, &cols[0])?,
B::try_get(res, pre, &cols[1])?,
C::try_get(res, pre, &cols[2])?,
D::try_get(res, pre, &cols[3])?,
E::try_get(res, pre, &cols[4])?,
F::try_get(res, pre, &cols[5])?,
))
}
}

fn try_get_many_with_slice_len_of(len: usize, cols: &[String]) -> Result<(), TryGetError> {
if cols.len() < len {
Err(TryGetError::DbErr(DbErr::Query(format!(
Expand Down Expand Up @@ -501,6 +543,8 @@ macro_rules! try_from_u64_err {
try_from_u64_err!(A, B);
try_from_u64_err!(A, B, C);
try_from_u64_err!(A, B, C, D);
try_from_u64_err!(A, B, C, D, E);
try_from_u64_err!(A, B, C, D, E, F);

macro_rules! try_from_u64_numeric {
( $type: ty ) => {
Expand Down Expand Up @@ -540,10 +584,28 @@ macro_rules! try_from_u64_string {

try_from_u64_string!(String);

try_from_u64_err!(bool);
try_from_u64_err!(f32);
try_from_u64_err!(f64);
try_from_u64_err!(Vec<u8>);

#[cfg(feature = "with-uuid")]
try_from_u64_err!(uuid::Uuid);
#[cfg(feature = "with-json")]
try_from_u64_err!(serde_json::Value);

#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::NaiveDate);

#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::NaiveTime);

#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::NaiveDateTime);

#[cfg(feature = "with-chrono")]
try_from_u64_err!(chrono::DateTime<chrono::FixedOffset>);

#[cfg(feature = "with-rust_decimal")]
try_from_u64_err!(rust_decimal::Decimal);

#[cfg(feature = "with-uuid")]
try_from_u64_err!(uuid::Uuid);