Skip to main content

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