databend_common_ast/ast/statements/
update.rs1use std::fmt::Display;
16use std::fmt::Formatter;
17
18use derive_visitor::Drive;
19use derive_visitor::DriveMut;
20
21use crate::ast::Expr;
22use crate::ast::Hint;
23use crate::ast::Identifier;
24use crate::ast::MutationSource;
25use crate::ast::MutationUpdateExpr;
26use crate::ast::TableAlias;
27use crate::ast::With;
28use crate::ast::write_comma_separated_list;
29use crate::ast::write_dot_separated_list;
30
31#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
32pub struct UpdateStmt {
33 pub hints: Option<Hint>,
34 pub catalog: Option<Identifier>,
35 pub database: Option<Identifier>,
36 pub table: Identifier,
37 pub table_alias: Option<TableAlias>,
38 pub update_list: Vec<MutationUpdateExpr>,
39 pub from: Option<MutationSource>,
40 pub selection: Option<Expr>,
41 pub with: Option<With>,
43}
44
45impl Display for UpdateStmt {
46 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
47 if let Some(cte) = &self.with {
48 write!(f, "WITH {} ", cte)?;
49 }
50 write!(f, "UPDATE ")?;
51 if let Some(hints) = &self.hints {
52 write!(f, "{} ", hints)?;
53 }
54 write_dot_separated_list(
55 f,
56 self.catalog
57 .iter()
58 .chain(&self.database)
59 .chain(Some(&self.table)),
60 )?;
61 if let Some(alias) = &self.table_alias {
62 write!(f, " AS {}", alias)?;
63 }
64 write!(f, " SET ")?;
65 write_comma_separated_list(f, &self.update_list)?;
66 if let Some(from) = &self.from {
67 write!(f, " FROM {} ", from)?;
68 }
69 if let Some(conditions) = &self.selection {
70 write!(f, " WHERE {conditions}")?;
71 }
72 Ok(())
73 }
74}
75
76#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
77pub struct UpdateExpr {
78 pub name: Identifier,
79 pub expr: Expr,
80}
81
82impl Display for UpdateExpr {
83 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
84 write!(f, "{} = {}", self.name, self.expr)
85 }
86}