github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/parsers/tree/grant.go (about) 1 // Copyright 2021 Matrix Origin 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 15 package tree 16 17 type GrantType int 18 19 const ( 20 GrantTypePrivilege GrantType = iota 21 GrantTypeRole 22 GrantTypeProxy 23 ) 24 25 type Grant struct { 26 statementImpl 27 Typ GrantType 28 GrantPrivilege GrantPrivilege 29 GrantRole GrantRole 30 GrantProxy GrantProxy 31 } 32 33 func (node *Grant) Format(ctx *FmtCtx) { 34 switch node.Typ { 35 case GrantTypePrivilege: 36 node.GrantPrivilege.Format(ctx) 37 case GrantTypeRole: 38 node.GrantRole.Format(ctx) 39 case GrantTypeProxy: 40 node.GrantProxy.Format(ctx) 41 } 42 } 43 44 func (node *Grant) GetStatementType() string { return "Grant" } 45 func (node *Grant) GetQueryType() string { return QueryTypeDCL } 46 47 func NewGrant() *Grant { 48 return &Grant{} 49 } 50 51 type GrantPrivilege struct { 52 statementImpl 53 Privileges []*Privilege 54 //grant privileges 55 ObjType ObjectType 56 //grant privileges 57 Level *PrivilegeLevel 58 Roles []*Role 59 GrantOption bool 60 } 61 62 func (node *GrantPrivilege) Format(ctx *FmtCtx) { 63 ctx.WriteString("grant") 64 if node.Privileges != nil { 65 prefix := " " 66 for _, p := range node.Privileges { 67 ctx.WriteString(prefix) 68 p.Format(ctx) 69 prefix = ", " 70 } 71 } 72 ctx.WriteString(" on") 73 if node.ObjType != OBJECT_TYPE_NONE { 74 ctx.WriteByte(' ') 75 ctx.WriteString(node.ObjType.String()) 76 } 77 if node.Level != nil { 78 ctx.WriteByte(' ') 79 node.Level.Format(ctx) 80 } 81 82 if node.Roles != nil { 83 ctx.WriteString(" to") 84 prefix := " " 85 for _, r := range node.Roles { 86 ctx.WriteString(prefix) 87 r.Format(ctx) 88 prefix = ", " 89 } 90 } 91 if node.GrantOption { 92 ctx.WriteString(" with grant option") 93 } 94 } 95 96 func (node *GrantPrivilege) GetStatementType() string { return "Grant Privilege" } 97 func (node *GrantPrivilege) GetQueryType() string { return QueryTypeDCL } 98 99 type GrantRole struct { 100 statementImpl 101 Roles []*Role 102 Users []*User 103 GrantOption bool 104 } 105 106 func (node *GrantRole) Format(ctx *FmtCtx) { 107 ctx.WriteString("grant") 108 if node.Roles != nil { 109 prefix := " " 110 for _, r := range node.Roles { 111 ctx.WriteString(prefix) 112 r.Format(ctx) 113 prefix = ", " 114 } 115 } 116 if node.Users != nil { 117 ctx.WriteString(" to") 118 prefix := " " 119 for _, r := range node.Users { 120 ctx.WriteString(prefix) 121 r.Format(ctx) 122 prefix = ", " 123 } 124 } 125 if node.GrantOption { 126 ctx.WriteString(" with grant option") 127 } 128 } 129 130 func (node *GrantRole) GetStatementType() string { return "Grant Role" } 131 func (node *GrantRole) GetQueryType() string { return QueryTypeDCL } 132 133 type GrantProxy struct { 134 statementImpl 135 ProxyUser *User 136 Users []*User 137 GrantOption bool 138 } 139 140 func (node *GrantProxy) Format(ctx *FmtCtx) { 141 ctx.WriteString("grant") 142 if node.ProxyUser != nil { 143 ctx.WriteString(" proxy on ") 144 node.ProxyUser.Format(ctx) 145 } 146 if node.Users != nil { 147 ctx.WriteString(" to") 148 prefix := " " 149 for _, r := range node.Users { 150 ctx.WriteString(prefix) 151 r.Format(ctx) 152 prefix = ", " 153 } 154 } 155 if node.GrantOption { 156 ctx.WriteString(" with grant option") 157 } 158 } 159 160 func (node *GrantProxy) GetStatementType() string { return "Grant Proxy" } 161 func (node *GrantProxy) GetQueryType() string { return QueryTypeDCL }