From 363f940e0bb7fb89a3a0c5f021186d435c06e333 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Wed, 20 Jul 2022 21:57:48 +0900 Subject: [PATCH 1/3] distinct support --- src/query/helper.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/query/helper.rs b/src/query/helper.rs index 6ab31d53b..7b78e7db3 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -8,6 +8,9 @@ use sea_query::{ }; pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement}; +#[cfg(feature = "sqlx-postgres")] +use sea_query::IntoColumnRef; + // LINT: when the column does not appear in tables selected from // LINT: when there is a group by clause, but some columns don't have aggregate functions // LINT: when the join table or column does not exists @@ -149,6 +152,61 @@ pub trait QuerySelect: Sized { self } + /// Add a DISTINCT expression + /// ``` + /// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend}; + /// struct Input { + /// name: Option, + /// } + /// let input = Input { + /// name: Some("cheese".to_owned()), + /// }; + /// assert_eq!( + /// cake::Entity::find() + /// .filter( + /// Condition::all().add_option(input.name.map(|n| cake::Column::Name.contains(&n))) + /// ) + /// .distinct() + /// .build(DbBackend::MySql) + /// .to_string(), + /// "SELECT DISTINCT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%cheese%'" + /// ); + /// ``` + fn distinct(mut self) -> Self { + self.query().distinct(); + self + } + + /// Add a DISTINCT ON expression + /// NOTE: this function is only supported by `sqlx-postgres` + /// ``` + /// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend}; + /// struct Input { + /// name: Option, + /// } + /// let input = Input { + /// name: Some("cheese".to_owned()), + /// }; + /// assert_eq!( + /// cake::Entity::find() + /// .filter( + /// Condition::all().add_option(input.name.map(|n| cake::Column::Name.contains(&n))) + /// ) + /// .distinct_on(cake::Column::Name) + /// .build(DbBackend::Postgres) + /// .to_string(), + /// "SELECT DISTINCT ON (\"name\") \"cake\".\"id\", \"cake\".\"name\" FROM \"cake\" WHERE \"cake\".\"name\" LIKE '%cheese%'" + /// ); + /// ``` + #[cfg(feature = "sqlx-postgres")] + fn distinct_on(mut self, col: C) -> Self + where + C: IntoColumnRef, + { + self.query().distinct_on(vec![col]); + self + } + #[doc(hidden)] fn join_join(mut self, join: JoinType, rel: RelationDef, via: Option) -> Self { if let Some(via) = via { From c9871cb2d25de92336501799faa7eb29595ef95b Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Mon, 25 Jul 2022 21:05:22 +0900 Subject: [PATCH 2/3] remove feature flag --- src/query/helper.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index 7b78e7db3..41ee57bc1 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -8,7 +8,6 @@ use sea_query::{ }; pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement}; -#[cfg(feature = "sqlx-postgres")] use sea_query::IntoColumnRef; // LINT: when the column does not appear in tables selected from @@ -198,7 +197,6 @@ pub trait QuerySelect: Sized { /// "SELECT DISTINCT ON (\"name\") \"cake\".\"id\", \"cake\".\"name\" FROM \"cake\" WHERE \"cake\".\"name\" LIKE '%cheese%'" /// ); /// ``` - #[cfg(feature = "sqlx-postgres")] fn distinct_on(mut self, col: C) -> Self where C: IntoColumnRef, From db1111c0e5c5935b7e0782234906b741490628ff Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Mon, 25 Jul 2022 21:05:51 +0900 Subject: [PATCH 3/3] fix argument --- src/query/helper.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index 41ee57bc1..56b880e2b 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -191,17 +191,18 @@ pub trait QuerySelect: Sized { /// .filter( /// Condition::all().add_option(input.name.map(|n| cake::Column::Name.contains(&n))) /// ) - /// .distinct_on(cake::Column::Name) + /// .distinct_on([cake::Column::Name]) /// .build(DbBackend::Postgres) /// .to_string(), /// "SELECT DISTINCT ON (\"name\") \"cake\".\"id\", \"cake\".\"name\" FROM \"cake\" WHERE \"cake\".\"name\" LIKE '%cheese%'" /// ); /// ``` - fn distinct_on(mut self, col: C) -> Self + fn distinct_on(mut self, cols: I) -> Self where - C: IntoColumnRef, + T: IntoColumnRef, + I: IntoIterator, { - self.query().distinct_on(vec![col]); + self.query().distinct_on(cols); self }