Skip to content

Commit 647e0ab

Browse files
Lingyu Songtiancaiamao
authored andcommitted
parser, ast: add SET ROLE support (pingcap#228)
1 parent 82e38e8 commit 647e0ab

File tree

10 files changed

+5783
-5663
lines changed

10 files changed

+5783
-5663
lines changed

ast/functions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ const (
233233
Collation = "collation"
234234
ConnectionID = "connection_id"
235235
CurrentUser = "current_user"
236+
CurrentRole = "current_role"
236237
Database = "database"
237238
FoundRows = "found_rows"
238239
LastInsertId = "last_insert_id"

ast/misc.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var (
4040
_ StmtNode = &PrepareStmt{}
4141
_ StmtNode = &RollbackStmt{}
4242
_ StmtNode = &SetPwdStmt{}
43+
_ StmtNode = &SetRoleStmt{}
4344
_ StmtNode = &SetStmt{}
4445
_ StmtNode = &UseStmt{}
4546
_ StmtNode = &FlushStmt{}
@@ -758,6 +759,60 @@ func (n *ChangeStmt) Accept(v Visitor) (Node, bool) {
758759
return v.Leave(n)
759760
}
760761

762+
// SetRoleStmtType is the type for FLUSH statement.
763+
type SetRoleStmtType int
764+
765+
// SetRole statement types.
766+
const (
767+
SetRoleDefault SetRoleStmtType = iota
768+
SetRoleNone
769+
SetRoleAll
770+
SetRoleAllExcept
771+
SetRoleRegular
772+
)
773+
774+
type SetRoleStmt struct {
775+
stmtNode
776+
777+
SetRoleOpt SetRoleStmtType
778+
RoleList []*auth.RoleIdentity
779+
}
780+
781+
func (n *SetRoleStmt) Restore(ctx *RestoreCtx) error {
782+
ctx.WriteKeyWord("SET ROLE")
783+
switch n.SetRoleOpt {
784+
case SetRoleDefault:
785+
ctx.WriteKeyWord(" DEFAULT")
786+
case SetRoleNone:
787+
ctx.WriteKeyWord(" NONE")
788+
case SetRoleAll:
789+
ctx.WriteKeyWord(" ALL")
790+
case SetRoleAllExcept:
791+
ctx.WriteKeyWord(" ALL EXCEPT")
792+
}
793+
for i, role := range n.RoleList {
794+
ctx.WritePlain(" ")
795+
err := role.Restore(ctx)
796+
if err != nil {
797+
return errors.Annotate(err, "An error occurred while restore SetRoleStmt.RoleList")
798+
}
799+
if i != len(n.RoleList)-1 {
800+
ctx.WritePlain(",")
801+
}
802+
}
803+
return nil
804+
}
805+
806+
// Accept implements Node Accept interface.
807+
func (n *SetRoleStmt) Accept(v Visitor) (Node, bool) {
808+
newNode, skipChildren := v.Enter(n)
809+
if skipChildren {
810+
return v.Leave(newNode)
811+
}
812+
n = newNode.(*SetRoleStmt)
813+
return v.Leave(n)
814+
}
815+
761816
// UserSpec is used for parsing create user statement.
762817
type UserSpec struct {
763818
User *auth.UserIdentity

auth/auth.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ type UserIdentity struct {
3333
AuthHostname string // Match in privs system (i.e. could be a wildcard)
3434
}
3535

36-
type RoleIdentity struct {
37-
Username string
38-
Hostname string
39-
}
40-
4136
// Restore implements Node interface.
4237
func (user *UserIdentity) Restore(ctx *RestoreCtx) error {
4338
if user.CurrentUser {
@@ -67,6 +62,26 @@ func (user *UserIdentity) AuthIdentityString() string {
6762
return fmt.Sprintf("%s@%s", user.AuthUsername, user.AuthHostname)
6863
}
6964

65+
type RoleIdentity struct {
66+
Username string
67+
Hostname string
68+
}
69+
70+
func (role *RoleIdentity) Restore(ctx *RestoreCtx) error {
71+
ctx.WriteName(role.Username)
72+
if role.Hostname != "" {
73+
ctx.WritePlain("@")
74+
ctx.WriteName(role.Hostname)
75+
}
76+
return nil
77+
}
78+
79+
// String converts UserIdentity to the format user@host.
80+
func (role *RoleIdentity) String() string {
81+
// TODO: Escape username and hostname.
82+
return fmt.Sprintf("`%s`@`%s`", role.Username, role.Hostname)
83+
}
84+
7085
// CheckScrambledPassword check scrambled password received from client.
7186
// The new authentication is performed in following manner:
7287
// SERVER: public_seed=create_random_string()

go.sum1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446 h1:/NRJ5vAYo
9494
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
9595
github.com/shirou/gopsutil v2.18.10+incompatible h1:cy84jW6EVRPa5g9HAHrlbxMSIjBhDSX0OFYyMYminYs=
9696
github.com/shirou/gopsutil v2.18.10+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
97+
github.com/shirou/gopsutil v2.18.12+incompatible h1:1eaJvGomDnH74/5cF4CTmTbLHAriGFsTZppLXDX93OM=
98+
github.com/shirou/gopsutil v2.18.12+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
9799
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
98100
github.com/shurcooL/vfsgen v0.0.0-20181020040650-a97a25d856ca/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
99101
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=

misc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ var tokenMap = map[string]int{
204204
"CURRENT_TIME": currentTime,
205205
"CURRENT_TIMESTAMP": currentTs,
206206
"CURRENT_USER": currentUser,
207+
"CURRENT_ROLE": currentRole,
207208
"CURTIME": curTime,
208209
"DATA": data,
209210
"DATABASE": database,

mysql/errcode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ const (
894894
ErrInvalidJSONPathWildcard = 3149
895895
ErrInvalidJSONContainsPathType = 3150
896896
ErrJSONUsedAsKey = 3152
897+
ErrRoleNotGranted = 3530
897898
ErrWindowNoSuchWindow = 3579
898899
ErrWindowCircularityInWindowGraph = 3580
899900
ErrWindowNoChildPartitioning = 3581

mysql/errname.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ var MySQLErrName = map[uint16]string{
912912
ErrWindowNoGroupOrderUnused: "ASC or DESC with GROUP BY isn't allowed with window functions; put ASC or DESC in ORDER BY",
913913
ErrWindowExplainJson: "To get information about window functions use EXPLAIN FORMAT=JSON",
914914
ErrWindowFunctionIgnoresFrame: "Window function '%s' ignores the frame clause of window '%s' and aggregates over the whole partition",
915+
ErrRoleNotGranted: "%s is is not granted to %s",
915916

916917
// TiDB errors.
917918
ErrMemExceedThreshold: "%s holds %dB memory, exceeds threshold %dB.%s",

0 commit comments

Comments
 (0)