Skip to content

Commit

Permalink
Merge pull request #671 from IgnisDa/master
Browse files Browse the repository at this point in the history
Add SQL round function
  • Loading branch information
ikrivosheev authored Aug 4, 2023
2 parents cf30ffe + 6cf38b2 commit 42d0bd5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ pub trait QueryBuilder: QuotedBuilder + EscapeBuilder + TableRefBuilder {
Function::BitOr => "BIT_OR",
Function::Custom(_) => "",
Function::Random => self.random_function(),
Function::Round => "ROUND",
#[cfg(feature = "backend-postgres")]
Function::PgFunction(_) => unimplemented!(),
}
Expand Down
65 changes: 65 additions & 0 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub enum Function {
BitAnd,
BitOr,
Random,
Round,
#[cfg(feature = "backend-postgres")]
PgFunction(PgFunction),
}
Expand Down Expand Up @@ -579,6 +580,70 @@ impl Func {
FunctionCall::new(Function::BitOr).arg(expr)
}

/// Call `ROUND` function.
///
/// # Examples
///
/// ```
/// use sea_query::tests_cfg::Character::Character;
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select().expr(Func::round(5.654)).to_owned();
///
/// assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT ROUND(5.654)"#);
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT ROUND(5.654)"#
/// );
///
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT ROUND(5.654)"#
/// );
/// ```
pub fn round<A>(expr: A) -> FunctionCall
where
A: Into<SimpleExpr>,
{
FunctionCall::new(Function::Round).arg(expr)
}

/// Call `ROUND` function with the precision.
///
/// # Examples
///
/// ```
/// use sea_query::tests_cfg::Character::Character;
/// use sea_query::{tests_cfg::*, *};
///
/// let query = Query::select()
/// .expr(Func::round_with_precision(5.654, 2))
/// .to_owned();
///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// r#"SELECT ROUND(5.654, 2)"#
/// );
///
/// assert_eq!(
/// query.to_string(PostgresQueryBuilder),
/// r#"SELECT ROUND(5.654, 2)"#
/// );
///
/// assert_eq!(
/// query.to_string(SqliteQueryBuilder),
/// r#"SELECT ROUND(5.654, 2)"#
/// );
/// ```
pub fn round_with_precision<A, B>(a: A, b: B) -> FunctionCall
where
A: Into<SimpleExpr>,
B: Into<SimpleExpr>,
{
FunctionCall::new(Function::Round).args([a.into(), b.into()])
}

/// Call `RANDOM` function.
///
/// # Examples
Expand Down

0 comments on commit 42d0bd5

Please sign in to comment.