From 2dba3c3c88679fdb82cd97bf4e7a0b8e9c66e137 Mon Sep 17 00:00:00 2001 From: Diptesh Choudhuri Date: Sat, 28 Sep 2024 13:22:46 +0530 Subject: [PATCH] Md5 function (#786) * feat: add md5 declaration * feat: allow calling function * tests: add tests for new function * feat: remove query builder calls * chore: add docs about postgres and mysql * fix: minor doc change for current date --- src/backend/query_builder.rs | 1 + src/expr.rs | 2 +- src/func.rs | 30 ++++++++++++++++++++++++++++++ tests/mysql/query.rs | 10 ++++++++++ tests/postgres/query.rs | 10 ++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/backend/query_builder.rs b/src/backend/query_builder.rs index da0eea67..7bf29b55 100644 --- a/src/backend/query_builder.rs +++ b/src/backend/query_builder.rs @@ -676,6 +676,7 @@ pub trait QueryBuilder: Function::Custom(_) => "", Function::Random => self.random_function(), Function::Round => "ROUND", + Function::Md5 => "MD5", #[cfg(feature = "backend-postgres")] Function::PgFunction(_) => unimplemented!(), } diff --git a/src/expr.rs b/src/expr.rs index 165fb09c..9c6f6544 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1980,7 +1980,7 @@ impl Expr { SimpleExpr::FunctionCall(func) } - /// Keyword `CURRENT_TIMESTAMP`. + /// Keyword `CURRENT_DATE`. /// /// # Examples /// diff --git a/src/func.rs b/src/func.rs index 50e43849..9ee69379 100644 --- a/src/func.rs +++ b/src/func.rs @@ -25,6 +25,7 @@ pub enum Function { BitOr, Random, Round, + Md5, #[cfg(feature = "backend-postgres")] PgFunction(PgFunction), } @@ -725,4 +726,33 @@ impl Func { pub fn random() -> FunctionCall { FunctionCall::new(Function::Random) } + + /// Call `MD5` function, this is only available in Postgres and MySQL. + /// + /// # Examples + /// + /// ``` + /// use sea_query::{tests_cfg::*, *}; + /// + /// let query = Query::select() + /// .expr(Func::md5(Expr::col((Char::Table, Char::Character)))) + /// .from(Char::Table) + /// .to_owned(); + /// + /// assert_eq!( + /// query.to_string(MysqlQueryBuilder), + /// r#"SELECT MD5(`character`.`character`) FROM `character`"# + /// ); + /// + /// assert_eq!( + /// query.to_string(PostgresQueryBuilder), + /// r#"SELECT MD5("character"."character") FROM "character""# + /// ); + /// ``` + pub fn md5(expr: T) -> FunctionCall + where + T: Into, + { + FunctionCall::new(Function::Md5).arg(expr) + } } diff --git a/tests/mysql/query.rs b/tests/mysql/query.rs index a9e46525..c0bb2e7d 100644 --- a/tests/mysql/query.rs +++ b/tests/mysql/query.rs @@ -1061,6 +1061,16 @@ fn select_61() { ); } +#[test] +fn md5_fn() { + assert_eq!( + Query::select() + .expr(Func::md5(Expr::val("test"))) + .to_string(MysqlQueryBuilder), + r#"SELECT MD5('test')"# + ); +} + #[test] #[allow(clippy::approx_constant)] fn insert_2() { diff --git a/tests/postgres/query.rs b/tests/postgres/query.rs index cf88dc96..804818b1 100644 --- a/tests/postgres/query.rs +++ b/tests/postgres/query.rs @@ -1925,6 +1925,16 @@ fn sub_query_with_fn() { ); } +#[test] +fn md5_fn() { + assert_eq!( + Query::select() + .expr(Func::md5(Expr::val("test"))) + .to_string(PostgresQueryBuilder), + r#"SELECT MD5('test')"# + ); +} + #[test] fn select_array_contains_bin_oper() { assert_eq!(