Struct sea_query::query::UpdateStatement

source ยท
pub struct UpdateStatement { /* private fields */ }
Expand description

Update existing rows in the table

Examples

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

let query = Query::update()
    .table(Glyph::Table)
    .values([(Glyph::Aspect, 1.23.into()), (Glyph::Image, "123".into())])
    .and_where(Expr::col(Glyph::Id).eq(1))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"UPDATE `glyph` SET `aspect` = 1.23, `image` = '123' WHERE `id` = 1"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 1.23, "image" = '123' WHERE "id" = 1"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 1.23, "image" = '123' WHERE "id" = 1"#
);

Implementationsยง

Construct a new UpdateStatement

Specify which table to update.

Examples

See UpdateStatement::values

Update column values. To set multiple column-value pairs at once.

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

let query = Query::update()
    .table(Glyph::Table)
    .values([
        (Glyph::Aspect, 2.1345.into()),
        (Glyph::Image, "235m".into()),
    ])
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m'"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 2.1345, "image" = '235m'"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 2.1345, "image" = '235m'"#
);

Update column value by SimpleExpr.

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

let query = Query::update()
    .table(Glyph::Table)
    .value(Glyph::Aspect, Expr::cust("60 * 24 * 24"))
    .values([
        (Glyph::Image, "24B0E11951B03B07F8300FD003983F03F0780060".into()),
    ])
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"UPDATE `glyph` SET `aspect` = 60 * 24 * 24, `image` = '24B0E11951B03B07F8300FD003983F03F0780060'"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 60 * 24 * 24, "image" = '24B0E11951B03B07F8300FD003983F03F0780060'"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 60 * 24 * 24, "image" = '24B0E11951B03B07F8300FD003983F03F0780060'"#
);
๐Ÿ‘ŽDeprecated since 0.27.0: Please use the [UpdateStatement::values]
๐Ÿ‘ŽDeprecated since 0.27.0: Please use the [UpdateStatement::value]
๐Ÿ‘ŽDeprecated since 0.27.0: Please use the [UpdateStatement::value]

Limit number of updated rows.

RETURNING expressions.

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

let query = Query::update()
    .table(Glyph::Table)
    .value(Glyph::Aspect, 2.1345)
    .value(Glyph::Image, "235m")
    .returning(Query::returning().columns([Glyph::Id]))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m'"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 2.1345, "image" = '235m' RETURNING "id""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 2.1345, "image" = '235m' RETURNING "id""#
);

RETURNING expressions for a column.

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

let query = Query::update()
    .table(Glyph::Table)
    .table(Glyph::Table)
    .value(Glyph::Aspect, 2.1345)
    .value(Glyph::Image, "235m")
    .returning_col(Glyph::Id)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m'"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 2.1345, "image" = '235m' RETURNING "id""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 2.1345, "image" = '235m' RETURNING "id""#
);

RETURNING expressions all columns.

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

let query = Query::update()
    .table(Glyph::Table)
    .table(Glyph::Table)
    .value(Glyph::Aspect, 2.1345)
    .value(Glyph::Image, "235m")
    .returning_all()
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"UPDATE `glyph` SET `aspect` = 2.1345, `image` = '235m'"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 2.1345, "image" = '235m' RETURNING *"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = 2.1345, "image" = '235m' RETURNING *"#
);

Create a WithQuery by specifying a WithClause to execute this query with.

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

let select = SelectStatement::new()
        .columns([Glyph::Id])
        .from(Glyph::Table)
        .and_where(Expr::col(Glyph::Image).like("0%"))
        .to_owned();
    let cte = CommonTableExpression::new()
        .query(select)
        .column(Glyph::Id)
        .table_name(Alias::new("cte"))
        .to_owned();
    let with_clause = WithClause::new().cte(cte).to_owned();
    let update = UpdateStatement::new()
        .table(Glyph::Table)
        .and_where(Expr::col(Glyph::Id).in_subquery(SelectStatement::new().column(Glyph::Id).from(Alias::new("cte")).to_owned()))
        .value(Glyph::Aspect, Expr::cust("60 * 24 * 24"))
        .to_owned();
    let query = update.with(with_clause);

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"WITH `cte` (`id`) AS (SELECT `id` FROM `glyph` WHERE `image` LIKE '0%') UPDATE `glyph` SET `aspect` = 60 * 24 * 24 WHERE `id` IN (SELECT `id` FROM `cte`)"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"WITH "cte" ("id") AS (SELECT "id" FROM "glyph" WHERE "image" LIKE '0%') UPDATE "glyph" SET "aspect" = 60 * 24 * 24 WHERE "id" IN (SELECT "id" FROM "cte")"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"WITH "cte" ("id") AS (SELECT "id" FROM "glyph" WHERE "image" LIKE '0%') UPDATE "glyph" SET "aspect" = 60 * 24 * 24 WHERE "id" IN (SELECT "id" FROM "cte")"#
);

Trait Implementationsยง

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Where condition, expressed with any and all. Calling cond_where multiple times will conjoin them. Calling or_where after cond_where will panic. Read more
And where condition. Calling or_where after and_where will panic. Read more
Optional and where, short hand for if c.is_some() q.and_where(c). Read more
Formats the value using the given formatter. Read more
Returns the โ€œdefault valueโ€ for a type. Read more
Clear order expressions
Order by column. Read more
Order by SimpleExpr.
Order by custom string.
Order by vector of columns.
Order by column with nulls order option. Read more
Order by SimpleExpr with nulls order option.
Order by custom string with nulls order option.
Order by vector of columns with nulls order option.
Build corresponding SQL statement into the SqlWriter for certain database backend and collect query parameters
Build corresponding SQL statement for certain database backend and collect query parameters into a vector
Build corresponding SQL statement for certain database backend and collect query parameters
Build corresponding SQL statement for certain database backend and return SQL string Read more
Build corresponding SQL statement for certain database backend and collect query parameters into a vector Read more
Build corresponding SQL statement for certain database backend and collect query parameters 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.

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.