databend_common_ast/ast/statements/
delete.rs

1// Copyright 2021 Datafuse Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use 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::TableReference;
24use crate::ast::With;
25
26#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
27pub struct DeleteStmt {
28    pub hints: Option<Hint>,
29    pub table: TableReference,
30    pub selection: Option<Expr>,
31    // With clause, common table expression
32    pub with: Option<With>,
33}
34
35impl Display for DeleteStmt {
36    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
37        if let Some(cte) = &self.with {
38            write!(f, "WITH {} ", cte)?;
39        }
40        write!(f, "DELETE ")?;
41        if let Some(hints) = &self.hints {
42            write!(f, "{} ", hints)?;
43        }
44        write!(f, "FROM {}", self.table)?;
45        if let Some(conditions) = &self.selection {
46            write!(f, " WHERE {conditions}")?;
47        }
48        Ok(())
49    }
50}