sea_query::expr

Trait ExprTrait

source
pub trait ExprTrait: Sized {
Show 35 methods // Required methods fn as_enum<N>(self, type_name: N) -> SimpleExpr where N: IntoIden; fn binary<O, R>(self, op: O, right: R) -> SimpleExpr where O: Into<BinOper>, R: Into<SimpleExpr>; fn cast_as<N>(self, type_name: N) -> SimpleExpr where N: IntoIden; fn unary(self, o: UnOper) -> SimpleExpr; // Provided methods fn add<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn and<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn between<A, B>(self, a: A, b: B) -> SimpleExpr where A: Into<SimpleExpr>, B: Into<SimpleExpr> { ... } fn div<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn eq<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn equals<C>(self, col: C) -> SimpleExpr where C: IntoColumnRef { ... } fn gt<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn gte<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn in_subquery(self, sel: SelectStatement) -> SimpleExpr { ... } fn in_tuples<V, I>(self, v: I) -> SimpleExpr where V: IntoValueTuple, I: IntoIterator<Item = V> { ... } fn is<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn is_in<V, I>(self, v: I) -> SimpleExpr where V: Into<SimpleExpr>, I: IntoIterator<Item = V> { ... } fn is_not<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn is_not_in<V, I>(self, v: I) -> SimpleExpr where V: Into<SimpleExpr>, I: IntoIterator<Item = V> { ... } fn is_not_null(self) -> SimpleExpr { ... } fn is_null(self) -> SimpleExpr { ... } fn left_shift<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn like<L>(self, like: L) -> SimpleExpr where L: IntoLikeExpr { ... } fn lt<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn lte<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn modulo<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn mul<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn ne<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn not(self) -> SimpleExpr { ... } fn not_between<A, B>(self, a: A, b: B) -> SimpleExpr where A: Into<SimpleExpr>, B: Into<SimpleExpr> { ... } fn not_equals<C>(self, col: C) -> SimpleExpr where C: IntoColumnRef { ... } fn not_in_subquery(self, sel: SelectStatement) -> SimpleExpr { ... } fn not_like<L>(self, like: L) -> SimpleExpr where L: IntoLikeExpr { ... } fn or<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn right_shift<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... } fn sub<R>(self, right: R) -> SimpleExpr where R: Into<SimpleExpr> { ... }
}
Expand description

“Operator” methods for building complex expressions.

§Examples

use sea_query::*;

let expr = 1_i32.cast_as(Alias::new("REAL"));

let expr = Func::char_length("abc").eq(3_i32);

let expr = Expr::current_date()
    .cast_as(Alias::new("TEXT"))
    .like("2024%");

Required Methods§

source

fn as_enum<N>(self, type_name: N) -> SimpleExpr
where N: IntoIden,

Express a AS enum expression.

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

let query = Query::insert()
    .into_table(Char::Table)
    .columns([Char::FontSize])
    .values_panic(["large".as_enum(Alias::new("FontSizeEnum"))])
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"INSERT INTO `character` (`font_size`) VALUES ('large')"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "character" ("font_size") VALUES (CAST('large' AS FontSizeEnum))"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"INSERT INTO "character" ("font_size") VALUES ('large')"#
);
source

fn binary<O, R>(self, op: O, right: R) -> SimpleExpr
where O: Into<BinOper>, R: Into<SimpleExpr>,

Create any binary operation

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .cond_where(all![
        Char::SizeW.into_column_ref().binary(BinOper::SmallerThan, 10),
        Char::SizeW.into_column_ref().binary(BinOper::GreaterThan, Char::SizeH.into_column_ref())
    ])
    .to_owned();
assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` < 10 AND `size_w` > `size_h`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" < 10 AND "size_w" > "size_h""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" < 10 AND "size_w" > "size_h""#
);
source

fn cast_as<N>(self, type_name: N) -> SimpleExpr
where N: IntoIden,

Express a CAST AS expression.

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

let query = Query::select()
    .expr("1".cast_as(Alias::new("integer")))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT CAST('1' AS integer)"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT CAST('1' AS integer)"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT CAST('1' AS integer)"#
);
source

fn unary(self, o: UnOper) -> SimpleExpr

Apply any unary operator to the expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(Expr::col((Char::Table, Char::SizeW)).is_null().unary(UnOper::Not))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE NOT `character`.`size_w` IS NULL"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
);

Provided Methods§

source

fn add<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an arithmetic addition operation.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(1.add(1).eq(2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 + 1 = 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 + 1 = 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 + 1 = 2"#
);
source

fn and<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

source

fn between<A, B>(self, a: A, b: B) -> SimpleExpr
where A: Into<SimpleExpr>, B: Into<SimpleExpr>,

Express a BETWEEN expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::SizeW).into_column_ref().between(1, 10))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` BETWEEN 1 AND 10"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" BETWEEN 1 AND 10"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" BETWEEN 1 AND 10"#
);
source

