Skip to content

Commit

Permalink
parser: add DROP ROLE support (pingcap#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lingyu Song authored and jackysp committed Mar 12, 2019
1 parent 32e93bd commit 08aa20a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
11 changes: 8 additions & 3 deletions ast/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,13 +887,18 @@ func (n *AlterUserStmt) Accept(v Visitor) (Node, bool) {
type DropUserStmt struct {
stmtNode

IfExists bool
UserList []*auth.UserIdentity
IfExists bool
IsDropRole bool
UserList []*auth.UserIdentity
}

// Restore implements Node interface.
func (n *DropUserStmt) Restore(ctx *RestoreCtx) error {
ctx.WriteKeyWord("DROP USER ")
if n.IsDropRole {
ctx.WriteKeyWord("DROP ROLE ")
} else {
ctx.WriteKeyWord("DROP USER ")
}
if n.IfExists {
ctx.WriteKeyWord("IF EXISTS ")
}
Expand Down
4 changes: 4 additions & 0 deletions mysql/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ const (
GlobalStatusTable = "GLOBAL_STATUS"
// TiDBTable is the table contains tidb info.
TiDBTable = "tidb"
// RoleEdgesTable is the table contains role relation info
RoleEdgeTable = "role_edges"
// DefaultRoleTable is the table contain default active role info
DefaultRoleTable = "default_roles"
)

// PrivilegeType privilege
Expand Down
22 changes: 20 additions & 2 deletions parser.go

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

16 changes: 14 additions & 2 deletions parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -2429,19 +2429,31 @@ DropViewStmt:
DropUserStmt:
"DROP" "USER" UsernameList
{
$$ = &ast.DropUserStmt{IfExists: false, UserList: $3.([]*auth.UserIdentity)}
$$ = &ast.DropUserStmt{IsDropRole: false, IfExists: false, UserList: $3.([]*auth.UserIdentity)}
}
| "DROP" "USER" "IF" "EXISTS" UsernameList
{
$$ = &ast.DropUserStmt{IfExists: true, UserList: $5.([]*auth.UserIdentity)}
$$ = &ast.DropUserStmt{IsDropRole: false, IfExists: true, UserList: $5.([]*auth.UserIdentity)}
}

DropRoleStmt:
"DROP" "ROLE" RolenameList
{
tmp := make([]*auth.UserIdentity, 0, 10)
roleList := $3.([]*auth.RoleIdentity)
for _, r := range roleList {
tmp = append(tmp, &auth.UserIdentity{Username:r.Username, Hostname: r.Hostname})
}
$$ = &ast.DropUserStmt{IsDropRole: true, IfExists: false, UserList: tmp}
}
| "DROP" "ROLE" "IF" "EXISTS" RolenameList
{
tmp := make([]*auth.UserIdentity, 0, 10)
roleList := $5.([]*auth.RoleIdentity)
for _, r := range roleList {
tmp = append(tmp, &auth.UserIdentity{Username:r.Username, Hostname: r.Hostname})
}
$$ = &ast.DropUserStmt{IsDropRole: true, IfExists: true, UserList: tmp}
}

DropStatsStmt:
Expand Down

0 comments on commit 08aa20a

Please sign in to comment.