Skip to content

Commit

Permalink
Add the support of GRANT PRIVILEGE
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hulk committed Nov 3, 2023
1 parent 6d29560 commit 5be2c9a
Show file tree
Hide file tree
Showing 8 changed files with 971 additions and 0 deletions.
78 changes: 78 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3866,3 +3866,81 @@ func (e *ExplainExpr) String(level int) string {
builder.WriteString(e.Statement.String(level))
return builder.String()
}

type PrivilegeExpr struct {
PrivilegePos Pos
PrivilegeEnd Pos
Keywords []string
Params *ParamExprList
}

func (p *PrivilegeExpr) Pos() Pos {
return p.PrivilegePos
}

func (p *PrivilegeExpr) End() Pos {
return p.PrivilegeEnd
}

func (p *PrivilegeExpr) String(level int) string {
var builder strings.Builder
for i, keyword := range p.Keywords {
if i > 0 {
builder.WriteByte(' ')
}
builder.WriteString(keyword)
}
builder.WriteString(p.Params.String(level))
return builder.String()
}

type GrantPrivilegeExpr struct {
GrantPos Pos
StatementEnd Pos
OnCluster *OnClusterExpr
Privileges []*PrivilegeExpr
On *TableIdentifier
To []*Ident
WithOptions []string
}

func (g *GrantPrivilegeExpr) Pos() Pos {
return g.GrantPos
}

func (g *GrantPrivilegeExpr) End() Pos {
return g.StatementEnd
}

func (g *GrantPrivilegeExpr) Type() string {
return "GRANT PRIVILEGE"
}

func (g *GrantPrivilegeExpr) String(level int) string {
var builder strings.Builder
builder.WriteString("GRANT ")
if g.OnCluster != nil {
builder.WriteString(NewLine(level))
builder.WriteString(g.OnCluster.String(level))
}
for i, privilege := range g.Privileges {
if i > 0 {
builder.WriteString(", ")
}
builder.WriteString(privilege.String(level))
}
builder.WriteString(" ON ")
builder.WriteString(g.On.String(level))
builder.WriteString(" TO ")
for i, role := range g.To {
if i > 0 {
builder.WriteString(", ")
}
builder.WriteString(role.String(level))
}
for _, option := range g.WithOptions {
builder.WriteString(" WITH " + option + " OPTION")
}

return builder.String()
}
27 changes: 27 additions & 0 deletions parser/keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package parser

