feat: add function to allow order by nulls (#2228)

* feat: add function to allow order by nulls

* fix: make the correct import

* fix: use correct backend for doc tests

* chore: add space
This commit is contained in:
Diptesh Choudhuri 2024-05-27 13:30:56 +05:30 committed by GitHub
parent 2b6e72474a
commit 59fb6c11b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,8 +3,8 @@ use crate::{
PrimaryKeyToColumn, RelationDef, PrimaryKeyToColumn, RelationDef,
}; };
use sea_query::{ use sea_query::{
Alias, ConditionType, Expr, Iden, IntoCondition, IntoIden, LockBehavior, LockType, SeaRc, Alias, ConditionType, Expr, Iden, IntoCondition, IntoIden, LockBehavior, LockType,
SelectExpr, SelectStatement, SimpleExpr, TableRef, NullOrdering, SeaRc, SelectExpr, SelectStatement, SimpleExpr, TableRef,
}; };
pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement}; pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement};
@ -630,6 +630,28 @@ pub trait QueryOrder: Sized {
.order_by_expr(col.into_simple_expr(), Order::Desc); .order_by_expr(col.into_simple_expr(), Order::Desc);
self self
} }
/// Add an order_by expression with nulls ordering option
/// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
/// use sea_query::NullOrdering;
///
/// assert_eq!(
/// cake::Entity::find()
/// .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"#
/// );
/// ```
fn order_by_with_nulls<C>(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 // LINT: when the column does not appear in tables selected from