Skip to main content

databend_common_ast/ast/statements/
worker.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::collections::BTreeMap;
16use std::fmt::Display;
17use std::fmt::Formatter;
18
19use derive_visitor::Drive;
20use derive_visitor::DriveMut;
21
22use crate::ast::Identifier;
23use crate::ast::statements::tag::TagSetItem;
24use crate::ast::write_comma_separated_list;
25
26#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
27pub struct ShowWorkersStmt {}
28
29impl Display for ShowWorkersStmt {
30    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
31        write!(f, "SHOW WORKERS")
32    }
33}
34
35#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
36pub struct CreateWorkerStmt {
37    pub if_not_exists: bool,
38    pub name: Identifier,
39    pub options: BTreeMap<String, String>,
40}
41
42impl Display for CreateWorkerStmt {
43    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
44        write!(f, "CREATE WORKER ")?;
45        if self.if_not_exists {
46            write!(f, "IF NOT EXISTS ")?;
47        }
48        write!(f, "{}", self.name)?;
49
50        if !self.options.is_empty() {
51            write!(f, " WITH ")?;
52            for (idx, (key, value)) in self.options.iter().enumerate() {
53                if idx != 0 {
54                    write!(f, ",")?;
55                }
56                write!(f, " {} = '{}'", key, value)?;
57            }
58        }
59
60        Ok(())
61    }
62}
63
64#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
65pub struct DropWorkerStmt {
66    pub if_exists: bool,
67    pub name: Identifier,
68}
69
70impl Display for DropWorkerStmt {
71    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
72        write!(f, "DROP WORKER ")?;
73        if self.if_exists {
74            write!(f, "IF EXISTS ")?;
75        }
76        write!(f, "{}", self.name)
77    }
78}
79
80#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
81pub enum AlterWorkerAction {
82    SetTag { tags: Vec<TagSetItem> },
83    UnsetTag { tags: Vec<Identifier> },
84    SetOptions { options: BTreeMap<String, String> },
85    UnsetOptions { options: Vec<Identifier> },
86    Suspend,
87    Resume,
88}
89
90impl Display for AlterWorkerAction {
91    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
92        match self {
93            AlterWorkerAction::SetTag { tags } => {
94                write!(f, "SET TAG ")?;
95                write_comma_separated_list(f, tags)
96            }
97            AlterWorkerAction::UnsetTag { tags } => {
98                write!(f, "UNSET TAG ")?;
99                write_comma_separated_list(f, tags)
100            }
101            AlterWorkerAction::SetOptions { options } => {
102                write!(f, "SET ")?;
103                for (idx, (key, value)) in options.iter().enumerate() {
104                    if idx != 0 {
105                        write!(f, ",")?;
106                    }
107                    write!(f, "{} = '{}'", key, value)?;
108                }
109                Ok(())
110            }
111            AlterWorkerAction::UnsetOptions { options } => {
112                write!(f, "UNSET ")?;
113                write_comma_separated_list(f, options)
114            }
115            AlterWorkerAction::Suspend => write!(f, "SUSPEND"),
116            AlterWorkerAction::Resume => write!(f, "RESUME"),
117        }
118    }
119}
120
121#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
122pub struct AlterWorkerStmt {
123    pub name: Identifier,
124    pub action: AlterWorkerAction,
125}
126
127impl Display for AlterWorkerStmt {
128    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
129        write!(f, "ALTER WORKER {} {}", self.name, self.action)
130    }
131}