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(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
pub fn order_by_tbl<T, C>(
&mut self,
table: T,
col: C,
order: Order
) -> &mut Self where
T: IntoIden,
C: IntoIden,
Please use the [OrderedStatement::order_by
] with a tuple as [ColumnRef
]
pub fn order_by_columns<T>(&mut self, cols: Vec<(T, Order)>) -> &mut Self where
T: IntoColumnRef,
pub fn order_by_table_columns<T, C>(
&mut self,
cols: Vec<(T, C, Order)>
) -> &mut Self where
T: IntoIden,
C: IntoIden,
Please use the [OrderedStatement::order_by_columns
] with a tuple as [ColumnRef
]
Please use [ConditionalStatement::cond_where
]. Calling or_where
after and_where
will panic.
Construct a new UpdateStatement
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"#
);
Alias of UpdateStatement::col_expr
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"#
);
RETURNING expressions.
Note:
Works on
- PostgreSQL
- SQLite
- SQLite version >= 3.35.0
- Note that sea-query won’t try to enforce either of these constraints
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 "id""#
);
RETURNING a column after update.
Wrapper over UpdateStatement::returning()
.
Note:
Works on
- PostgreSQL
- SQLite
- SQLite version >= 3.35.0
- Note that sea-query won’t try to enforce either of these constraints
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""#
);
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
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
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
Order by column. Read more
Please use the [OrderedStatement::order_by
] with a tuple as [ColumnRef
]
Order by SimpleExpr
.
Order by custom string.
Order by vector of columns.
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 Self where
T: IntoColumnRef,
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
fn order_by_expr_with_nulls(
&mut self,
expr: SimpleExpr,
order: Order,
nulls: NullOrdering
) -> &mut Self
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)>
) -> &mut Self where
T: ToString,
fn order_by_customs_with_nulls<T>(
&mut self,
cols: Vec<(T, Order, NullOrdering)>
) -> &mut Self where
T: ToString,
Order by custom string with nulls order option.
fn order_by_columns_with_nulls<T>(
&mut self,
cols: Vec<(T, Order, NullOrdering)>
) -> &mut Self where
T: IntoColumnRef,
fn order_by_columns_with_nulls<T>(
&mut self,
cols: Vec<(T, Order, NullOrdering)>
) -> &mut Self where
T: IntoColumnRef,
Order by vector of columns with nulls order option.
fn build_collect_any_into(
&self,
query_builder: &dyn QueryBuilder,
sql: &mut SqlWriter,
collector: &mut dyn FnMut(Value)
)
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
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
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
fn build_collect<T: QueryBuilder>(
&self,
query_builder: T,
collector: &mut dyn FnMut(Value)
) -> String
fn build_collect<T: QueryBuilder>(
&self,
query_builder: T,
collector: &mut dyn FnMut(Value)
) -> String
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 return SQL string Read more
Auto Trait Implementations
impl !RefUnwindSafe for UpdateStatement
impl Send for UpdateStatement
impl Sync for UpdateStatement
impl Unpin for UpdateStatement
impl !UnwindSafe for UpdateStatement
Blanket Implementations
Mutably borrows from an owned value. Read more