Skip to content

Commit 2dba3c3

Browse files
authored
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
1 parent 53fd4e9 commit 2dba3c3

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

src/backend/query_builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ pub trait QueryBuilder:
676676
Function::Custom(_) => "",
677677
Function::Random => self.random_function(),
678678
Function::Round => "ROUND",
679+
Function::Md5 => "MD5",
679680
#[cfg(feature = "backend-postgres")]
680681
Function::PgFunction(_) => unimplemented!(),
681682
}

src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1980,7 +1980,7 @@ impl Expr {
19801980
SimpleExpr::FunctionCall(func)
19811981
}
19821982

1983-
/// Keyword `CURRENT_TIMESTAMP`.
1983+
/// Keyword `CURRENT_DATE`.
19841984
///
19851985
/// # Examples
19861986
///

src/func.rs

+30
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub enum Function {
2525
BitOr,
2626
Random,
2727
Round,
28+
Md5,
2829
#[cfg(feature = "backend-postgres")]
2930
PgFunction(PgFunction),
3031
}
@@ -725,4 +726,33 @@ impl Func {
725726
pub fn random() -> FunctionCall {
726727
FunctionCall::new(Function::Random)
727728
}
729+
730+
/// Call `MD5` function, this is only available in Postgres and MySQL.
731+
///
732+
/// # Examples
733+
///
734+
/// ```
735+
/// use sea_query::{tests_cfg::*, *};
736+
///
737+
/// let query = Query::select()
738+
/// .expr(Func::md5(Expr::col((Char::Table, Char::Character))))
739+
/// .from(Char::Table)
740+
/// .to_owned();
741+
///
742+
/// assert_eq!(
743+
/// query.to_string(MysqlQueryBuilder),
744+
/// r#"SELECT MD5(`character`.`character`) FROM `character`"#
745+
/// );
746+
///
747+
/// assert_eq!(
748+
/// query.to_string(PostgresQueryBuilder),
749+
/// r#"SELECT MD5("character"."character") FROM "character""#
750+
/// );
751+
/// ```
752+
pub fn md5<T>(expr: T) -> FunctionCall
753+
where
754+
T: Into<SimpleExpr>,
755+
{
756+
FunctionCall::new(Function::Md5).arg(expr)
757+
}
728758
}

tests/mysql/query.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,16 @@ fn select_61() {
10611061
);
10621062
}
10631063

1064+
#[test]
1065+
fn md5_fn() {
1066+
assert_eq!(
1067+
Query::select()
1068+
.expr(Func::md5(Expr::val("test")))
1069+
.to_string(MysqlQueryBuilder),
1070+
r#"SELECT MD5('test')"#
1071+
);
1072+
}
1073+
10641074
#[test]
10651075
#[allow(clippy::approx_constant)]
10661076
fn insert_2() {

tests/postgres/query.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,16 @@ fn sub_query_with_fn() {
19251925
);
19261926
}
19271927

1928+
#[test]
1929+
fn md5_fn() {
1930+
assert_eq!(
1931+
Query::select()
1932+
.expr(Func::md5(Expr::val("test")))
1933+
.to_string(PostgresQueryBuilder),
1934+
r#"SELECT MD5('test')"#
1935+
);
1936+
}
1937+
19281938
#[test]
19291939
fn select_array_contains_bin_oper() {
19301940
assert_eq!(

0 commit comments

Comments
 (0)