databend_common_ast/ast/statements/
data_mask.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::quote::QuotedString;
22use crate::ast::CreateOption;
23use crate::ast::Expr;
24use crate::ast::TypeName;
25
26#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
27pub struct DataMaskArg {
28    pub arg_name: String,
29    pub arg_type: TypeName,
30}
31
32#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
33pub struct DataMaskPolicy {
34    pub args: Vec<DataMaskArg>,
35    pub return_type: TypeName,
36    pub body: Expr,
37    pub comment: Option<String>,
38}
39
40#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
41pub struct CreateDatamaskPolicyStmt {
42    pub create_option: CreateOption,
43    pub name: String,
44    pub policy: DataMaskPolicy,
45}
46
47impl Display for CreateDatamaskPolicyStmt {
48    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
49        write!(f, "CREATE ")?;
50        if let CreateOption::CreateOrReplace = self.create_option {
51            write!(f, "OR REPLACE ")?;
52        }
53        write!(f, "MASKING POLICY ")?;
54        if let CreateOption::CreateIfNotExists = self.create_option {
55            write!(f, "IF NOT EXISTS ")?;
56        }
57        write!(f, "{} AS (", self.name)?;
58        let mut flag = false;
59        for arg in &self.policy.args {
60            if flag {
61                write!(f, ",")?;
62            }
63            flag = true;
64            write!(f, "{} {}", arg.arg_name, arg.arg_type)?;
65        }
66        write!(
67            f,
68            ") RETURNS {} -> {}",
69            self.policy.return_type, self.policy.body
70        )?;
71        if let Some(comment) = &self.policy.comment {
72            write!(f, " COMMENT = {}", QuotedString(comment, '\''))?;
73        }
74
75        Ok(())
76    }
77}
78
79#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
80pub struct DropDatamaskPolicyStmt {
81    pub if_exists: bool,
82    pub name: String,
83}
84
85impl Display for DropDatamaskPolicyStmt {
86    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
87        write!(f, "DROP MASKING POLICY ")?;
88        if self.if_exists {
89            write!(f, "IF EXISTS ")?;
90        }
91        write!(f, "{}", self.name)?;
92
93        Ok(())
94    }
95}
96
97#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
98pub struct DescDatamaskPolicyStmt {
99    pub name: String,
100}
101
102impl Display for DescDatamaskPolicyStmt {
103    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
104        write!(f, "DESCRIBE MASKING POLICY {}", self.name)?;
105
106        Ok(())
107    }
108}