use crate::{
backend::QueryBuilder,
expr::*,
prepare::*,
query::{condition::*, OrderedStatement},
types::*,
value::*,
QueryStatementBuilder, QueryStatementWriter, ReturningClause, SubQueryStatement, WithClause,
WithQuery,
};
#[derive(Debug, Clone)]
pub struct UpdateStatement {
pub(crate) table: Option<Box<TableRef>>,
pub(crate) values: Vec<(DynIden, Box<SimpleExpr>)>,
pub(crate) r#where: ConditionHolder,
pub(crate) orders: Vec<OrderExpr>,
pub(crate) limit: Option<Value>,
pub(crate) returning: Option<ReturningClause>,
}
impl Default for UpdateStatement {
fn default() -> Self {
Self::new()
}
}
impl UpdateStatement {
pub fn new() -> Self {
Self {
table: None,
values: Vec::new(),
r#where: ConditionHolder::new(),
orders: Vec::new(),
limit: None,
returning: None,
}
}
#[allow(clippy::wrong_self_convention)]
pub fn table<T>(&mut self, tbl_ref: T) -> &mut Self
where
T: IntoTableRef,
{
self.table = Some(Box::new(tbl_ref.into_table_ref()));
self
}
pub fn values<T, I>(&mut self, values: I) -> &mut Self
where
T: IntoIden,
I: IntoIterator<Item = (T, SimpleExpr)>,
{
for (k, v) in values.into_iter() {
self.values.push((k.into_iden(), Box::new(v)));
}
self
}
pub fn value<C, T>(&mut self, col: C, value: T) -> &mut Self
where
C: IntoIden,
T: Into<SimpleExpr>,
{
self.values.push((col.into_iden(), Box::new(value.into())));
self
}
#[deprecated(since = "0.27.0", note = "Please use the [`UpdateStatement::values`]")]
pub fn exprs<T, I>(&mut self, values: I) -> &mut Self
where
T: IntoIden,
I: IntoIterator<Item = (T, SimpleExpr)>,
{
self.values(values)
}
#[deprecated(since = "0.27.0", note = "Please use the [`UpdateStatement::value`]")]
pub fn col_expr<C, T>(&mut self, col: C, expr: T) -> &mut Self
where
C: IntoIden,
T: Into<SimpleExpr>,
{
self.value(col, expr)
}
#[deprecated(since = "0.27.0", note = "Please use the [`UpdateStatement::value`]")]
pub fn value_expr<C, T>(&mut self, col: C, expr: T) -> &mut Self
where
C: IntoIden,
T: Into<SimpleExpr>,
{
self.value(col, expr)
}
pub fn limit(&mut self, limit: u64) -> &mut Self {
self.limit = Some(limit.into());
self
}
pub fn returning(&mut self, returning: ReturningClause) -> &mut Self {
self.returning = Some(returning);
self
}
pub fn returning_col<C>(&mut self, col: C) -> &mut Self
where
C: IntoColumnRef,
{
self.returning(ReturningClause::Columns(vec![col.into_column_ref()]))
}
pub fn returning_all(&mut self) -> &mut Self {
self.returning(ReturningClause::All)
}
pub fn with(self, clause: WithClause) -> WithQuery {
clause.query(self)
}
}
impl QueryStatementBuilder for UpdateStatement {
fn build_collect_any_into(&self, query_builder: &dyn QueryBuilder, sql: &mut dyn SqlWriter) {
query_builder.prepare_update_statement(self, sql);
}
fn into_sub_query_statement(self) -> SubQueryStatement {
SubQueryStatement::UpdateStatement(self)
}
}
impl QueryStatementWriter for UpdateStatement {
fn build_collect_into<T: QueryBuilder>(&self, query_builder: T, sql: &mut dyn SqlWriter) {
query_builder.prepare_update_statement(self, sql);
}
}
impl OrderedStatement for UpdateStatement {
fn add_order_by(&mut self, order: OrderExpr) -> &mut Self {
self.orders.push(order);
self
}
fn clear_order_by(&mut self) -> &mut Self {
self.orders = Vec::new();
self
}
}
impl ConditionalStatement for UpdateStatement {
fn and_or_where(&mut self, condition: LogicalChainOper) -> &mut Self {
self.r#where.add_and_or(condition);
self
}
fn cond_where<C>(&mut self, condition: C) -> &mut Self
where
C: IntoCondition,
{
self.r#where.add_condition(condition.into_condition());
self
}
}