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(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§

§

impl UpdateStatement

pub fn to_string<T>(&self, query_builder: T) -> Stringwhere T: QueryBuilder,

pub fn build<T>(&self, query_builder: T) -> (String, Values)where T: QueryBuilder,

pub fn build_any(&self, query_builder: &dyn QueryBuilder) -> (String, Values)

§

impl UpdateStatement

pub fn order_by<T>(&mut self, col: T, order: Order) -> &mut UpdateStatementwhere T: IntoColumnRef,

pub fn order_by_tbl<T, C>( &mut self, table: T, col: C, order: Order ) -> &mut UpdateStatementwhere T: IntoIden, C: IntoIden,

👎Deprecated since 0.9.0: Please use the [OrderedStatement::order_by] with a tuple as [ColumnRef]

pub fn order_by_expr( &mut self, expr: SimpleExpr, order: Order ) -> &mut UpdateStatement

pub fn order_by_customs<T>( &mut self, cols: Vec<(T, Order), Global> ) -> &mut UpdateStatementwhere T: ToString,

pub fn order_by_columns<T>( &mut self, cols: Vec<(T, Order), Global> ) -> &mut UpdateStatementwhere T: IntoColumnRef,

pub fn order_by_table_columns<T, C>( &mut self, cols: Vec<(T, C, Order), Global> ) -> &mut UpdateStatementwhere T: IntoIden, C: IntoIden,

👎Deprecated since 0.9.0: Please use the [OrderedStatement::order_by_columns] with a tuple as [ColumnRef]
§

impl UpdateStatement

pub fn and_where(&mut self, other: SimpleExpr) -> &mut UpdateStatement

pub fn and_where_option( &mut self, other: Option<SimpleExpr> ) -> &mut UpdateStatement

pub fn or_where(&mut self, other: SimpleExpr) -> &mut UpdateStatement

👎Deprecated since 0.12.0: Please use [ConditionalStatement::cond_where]. Calling or_where after and_where will panic.

pub fn cond_where<C>(&mut self, condition: C) -> &mut UpdateStatementwhere C: IntoCondition,

§

impl UpdateStatement

pub fn new() -> UpdateStatement

Construct a new UpdateStatement

pub fn table<T>(&mut self, tbl_ref: T) -> &mut UpdateStatementwhere T: IntoTableRef,

Specify which table to update.

Examples

See UpdateStatement::values

pub fn into_table<T>(&mut self, table: T) -> &mut UpdateStatementwhere T: IntoTableRef,

👎Deprecated since 0.5.0: Please use the UpdateStatement::table function instead

pub fn col_expr<T>(&mut self, col: T, expr: SimpleExpr) -> &mut UpdateStatementwhere T: IntoIden,

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

pub fn value_expr<T>( &mut self, col: T, expr: SimpleExpr ) -> &mut UpdateStatementwhere T: IntoIden,

pub fn values<T, I>(&mut self, values: I) -> &mut UpdateStatementwhere T: IntoIden, I: IntoIterator<Item = (T, Value)>,

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

pub fn value<T>(&mut self, col: T, value: Value) -> &mut UpdateStatementwhere T: IntoIden,

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

pub fn limit(&mut self, limit: u64) -> &mut UpdateStatement

Limit number of updated rows.

pub fn returning(&mut self, returning: ReturningClause) -> &mut UpdateStatement

RETURNING expressions.

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))
    .returning(Query::returning().columns([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 RETURNING "id""#
);

pub fn returning_col<C>(&mut self, col: C) -> &mut UpdateStatementwhere C: IntoColumnRef,

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.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 RETURNING "id""#
);

pub fn returning_all(&mut self) -> &mut UpdateStatement

RETURNING expressions all columns.

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

let query = Query::insert()
    .into_table(Glyph::Table)
    .columns([Glyph::Image])
    .values_panic(vec!["12A".into()])
    .returning_all()
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    "INSERT INTO `glyph` (`image`) VALUES ('12A')"
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING *"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"INSERT INTO "glyph" ("image") VALUES ('12A') RETURNING *"#
);

