pub struct Func;
Expand description

Function call helper.

Implementations§

§

impl Func

pub fn cust<T>(func: T) -> FunctionCallwhere 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')"#
);

pub fn max<T>(expr: T) -> FunctionCallwhere 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""#
);

pub fn min<T>(expr: T) -> FunctionCallwhere 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""#
);

pub fn sum<T>(expr: T) -> FunctionCallwhere 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""#
);

pub fn avg<T>(expr: T) -> FunctionCallwhere 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""#
);

pub fn abs<T>(expr: T) -> FunctionCallwhere 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""#
);

pub fn count<T>(expr: T) -> FunctionCallwhere 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""#
);

pub fn char_length<T>(expr: T) -> FunctionCallwhere 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""#
);

pub fn if_null<A, B>(a: A, b: B) -> FunctionCallwhere 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""#
);

pub fn cast_as<V, I>(expr: V, iden: I) -> FunctionCallwhere 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)"#
);

pub fn coalesce<I>(args: I) -> FunctionCallwhere 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""#
);

pub fn lower<T>(expr: T) -> FunctionCallwhere 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""#
);

pub fn upper<T>(expr: T) -> FunctionCallwhere 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""#
);

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

Trait Implementations§

§

impl Clone for Func

§

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
§

impl Debug for Func

§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Func

§

impl Send for Func

§

impl Sync for Func

§

impl Unpin for Func

§

impl UnwindSafe for Func

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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