sea_query::func

Struct Func

source
pub struct Func;
Expand description

Function call helper.

Implementationsยง

sourceยง

impl Func

source

pub fn cust<T>(func: T) -> FunctionCall
where T: IntoIden,

Call a custom function.

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

struct MyFunction;

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

let query = Query::select()
    .expr(Func::cust(MyFunction).arg("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')"#
);
source

pub fn max<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call MAX function.

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

let query = Query::select()
    .expr(Func::max(Expr::col((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""#
);
source

pub fn min<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call MIN function.

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

let query = Query::select()
    .expr(Func::min(Expr::col((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""#
);
source

pub fn sum<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call SUM function.

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

let query = Query::select()
    .expr(Func::sum(Expr::col((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""#
);
source

pub fn avg<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call AVG function.

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

let query = Query::select()
    .expr(Func::avg(Expr::col((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""#
);
source

pub fn abs<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call ABS function.

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

let query = Query::select()
    .expr(Func::abs(Expr::col((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""#
);
source

pub fn count<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call COUNT function.

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

let query = Query::select()
    .expr(Func::count(Expr::col((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""#
);
source

pub fn count_distinct<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call COUNT function with the DISTINCT modifier.

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

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

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

pub fn char_length<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call CHAR_LENGTH function.

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

let query = Query::select()
    .expr(Func::char_length(Expr::col((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""#
);
source

pub fn if_null<A, B>(a: A, b: B) -> FunctionCall
where A: Into<SimpleExpr>, B: Into<SimpleExpr>,

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""#
);
source

pub fn cast_as<V, I>(expr: V, iden: I) -> FunctionCall
where V: Into<SimpleExpr>, I: IntoIden,

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)"#
);
source

pub fn cast_as_quoted<V, I>(expr: V, iden: I, q: Quote) -> FunctionCall
where V: Into<SimpleExpr>, I: IntoIden,

Call CAST function with a case-sensitive custom type.

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

let query = Query::select()
    .expr(Func::cast_as_quoted(
        "hello",
        Alias::new("MyType"),
        '"'.into(),
    ))
    .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")"#
);
source

pub fn coalesce<I>(args: I) -> FunctionCall
where I: IntoIterator<Item = SimpleExpr>,

Call COALESCE function.

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

let query = Query::select()
    .expr(Func::coalesce([
        Expr::col(Char::SizeW).into(),
        Expr::col(Char::SizeH).into(),
        Expr::val(12).into(),
    ]))
    .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""#
);
source

pub fn lower<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call LOWER function.

ยงExamples
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""#
);
use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .column(Font::Id)
    .from(Font::Table)
    .and_where(Expr::expr(Func::lower(Expr::col(Font::Name))).eq("abc".trim().to_lowercase()))
    .take();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    "SELECT `id` FROM `font` WHERE LOWER(`name`) = 'abc'"
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "id" FROM "font" WHERE LOWER("name") = 'abc'"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "id" FROM "font" WHERE LOWER("name") = 'abc'"#
);
source

pub fn upper<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call UPPER function.

ยงExamples
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""#
);
source

pub fn bit_and<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call BIT_AND function, this is not supported on SQLite.

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

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

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT BIT_AND(`font_size`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT BIT_AND("font_size") FROM "character""#
);
source

pub fn bit_or<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

Call BIT_OR function, this is not supported on SQLite.

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

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

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT BIT_OR(`font_size`) FROM `character`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT BIT_OR("font_size") FROM "character""#
);
source

pub fn round<A>(expr: A) -> FunctionCall
where A: Into<SimpleExpr>,

Call ROUND function.

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

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

assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT ROUND(5.654)"#);

assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT ROUND(5.654)"#
);

assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT ROUND(5.654)"#
);
source

pub fn round_with_precision<A, B>(a: A, b: B) -> FunctionCall
where A: Into<SimpleExpr>, B: Into<SimpleExpr>,

Call ROUND function with the precision.

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

let query = Query::select()
    .expr(Func::round_with_precision(5.654, 2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT ROUND(5.654, 2)"#
);

assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT ROUND(5.654, 2)"#
);

assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT ROUND(5.654, 2)"#
);
source

pub fn random() -> FunctionCall

Call RANDOM function.

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

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

assert_eq!(query.to_string(MysqlQueryBuilder), r#"SELECT RAND()"#);

assert_eq!(query.to_string(PostgresQueryBuilder), r#"SELECT RANDOM()"#);

assert_eq!(query.to_string(SqliteQueryBuilder), r#"SELECT RANDOM()"#);
source

pub fn md5<T>(expr: T) -> FunctionCall
where T: Into<SimpleExpr>,

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""#
);

Trait Implementationsยง

sourceยง

impl Clone for Func

sourceยง

fn clone(&self) -> Func

Returns a copy of the value. Read more
1.0.0 ยท sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
sourceยง

impl Debug for Func

sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementationsยง

Blanket Implementationsยง

sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
sourceยง

impl<T> CloneToUninit for T
where T: Clone,

sourceยง

unsafe fn clone_to_uninit(&self, dst: *mut T)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
sourceยง

impl<T> From<T> for T

sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

sourceยง

fn into(self) -> U

Calls U::from(self).

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

sourceยง

impl<T> ToOwned for T
where T: Clone,

sourceยง

type Owned = T

The resulting type after obtaining ownership.
sourceยง

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.