From 1de449edabf01fde0e60ba0ce114d99d6bdcea64 Mon Sep 17 00:00:00 2001 From: imtbkcat Date: Thu, 7 Mar 2019 18:45:22 +0800 Subject: [PATCH 1/2] add DropRoleSTmt --- ast/misc.go | 11 ++++++++--- parser.go | 22 ++++++++++++++++++++-- parser.y | 16 ++++++++++++++-- parser_test.go | 6 +++--- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/ast/misc.go b/ast/misc.go index d69a22360..e0805955a 100755 --- a/ast/misc.go +++ b/ast/misc.go @@ -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 ") } diff --git a/parser.go b/parser.go index 44f42b04c..c3281244b 100644 --- a/parser.go +++ b/parser.go @@ -8438,11 +8438,29 @@ yynewstate: } case 233: { - parser.yyVAL.statement = &ast.DropUserStmt{IfExists: false, UserList: yyS[yypt-0].item.([]*auth.UserIdentity)} + parser.yyVAL.statement = &ast.DropUserStmt{IsDropRole: false, IfExists: false, UserList: yyS[yypt-0].item.([]*auth.UserIdentity)} } case 234: { - parser.yyVAL.statement = &ast.DropUserStmt{IfExists: true, UserList: yyS[yypt-0].item.([]*auth.UserIdentity)} + parser.yyVAL.statement = &ast.DropUserStmt{IsDropRole: false, IfExists: true, UserList: yyS[yypt-0].item.([]*auth.UserIdentity)} + } + case 235: + { + tmp := make([]*auth.UserIdentity, 0, 10) + roleList := yyS[yypt-0].item.([]*auth.RoleIdentity) + for _, r := range roleList { + tmp = append(tmp, &auth.UserIdentity{Username: r.Username, Hostname: r.Hostname}) + } + parser.yyVAL.statement = &ast.DropUserStmt{IsDropRole: true, IfExists: false, UserList: tmp} + } + case 236: + { + tmp := make([]*auth.UserIdentity, 0, 10) + roleList := yyS[yypt-0].item.([]*auth.RoleIdentity) + for _, r := range roleList { + tmp = append(tmp, &auth.UserIdentity{Username: r.Username, Hostname: r.Hostname}) + } + parser.yyVAL.statement = &ast.DropUserStmt{IsDropRole: true, IfExists: true, UserList: tmp} } case 237: { diff --git a/parser.y b/parser.y index 3850818da..320bf1a2f 100644 --- a/parser.y +++ b/parser.y @@ -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: diff --git a/parser_test.go b/parser_test.go index 24efc056a..110c61a14 100755 --- a/parser_test.go +++ b/parser_test.go @@ -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`"}, From 786e747a6f116fec80cb8e78df31e24ec60adee3 Mon Sep 17 00:00:00 2001 From: imtbkcat Date: Thu, 7 Mar 2019 19:50:40 +0800 Subject: [PATCH 2/2] add table const --- mysql/const.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mysql/const.go b/mysql/const.go index ea0216869..334f047eb 100644 --- a/mysql/const.go +++ b/mysql/const.go @@ -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