logo
pub struct Func;
Expand description

Function call helper.

Implementations

Call a custom function.

Examples
use sea_query::{tests_cfg::*, *};

struct MyFunction;

impl Iden for MyFunction {
    fn unquoted(&self, s: &mut dyn FmtWrite) {
        write!(s, "MY_FUNCTION").unwrap();
    }
}

let query = Query::select()
    .expr(Func::cust(MyFunction).args(vec![Expr::val("hello")]))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT MY_FUNCTION('hello')"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT MY_FUNCTION('hello')"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT MY_FUNCTION('hello')"#
);

Call MAX function.

Examples
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::max(Expr::tbl(Char::Table, Char::SizeW)))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT MAX(`character`.`size_w`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT MAX("character"."size_w") FROM "character""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT MAX("character"."size_w") FROM "character""#
);

Call MIN function.

Examples
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::min(Expr::tbl(Char::Table, Char::SizeH)))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT MIN(`character`.`size_h`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT MIN("character"."size_h") FROM "character""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT MIN("character"."size_h") FROM "character""#
);

Call SUM function.

Examples
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::sum(Expr::tbl(Char::Table, Char::SizeH)))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT SUM(`character`.`size_h`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT SUM("character"."size_h") FROM "character""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT SUM("character"."size_h") FROM "character""#
);

Call AVG function.

Examples
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::avg(Expr::tbl(Char::Table, Char::SizeH)))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT AVG(`character`.`size_h`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT AVG("character"."size_h") FROM "character""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT AVG("character"."size_h") FROM "character""#
);

Call ABS function.

Examples
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::abs(Expr::tbl(Char::Table, Char::SizeH)))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT ABS(`character`.`size_h`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT ABS("character"."size_h") FROM "character""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT ABS("character"."size_h") FROM "character""#
);

Call COUNT function.

Examples
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::count(Expr::tbl(Char::Table, Char::Id)))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT COUNT(`character`.`id`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT COUNT("character"."id") FROM "character""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT COUNT("character"."id") FROM "character""#
);

Call CHAR_LENGTH function.

Examples
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::char_length(Expr::tbl(Char::Table, Char::Character)))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT CHAR_LENGTH(`character`.`character`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT CHAR_LENGTH("character"."character") FROM "character""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT LENGTH("character"."character") FROM "character""#
);

Call IF NULL function.

Examples
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::if_null(
        Expr::col(Char::SizeW),
        Expr::col(Char::SizeH),
    ))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT IFNULL(`size_w`, `size_h`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT COALESCE("size_w", "size_h") FROM "character""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT IFNULL("size_w", "size_h") FROM "character""#
);

Call CAST function with a custom type.

Examples
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::cast_as("hello", Alias::new("MyType")))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT CAST('hello' AS MyType)"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT CAST('hello' AS MyType)"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT CAST('hello' AS MyType)"#
);

Call COALESCE function.

Examples
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::coalesce([
        Expr::col(Char::SizeW),
        Expr::col(Char::SizeH),
        Expr::val(12),
    ]))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT COALESCE(`size_w`, `size_h`, 12) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT COALESCE("size_w", "size_h", 12) FROM "character""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT COALESCE("size_w", "size_h", 12) FROM "character""#
);

Call LOWER function.

Examples
use sea_query::tests_cfg::Character::Character;
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::lower(Expr::col(Char::Character)))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT LOWER(`character`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT LOWER("character") FROM "character""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT LOWER("character") FROM "character""#
);

Call UPPER function.

Examples
use sea_query::tests_cfg::Character::Character;
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .expr(Func::upper(Expr::col(Char::Character)))
    .from(Char::Table)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT UPPER(`character`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT UPPER("character") FROM "character""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT UPPER("character") FROM "character""#
);

Call CURRENT_TIMESTAMP function.

Examples
use sea_query::tests_cfg::Character::Character;
use sea_query::{tests_cfg::*, *};

let query = Query::select().expr(Func::current_timestamp()).to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT CURRENT_TIMESTAMP"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT CURRENT_TIMESTAMP"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT CURRENT_TIMESTAMP"#
);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more