Skip to content

Commit

Permalink
ast: add GrantRoleStmt to support GRANT ROLE (pingcap#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lingyu Song authored and jackysp committed Mar 21, 2019
1 parent 5c91e4a commit e69214e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
54 changes: 54 additions & 0 deletions ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,60 @@ func (n *GrantStmt) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// GrantRoleStmt is the struct for GRANT TO statement.
type GrantRoleStmt struct {
stmtNode

Roles []*auth.RoleIdentity
Users []*auth.UserIdentity
}

// Accept implements Node Accept interface.
func (n *GrantRoleStmt) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*GrantRoleStmt)
return v.Leave(n)
}

// Restore implements Node interface.
func (n *GrantRoleStmt) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord("GRANT ")
if len(n.Roles) > 0 {
for i, role := range n.Roles {
if i != 0 {
ctx.WritePlain(", ")
}
if err := role.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore GrantRoleStmt.Roles[%d]", i)
}
}
}
ctx.WriteKeyWord(" TO ")
for i, v := range n.Users {
if i != 0 {
ctx.WritePlain(", ")
}
if err := v.Restore(ctx); err != nil {
return errors.Annotatef(err, "An error occurred while restore GrantStmt.Users[%d]", i)
}
}
return nil
}

// SecureText implements SensitiveStatement interface.
func (n *GrantRoleStmt) SecureText() string {
text := n.text
// Filter "identified by xxx" because it would expose password information.
idx := strings.Index(strings.ToLower(text), "identified")
if idx > 0 {
text = text[:idx]
}
return text
}

// Ident is the table identifier composed of schema name and table name.
type Ident struct {
Schema model.CIStr
Expand Down
7 changes: 7 additions & 0 deletions parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -7688,6 +7688,10 @@ GrantStmt:
GrantRoleStmt:
"GRANT" RolenameList "TO" UsernameList
{
$$ = &ast.GrantRoleStmt {
Roles: $2.([]*auth.RoleIdentity),
Users: $4.([]*auth.UserIdentity),
}
}

WithGrantOptionOpt:
Expand Down

0 comments on commit e69214e

Please sign in to comment.