Skip to content

Commit

Permalink
Md5 function (#786)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
IgnisDa authored Sep 28, 2024
1 parent 53fd4e9 commit 2dba3c3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/backend/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
}
Expand Down
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1980,7 +1980,7 @@ impl Expr {
SimpleExpr::FunctionCall(func)
}

/// Keyword `CURRENT_TIMESTAMP`.
/// Keyword `CURRENT_DATE`.
///
/// # Examples
///
Expand Down
30 changes: 30 additions & 0 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum Function {
BitOr,
Random,
Round,
Md5,
#[cfg(feature = "backend-postgres")]
PgFunction(PgFunction),
}
Expand Down Expand Up @@ -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<T>(expr: T) -> FunctionCall
where
T: Into<SimpleExpr>,
{
FunctionCall::new(Function::Md5).arg(expr)
}
}
10 changes: 10 additions & 0 deletions tests/mysql/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
10 changes: 10 additions & 0 deletions tests/postgres/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down

0 comments on commit 2dba3c3

Please sign in to comment.