Struct sea_query::query::UpdateStatement[][src]

pub struct UpdateStatement { /* fields omitted */ }
Expand description

Update existing rows in the table

Examples

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

let query = Query::update()
    .table(Glyph::Table)
    .values(vec![
        (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

πŸ‘Ž Deprecated since 0.9.0:

Please use the [OrderedStatement::order_by] with a tuple as [ColumnRef]

πŸ‘Ž Deprecated since 0.9.0:

Please use the [OrderedStatement::order_by_columns] with a tuple as [ColumnRef]

πŸ‘Ž Deprecated since 0.12.0:

Please use [ConditionalStatement::cond_where]. Calling or_where after and_where will panic.

Construct a new UpdateStatement

Specify which table to update.

Examples

See UpdateStatement::values

πŸ‘Ž Deprecated since 0.5.0:

Please use the UpdateStatement::table function instead

Update column value by SimpleExpr.

Examples

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

let query = Query::update()
    .table(Glyph::Table)
    .col_expr(Glyph::Aspect, Expr::cust("60 * 24 * 24"))
    .values(vec![
        (Glyph::Image, "24B0E11951B03B07F8300FD003983F03F0780060".into()),
    ])
    .and_where(Expr::col(Glyph::Id).eq(1))
    .to_owned();

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

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(vec![
        (Glyph::Aspect, 2.1345.into()),
        (Glyph::Image, "235m".into()),
    ])
    .and_where(Expr::col(Glyph::Id).eq(1))
    .to_owned();

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

Update column values.

Examples

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

let query = Query::update()
    .table(Glyph::Table)
    .value(Glyph::Aspect, 2.1345.into())
    .value(Glyph::Image, "235m".into())
    .and_where(Expr::col(Glyph::Id).eq(1))
    .to_owned();

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

Limit number of updated rows.

RETURNING expressions. Postgres only.

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

let query = Query::update()
    .table(Glyph::Table)
    .value(Glyph::Aspect, 2.1345.into())
    .value(Glyph::Image, "235m".into())
    .and_where(Expr::col(Glyph::Id).eq(1))
    .returning(Query::select().column(Glyph::Id).take())
    .to_owned();

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

RETURNING a column after update. Postgres only. Wrapper over UpdateStatement::returning().

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

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

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

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. This cannot be mixed with ConditionalStatement::or_where. 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

πŸ‘Ž Deprecated since 0.12.0:

Please use [ConditionalStatement::cond_where]. Calling or_where after and_where will panic.

Or where condition. This cannot be mixed with ConditionalStatement::and_where. Calling or_where after and_where will panic. Read more

Formats the value using the given formatter. Read more

Returns the β€œdefault value” for a type. Read more

Order by column. Read more

πŸ‘Ž Deprecated since 0.9.0:

Please use the [OrderedStatement::order_by] with a tuple as [ColumnRef]

Order by SimpleExpr.

Order by custom string.

Order by vector of columns.

πŸ‘Ž Deprecated since 0.9.0:

Please use the [OrderedStatement::order_by_columns] with a tuple as [ColumnRef]

Build corresponding SQL statement for certain database backend and collect query parameters

Examples

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

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

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

let mut params = Vec::new();
let mut collector = |v| params.push(v);

assert_eq!(
    query.build_collect(MysqlQueryBuilder, &mut collector),
    r#"UPDATE `glyph` SET `aspect` = ?, `image` = ? WHERE `id` = ?"#
);
assert_eq!(
    params,
    vec![
        Value::Double(Some(2.1345)),
        Value::String(Some(Box::new(String::from("235m")))),
        Value::Int(Some(1)),
    ]
);

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 into a vector

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

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

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

πŸ”¬ This is a nightly-only experimental API. (toowned_clone_into)

recently added

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.