-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
create_role.go
109 lines (92 loc) · 3.14 KB
/
create_role.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plan
import (
"fmt"
"strings"
"github.com/dolthub/go-mysql-server/sql/types"
"github.com/dolthub/go-mysql-server/sql"
)
// CreateRole represents the statement CREATE ROLE.
type CreateRole struct {
IfNotExists bool
Roles []UserName
MySQLDb sql.Database
}
// NewCreateRole returns a new CreateRole node.
func NewCreateRole(ifNotExists bool, roles []UserName) *CreateRole {
return &CreateRole{
IfNotExists: ifNotExists,
Roles: roles,
MySQLDb: sql.UnresolvedDatabase("mysql"),
}
}
var _ sql.Node = (*CreateRole)(nil)
var _ sql.CollationCoercible = (*CreateRole)(nil)
// Schema implements the interface sql.Node.
func (n *CreateRole) Schema() sql.Schema {
return types.OkResultSchema
}
// String implements the interface sql.Node.
func (n *CreateRole) String() string {
roles := make([]string, len(n.Roles))
for i, role := range n.Roles {
roles[i] = role.String("")
}
ifNotExists := ""
if n.IfNotExists {
ifNotExists = "IfNotExists: "
}
return fmt.Sprintf("CreateRole(%s%s)", ifNotExists, strings.Join(roles, ", "))
}
// Database implements the interface sql.Databaser.
func (n *CreateRole) Database() sql.Database {
return n.MySQLDb
}
// WithDatabase implements the interface sql.Databaser.
func (n *CreateRole) WithDatabase(db sql.Database) (sql.Node, error) {
nn := *n
nn.MySQLDb = db
return &nn, nil
}
// Resolved implements the interface sql.Node.
func (n *CreateRole) Resolved() bool {
_, ok := n.MySQLDb.(sql.UnresolvedDatabase)
return !ok
}
func (n *CreateRole) IsReadOnly() bool {
return false
}
// Children implements the interface sql.Node.
func (n *CreateRole) Children() []sql.Node {
return nil
}
// WithChildren implements the interface sql.Node.
func (n *CreateRole) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 0 {
return nil, sql.ErrInvalidChildrenNumber.New(n, len(children), 0)
}
return n, nil
}
// CheckPrivileges implements the interface sql.Node.
func (n *CreateRole) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
// Both CREATE ROLE and CREATE USER are valid privileges, so we use an OR
subject := sql.PrivilegeCheckSubject{}
return opChecker.UserHasPrivileges(ctx, sql.NewPrivilegedOperation(subject, sql.PrivilegeType_CreateRole)) ||
opChecker.UserHasPrivileges(ctx, sql.NewPrivilegedOperation(subject, sql.PrivilegeType_CreateUser))
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (*CreateRole) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 7
}