fn div<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an arithmetic division operation.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(1.div(1).eq(2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 / 1 = 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 / 1 = 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 / 1 = 2"#
);
source

fn eq<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an equal (=) expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    // Sometimes, you'll have to qualify the call because of conflicting std traits.
    .and_where(ExprTrait::eq("What!", "Nothing"))
    .and_where(Char::Id.into_column_ref().eq(1))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'What!' = 'Nothing' AND `id` = 1"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing' AND "id" = 1"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'What!' = 'Nothing' AND "id" = 1"#
);
source

fn equals<C>(self, col: C) -> SimpleExpr
where C: IntoColumnRef,

Express a equal expression between two table columns, you will mainly use this to relate identical value between two table columns.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::FontId).into_column_ref().equals((Font::Table, Font::Id)))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`font_id` = `font`.`id`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" = "font"."id""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" = "font"."id""#
);
source

fn gt<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a greater than (>) expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::SizeW).into_column_ref().gt(2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` > 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" > 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" > 2"#
);
source

fn gte<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a greater than or equal (>=) expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::SizeW).into_column_ref().gte(2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` >= 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" >= 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" >= 2"#
);
source

fn in_subquery(self, sel: SelectStatement) -> SimpleExpr

Express a IN sub-query expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(Char::SizeW.into_column_ref().in_subquery(
        Query::select()
            .expr(Expr::cust("3 + 2 * 2"))
            .take()
    ))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` IN (SELECT 3 + 2 * 2)"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" IN (SELECT 3 + 2 * 2)"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" IN (SELECT 3 + 2 * 2)"#
);
source

fn in_tuples<V, I>(self, v: I) -> SimpleExpr
where V: IntoValueTuple, I: IntoIterator<Item = V>,

Express a IN sub expression.

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

let query = Query::select()
    .columns([Char::Character, Char::FontId])
    .from(Char::Table)
    .and_where(
        ExprTrait::in_tuples(
            Expr::tuple([
                Expr::col(Char::Character).into(),
                Expr::col(Char::FontId).into(),
            ]),
            [(1, String::from("1")), (2, String::from("2"))]
        )
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `font_id` FROM `character` WHERE (`character`, `font_id`) IN ((1, '1'), (2, '2'))"#
);

assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "font_id" FROM "character" WHERE ("character", "font_id") IN ((1, '1'), (2, '2'))"#
);

assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "font_id" FROM "character" WHERE ("character", "font_id") IN ((1, '1'), (2, '2'))"#
);
source

fn is<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a IS expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::Ascii).into_column_ref().is(true))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`ascii` IS TRUE"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS TRUE"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS TRUE"#
);
source

fn is_in<V, I>(self, v: I) -> SimpleExpr
where V: Into<SimpleExpr>, I: IntoIterator<Item = V>,

Express a IN expression.

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

let query = Query::select()
    .columns([Char::Id])
    .from(Char::Table)
    .and_where(
        (Char::Table, Char::SizeW)
            .into_column_ref()
            .is_in([1, 2, 3]),
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `id` FROM `character` WHERE `character`.`size_w` IN (1, 2, 3)"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "id" FROM "character" WHERE "character"."size_w" IN (1, 2, 3)"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "id" FROM "character" WHERE "character"."size_w" IN (1, 2, 3)"#
);

Empty value list

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

let query = Query::select()
    .columns([Char::Id])
    .from(Char::Table)
    .and_where(
        (Char::Table, Char::SizeW)
            .into_column_ref()
            .is_in(Vec::<u8>::new()),
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `id` FROM `character` WHERE 1 = 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "id" FROM "character" WHERE 1 = 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "id" FROM "character" WHERE 1 = 2"#
);
source

fn is_not<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a IS NOT expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::Ascii).into_column_ref().is_not(true))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`ascii` IS NOT TRUE"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS NOT TRUE"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."ascii" IS NOT TRUE"#
);
source

fn is_not_in<V, I>(self, v: I) -> SimpleExpr
where V: Into<SimpleExpr>, I: IntoIterator<Item = V>,

Express a NOT IN expression.

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

let query = Query::select()
    .columns([Char::Id])
    .from(Char::Table)
    .and_where(
        (Char::Table, Char::SizeW)
            .into_column_ref()
            .is_not_in([1, 2, 3]),
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `id` FROM `character` WHERE `character`.`size_w` NOT IN (1, 2, 3)"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "id" FROM "character" WHERE "character"."size_w" NOT IN (1, 2, 3)"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "id" FROM "character" WHERE "character"."size_w" NOT IN (1, 2, 3)"#
);

Empty value list

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

let query = Query::select()
    .columns([Char::Id])
    .from(Char::Table)
    .and_where(
        (Char::Table, Char::SizeW)
            .into_column_ref()
            .is_not_in(Vec::<u8>::new()),
    )
    .to_owned();

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

fn is_not_null(self) -> SimpleExpr

Express a IS NOT NULL expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::SizeW).into_column_ref().is_not_null())
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` IS NOT NULL"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NOT NULL"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NOT NULL"#
);
source

fn is_null(self) -> SimpleExpr

Express a IS NULL expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::SizeW).into_column_ref().is_null())
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` IS NULL"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NULL"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" IS NULL"#
);
source

fn left_shift<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a bitwise left shift.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(1.left_shift(1).eq(2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 << 1 = 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 << 1 = 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 << 1 = 2"#
);
source

fn like<L>(self, like: L) -> SimpleExpr
where L: IntoLikeExpr,

Express a LIKE expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::Character).into_column_ref().like("Ours'%"))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`character` LIKE 'Ours\'%'"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE E'Ours\'%'"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE 'Ours''%'"#
);

Like with ESCAPE

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::Character).into_column_ref().like(LikeExpr::new(r"|_Our|_").escape('|')))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`character` LIKE '|_Our|_' ESCAPE '|'"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE '|_Our|_' ESCAPE '|'"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" LIKE '|_Our|_' ESCAPE '|'"#
);
source

fn lt<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a less than (<) expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::SizeW).into_column_ref().lt(2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` < 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" < 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" < 2"#
);
source

fn lte<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a less than or equal (<=) expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::SizeW).into_column_ref().lte(2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` <= 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" <= 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" <= 2"#
);
source

fn modulo<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an arithmetic modulo operation.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(1.modulo(1).eq(2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 % 1 = 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 % 1 = 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 % 1 = 2"#
);
source

fn mul<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an arithmetic multiplication operation.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(1.mul(1).eq(2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 * 1 = 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 * 1 = 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 * 1 = 2"#
);
source

fn ne<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a not equal (<>) expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    // Sometimes, you'll have to qualify the call because of conflicting std traits.
    .and_where(ExprTrait::ne("Morning", "Good"))
    .and_where(Char::Id.into_column_ref().ne(1))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 'Morning' <> 'Good' AND `id` <> 1"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good' AND "id" <> 1"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 'Morning' <> 'Good' AND "id" <> 1"#
);
source

fn not(self) -> SimpleExpr

Negates an expression with NOT.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(ExprTrait::not(Expr::col((Char::Table, Char::SizeW)).is_null()))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE NOT `character`.`size_w` IS NULL"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE NOT "character"."size_w" IS NULL"#
);
source

fn not_between<A, B>(self, a: A, b: B) -> SimpleExpr
where A: Into<SimpleExpr>, B: Into<SimpleExpr>,

Express a NOT BETWEEN expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::SizeW).into_column_ref().not_between(1, 10))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`size_w` NOT BETWEEN 1 AND 10"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" NOT BETWEEN 1 AND 10"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."size_w" NOT BETWEEN 1 AND 10"#
);
source

fn not_equals<C>(self, col: C) -> SimpleExpr
where C: IntoColumnRef,

Express a not equal expression between two table columns, you will mainly use this to relate identical value between two table columns.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::FontId).into_column_ref().not_equals((Font::Table, Font::Id)))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`font_id` <> `font`.`id`"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" <> "font"."id""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."font_id" <> "font"."id""#
);
source

fn not_in_subquery(self, sel: SelectStatement) -> SimpleExpr

Express a NOT IN sub-query expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(Char::SizeW.into_column_ref().not_in_subquery(
        Query::select()
            .expr(Expr::cust("3 + 2 * 2"))
            .take()
    ))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `size_w` NOT IN (SELECT 3 + 2 * 2)"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" NOT IN (SELECT 3 + 2 * 2)"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "size_w" NOT IN (SELECT 3 + 2 * 2)"#
);
source

fn not_like<L>(self, like: L) -> SimpleExpr
where L: IntoLikeExpr,

Express a NOT LIKE expression.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where((Char::Table, Char::Character).into_column_ref().not_like("Ours'%"))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE `character`.`character` NOT LIKE 'Ours\'%'"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" NOT LIKE E'Ours\'%'"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE "character"."character" NOT LIKE 'Ours''%'"#
);
source

fn or<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a logical OR operation.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(false.or(true))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE FALSE OR TRUE"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE FALSE OR TRUE"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE FALSE OR TRUE"#
);
source

fn right_shift<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express a bitwise right shift.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(1.right_shift(1).eq(2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 >> 1 = 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 >> 1 = 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 >> 1 = 2"#
);
source

fn sub<R>(self, right: R) -> SimpleExpr
where R: Into<SimpleExpr>,

Express an arithmetic subtraction operation.

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

let query = Query::select()
    .columns([Char::Character, Char::SizeW, Char::SizeH])
    .from(Char::Table)
    .and_where(1.sub(1).eq(2))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `character`, `size_w`, `size_h` FROM `character` WHERE 1 - 1 = 2"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 - 1 = 2"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"SELECT "character", "size_w", "size_h" FROM "character" WHERE 1 - 1 = 2"#
);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

source§

impl<T> ExprTrait for T
where T: Into<SimpleExpr>,

This generic implementation covers all expression types, including ColumnRef, Value, FunctionCall, SimpleExpr