Skip to content

Commit

Permalink
parser: add support for (pingcap#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
caohe authored and WangXiangUSTC committed Mar 25, 2019
1 parent 6c89706 commit e6412b2
Show file tree
Hide file tree
Showing 4 changed files with 5,579 additions and 5,359 deletions.
174 changes: 174 additions & 0 deletions ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,9 @@ const (
ShowMasterStatus
ShowPrivileges
ShowErrors
ShowBindings
ShowPumpStatus
ShowDrainerStatus
)

// ShowStmt is a statement to provide information about databases, tables, columns and so on.
Expand All @@ -1004,6 +1007,177 @@ type ShowStmt struct {
Where ExprNode
}

// Restore implements Node interface.
func (n *ShowStmt) Restore(ctx *RestoreCtx) error {
restoreOptFull := func() {
if n.Full {
ctx.WriteKeyWord("FULL ")
}
}
restoreShowDatabaseNameOpt := func() {
if n.DBName != "" {
// FROM OR IN
ctx.WriteKeyWord(" IN ")
ctx.WriteName(n.DBName)
}
}
restoreGlobalScope := func() {
if n.GlobalScope {
ctx.WriteKeyWord("GLOBAL ")
} else {
ctx.WriteKeyWord("SESSION ")
}
}
restoreShowLikeOrWhereOpt := func() error {
if n.Pattern != nil && n.Pattern.Pattern != nil {
ctx.WriteKeyWord(" LIKE ")
if err := n.Pattern.Pattern.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore ShowStmt.Pattern")
}
} else if n.Where != nil {
ctx.WriteKeyWord(" WHERE ")
if err := n.Where.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore ShowStmt.Where")
}
}
return nil
}

ctx.WriteKeyWord("SHOW ")
switch n.Tp {
case ShowCreateTable:
ctx.WriteKeyWord("CREATE TABLE ")
if err := n.Table.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore ShowStmt.Table")
}
case ShowCreateView:
ctx.WriteKeyWord("CREATE VIEW ")
if err := n.Table.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore ShowStmt.VIEW")
}
case ShowCreateDatabase:
ctx.WriteKeyWord("CREATE DATABASE ")
if n.IfNotExists {
ctx.WriteKeyWord("IF NOT EXISTS ")
}
ctx.WriteName(n.DBName)
case ShowCreateUser:
ctx.WriteKeyWord("CREATE USER ")
if err := n.User.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore ShowStmt.User")
}
case ShowGrants:
ctx.WriteKeyWord("GRANTS")
if n.User != nil {
ctx.WriteKeyWord(" FOR ")
if err := n.User.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore ShowStmt.User")
}
}
case ShowMasterStatus:
ctx.WriteKeyWord("MASTER STATUS")
case ShowProcessList:
restoreOptFull()
ctx.WriteKeyWord("PROCESSLIST")
case ShowStatsMeta:
ctx.WriteKeyWord("STATS_META")
if err := restoreShowLikeOrWhereOpt(); err != nil {
return err
}
case ShowStatsHistograms:
ctx.WriteKeyWord("STATS_HISTOGRAMS")
if err := restoreShowLikeOrWhereOpt(); err != nil {
return err
}
case ShowStatsBuckets:
ctx.WriteKeyWord("STATS_BUCKETS")
if err := restoreShowLikeOrWhereOpt(); err != nil {
return err
}
case ShowStatsHealthy:
ctx.WriteKeyWord("STATS_HEALTHY")
if err := restoreShowLikeOrWhereOpt(); err != nil {
return err
}
case ShowProfiles:
ctx.WriteKeyWord("PROFILES")
case ShowPrivileges:
ctx.WriteKeyWord("PRIVILEGES")
// ShowTargetFilterable
default:
switch n.Tp {
case ShowEngines:
ctx.WriteKeyWord("ENGINES")
case ShowDatabases:
ctx.WriteKeyWord("DATABASES")
case ShowCharset:
ctx.WriteKeyWord("CHARSET")
case ShowTables:
restoreOptFull()
ctx.WriteKeyWord("TABLES")
restoreShowDatabaseNameOpt()
case ShowTableStatus:
ctx.WriteKeyWord("TABLE STATUS")
restoreShowDatabaseNameOpt()
case ShowIndex:
// here can be INDEX INDEXES KEYS
// FROM or IN
ctx.WriteKeyWord("INDEX IN ")
if err := n.Table.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while resotre ShowStmt.Table")
} // TODO: remember to check this case
case ShowColumns: // equivalent to SHOW FIELDS
restoreOptFull()
ctx.WriteKeyWord("COLUMNS")
if n.Table != nil {
// FROM or IN
ctx.WriteKeyWord(" IN ")
if err := n.Table.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while resotre ShowStmt.Table")
}
}
restoreShowDatabaseNameOpt()
case ShowWarnings:
ctx.WriteKeyWord("WARNINGS")
case ShowErrors:
ctx.WriteKeyWord("ERRORS")
case ShowVariables:
restoreGlobalScope()
ctx.WriteKeyWord("VARIABLES")
case ShowStatus:
restoreGlobalScope()
ctx.WriteKeyWord("STATUS")
case ShowCollation:
ctx.WriteKeyWord("COLLATION")
case ShowTriggers:
ctx.WriteKeyWord("TRIGGERS")
restoreShowDatabaseNameOpt()
case ShowProcedureStatus:
ctx.WriteKeyWord("PROCEDURE STATUS")
case ShowEvents:
ctx.WriteKeyWord("EVENTS")
restoreShowDatabaseNameOpt()
case ShowPlugins:
ctx.WriteKeyWord("PLUGINS")
case ShowBindings:
if n.GlobalScope {
ctx.WriteKeyWord("GLOBAL ")
} else {
ctx.WriteKeyWord("SESSION ")
}
ctx.WriteKeyWord("BINDINGS")
case ShowPumpStatus:
ctx.WriteKeyWord("PUMP STATUS")
case ShowDrainerStatus:
ctx.WriteKeyWord("DRAINER STATUS")
default:
return errors.New("Unknown ShowStmt type")
}
restoreShowLikeOrWhereOpt()
}
return nil
}

// Accept implements Node Accept interface.
func (n *ShowStmt) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
Expand Down
2 changes: 2 additions & 0 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ var tokenMap = map[string]int{
"DIV": div,
"DO": do,
"DOUBLE": doubleType,
"DRAINER": drainer,
"DROP": drop,
"DUAL": dual,
"DUPLICATE": duplicate,
Expand Down Expand Up @@ -390,6 +391,7 @@ var tokenMap = map[string]int{
"PROCESS": process,
"PROCESSLIST": processlist,
"PROFILES": profiles,
"PUMP": pump,
"QUARTER": quarter,
"QUERY": query,
"QUERIES": queries,
Expand Down
Loading

0 comments on commit e6412b2

Please sign in to comment.