use crate::SqlWriterValues;
use crate::expr::Expr;
use crate::expr::write_expr;
use crate::query::Returning;
use crate::query::With;
use crate::query::write_returning;
use crate::query::write_with;
use crate::types::Iden;
use crate::types::IntoIden;
use crate::types::IntoTableRef;
use crate::types::TableRef;
use crate::types::write_iden;
use crate::types::write_table_ref;
use crate::writer::SqlWriter;
#[derive(Default, Debug, Clone, PartialEq)]
pub struct Update {
table: Option<TableRef>,
values: Vec<(Iden, Expr)>,
conditions: Vec<Expr>,
returning: Option<Returning>,
with: Option<With>,
}
impl Update {
pub fn new() -> Self {
Self::default()
}
pub fn to_values(&self) -> SqlWriterValues {
let mut w = SqlWriterValues::new();
write_update(&mut w, self);
w
}
pub fn to_sql(&self) -> String {
let mut sql = String::new();
write_update(&mut sql, self);
sql
}
pub fn table<T>(mut self, table: T) -> Self
where
T: IntoTableRef,
{
self.table = Some(table.into());
self
}
pub fn values<T, I>(mut self, values: I) -> Self
where
T: IntoIden,
I: IntoIterator<Item = (T, Expr)>,
{
for (k, v) in values.into_iter() {
self.values.push((k.into_iden(), v));
}
self
}
pub fn and_where<T>(mut self, expr: T) -> Self
where
T: Into<Expr>,
{
self.conditions.push(expr.into());
self
}
pub fn returning(mut self, returning: Returning) -> Self {
self.returning = Some(returning);
self
}
pub fn with(mut self, with: With) -> Self {
self.with = Some(with);
self
}
}
pub(crate) fn write_update<W: SqlWriter>(w: &mut W, update: &Update) {
if let Some(with) = &update.with {
write_with(w, with);
w.push_char(' ');
}
w.push_str("UPDATE ");
if let Some(table) = &update.table {
write_table_ref(w, table);
}
if !update.values.is_empty() {
w.push_str(" SET ");
for (i, (col, val)) in update.values.iter().enumerate() {
if i > 0 {
w.push_str(", ");
}
write_iden(w, col);
w.push_str(" = ");
write_expr(w, val);
}
}
if let Some(condition) = Expr::from_conditions(update.conditions.clone()) {
w.push_str(" WHERE ");
write_expr(w, &condition);
}
if let Some(returning) = &update.returning {
write_returning(w, returning);
}
}