Skip to main content

databend_common_ast/ast/statements/
row_access_policy.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::Identifier;
23use crate::ast::TypeName;
24use crate::ast::write_comma_separated_list;
25
26#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
27pub struct RowAccessPolicyType {
28    pub name: String,
29    pub data_type: TypeName,
30}
31
32impl Display for RowAccessPolicyType {
33    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34        write!(f, "{} {}", self.name, self.data_type)
35    }
36}
37
38#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
39pub struct RowAccessPolicyDefinition {
40    pub parameters: Vec<RowAccessPolicyType>,
41    pub definition: Box<Expr>,
42}
43
44impl Display for RowAccessPolicyDefinition {
45    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46        write!(f, "(")?;
47        write_comma_separated_list(f, &self.parameters)?;
48        write!(f, ")")?;
49        write!(f, " RETURNS BOOLEAN ->")?;
50        write!(f, " {}", self.definition)
51    }
52}
53
54#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
55pub struct CreateRowAccessPolicyStmt {
56    pub if_not_exists: bool,
57    pub name: Identifier,
58    pub description: Option<String>,
59    pub definition: RowAccessPolicyDefinition,
60}
61
62// CREATE ROW ACCESS POLICY [ IF NOT EXISTS ] <name> AS
63// ( <arg_name> <arg_type> [ , ... ] ) RETURNS BOOLEAN -> <body>
64// [ COMMENT = '<string_literal>' ]
65impl Display for CreateRowAccessPolicyStmt {
66    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
67        write!(f, "CREATE ROW ACCESS POLICY")?;
68        if self.if_not_exists {
69            write!(f, " IF NOT EXISTS")?;
70        }
71        write!(f, " {} AS {}", self.name, self.definition)?;
72        if let Some(description) = &self.description {
73            write!(f, " COMMENT = '{description}'")?;
74        }
75        Ok(())
76    }
77}
78
79#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
80pub struct DropRowAccessPolicyStmt {
81    pub if_exists: bool,
82    pub name: String,
83}
84
85impl Display for DropRowAccessPolicyStmt {
86    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
87        write!(f, "DROP ROW ACCESS 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 DescRowAccessPolicyStmt {
99    pub name: String,
100}
101
102impl Display for DescRowAccessPolicyStmt {
103    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
104        write!(f, "DESCRIBE ROW ACCESS POLICY {}", self.name)?;
105
106        Ok(())
107    }
108}