Skip to content

Instantly share code, notes, and snippets.

@bopjiang
Last active April 2, 2019 00:59
Show Gist options
  • Save bopjiang/b5d7de1471d1a1be3387e33c6eb30591 to your computer and use it in GitHub Desktop.
Save bopjiang/b5d7de1471d1a1be3387e33c6eb30591 to your computer and use it in GitHub Desktop.
package main
import (
"strings"
"fmt"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser"
_ "github.com/pingcap/tidb/types/parser_driver" // https://github.com/pingcap/parser/issues/43
)
type visitor struct{}
var (
level = 0
sp = "++"
)
func (v *visitor) Enter(in ast.Node) (out ast.Node, skipChildren bool) {
s := strings.Repeat("\t", level)
fmt.Printf("L-%d %s%T %s\n", level, s, in, in.Text())
level += 1
return in, false
}
func (v *visitor) Leave(in ast.Node) (out ast.Node, ok bool) {
level -= 1
//s := strings.Repeat("\t", level)
//fmt.Printf("L%d%s%T\n", level, s, in)
return in, true
}
func main() {
sql := "SELECT t1.a, t2.b FROM t1 JOIN t2 ON t1.id = t2.fid WHERE t1.c>100"
sqlParser := parser.New()
stmtNodes,_, err := sqlParser.Parse(sql, "", "")
if err != nil {
fmt.Printf("parse error:\n%v\n%s", err, sql)
return
}
for _, stmtNode := range stmtNodes {
v := visitor{}
stmtNode.Accept(&v)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment