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

Source§

impl UpdateStatement

Source

pub fn new() -> UpdateStatement

Construct a new UpdateStatement

Source

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

Specify which table to update.

§Examples

See UpdateStatement::values

Source

pub fn from<R>(&mut self, tbl_ref: R) -> &mut UpdateStatement
where R: IntoTableRef,

Update using data from another table (UPDATE .. FROM ..).

§MySQL Notes

MySQL doesn’t support the UPDATE FROM syntax. And the current implementation attempt to tranform it to the UPDATE JOIN syntax, which only works for one join target.

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

let query = Query::update()
    .table(Glyph::Table)
    .value(Glyph::Tokens, Expr::column((Char::Table, Char::Character)))
    .from(Char::Table)
    .cond_where(
        Expr::col((Glyph::Table, Glyph::Image))
            .eq(Expr::col((Char::Table, Char::UserData))),
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    "UPDATE `glyph` JOIN `character` ON `glyph`.`image` = `character`.`user_data` SET `glyph`.`tokens` = `character`.`character`"
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"UPDATE "glyph" SET "tokens" = "character"."character" FROM "character" WHERE "glyph"."image" = "character"."user_data""#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"UPDATE "glyph" SET "tokens" = "character"."character" FROM "character" WHERE "glyph"."image" = "character"."user_data""#
);
Source

pub fn values<T, I>(&mut self, values: I) -> &mut UpdateStatement
where T: IntoIden, I: IntoIterator<Item = (T, SimpleExpr)>,

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

pub fn value<C, T>(&mut self, col: C, value: T) -> &mut UpdateStatement
where C: IntoIden, T: Into<SimpleExpr>,

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

let query = Query::update()
    .table(Glyph::Table)
    .value(Glyph::Aspect, Expr::value(Value::Int(None)))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"UPDATE `glyph` SET `aspect` = NULL"#
);
assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = NULL"#
);
assert_eq!(
    query.to_string(SqliteQueryBuilder),
    r#"UPDATE "glyph" SET "aspect" = NULL"#
);
Source

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

Limit number of updated rows.

Source

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

pub fn returning_col<C>(&mut self, col: C) -> &mut UpdateStatement
where 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)
    .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""#
);
Source

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

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

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

pub fn with_cte<C>(&mut self, clause: C) -> &mut UpdateStatement
where C: Into<WithClause>,

Create a Common Table Expression by specifying a [CommonTableExpression] or 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 query = 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"))
        .with_cte(with_clause)
        .to_owned();

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

pub fn get_values(&self) -> &[(SeaRc<dyn Iden>, Box<SimpleExpr>)]

Get column values

Source§

impl UpdateStatement

Source§

impl UpdateStatement

Source

pub fn build_collect_into<T>(&self, query_builder: T, sql: &mut dyn SqlWriter)
where T: QueryBuilder,

Source

pub fn build_collect<T>( &self, query_builder: T, sql: &mut dyn SqlWriter, ) -> String
where T: QueryBuilder,

Source

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

Source

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

Source§

impl UpdateStatement

Source

pub fn add_order_by(&mut self, order: OrderExpr) -> &mut UpdateStatement

Source

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

Source

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

Source

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

Source

pub fn order_by_customs<I, T>(&mut self, cols: I) -> &mut UpdateStatement
where T: ToString, I: IntoIterator<Item = (T, Order)>,

Source

pub fn order_by_columns<I, T>(&mut self, cols: I) -> &mut UpdateStatement
where T: IntoColumnRef, I: IntoIterator<Item = (T, Order)>,

Source

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

Source

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

Source

pub fn order_by_customs_with_nulls<I, T>( &mut self, cols: I, ) -> &mut UpdateStatement
where T: ToString, I: IntoIterator<Item = (T, Order, NullOrdering)>,

Source

pub fn order_by_columns_with_nulls<I, T>( &mut self, cols: I, ) -> &mut UpdateStatement
where T: IntoColumnRef, I: IntoIterator<Item = (T, Order, NullOrdering)>,

Source§

impl UpdateStatement

Trait Implementations§

Source§

impl Clone for UpdateStatement

Source§

fn clone(&self) -> UpdateStatement

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl ConditionalStatement for UpdateStatement

Source§

fn cond_where<C>(&mut self, condition: C) -> &mut UpdateStatement
where 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
Source§

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

And where condition. Calling or_where after and_where will panic. Read more
Source§

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

impl Debug for UpdateStatement

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for UpdateStatement

Source§

fn default() -> UpdateStatement

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

impl OrderedStatement for UpdateStatement

Source§

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

Clear order expressions
Source§

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

Order by column. Read more
Source§

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

Order by SimpleExpr.
Source§

fn order_by_customs<I, T>(&mut self, cols: I) -> &mut Self
where T: ToString, I: IntoIterator<Item = (T, Order)>,

Order by custom string.
Source§

fn order_by_columns<I, T>(&mut self, cols: I) -> &mut Self
where T: IntoColumnRef, I: IntoIterator<Item = (T, Order)>,

Order by vector of columns.
Source§

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

Order by column with nulls order option. Read more
Source§

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

Order by SimpleExpr with nulls order option.
Source§

fn order_by_customs_with_nulls<I, T>(&mut self, cols: I) -> &mut Self
where T: ToString, I: IntoIterator<Item = (T, Order, NullOrdering)>,

Order by custom string with nulls order option.
Source§

fn order_by_columns_with_nulls<I, T>(&mut self, cols: I) -> &mut Self
where T: IntoColumnRef, I: IntoIterator<Item = (T, Order, NullOrdering)>,

Order by vector of columns with nulls order option.
Source§

impl PartialEq for UpdateStatement

Source§

fn eq(&self, other: &UpdateStatement) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl QueryStatementBuilder for UpdateStatement

Source§

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

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

fn into_sub_query_statement(self) -> SubQueryStatement

Source§

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

fn build_collect_any( &self, query_builder: &dyn QueryBuilder, sql: &mut dyn SqlWriter, ) -> String

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

impl QueryStatementWriter for UpdateStatement

Source§

fn build_collect_into<T>(&self, query_builder: T, sql: &mut dyn SqlWriter)
where T: QueryBuilder,

Source§

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

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

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

fn build_collect<T>(&self, query_builder: T, sql: &mut dyn SqlWriter) -> String
where T: QueryBuilder,

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

impl SqlxBinder for UpdateStatement

Source§

fn build_sqlx<T>(&self, query_builder: T) -> (String, SqlxValues)
where T: QueryBuilder,

Source§

fn build_any_sqlx( &self, query_builder: &dyn QueryBuilder, ) -> (String, SqlxValues)

Source§

impl StatementBuilder for UpdateStatement

Source§

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

Method to call in order to build a Statement
Source§

impl StructuralPartialEq for UpdateStatement

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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

Source§

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

Source§

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

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T