Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: add DROP ROLE support #237

Merged
merged 3 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about using a new DropRoleStmt?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually there is no difference between DROP USER and DROP ROLE. I think IsDropRole can be removed.

}
| "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