Repository: SeaQL/sea-query (language: rust)

Issue:
SQL `ROUND` function
<!--

Welcome! Thank you for suggesting features!

First of all, please star our repo. Your support is vital to the continued maintenance of SeaORM.

Want to ask a question? You can reach us via:

- Discord: https://discord.com/invite/uCPdDXzbdv
- GitHub Discussions: https://github.com/SeaQL/sea-orm/discussions/new

All feature requests should start from a GitHub Discussion thread.

After a throughout discussion, when the solution is clear, we will create a tracking issue with a feature specification.

-->

## Motivation

I need to use the SQL `ROUND` function.

## Proposed Solutions

<!-- What are the proposed solutions? How it solve the problem or achieve the goal? -->

## Current Workarounds

Custom Function `Func::cust`.



Discussion / hints:
Moved from https://github.com/SeaQL/sea-orm/issues/1802

Fix patch:
diff --git a/src/backend/query_builder.rs b/src/backend/query_builder.rs
index b0d9f26e7..fd03d2ee7 100644
--- a/src/backend/query_builder.rs
+++ b/src/backend/query_builder.rs
@@ -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!(),
                 }
diff --git a/src/func.rs b/src/func.rs
index 38b079c83..c020eb118 100644
--- a/src/func.rs
+++ b/src/func.rs
@@ -24,6 +24,7 @@ pub enum Function {
     BitAnd,
     BitOr,
     Random,
+    Round,
     #[cfg(feature = "backend-postgres")]
     PgFunction(PgFunction),
 }
@@ -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


Test patch: