From e3217d0cda0458090102e0bf4e1864b12176c735 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Sat, 21 Jan 2023 15:23:55 +0800 Subject: [PATCH 1/4] Added `QueryTrait::maybe` --- src/query/traits.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/query/traits.rs b/src/query/traits.rs index cfc35350d..4015a7733 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -23,4 +23,49 @@ pub trait QueryTrait { self.as_query().build_any(query_builder.as_ref()), ) } + + /// Perform some operations on the [QueryTrait::QueryStatement] with the given `Option` value + /// + /// # Example + /// + /// ``` + /// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend}; + /// + /// assert_eq!( + /// cake::Entity::find() + /// .select_only() + /// // Select column + /// .maybe(cake::Column::Id, |mut query, column| { + /// if let Some(col) = column.into() { + /// query = query.column_as(col.count(), "count"); + /// } + /// query + /// }) + /// // Limit result to the first 100 rows + /// .maybe(Some(100), |mut query, limit| { + /// if let Some(n) = limit.into() { + /// query = query.limit(n); + /// } + /// query + /// }) + /// // Do nothing + /// .maybe(None, |mut query, offset| { + /// if let Some(n) = offset.into() { + /// query = query.offset(n); + /// } + /// query + /// }) + /// .build(DbBackend::Postgres) + /// .to_string(), + /// r#"SELECT COUNT("cake"."id") AS "count" FROM "cake" LIMIT 100"# + /// ); + /// ``` + fn maybe(self, val: T, if_some: F) -> Self + where + Self: Sized, + T: Into>, + F: FnOnce(Self, T) -> Self, + { + if_some(self, val) + } } From eed3355b0c7a1a3dd6c1462f5d1b98517a618786 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Wed, 25 Jan 2023 14:21:35 +0800 Subject: [PATCH 2/4] Make the API better --- src/query/traits.rs | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/src/query/traits.rs b/src/query/traits.rs index 4015a7733..69b5d7659 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -33,39 +33,25 @@ pub trait QueryTrait { /// /// assert_eq!( /// cake::Entity::find() - /// .select_only() - /// // Select column - /// .maybe(cake::Column::Id, |mut query, column| { - /// if let Some(col) = column.into() { - /// query = query.column_as(col.count(), "count"); - /// } - /// query - /// }) - /// // Limit result to the first 100 rows - /// .maybe(Some(100), |mut query, limit| { - /// if let Some(n) = limit.into() { - /// query = query.limit(n); - /// } - /// query - /// }) - /// // Do nothing - /// .maybe(None, |mut query, offset| { - /// if let Some(n) = offset.into() { - /// query = query.offset(n); - /// } - /// query + /// .apply_if(Some(3), |mut query, v| { + /// query.filter(cake::Column::Id.eq(v)) /// }) + /// .apply_if(Some(100), QuerySelect::limit) + /// .apply_if(None, QuerySelect::offset) // no-op /// .build(DbBackend::Postgres) /// .to_string(), - /// r#"SELECT COUNT("cake"."id") AS "count" FROM "cake" LIMIT 100"# + /// r#"SELECT "cake"."id", "cake"."name" FROM "cake" WHERE "cake"."id" = 3 LIMIT 100"# /// ); /// ``` - fn maybe(self, val: T, if_some: F) -> Self + fn apply_if(self, val: Option, if_some: F) -> Self where Self: Sized, - T: Into>, F: FnOnce(Self, T) -> Self, { - if_some(self, val) + if let Some(val) = val { + if_some(self, val) + } else { + self + } } } From d4fc1530d7ae1f1bde927c46c432590438f63b52 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Wed, 25 Jan 2023 14:24:03 +0800 Subject: [PATCH 3/4] Docs --- src/query/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query/traits.rs b/src/query/traits.rs index 69b5d7659..821392471 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -24,7 +24,7 @@ pub trait QueryTrait { ) } - /// Perform some operations on the [QueryTrait::QueryStatement] with the given `Option` value + /// Apply a operation on the [QueryTrait::QueryStatement] if the given `Option` is `Some(_)` /// /// # Example /// From 301e85647d5b716265363a83c1c50954cf2151c4 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Thu, 26 Jan 2023 16:52:55 +0800 Subject: [PATCH 4/4] Update src/query/traits.rs --- src/query/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query/traits.rs b/src/query/traits.rs index 821392471..2dd30d564 100644 --- a/src/query/traits.rs +++ b/src/query/traits.rs @@ -24,7 +24,7 @@ pub trait QueryTrait { ) } - /// Apply a operation on the [QueryTrait::QueryStatement] if the given `Option` is `Some(_)` + /// Apply an operation on the [QueryTrait::QueryStatement] if the given `Option` is `Some(_)` /// /// # Example ///