databend_common_ast/ast/statements/
principal.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::Identifier;
23
24#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
25pub struct ShareNameIdent {
26    pub tenant: Identifier,
27    pub share: Identifier,
28}
29
30impl Display for ShareNameIdent {
31    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
32        write!(f, "{}.{}", self.tenant, self.share)
33    }
34}
35
36#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
37pub struct UserIdentity {
38    pub username: String,
39    pub hostname: String,
40}
41
42impl Display for UserIdentity {
43    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
44        write!(
45            f,
46            "{}@{}",
47            QuotedString(&self.username, '\''),
48            QuotedString(&self.hostname, '\''),
49        )
50    }
51}
52
53#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
54pub enum PrincipalIdentity {
55    User(UserIdentity),
56    Role(String),
57}
58
59impl Display for PrincipalIdentity {
60    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
61        match self {
62            PrincipalIdentity::User(u) => write!(f, " USER {u}"),
63            PrincipalIdentity::Role(r) => write!(f, " ROLE '{r}'"),
64        }
65    }
66}
67
68#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
69pub enum CreateOption {
70    Create,
71    CreateIfNotExists,
72    CreateOrReplace,
73}
74
75#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
76pub enum AuthType {
77    NoPassword,
78    Sha256Password,
79    DoubleSha1Password,
80    JWT,
81}
82
83impl Display for AuthType {
84    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
85        write!(f, "{}", match self {
86            AuthType::NoPassword => "no_password",
87            AuthType::Sha256Password => "sha256_password",
88            AuthType::DoubleSha1Password => "double_sha1_password",
89            AuthType::JWT => "jwt",
90        })
91    }
92}
93
94#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
95pub enum CatalogType {
96    Default,
97    Hive,
98    Iceberg,
99}
100
101impl Display for CatalogType {
102    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
103        match self {
104            CatalogType::Default => write!(f, "DEFAULT"),
105            CatalogType::Hive => write!(f, "HIVE"),
106            CatalogType::Iceberg => write!(f, "ICEBERG"),
107        }
108    }
109}
110
111#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
112pub enum UserPrivilegeType {
113    // UsagePrivilege is a synonym for “no privileges”, if object is udf, means can use this udf
114    Usage,
115    // Privilege to select rows from tables in a database.
116    Select,
117    // Privilege to insert into tables in a database.
118    Insert,
119    // Privilege to update rows in a table
120    Update,
121    // Privilege to delete rows in a table
122    Delete,
123    // Privilege to create databases or tables.
124    Create,
125    // Privilege to drop databases or tables.
126    Drop,
127    // Privilege to alter databases or tables.
128    Alter,
129    // Privilege to Kill query, Set global configs, etc.
130    Super,
131    // Privilege to Create User.
132    CreateUser,
133    // Privilege to Create Role.
134    CreateRole,
135    // Privilege to Grant/Revoke privileges to users or roles
136    Grant,
137    // Privilege to Create Stage.
138    CreateStage,
139    // Privilege to Drop role.
140    DropRole,
141    // Privilege to Drop user.
142    DropUser,
143    // Privilege to Create/Drop DataMask.
144    CreateDataMask,
145    // Privilege to Own a databend object such as database/table.
146    Ownership,
147    // Privilege to Read stage
148    Read,
149    // Privilege to Write stage
150    Write,
151    // Privilege to Create database
152    CreateDatabase,
153    // Privilege to Create warehouse
154    CreateWarehouse,
155    // Privilege to Access connection
156    AccessConnection,
157    // Privilege to Create connection
158    CreateConnection,
159    // Privilege to Access sequence
160    AccessSequence,
161    // Privilege to Create sequence
162    CreateSequence,
163    // Discard Privilege Type
164    Set,
165}
166
167impl Display for UserPrivilegeType {
168    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
169        write!(f, "{}", match self {
170            UserPrivilegeType::Usage => "USAGE",
171            UserPrivilegeType::Create => "CREATE",
172            UserPrivilegeType::Update => "UPDATE",
173            UserPrivilegeType::Select => "SELECT",
174            UserPrivilegeType::Insert => "INSERT",
175            UserPrivilegeType::Delete => "DELETE",
176            UserPrivilegeType::Drop => "DROP",
177            UserPrivilegeType::Alter => "ALTER",
178            UserPrivilegeType::Super => "SUPER",
179            UserPrivilegeType::CreateUser => "CREATE USER",
180            UserPrivilegeType::DropUser => "DROP USER",
181            UserPrivilegeType::CreateRole => "CREATE ROLE",
182            UserPrivilegeType::DropRole => "DROP ROLE",
183            UserPrivilegeType::CreateStage => "CREATE STAGE",
184            UserPrivilegeType::Grant => "GRANT",
185            UserPrivilegeType::Set => "SET",
186            UserPrivilegeType::CreateDataMask => "CREATE DATAMASK",
187            UserPrivilegeType::Ownership => "OWNERSHIP",
188            UserPrivilegeType::Read => "Read",
189            UserPrivilegeType::Write => "Write",
190            UserPrivilegeType::CreateDatabase => "CREATE DATABASE",
191            UserPrivilegeType::CreateWarehouse => "CREATE WAREHOUSE",
192            UserPrivilegeType::CreateConnection => "CREATE CONNECTION",
193            UserPrivilegeType::AccessConnection => "ACCESS CONNECTION",
194            UserPrivilegeType::CreateSequence => "CREATE SEQUENCE",
195            UserPrivilegeType::AccessSequence => "ACCESS SEQUENCE",
196        })
197    }
198}
199
200#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
201pub enum ShareGrantObjectPrivilege {
202    // For DATABASE or SCHEMA
203    Usage,
204    // For DATABASE
205    ReferenceUsage,
206    // For TABLE or VIEW
207    Select,
208}
209
210impl Display for ShareGrantObjectPrivilege {
211    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
212        match *self {
213            ShareGrantObjectPrivilege::Usage => write!(f, "USAGE"),
214            ShareGrantObjectPrivilege::ReferenceUsage => write!(f, "REFERENCE_USAGE"),
215            ShareGrantObjectPrivilege::Select => write!(f, "SELECT"),
216        }
217    }
218}