Skip to content

Commit

Permalink
parser: add DROP ROLE support (#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 9fca026 commit 3f6280b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 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
6 changes: 3 additions & 3 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2114,9 +2114,9 @@ func (s *testParserSuite) TestPrivilege(c *C) {
{`ALTER USER IF EXISTS USER() IDENTIFIED BY 'new-password'`, true, "ALTER USER IF EXISTS USER() IDENTIFIED BY 'new-password'"},
{`DROP USER 'root'@'localhost', 'root1'@'localhost'`, true, "DROP USER `root`@`localhost`, `root1`@`localhost`"},
{`DROP USER IF EXISTS 'root'@'localhost'`, true, "DROP USER IF EXISTS `root`@`localhost`"},
{`DROP ROLE 'role'@'localhost', 'role1'@'localhost'`, true, ""},
{`DROP ROLE 'administrator', 'developer';`, true, ""},
{`DROP ROLE IF EXISTS 'role'@'localhost'`, true, ""},
{`DROP ROLE 'role'@'localhost', 'role1'@'localhost'`, true, "DROP ROLE `role`@`localhost`, `role1`@`localhost`"},
{`DROP ROLE 'administrator', 'developer';`, true, "DROP ROLE `administrator`@`%`, `developer`@`%`"},
{`DROP ROLE IF EXISTS 'role'@'localhost'`, true, "DROP ROLE IF EXISTS `role`@`localhost`"},

// for grant statement
{"GRANT ALL ON db1.* TO 'jeffrey'@'localhost';", true, "GRANT ALL ON `db1`.* TO `jeffrey`@`localhost`"},
Expand Down

0 comments on commit 3f6280b

Please sign in to comment.