From 7e001b2f1752889bf77cac68673c651156b5524d Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Thu, 16 May 2024 16:28:03 +0530 Subject: [PATCH 1/4] feat: add function to allow order by nulls --- src/query/helper.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index dc2629ae0..5378ef99d 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -3,8 +3,8 @@ use crate::{ PrimaryKeyToColumn, RelationDef, }; use sea_query::{ - Alias, ConditionType, Expr, Iden, IntoCondition, IntoIden, LockBehavior, LockType, SeaRc, - SelectExpr, SelectStatement, SimpleExpr, TableRef, + Alias, ConditionType, Expr, Iden, IntoCondition, IntoIden, LockBehavior, LockType, + NullOrdering, SeaRc, SelectExpr, SelectStatement, SimpleExpr, TableRef, }; pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement}; @@ -630,6 +630,27 @@ pub trait QueryOrder: Sized { .order_by_expr(col.into_simple_expr(), Order::Desc); self } + + /// Add an order_by expression with nulls ordering option + /// ``` + /// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend, NullOrdering}; + /// + /// assert_eq!( + /// cake::Entity::find() + /// .order_by_with_nulls(cake::Column::Id, Order::Asc, NullOrdering::First) + /// .build(DbBackend::MySql) + /// .to_string(), + /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` ASC NULLS FIRST" + /// ); + /// ``` + fn order_by_with_nulls(mut self, col: C, ord: Order, nulls: NullOrdering) -> Self + where + C: IntoSimpleExpr, + { + self.query() + .order_by_expr_with_nulls(col.into_simple_expr(), ord, nulls); + self + } } // LINT: when the column does not appear in tables selected from From f8998022453a6b3268b42a16c78a03030c04569a Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Thu, 16 May 2024 16:31:46 +0530 Subject: [PATCH 2/4] fix: make the correct import --- src/query/helper.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index 5378ef99d..7b85fc97a 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -633,7 +633,8 @@ pub trait QueryOrder: Sized { /// Add an order_by expression with nulls ordering option /// ``` - /// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend, NullOrdering}; + /// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend}; + /// use sea_query::NullOrdering; /// /// assert_eq!( /// cake::Entity::find() From 4660efb128ddbd2643db9d000280ca2862fcddc3 Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Thu, 16 May 2024 16:41:16 +0530 Subject: [PATCH 3/4] fix: use correct backend for doc tests --- src/query/helper.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index 7b85fc97a..d7c044b7f 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -639,9 +639,9 @@ pub trait QueryOrder: Sized { /// assert_eq!( /// cake::Entity::find() /// .order_by_with_nulls(cake::Column::Id, Order::Asc, NullOrdering::First) - /// .build(DbBackend::MySql) + /// .build(DbBackend::Postgres) /// .to_string(), - /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` ORDER BY `cake`.`id` ASC NULLS FIRST" + /// r#"SELECT "cake"."id", "cake"."name" FROM "cake" ORDER BY "cake"."id" ASC NULLS FIRST"# /// ); /// ``` fn order_by_with_nulls(mut self, col: C, ord: Order, nulls: NullOrdering) -> Self From a6d3eb4dd03dea2e454cc20e100caf2bf9a9a4dc Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Thu, 16 May 2024 17:02:12 +0530 Subject: [PATCH 4/4] chore: add space --- src/query/helper.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query/helper.rs b/src/query/helper.rs index d7c044b7f..cf3c1df09 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -641,7 +641,7 @@ pub trait QueryOrder: Sized { /// .order_by_with_nulls(cake::Column::Id, Order::Asc, NullOrdering::First) /// .build(DbBackend::Postgres) /// .to_string(), - /// r#"SELECT "cake"."id", "cake"."name" FROM "cake" ORDER BY "cake"."id" ASC NULLS FIRST"# + /// r#"SELECT "cake"."id", "cake"."name" FROM "cake" ORDER BY "cake"."id" ASC NULLS FIRST"# /// ); /// ``` fn order_by_with_nulls(mut self, col: C, ord: Order, nulls: NullOrdering) -> Self