Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Md5 function #786

Merged
merged 6 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -721,4 +722,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
Loading