pub fn with(self, clause: WithClause) -> WithQuery

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()))
        .col_expr(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§

§

impl Clone for UpdateStatement

§

fn clone(&self) -> UpdateStatement

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 ConditionalStatement for UpdateStatement

§

fn cond_where<C>(&mut self, condition: C) -> &mut UpdateStatementwhere C: IntoCondition,

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
§

fn and_where(&mut self, other: SimpleExpr) -> &mut Self

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

fn and_where_option(&mut self, other: Option<SimpleExpr>) -> &mut Self

Optional and where, short hand for if c.is_some() q.and_where(c). Read more
§

fn or_where(&mut self, other: SimpleExpr) -> &mut Self

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

impl Debug for UpdateStatement

§

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

Formats the value using the given formatter. Read more
§

impl Default for UpdateStatement

§

fn default() -> UpdateStatement

Returns the “default value” for a type. Read more
§

impl OrderedStatement for UpdateStatement

§

fn clear_order_by(&mut self) -> &mut UpdateStatement

Clear order expressions
§

fn order_by<T>(&mut self, col: T, order: Order) -> &mut Selfwhere T: IntoColumnRef,

Order by column. Read more
§

fn order_by_tbl<T, C>(&mut self, table: T, col: C, order: Order) -> &mut Selfwhere T: IntoIden, C: IntoIden,

👎Deprecated since 0.9.0: Please use the [OrderedStatement::order_by] with a tuple as [ColumnRef]
§

fn order_by_expr(&mut self, expr: SimpleExpr, order: Order) -> &mut Self

Order by SimpleExpr.
§

fn order_by_customs<T>(&mut self, cols: Vec<(T, Order), Global>) -> &mut Selfwhere T: ToString,

Order by custom string.
§

fn order_by_columns<T>(&mut self, cols: Vec<(T, Order), Global>) -> &mut Selfwhere T: IntoColumnRef,

Order by vector of columns.
§

fn order_by_table_columns<T, C>( &mut self, cols: Vec<(T, C, Order), Global> ) -> &mut Selfwhere T: IntoIden, C: IntoIden,

👎Deprecated since 0.9.0: Please use the [OrderedStatement::order_by_columns] with a tuple as [ColumnRef]
§

fn order_by_with_nulls<T>( &mut self, col: T, order: Order, nulls: NullOrdering ) -> &mut Selfwhere T: IntoColumnRef,

Order by column with nulls order option. Read more
§

fn order_by_expr_with_nulls( &mut self, expr: SimpleExpr, order: Order, nulls: NullOrdering ) -> &mut Self

Order by SimpleExpr with nulls order option.
§

fn order_by_customs_with_nulls<T>( &mut self, cols: Vec<(T, Order, NullOrdering), Global> ) -> &mut Selfwhere T: ToString,

Order by custom string with nulls order option.
§

fn order_by_columns_with_nulls<T>( &mut self, cols: Vec<(T, Order, NullOrdering), Global> ) -> &mut Selfwhere T: IntoColumnRef,

Order by vector of columns with nulls order option.
§

impl QueryStatementBuilder for UpdateStatement

§

fn build_collect_any_into( &self, query_builder: &dyn QueryBuilder, sql: &mut SqlWriter, collector: &mut dyn FnMut(Value) )

Build corresponding SQL statement into the SqlWriter for certain database backend and collect query parameters
§

fn into_sub_query_statement(self) -> SubQueryStatement

§

fn build_any(&self, query_builder: &dyn QueryBuilder) -> (String, Values)

Build corresponding SQL statement for certain database backend and collect query parameters into a vector
§

fn build_collect_any( &self, query_builder: &dyn QueryBuilder, collector: &mut dyn FnMut(Value) ) -> String

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

impl QueryStatementWriter for UpdateStatement

§

fn build_collect<T>( &self, query_builder: T, collector: &mut dyn FnMut(Value) ) -> Stringwhere T: QueryBuilder,

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)),
    ]
);
§

fn to_string<T>(&self, query_builder: T) -> Stringwhere T: QueryBuilder,

Build corresponding SQL statement for certain database backend and return SQL string Read more
§

fn build<T>(&self, query_builder: T) -> (String, Values)where T: QueryBuilder,

Build corresponding SQL statement for certain database backend and collect query parameters into a vector Read more
§

impl StatementBuilder for UpdateStatement

§

fn build(&self, db_backend: &DatabaseBackend) -> Statement

Method to call in order to build a Statement

Auto Trait Implementations§

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,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · 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>,

const: unstable · 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> Same<T> for T

§

type Output = T

Should always be Self
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.
const: unstable · 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.
const: unstable · 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