const (
KeywordAdd = "ADD"
KeywordAdmin = "ADMIN"
KeywordAfter = "AFTER"
KeywordAlias = "ALIAS"
KeywordAll = "ALL"
Expand Down Expand Up @@ -29,8 +30,10 @@ const (
KeywordCodec = "CODEC"
KeywordCollate = "COLLATE"
KeywordColumn = "COLUMN"
KeywordColumns = "COLUMNS"
KeywordComment = "COMMENT"
KeywordCompiled = "COMPILED"
KeywordConfig = "CONFIG"
KeywordConstraint = "CONSTRAINT"
KeywordCreate = "CREATE"
KeywordCross = "CROSS"
Expand Down Expand Up @@ -78,7 +81,9 @@ const (
KeywordFrom = "FROM"
KeywordFull = "FULL"
KeywordFunction = "FUNCTION"
KeywordFunctions = "FUNCTIONS"
KeywordGlobal = "GLOBAL"
KeywordGrant = "GRANT"
KeywordGranularity = "GRANULARITY"
KeywordGroup = "GROUP"
KeywordHaving = "HAVING"
Expand Down Expand Up @@ -120,6 +125,7 @@ const (
KeywordModify = "MODIFY"
KeywordMonth = "MONTH"
KeywordMove = "MOVE"
KeywordMoves = "MOVES"
KeywordMutation = "MUTATION"
KeywordNan_sql = "NAN_SQL"
KeywordNo = "NO"
Expand All @@ -129,27 +135,34 @@ const (
KeywordOffset = "OFFSET"
KeywordOn = "ON"
KeywordOptimize = "OPTIMIZE"
KeywordOption = "OPTION"
KeywordOr = "OR"
KeywordOrder = "ORDER"
KeywordOuter = "OUTER"
KeywordOutfile = "OUTFILE"
KeywordOver = "OVER"
KeywordPartition = "PARTITION"
KeywordPipeline = "PIPELINE"
KeywordPolicy = "POLICY"
KeywordPopulate = "POPULATE"
KeywordPreceding = "PRECEDING"
KeywordPrewhere = "PREWHERE"
KeywordPrimary = "PRIMARY"
KeywordProjection = "PROJECTION"
KeywordQuarter = "QUARTER"
KeywordQuery = "QUERY"
KeywordQueues = "QUEUES"
KeywordQuota = "QUOTA"
KeywordRange = "RANGE"
KeywordRefresh = "REFRESH"
KeywordReload = "RELOAD"
KeywordRemove = "REMOVE"
KeywordRename = "RENAME"
KeywordReplace = "REPLACE"
KeywordReplica = "REPLICA"
KeywordReplicated = "REPLICATED"
KeywordReplication = "REPLICATION"
KeywordRestart = "RESTART"
KeywordRight = "RIGHT"
KeywordRole = "ROLE"
KeywordRollup = "ROLLUP"
Expand All @@ -163,6 +176,7 @@ const (
KeywordSet = "SET"
KeywordSettings = "SETTINGS"
KeywordShow = "SHOW"
KeywordShutdown = "SHUTDOWN"
KeywordSource = "SOURCE"
KeywordStart = "START"
KeywordStop = "STOP"
Expand Down Expand Up @@ -208,6 +222,7 @@ const (

var keywords = NewSet(
KeywordAdd,
KeywordAdmin,
KeywordAfter,
KeywordAlias,
KeywordAll,
Expand Down Expand Up @@ -235,8 +250,10 @@ var keywords = NewSet(
KeywordCodec,
KeywordCollate,
KeywordColumn,
KeywordColumns,
KeywordComment,
KeywordCompiled,
KeywordConfig,
KeywordConstraint,
KeywordCreate,
KeywordCross,
Expand Down Expand Up @@ -284,7 +301,9 @@ var keywords = NewSet(
KeywordFrom,
KeywordFull,
KeywordFunction,
KeywordFunctions,
KeywordGlobal,
KeywordGrant,
KeywordGranularity,
KeywordGroup,
KeywordHaving,
Expand Down Expand Up @@ -326,6 +345,7 @@ var keywords = NewSet(
KeywordModify,
KeywordMonth,
KeywordMove,
KeywordMoves,
KeywordMutation,
KeywordNan_sql,
KeywordNo,
Expand All @@ -335,27 +355,33 @@ var keywords = NewSet(
KeywordOffset,
KeywordOn,
KeywordOptimize,
KeywordOption,
KeywordOr,
KeywordOrder,
KeywordOuter,
KeywordOutfile,
KeywordOver,
KeywordPartition,
KeywordPipeline,
KeywordPolicy,
KeywordPopulate,
KeywordPreceding,
KeywordPrewhere,
KeywordPrimary,
KeywordProjection,
KeywordQuarter,
KeywordQuery,
KeywordQueues,
KeywordQuota,
KeywordRange,
KeywordReload,
KeywordRemove,
KeywordRename,
KeywordReplace,
KeywordReplica,
KeywordReplicated,
KeywordReplication,
KeywordRestart,
KeywordRight,
KeywordRole,
KeywordRollup,
Expand All @@ -369,6 +395,7 @@ var keywords = NewSet(
KeywordSet,
KeywordSettings,
KeywordShow,
KeywordShutdown,
KeywordSource,
KeywordStart,
KeywordStop,
Expand Down
Loading

0 comments on commit 5be2c9a

Please sign in to comment.