Func

Enum Func 

Source
#[non_exhaustive]
pub enum Func {
Show 21 variants Max, Min, Sum, Avg, Abs, Count, IfNull, Greatest, Least, CharLength, Cast, Custom(DynIden), Coalesce, Lower, Upper, BitAnd, BitOr, Random, Round, Md5, PgFunction(PgFunc),
}
Expand description

Known SQL functions.

If something is not supported here, you can use Function::Custom.

Variants (Non-exhaustive)ยง

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
ยง

Max

ยง

Min

ยง

Sum

ยง

Avg

ยง

Abs

ยง

Count

ยง

IfNull

ยง

Greatest

ยง

Least

ยง

CharLength

ยง

Cast

ยง

Custom(DynIden)

ยง

Coalesce

ยง

Lower

ยง

Upper

ยง

BitAnd

ยง

BitOr

ยง

Random

ยง

Round

ยง

Md5

ยง

PgFunction(PgFunc)

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::*, *};

#[derive(Iden)]
#[iden = "MY_FUNCTION"]
struct MyFunction;

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<Expr>,

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<Expr>,

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<Expr>,

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<Expr>,

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<Expr>,

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<Expr>,

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<Expr>,

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<Expr>,

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 greatest<I>(args: I) -> FunctionCall
where I: IntoIterator<Item = Expr>,

Call GREATEST function.

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

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

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

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

Call LEAST function.

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

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

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

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

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<Expr>, I: IntoIden,

Call CAST function with a custom type.

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

let query = Query::select()
    .expr(Func::cast_as("hello", "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, type: I) -> FunctionCall
where V: Into<Expr>, I: Into<TypeRef>,

Call CAST function with a case-sensitive custom type.

Type can be qualified with a schema name.

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

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

// Also works with a schema-qualified type name:

let query = Query::select()
    .expr(Func::cast_as_quoted("hello", ("MySchema", "MyType")))
    .to_owned();

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

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

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<Expr>,

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(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<Expr>,

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<Expr>,

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<Expr>,

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<Expr>,

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<Expr>, B: Into<Expr>,

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<Expr>,

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 duplicate 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
Sourceยง

impl PartialEq for Func

Sourceยง

fn eq(&self, other: &Func) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl StructuralPartialEq for Func

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, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.