Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQL round function #671

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 25 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,30 @@ 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, 2)).to_owned();
IgnisDa marked this conversation as resolved.
Show resolved Hide resolved
///
/// 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<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
Loading