Last active
April 2, 2019 00:59
Revisions
-
Peter Jiang revised this gist
Apr 2, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import ( "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{} -
Peter Jiang revised this gist
Apr 2, 2019 . 1 changed file with 15 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ package main import ( @@ -28,4 +27,19 @@ func (v *visitor) Leave(in ast.Node) (out ast.Node, ok bool) { //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) } } -
Peter Jiang created this gist
Apr 2, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ ➜ ast go run select.go package main import ( "strings" "fmt" "github.com/pingcap/parser/ast" "github.com/pingcap/parser" _ "github.com/pingcap/tidb/types/parser_driver" ) 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 }