Skip to content

Commit

Permalink
feat(function): Add support for GREATEST / LEAST function (#844)
Browse files Browse the repository at this point in the history
Signed-off-by: MATILLAT Quentin <[email protected]>
  • Loading branch information
tinou98 authored Dec 1, 2024
1 parent f914cc3 commit caee3b2
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,8 @@ pub trait QueryBuilder:
Function::Coalesce => "COALESCE",
Function::Count => "COUNT",
Function::IfNull => self.if_null_function(),
Function::Greatest => self.greatest_function(),
Function::Least => self.least_function(),
Function::CharLength => self.char_length_function(),
Function::Cast => "CAST",
Function::Lower => "LOWER",
Expand Down Expand Up @@ -1459,6 +1461,18 @@ pub trait QueryBuilder:
"IFNULL"
}

#[doc(hidden)]
/// The name of the function that represents the "greatest" function.
fn greatest_function(&self) -> &str {
"GREATEST"
}

#[doc(hidden)]
/// The name of the function that represents the "least" function.
fn least_function(&self) -> &str {
"LEAST"
}

#[doc(hidden)]
/// The name of the function that returns the char length.
fn char_length_function(&self) -> &str {
Expand Down
8 changes: 8 additions & 0 deletions src/backend/sqlite/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ impl QueryBuilder for SqliteQueryBuilder {
sql.push_param(value.clone(), self as _);
}

fn greatest_function(&self) -> &str {
"MAX"
}

fn least_function(&self) -> &str {
"MIN"
}

fn char_length_function(&self) -> &str {
"LENGTH"
}
Expand Down
72 changes: 72 additions & 0 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum Function {
Abs,
Count,
IfNull,
Greatest,
Least,
CharLength,
Cast,
Custom(DynIden),
Expand Down Expand Up @@ -392,6 +394,76 @@ impl Func {
FunctionCall::new(Function::CharLength).arg(expr)
}

/// Call `GREATEST` function.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(Func::greatest([
/// Expr::col(Char::SizeW).into(),
/// Expr::col(Char::SizeH).into(),
/// ]))
/// .from(Char::Table)
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT GREATEST(`size_w`, `size_h`) FROM `character`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT GREATEST("size_w", "size_h") FROM "character""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT MAX("size_w", "size_h") FROM "character""#
/// );
/// ```
pub fn greatest<I>(args: I) -> FunctionCall
where
I: IntoIterator<Item = SimpleExpr>,
{
FunctionCall::new(Function::Greatest).args(args)
}

/// Call `LEAST` function.
///
/// # Examples
///
/// ```
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(Func::least([
/// Expr::col(Char::SizeW).into(),
/// Expr::col(Char::SizeH).into(),
/// ]))
/// .from(Char::Table)
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT LEAST(`size_w`, `size_h`) FROM `character`"#
/// );
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT LEAST("size_w", "size_h") FROM "character""#
/// );
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT MIN("size_w", "size_h") FROM "character""#
/// );
/// ```
pub fn least<I>(args: I) -> FunctionCall
where
I: IntoIterator<Item = SimpleExpr>,
{
FunctionCall::new(Function::Least).args(args)
}

/// Call `IF NULL` function.
///
/// # Examples
Expand Down

0 comments on commit caee3b2

Please sign in to comment.