From afea5fbaeaf99f19fc6d24b411a819c6ca6a0bbb Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Fri, 4 Aug 2023 12:01:44 +0530 Subject: [PATCH 1/3] feature: add SQL round function --- src/backend/query_builder.rs | 1 + src/func.rs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) 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..075f0a2b3 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,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(); + /// + /// 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: A, b: B) -> FunctionCall + where + A: Into, + B: Into, + { + FunctionCall::new(Function::Round).args([a.into(), b.into()]) + } + /// Call `RANDOM` function. /// /// # Examples From f2ed720435fdea3fa099a809e477680915e41e76 Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Fri, 4 Aug 2023 17:28:07 +0530 Subject: [PATCH 2/3] feat: add separate round func with precision --- src/func.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/func.rs b/src/func.rs index 075f0a2b3..58e9281c3 100644 --- a/src/func.rs +++ b/src/func.rs @@ -588,7 +588,30 @@ impl Func { /// use sea_query::tests_cfg::Character::Character; /// use sea_query::{tests_cfg::*, *}; /// - /// let query = Query::select().expr(Func::round(5.654, 2)).to_owned(); + /// 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(expr: A) -> FunctionCall + where + A: Into, + { + 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)"#); /// @@ -596,7 +619,7 @@ impl Func { /// /// assert_eq!(query.to_string(SqliteQueryBuilder), r#"SELECT ROUND(5.654, 2)"#); /// ``` - pub fn round(a: A, b: B) -> FunctionCall + pub fn round_with_precision(a: A, b: B) -> FunctionCall where A: Into, B: Into, From 6cf38b2d484036ab0730c2f9e42eb14d9a7aba7e Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Fri, 4 Aug 2023 17:29:43 +0530 Subject: [PATCH 3/3] style: apply cargo formatter --- src/func.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/func.rs b/src/func.rs index 58e9281c3..c020eb118 100644 --- a/src/func.rs +++ b/src/func.rs @@ -592,9 +592,15 @@ impl Func { /// /// 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(PostgresQueryBuilder), + /// r#"SELECT ROUND(5.654)"# + /// ); /// - /// assert_eq!(query.to_string(SqliteQueryBuilder), r#"SELECT ROUND(5.654)"#); + /// assert_eq!( + /// query.to_string(SqliteQueryBuilder), + /// r#"SELECT ROUND(5.654)"# + /// ); /// ``` pub fn round(expr: A) -> FunctionCall where @@ -611,13 +617,24 @@ impl Func { /// 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(); + /// 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(MysqlQueryBuilder), + /// r#"SELECT ROUND(5.654, 2)"# + /// ); /// - /// assert_eq!(query.to_string(PostgresQueryBuilder), 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)"#); + /// assert_eq!( + /// query.to_string(SqliteQueryBuilder), + /// r#"SELECT ROUND(5.654, 2)"# + /// ); /// ``` pub fn round_with_precision(a: A, b: B) -> FunctionCall where